Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

13 May 2013

Unsupported major.minor version 51.0

Problem: Unsupported major.minor version 51.0.

Cause: The compiled application is not compatible with the JDK version of the application server.

Solution: Compile the source code again using the correct JDK version as of the application server.

The reported major numbers are:

J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

6 Feb 2013

Spring Web Service

Create a Contract-First Spring Web Service
Contract-First Web Service is to develop web services by starting with the XML Schema/WSDL contract first followed by the Java code second.

Implementation

  1. Create the XML Schema.
    1. Define the XML Schema for the Request and Response messages.
    2. Save the XML Schema as "lucky.xsd" under the folder "WEB-INF/xsd".
    <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://com.blogspot.adaprognotebook/LuckyService/" elementFormDefault="qualified"> <xsd:element name="LuckyRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="LuckyResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="message" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
  2. Create and define the service interface in Java.

    LuckyService.java

    package main.java.com.blogspot.adaprognotebook.service; public interface LuckyService { String sayLuckyMessage(String name); }

    LuckyServiceImpl.java

    package main.java.com.blogspot.adaprognotebook.service; public class LuckyServiceImpl implements LuckyService { @Override public String sayLuckyMessage(String name) { if (name == null || name.trim().isEmpty()) { return "Tell me you name~~~"; } int number = (int)(Math.random()*100); return "Hello : " + name + "! Your lucky number is " + number + "."; } }
  3. Create the Object/XML binding.
    1. Create POJO classes for the Request (LuckyRequest.java) and Response (LuckyResponse.java) in Java.

      LuckyRequest.java

      package com.blogspot.adaprognotebook.message; public class LuckyRequest { private String name; public LuckyRequest() { super(); } public LuckyRequest(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

      LuckyResponse.java

      package com.blogspot.adaprognotebook.message; public class LuckyResponse { private String message; public LuckyResponse() { super(); } public LuckyResponse(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
    2. Define the Object/XML binding using JiBX for the purpose of marshalling and unmarshalling for the above model classes.
    3. binding.xml

      <binding name="binding" package="com.blogspot.adaprognotebook.message" trim-whitespace="true"> <namespace uri="http://localhost:8080/luckyservice/" default="elements"/> <mapping class="com.blogspot.adaprognotebook.message.LuckyRequest" name="LuckyRequest"> <value style="element" name="name" get-method="getName" set-method="setName"/> </mapping> <mapping class="com.blogspot.adaprognotebook.message.LuckyResponse" name="LuckyResponse"> <value style="element" name="message" get-method="getMessage" set-method="setMessage"/> </mapping>
  4. Configure Spring's Application Context.

    WEB-INF/spring/applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"? <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping"> <property name="endpointMap"> <map> <entry key="{http://localhost:8080/luckyservice/}/LuckyRequest" value-ref="luckyServiceEndpoint" /> </map> </property> </bean> <bean id="luckyService" class="com.blogspot.adaprognotebook.service.LuckyServiceImpl" /> <bean id="luckyServiceEndpoint" class="com.blogspot.adaprognotebook.endpoint.LuckyServiceEndpoint"> <property name="marshaller" ref="marshaller" /> <property name="unmarshaller" ref="unmarshaller" /> <property name="luckyService" ref="luckyService" /> </bean> <bean id="marshaller" class="org.springframework.oxm.jibx.JibxMarshaller"> <property name="targetClass" value="com.blogspot.adaprognotebook.message.LuckyResponse" /> </bean> <bean id="unmarshaller" class="org.springframework.oxm.jibx.JibxMarshaller"> <property name="targetClass" value="com.blogspot.adaprognotebook.message.LuckyRequest" /> </bean> <bean id="LuckyRequest" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"> <property name="schema" ref="schema" /> <property name="portTypeName" value="Hello" /> <property name="locationUri" value="http://localhost:8080/luckyservice/services/LuckyRequest" /> </bean> <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema"> <property name="xsd" value="/WEB-INF/xsd/lucky.xsd" /> </bean> </beans>
  5. Define the Endpoint.
    package com.blogspot.adaprognotebook.endpoint; import com.blogspot.adaprognotebook.message.HelloRequest; import com.blogspot.adaprognotebook.message.HelloResponse; import com.blogspot.adaprognotebook.service.HelloService; import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint; @SuppressWarnings("deprecation") public class HelloServiceEndpoint extends AbstractMarshallingPayloadEndpoint { private HelloService helloService; protected Object invokeInternal(Object requestObject) throws Exception { HelloRequest request = (HelloRequest) requestObject; String name = request.getName(); return new HelloResponse(helloService.sayhello(name)); } public HelloService getHelloService() { return helloService; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } }
  6. Deploy the web service to Web Server like Jetty.
  7. View the generated WSDL at http://localhost:8080/luckyservice/LuckyRequest.wsdl.
    <?xml version="1.0" encoding="UTF-8"?><wsdl:definitions targetNamespace="http://localhost:8080/luckyservice/" xmlns:sch="http://localhost:8080/luckyservice/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8080/luckyservice/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema elementFormDefault="qualified" targetNamespace="http://localhost:8080/luckyservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="LuckyRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="LuckyResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="message" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="LuckyResponse"> <wsdl:part element="sch:LuckyResponse" name="LuckyResponse"> </wsdl:part> </wsdl:message> <wsdl:message name="LuckyRequest"> <wsdl:part element="sch:LuckyRequest" name="LuckyRequest"> </wsdl:part> </wsdl:message> <wsdl:portType name="Lucky"> <wsdl:operation name="Lucky"> <wsdl:input message="sch:LuckyRequest" name="LuckyRequest"> </wsdl:input> <wsdl:output message="sch:LuckyResponse" name="LuckyResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="LuckySoap11" type="sch:Lucky"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="Lucky"> <soap:operation soapAction=""/> <wsdl:input name="LuckyRequest"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="LuckyResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="LuckyService"> <wsdl:port binding="sch:LuckySoap11" name="LuckySoap11"> <soap:address location="http://localhost:8080/luckyservice/services/LuckyRequest"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
  8. Test the web service using tools like SOAP UI.

31 Jan 2013

Copy Constructor in Java

Implement Copy Constructor in Java.

When an object is copied to the other using assignment operator, reference of the object, instead of value of the object, is copied. Therefore changes in one object apply to another.

Copy Constructor can be used to avoid the aforesaid problem.

Java Source Code

class Ball { private String color; private double radius; // A normal constructor public Ball(double radius, String color) { this.radius = radius; this.color = color; } // Copy Constructor Ball(Ball c) { System.out.println("Copy constructor called."); this.radius = c.radius; this.color = c.color; } public void setColor(String color) { this.color = color; } public void setRadius(double radius) { this.radius = radius; } // Overriding the toString of Object class @Override public String toString() { return "Radius: " + this.radius + ", Color: " + this.color; } } public class Box { public static void main(String[] args) { Ball b1 = new Ball(10, "Red"); System.out.println("Original values of b1:"); System.out.println(b1); System.out.println("*******************************************"); /** * Call a copy constructor. * Note that the values of c1 ARE NOT modified. */ Ball b2 = new Ball(b1); b2.setRadius(20); b2.setColor("Black"); System.out.println("After modification of b2 values,"); System.out.println("which is created using copy constructor:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("*******************************************"); /** * Copy by reference. * Note that the values of c1 ARE modified. */ Ball b3 = b1; b3.setRadius(20); b3.setColor("Black"); System.out.println("After modification of b3 values,"); System.out.println("which is created using assignment operator:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("[b3] " + b3); System.out.println("*******************************************"); /** * Changes of c1 values applied to c3. */ b1.setRadius(30); b1.setColor("Pink"); System.out.println("After modification of b1 values:"); System.out.println("[b1] " + b1); System.out.println("[b2] " + b2); System.out.println("[b3] " + b3); System.out.println("*******************************************"); } }

Output

Original values of b1: Radius: 10.0, Color: Red ******************************************* After modification of b2 values, which is created using copy constructor: [b1] Radius: 10.0, Color: Red [b2] Radius: 20.0, Color: Black ******************************************* After modification of b3 values, which is created using assignment operator: [b1] Radius: 20.0, Color: Black [b2] Radius: 20.0, Color: Black [b3] Radius: 20.0, Color: Black ******************************************* After modification of b1 values: [b1] Radius: 30.0, Color: Pink [b2] Radius: 20.0, Color: Black [b3] Radius: 30.0, Color: Pink *******************************************

29 Jan 2013

Enable Maven in Existing Eclipse Projects

A hands-on guide to enable Maven in existing Java projects in Eclipse.

In order to facilitate the build process in the software development life cycle, Maven can be used as a software project management and comprehension tool.

Projects are first configured as Maven-enabled. The POM (Project Object Model) file, which is an XML file (pom.xml), contains information about the project and configuration details used by Maven to build project. Configuration includes project dependencies, the plugins or goals that can be executed, the build profiles and so on.

Figure 1: Architecture of the Development Environment

Installation

  1. Install and Configure Maven.

Design

Suppose that three projects are created: myapp-common, myapp-web and myapp-admin.  As shown in Figure 4, myapp-web and myapp-admin have dependency on myapp-common, and this is configured in the POM file.

Figure 4: Projects Dependancies

Implementation

  1. Enable Maven for an existing Eclipse Project.
    1. Execute the following goal to create a maven project:
      mvn archetype:generate -DgroupId=com.blogspot.adaprognotebook-DartifactId=myapp-common -DarchetypeArtifactId=maven-archetype-quickstart mvn archetype:generate -DgroupId=com.blogspot.adaprognotebook-DartifactId=myapp-web -DarchetypeArtifactId=maven-archetype-webapp -Dpackage=war mvn archetype:generate -DgroupId=com.blogspot.adaprognotebook-DartifactId=myapp-admin -DarchetypeArtifactId=maven-archetype-webapp -Dpackage=war
    2. A directory with following structure is created for archetypes maven-archetype-quickstart:
      myapp-common |-- pom.xml |-- src |-- main |-- java |-- resources |-- test |-- java
    3. A directory with following structure is created for archetypes maven-archetype-webapp:
      myapp-web |-- pom.xml |-- src |-- main |-- resources |-- webapp |-- WEB-INF |-- web.xml |-- index.jsp myapp-admin |-- pom.xml |-- src |-- main |-- resources |-- webapp |-- WEB-INF |-- web.xml |-- index.jsp
    4. Create a /src/main/java directory in the myapp-web and myapp-admin projects.  Put the java source files into the /src/main/java directory of the myapp-common, myapp-web and myapp-admin projects appropriately.
    5. Put all the web content files into the /src/main/webapp directory of the myapp-web and myapp-admin projects appropriately.
    6. Generate the eclipse configuration files such that the project is eclipse-enabled:
      mvn eclipse:clean eclipse:eclipse -Dwtpversion=2.0
      The following eclipse configuration files will be generated:
      • .project and .classpath files
      • .setting/org.eclipse.jdt.core.prefs with project specific compiler settings
      • various configuration files for WTP (Web Tools Project), if the parameter wtpversion is set to a valid version (WTP configuration is not generated by default)
    7. Import the existing eclipse project from the File System (see Figure 5).
      Figure 5: Import existing projects into Workspace

    8. Configure eclipse to use the JRE of JDK (see Figure 6).
      Figure 6: Configure Installed JRE
    9. Add the following libraries in Proejct -> Preference -> Java Build Path:
      • Server Runtime
      • Web App Libraries
  2. Configure the connection to the DEV Nexus Server
    1. In the development client PC, open ${USER_HOME}\.m2\settings.xml using a text editor. If it does not exist, copy one from ${MAVEN_HOME}\conf\settings.xml.
    2. Add the following lines in the <servers> section:
      <servers> <server> <id>nexus-releases</id> <username>deployment</username> <password>test123</password> </server> <server> <id>nexus-snapshots</id> <username>deployment</username> <password>test123</password> </server> </servers>
    3. Add the following lines in the section:
      <mirror> <id>nexus</id> <name>nexus public mirror</name> <url>http://{Host of the Nexus server}/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror>
    4. In the POM file, where deployment to Nexus Server is required, add the following lines under the section:
      <distributionManagement> <repository> <id>nexus-snapshots</id> <name>Samvo Repository for Maven</name> <url>http://{Host of the Nexus server}/nexus/content/repositories/snapshots/</url> </repository> </distributionManagement>
  3. Configure the connection from Maven of the Build Server to the Development Tomcat Server
    1. In Build Server, open ${USER_HOME}\.m2\settings.xml using a text editor. If it does not exist, copy one from ${MAVEN_HOME}\conf\settings.xml.
    2. Add the following lines in the section:
      <server> <id>tomcat</id> <username>ad</username> <password>test123</password> </server>
References
  1. Maven, http://maven.apache.org/
  2. Introduction to Archetypes, http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
  3. Hudson, http://hudson-ci.org/
  4. Maven Central Repository Browser, http://search.maven.org/
  5. MVN Repository, http://mvnrepository.com/
  6. MVN Browser, http://www.mvnbrowser.com/
  7. Using Maven profiles and resource filtering, http://www.manydesigns.com/documentation/tutorials/using-maven-profiles-and-resource-filtering.html