Posts

Learn XML Schema by Example: 2 Polymorphism

Image
In the previous blog, Learn XML Schema by Example: 1. Complex type , I introduced the very simple idea of ComplexType in Schema. In this blog, I am going to introduce idea of polymorphism which is one of the very important concept in Object orientation. As shown in the above figure, I need Address to be replaced by AUAddress or USAddress in the instance xml file. All the Address, USAddress and AUAddress are complex types in the XML schema. These types are defined in the address.xsd file as shown in the following code. <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns="http://www.ojitha.org/address" targetNamespace="http://www.ojitha.org/address" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="AUAddress"> <xs:complexContent> <xs:extension base="Address"...

Learn XML Schema by Example: 1. Complex type

Image
XML Schema development is one of the major requirements for the Web services. It is time to get rid of the RPC style and move to DOCUMENT style. First of this series I would like to start with complex type. <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns="http://www.ojitha.org" targetNamespace="http://www.ojitha.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="process"> <xs:complexType> <xs:sequence> <xs:element name="person" type="personType"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="personType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name=...

SAAJ Unit testing client for JAX-WS web services

Image
JAX-WS is the simplest java web services. In this blog, I would like to use first example explained in the book Java Web Services: Up and Running , by Martin Kalin ( thanks Martin, great book). Here the SEI and SBI created from the above book. SEI package com.ojitha.ws.ex1; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style=Style.RPC) public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed(); } SBI package com.ojitha.ws.ex1; import java.util.Date; import javax.jws.WebService; @WebService(endpointInterface="com.ojitha.ws.ex1.TimeServer") public class TimeSeverImpl implements TimeServer { @Override public String getTimeAsString() { return new Date().toString(); } @Override public long getTimeAsElapsed() { return new Date().getTime(); } } The main objective of this blog is to introduce a SAA...

Tip: Java Instance initialization is critical

One of my previous blogs, I showed how the instances are initiated in the Java, if you wish see the  Java Constructor Loading Tips . In this blog, I will give an example, where developer can confused. For example, the following code compiles but throws the NPE in the runtime. package com.ojitha.green; /** * @author Ojitha * */ public class Child { //singleton public static final Child GIRL = new Child(); private Child() {} private static final Boolean GENDER = true; private Boolean gender = GENDER; public Boolean isMale(){ return gender;} public static void main(String... args){ String gender = Child.GIRL.isMale() ? "Female":"Male"; System.out.println(gender); } } In the line 17, you get the exception. At the time of creating GIRL, initializing the object variable is happen after the constructor is called. At the time of the GIRL singleton instance is created, Boolean type gender variable is already initialized to null, that cause to the N...

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=...

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 ...

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()...