Posts

Showing posts from 2011

JAX-WS Tutorial: Precise WSDL

I know this is very simple web service. But I would like to highlight the importance of the use of JAX-WS annotations to get rid of the meaningless WSDL which is the main document for the web service consumer. I think, precise WSDL is advantage to maximum use of the web service. Here the simple class package com.ojitha.ws.second; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService(name = "Warehouse", serviceName = "WarehouseService", targetNamespace = "http://www.ojitha.com/stock") @SOAPBinding(style = Style.RPC) public class WarehouseImpl { @WebResult(name = "totalStock") public int getInventory(@WebParam(name = "firstNumber") int stockOnHand, @WebParam(name = "secondNumber") int stockToDeliver) { return stockOnHand + stockToDeliver; } } Here the generated WSDL <wsdl:definitions name=&qu

Service Provider Framework Pattern Example

Image
This example is created based on the explanation found in the “Effective Java, Second Edition, Chapter 2: Creating and destroying objects”. This pattern about the framework which provide multiple implementations with the compact API. Complete decoupling of implementations made this pattern interesting to me always .  There are three main components: Service , provider, registration and Service Access API . In this example, there can be number of traveling implementations, for example, using bus, train, aircraft, car and so on. They are service providers. All these service implementations need be hide behind the registration. User doesn’t worry about the service implementation an only stick to the Service API. Let’s see the important TravelService interface which is the client completely rely on. I would like to keep this very simple with one service. package com.ojitha.travel.service; public interface TravelService { String getVehical(); } The particular service must be p

Developer Notes: Apache Commons FilterIterator

Image
Filtering the collection based on the criteria is a great advantage of FilterIterator. package com.blogspot.ojitha.commonexamples; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.Predicate; import org.apache.commons.collections.iterators.FilterIterator; public class ListFilteringExample { /** * @param args */ public static void main(String[] args) { Person a = new Person("Smith", "Wills"); a.setAge(39); Person b = new Person("Kris", "Torano"); b.setAge(15); Person c = new Person("Mark","Anthony"); c.setAge(25); Person d = new Person("Mad", "Max"); d.setAge(19); List<Person> persons = new ArrayList<Person>(); persons.add(a); persons.add(b); persons.add(c); persons.add(d); Predicate predicate = new AdultFilter(18); Iterator<Person> adults = new FilterIterator(persons.iterator()

Apache Commons Collections: Object equality using Predicate

The method equals() is the way to check the object equality in Java. You need to override equals() and hashCode() method to makes objects equals. However, in this blog, instead of obj1.equals(obj2) method, I am going to use more powerful Predicate from the Apache commons collection. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.blogspot.ojitha.commonexamples</groupId> <artifactId>Apache-common-example</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Apache-common-example</name> <url>http://ojitha.blogspot.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties&g

Developer Notes: Spring Web Workflow 1.0.6 Maven 2 environment for development

Version 1.0.9 is very old version. However, almost all the examples found in the google search is based on Ant and IVY configuration. Therefore, I thought this blog will be beneficial to the people who are still in Ant configuration.  Time to move to Maven. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ojitha.swffirst</groupId> <artifactId>swf-first</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>swf-first Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version>

OGNL Part 8: Lambda without closures

The part 7 is a tutorial on Selection. Here I explain the very easy example from the OGNL language guide . This is factorial function as shown in the following example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class Lambda { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { System.out.println(Ognl.getValue( "#fact = :[#this <=1 ? 1 : #this*#fact(#this-1)], #fact(4)", null)); } }   The output is 4*3*2*1=24.

OGNL Part 7: Tutorial on OGNL Selection

We’ve already discussed the  Selection in  the part 6 . Here the example of selecting first element or last element in the filtered array or list. ^ for the first element (lines 19-20) $ for the last element (lines 23-24) package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class SelectionTutorial { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Root root = new Root(); // selection System.out.println("slect integer which are greater than 2: " + Ognl.getValue("array.{? #this > 2}", root)); // return the first element System.out.println("select the first element in the array: " + Ognl.getValue("array.{^ #this > 2}", root)); //return the last element System.out.println("select the last element in the array: " + Ognl.getValue("array.{$ #this > 2}", root)); } } class Root { pr

OGNL Part 6: Selection

In the part 5 of this series, we’ve discussed the Projection concept. In this part, see the example for the selection which is very similar to concept of SQL selection. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class ProjectingExmaple { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { List<Integer> classes =(List<Integer>)Ognl.getValue("array.{? #this > 2}", new MyRoot()); for (Integer class1 : classes) { System.out.println(class1); } System.out.println("size of the output "+classes.size()); } } class MyRoot { private int[] array = {1,2,3,4}; public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } Here the output 3 4 size of the output 2 Reference OGNL language guide for more information.

OGNL Part 5: Projection

This is very important concept in the OGNL very similar to the database projection concept. In the part 4 , we’ve already discussed the Arrays. Following code is based on the array. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class ProjectingExmaple { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { List classes =(List )Ognl.getValue("array.{class}", new MyRoot()); for (Class class1 : classes) { System.out.println(class1); } System.out.println("size of the output "+classes.size()); } } class MyRoot { private int[] array = {1,2,3,4}; public int[] getArray() { return array; } public void setArray(int[] array) { this.array = array; } } Here the output class java.lang.Integer class java.lang.Integer class java.lang.Integer size of the output 4

OGNL Part 4: Lists and Arrays

In the part 3 , we’ve discussed about Maps. In this blog, let’s look at how to create array and lists. The most common thing is both are accessible via index. package au.com.ojitha.ongl; import java.util.List; import ognl.Ognl; import ognl.OgnlException; public class BasicListExample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { // List List<String> list = (List) Ognl.getValue("{'a','b','c'}", null); System.out.println(list + " is size of " + Ognl.getValue("size", list)); System.out .println("The third element is " + Ognl.getValue("[2]", list)); // array Integer[] intValues = (Integer[]) Ognl.getValue("new Integer[]{1,2,3}", null); System.out.println("Size of the array is " + Ognl.getValue("length", intValues)); System.out.println("The third element is " + Ogn

OGNL Part 3: How to read Map

In this example, explains how the OGNL access the Map.  Refer to the part 2 for basic understanding of navigation chain. import java.util.HashMap; import java.util.Map; import ognl.Ognl; import ognl.OgnlException; public class SecondExample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Map<String, String> mapValues = new HashMap<String, String>(); mapValues.put("lname", "Kumanayaka"); mapValues.put("fname", "Ojitha"); System.out.println(Ognl.getValue("fname+' '+ lname", mapValues)); } } OGNL always treat different kind of objects differently. Instead of accessing Map as in line 16 and 17, you can use OGNL to initialize the map as shown in the line 19 in the following code. package au.com.ojitha.ongl; import java.util.Map; import ognl.Ognl; import ognl.OgnlException; public class SecondExample { /** * @param args

OGNL Part 2: Navigation Chain

In the part 1 of this series explained how to setup the Eclipse project for testing. The navigation chain is the fundamental thing in the OGNL . See the following example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public class FirstEcample { /** * @param args * @throws OgnlException */ public static void main(String[] args) throws OgnlException { Person p = new Person(); p.setName(new Name()); //set the value Ognl.setValue("name.firstName", p, "Ojitha"); Ognl.setValue("name.lastName", p, "Kumanayaka"); //retrieve the value System.out.println(Ognl.getValue("name.firstName+' '+name.lastName", p)); } } class Person { private Name name; public Name getName() { return name; } public void setName(Name name) { this.name = name; } } class Name { private String firstName; private String lastName; public String getFirstName() { return firstName; }

OGNL Part 1: Getting started

In this series, I am going to explain the OGNL Language Guide with code example. In this first part, let’ se how to setup the Maven project in Eclipse 3.6 and run the first example. Here the Maven POM file <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>au.com.ojitha</groupId> <artifactId>OgnalExample-1</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.0.2</version> </dependency> </dependencies> </project> Here the simplest example. package au.com.ojitha.ongl; import ognl.Ognl; import ognl.OgnlException; public

Easy way to publish developer notes on blogger

EJB 3 JUnit testing with OpenEJB 3.1.4

This is quick example of how to create JUnit test to test the EJB 3.0 session bean. The approach is new innovation using @LocalClient. This will create EJB client for Java SE. The interface of the calculator class is, package com.ojitha.calculator; import javax.ejb.Local; @Local public interface CalculatorLocal { public int add(int a, int b); } This is the implement of the Calculator package com.ojitha.calculator; import javax.ejb.Stateless; /** * Session Bean implementation class Calculator */ @Stateless public class Calculator implements CalculatorLocal { /** * Default constructor. */ public Calculator() { } @Override public int add(int a, int b) { return a + b; } } Let's look at the openEJB based JUnit test class package com.ojitha.calculator; import static org.junit.Assert.*; import java.util.Properties; import javax.ejb.EJB; import javax.naming.InitialContext; import org.apache.openejb.api.LocalClient; import org.junit.After; import o

How to sort Java collection on multiple columns

Image
Java collection API, provide Comparator interface to sort the Java collection such as List. However, multi column sort is not possible. Multi column sort can be achieved using two Comparators in two different Collections.sort(Comparator...) calls. I am wonder why sort() method doesn't allow for varargs which is new in Java 5. Due to the limitation of sorting one column at once may giving performance problem (I don't know). Apache common has facility do multi column sorting. package com.oj.ex; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import org.apache.commons.collections.comparators.ComparatorChain; public class TestSorting { /** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { List<Payment> payments = new ArrayList<Pa

IBM developerWorks : Java Technology : Technical library

IBM developerWorks : Java Technology : Technical library : These are very good articles can be found in the IBM developer works.