Spring Integration Webapp( JPA/Hibernate | Webservices (JAX-WS|Apache CXF) | JQuery | EJB3 | Maven | Jboss | Annoatations)

Spring is one of the best Framework’s around in J2EE field which has made integration to various Framework pretty easy. This article is in series of articles which teach about spring integration in details. All source code is checked into svn which can be checked out and tested.

Assumption : Readers has basic knowledge of Servlets and JSP technologies.

Background : Sample project named testspring has been create in modular manner using Maven.  I used 2 differrent database schema names of mysql testjpa/testejb to test this project. I have configured

<property name=”hibernate.hbm2ddl.auto” value=”update” />

so that once proper connectivity is made program on startup will generate all tables in database

Any framework that has to integrated into J2EE frontend technology has to get control from Servlet Container, As all “http”(There can be other requests RMI/JMS..etc) requests are passed on to servlet container first which inturn based on configuration passes to corresponding framework.

All the integrations are generally handle using servlet mapping, or using listeners or filters in Webapplication.

To to integrate spring in webapplication we have to do the following

  • Configure spring Listener and Servlet in web.xml
	<!-- Configuration files for spring  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
            /WEB-INF/spring/app-config.xml,
            /WEB-INF/spring/app-*-config.xml
        </param-value>
	</context-param>

	<!-- Spring listeners -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

Now that you have configured spring now start configuring spring beans in Spring configuration files.( As per the above configuration file. All requests with url pattern services/* will get routed to spring dispatcher servlet. Which inturn takes care of request mapping based on Configuration files or using Annotations.

if you have observerd the spring servlet name is “dispatcher”

so the configuration file that is read by default will be dispatcher-servlet.xml and then other files with same name pattern defined in context-param will get loaded.

Attached is my Dispatcher Servlet Sample code

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<resources mapping="/assets/**" location="/assets/" />
	<resources mapping="/CSS/**" location="/CSS/" />
	<resources mapping="/images/**" location="/images/" />
	<resources mapping="/JS/**" location="/JS/" />
	<resources mapping="/swfupload/**" location="/swfupload/" />
	
	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory 
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>       
    -->

	<!-- freemarker config -->
	<beans:bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<beans:property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
		<beans:property name="freemarkerVariables">
			<beans:map>
				<beans:entry key="xml_escape" value-ref="fmXmlEscape" />
			</beans:map>
		</beans:property>
	</beans:bean>

	<beans:bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />

	<!--

		View resolvers can also be configured with ResourceBundles or XML
		files. If you need different view resolving based on Locale, you have
		to use the resource bundle resolver.
	-->
	<beans:bean id="viewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<beans:property name="cache" value="true" />
		<beans:property name="prefix" value="" />
		<beans:property name="suffix" value=".html" />

		<!--
			if you want to use the Spring FreeMarker macros, set this property to
			true
		-->
		<beans:property name="exposeSpringMacroHelpers" value="true" />

	</beans:bean>
    
       
       
</beans:beans>

Above code has Configuration to define view resolver (We can integrate different View Technologies in it JSP/Freemarker/Velocity…etc). I’m using freemarker in my example. It also has a commented code which uses jsp as view resolver

The above configuration is basic configuration for Spring MVC. If you have observed it we have defined that spring should discover beans by itself based on annotations rather than configuration stuff. Annotations make is easier for developer to define beans avoiding unnecessary xml configuration which is difficult to remember

Above configuration also lists out url pattern’s that have to be excluded in this flow (Example all css/js/images etc files ).

Below is same Spring MVC controller which is configured through annotations

C:\development\linkwithweb\svn\SpringTutorials\testspring\images

/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.northalley.template.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.northalley.template.services.WelcomeService;

/**
 * @author ashwin kumar
 * 
 */
@Controller
public class GenericController {

	@Autowired
	@Qualifier("welcomeService")
	private WelcomeService welcomeService;

	/**
	 * Constructor
	 */
	public GenericController() {
	}

	/**
	 * @param searchCriteria
	 * @param formBinding
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	@ResponseBody
	public Response search(SearchCriteria searchCriteria,
			BindingResult formBinding, WebRequest request) {
		Response response = new Response();
		response.setWelcome(welcomeService.hello("Ashwin"));

		return response;
	}

	@RequestMapping("/freemarkertest")
	public ModelAndView hello() {
		return new ModelAndView("hello", "greeting", "Hello world!");
	}

}

If you look into the code it defines that this class is a Spring MVC Controller(Class that handles all requests)

All the methods that you want to expose in this class should get a RequestMapping annotation with URL pattern and corresponding attributes defined for that

Ex:@RequestMapping(value = “/test”, method = RequestMethod.POST)   (This says any URL with services/test has to be handled by this method).. “services” is base url for spring requests which is configured in web.xml

You can directly render response from here using ResponseBody annotation or you can create model object and pass it to View Technology to render that by creating ModelAndView as response object

In the above example i have @ResponseBody annotation configured for one method and i have configured to receive JSON repsonse for any ResponseBody annotation in my app-config.xml

	<!--
		Define the OXM marshaller which is used to convert the Objects <->
		JSON/XML.
	-->
	<beans:bean id="oxmMarshaller"
		class="org.springframework.oxm.xstream.XStreamMarshaller" />

	<beans:bean id="marshallingHttpMessageConverter"
		class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
		<beans:property name="marshaller" ref="oxmMarshaller" />
		<beans:property name="unmarshaller" ref="oxmMarshaller" />
	</beans:bean>

	<!--
		Required for REST services in order to bind the return value to the
		ResponseBody.
	-->
	<beans:bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<beans:property name="messageConverters">
			<util:list id="beanList">
				<ref bean="marshallingHttpMessageConverter" />
			</util:list>
		</beans:property>
	</beans:bean>

Here is sample Spring MVC Design

Read more about spring MVC in

 

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

The below one is one of best link which explains all different way you can use Rest Style URL in Spring MVC

http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

 

Now that i have given basic Introduction to Spring MVC integration i’ll take you through JPA Configuration both in Tomcat as well as JBOSS.

JPA can be used in 2 ways ( Using Spring and Using EJB3 container) . As we don’t have EJB container in tomcat we use spring to configure JPA for us.

Let me start with JPA/Hibernate  using Spring in tomcat

I have configured JPA in app-jpa-config.xml in testspring-webapp/WEB-INF/spring   folder (Checkout code from svn to see project structure)

Here is snippet of the JPA config file

<?xml version="1.0" encoding="UTF-8"?>


<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


	<tx:annotation-driven transaction-manager="transactionManager" />

	<!--
        Data source
    -->
	<beans:bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<beans:property name="driverClassName">
			<beans:value>org.hsqldb.jdbcDriver</beans:value>
		</beans:property>
		<beans:property name="url">
			<beans:value>jdbc:hsqldb:mem:testspring-db</beans:value>
		</beans:property>
		<beans:property name="username">
			<beans:value>sa</beans:value>
		</beans:property>
		<beans:property name="password">
			<beans:value></beans:value>
		</beans:property>
	</beans:bean>

	<!--
        Configuration for Hibernate/JPA
    -->
	<beans:bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<beans:property name="persistenceUnitName" value="testspring-jpa" />
		<beans:property name="dataSource" ref="dataSource" />
		<beans:property name="jpaDialect">
			<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
		</beans:property>
		<beans:property name="jpaVendorAdapter">
			<beans:bean
				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<beans:property name="showSql" value="false" />
				<beans:property name="generateDdl" value="true" />
				<beans:property name="databasePlatform"
					value="org.hibernate.dialect.HSQLDialect" />
			</beans:bean>
		</beans:property>
	</beans:bean>

	<beans:bean id="transactionManager"
		class="org.springframework.orm.jpa.JpaTransactionManager">
		<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
		<beans:property name="dataSource" ref="dataSource" />
	</beans:bean>

</beans:beans>

As you see here we have configured persistenceUnitName as “testspring-jpa”. This name has been configured in persistence.xml of  testspring-java project module. Here is the snippet for that

<persistence
  xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">

 <persistence-unit name="testspring-jpa">
 </persistence-unit>

</persistence>

And now that we have configured spring we can create entities in JPA style and use EntityManager to fire our queries.

Here is the sample welcomeService that is used in our Spring MVC controller

/*
  
 */

package org.northalley.template.testspring.services;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.northalley.template.testspring.entities.Welcome;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("welcomeService")
public class WelcomeServiceImpl implements WelcomeService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public Welcome hello(String name) {
		if (name == null || name.trim().length() == 0)
			throw new RuntimeException("Name cannot be null or empty");

		Welcome welcome = null;
		try {
			Query q = entityManager
					.createQuery("select w from Welcome w where w.name = :name");
			q.setParameter("name", name);
			welcome = (Welcome) q.getSingleResult();
		} catch (NoResultException e) {
			welcome = new Welcome();
			welcome.setName(name);
			welcome.setMessage("Welcome " + name);
			entityManager.persist(welcome);
		}
		return welcome;
	}
}

Now Lets switch to other part of using JPA with EJB ( This can be tested in jboss)

Here is sample snippet of stateless session bean in testspring-ejb project that is using JPA to get list of users

package org.testspring.ejb.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testspring.ejb.entity.User;

/**
 * @author ashwin kumar
 * 
 */
@Stateless(mappedName = "userDAOBean")
public class UserDAOBean implements UserDAO {
	private static Log log = LogFactory.getLog(UserDAOBean.class);

	private EntityManager em;

	@PersistenceContext(unitName = "TestEntityManager")
	public void setEm(EntityManager em) {
		this.em = em;
	}

	public List<User> getUsers() {
		log.debug("getUsers");
		List<User> users = this.em.createQuery(
				"select user from User user order by user.lastName")
				.getResultList();
		return users;
	}

	// not using this method in this example app
	public User saveUser(User user) {
		log.debug("saveUser: " + user);
		this.em.persist(user);
		this.em.flush();
		return user;
	}
}

Below is code of User Entity i have created

package org.testspring.ejb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
//if the table name matches your class name, you don't need the 'name' annotation
@Table(name="lt_user") 
public class User extends BaseEntity {
     
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    //we don't need the Column annotation here bc the column name and this field name are the same
    private Long id;
    
    @Column(name="first_name",nullable=false,length=50)
    private String firstName;
    
    @Column(name="last_name",nullable=false,length=50)
    private String lastName;
    
    private Date birthday;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
 
}

Now that we are done with basic end to end integration .I’ll also show how to inject EJB in Servlet/Spring MVC

Below is sample servlet which called stateless bean . Same method is used in spring MVC too

package com.testspring.servlet;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.testspring.ejb.dao.UserDAO;
import org.testspring.ejb.entity.User;

import com.testspring.service.MathService;

/**
 * @author ashwin kumar
 * 
 */
public class UserAdminServlet extends HttpServlet {

	@EJB(mappedName = "testspring-ear-0.0.1-SNAPSHOT/UserDAOBean/local")
	private UserDAO userDao;

	WebApplicationContext context = null;


	/**
	 * @param filterConfig
	 * @throws ServletException
	 */
	public void init(ServletConfig filterConfig) throws ServletException {
		System.out.println("Inti calleddddddddddddddd");
		super.init(filterConfig);
		ServletContext servletContext = filterConfig.getServletContext();
		context = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		// set up an injected bean into application scope
		// servletContext.setAttribute("mybean", context.getBean("aBean"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println(((MathService) context.getBean("mathService")).add(
				1, 2));

		if (userDao == null) {
			try {
				Hashtable environment = new Hashtable();
				environment.put(Context.INITIAL_CONTEXT_FACTORY,
						"org.jnp.interfaces.NamingContextFactory");
				environment.put(Context.URL_PKG_PREFIXES,
						"org.jboss.naming:org.jnp.interfaces");
				environment.put(Context.PROVIDER_URL, "jnp://localhost:1099"); // remote
				// machine
				// IP

				Context context = new InitialContext(environment);
				userDao = (UserDAO) context
						.lookup("testspring-ear-0.0.1-SNAPSHOT/UserDAOBean/local");
				// context.lookup("userDAOBean");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<User> users = userDao.getUsers();
		for (int i = 0; i < users.size(); i++) {
			response.getWriter().append(
					"UserName :" + users.get(i).getFirstName() + " "
							+ users.get(i).getLastName()).append("<br/>");
			response.getWriter().append("id :" + users.get(i).getId()).append(
					"<br/>");
		}
	}
}

 

Now that i have covered all topics from frontend to db, now i’ll take you though how to configure webservices. I’ll show examples with both Apache CXF and JAX-WS

 

Webservices

Similar to spring MVC configuration through servlet , we have to configure both JAX-WS and Apache CXF through their own servlets. Below is servlet configuration to enable them

<!--  jax-ws -->
	<servlet>
		<servlet-name>jaxws-servlet</servlet-name>
		<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>jaxws-servlet</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>



<!-- CXF Configuration -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>
			 org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>

Now that we have configured respective URL patterns now lets see configuration in Spring that is required to expose Beans as webservices

Webservice configuration for spring is configured in app-ws-config.xml

<?xml version="1.0" encoding="UTF-8"?>

	<!--
	-->

<beans:beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"
	xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://jax-ws.dev.java.net/spring/core  http://jax-ws.dev.java.net/spring/core.xsd
        http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">


	<!-- Web service config for Apache CXF -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="orderProcess"
		implementor="org.northalley.template.testspring.controller.OrderProcessImpl"
		address="/OrderProcess" />

	<!-- Web service config for JAX-WS -->

	<wss:binding url="/ws">
		<wss:service>
			<ws:service bean="#helloWs" />
		</wss:service>
	</wss:binding>


	<beans:bean id="helloWs"
		class="org.northalley.template.testspring.controller.WebserviceController">
	</beans:bean>


</beans:beans>

Below is how i have expose OrderService as webservice for apache CXF

package org.northalley.template.testspring.controller;

import javax.jws.WebService;

@WebService(endpointInterface = "org.northalley.template.testspring.controller.OrderProcess")
public class OrderProcessImpl implements OrderProcess {

    public String processOrder(Order order) {
        return order.validate();
    }
}

Below is code of bean which is exposed by JAX-WS

/*
 * 
 */
package org.northalley.template.testspring.controller;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import org.northalley.template.testspring.services.WelcomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * @author ashwin kumar
 * 
 */
@WebService(name = "/hello")
@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL)
public class WebserviceController {

	@Autowired
	@Qualifier("welcomeService")
	private WelcomeService welcomeService;

	/**
	 * Constructor
	 */
	public WebserviceController() {
	}

	/**
	 * @param searchCriteria
	 * @param formBinding
	 * @param request
	 * @return
	 */
	@WebMethod(operationName = "getMessage")
	public String search() {
		return welcomeService.hello("Ashwin").getMessage();

	}

	@WebMethod(operationName = "sayHello")
	public String sayHelloToTheUser(@WebParam(name = "name") String userName) {
		return "Testing: " + " " + userName;
	}

}

Sample WSDL URL’s

http://localhost:8080/testspring-webapp/webservice/OrderProcess?wsdl

http://localhost:8080/testspring-webapp/ws?wsdl

 

And finally one more thing which i haven’t used in example. I have configured HSQLDB to used so that any demo’s need not have an external database but have Inbuilt DB

Here is how you do that

	<!-- H2 Database Console for managing the app's database -->
	<servlet>
		<servlet-name>H2Console</servlet-name>
		<servlet-class>org.h2.server.web.WebServlet</servlet-class>
		<init-param>
			<param-name>-webAllowOthers</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>H2Console</servlet-name>
		<url-pattern>/admin/h2/*</url-pattern>
	</servlet-mapping>

 

If you use http://localhost:8080/appname/admin/h2 you will goto to HSQLDB Admin console

I think i have covered all concepts and code snippets to get you started.

Code has been checked in to

https://linkwithweb.googlecode.com/svn/trunk/SpringTutorials/testspring

 

 

 

Checkout

mvn clean install package

testspring-war ( Webapp code for ear file)  (To be deployed on Jboss

testspring-ejb (Has ejb code)

testspring-ear  — Combining both ejb and war to create ear

———————————————–

testspring-java (Java code used in tomcat)

testspring-webapp ( Web configuration for tomcat)

to test on tomcat follow the below steps

cd testspring-webapp

mvn tomcat:run  and njoy playing with code

Njoy playing with code. Get back to me if you have any questions.

 

 

 

 

 

 

 

 

Liferay Build From Source and Deploy to Local Maven Repository

First of all download source from

http://www.liferay.com/downloads/liferay-portal/available-releases or you can download from svn using following command

svn –username guest co svn://svn.liferay.com/repos/public/portal/trunk portal-trunk

Once you download source extract to one Folder.(Dont have long path it might cross maximum path length in windows if you store this deep inside some path)

Prerequisites

———————–

ANT: Download apache ant and place it in path

Below one is needed for deployment to maven repository

GPG : http://www.gpg4win.org/download.html  (You might have to get older version as build searches for gpg.exe )

Older version can be downloaded from http://ftp.gpg4win.org/

If you want to setup maven repository on your machine then do the following

  1. Download nexus(Opensource Maven Repository) from http://nexus.sonatype.org/downloads/nexus-oss-webapp-1.9.2-bundle.tar.gz
  2. Follow following instructions for Installation http://www.sonatype.com/books/nexus-book/reference/install.html
  3. Start nexus and Open your browser to your newly created nexus (http://localhost:8081/nexus)
  4. Login as administrator (default login is admin / admin123) and Change your password
  5. Go to Repositories and click Add -> Hosted Repository
  6. Create Hosted Repository
  • Repository ID: liferay-ce-snapshots
  • Repository Name: Liferay Snapshot Repository
  • Provider: Maven2 Repository
  • Repository Policy: Snapshot
Now configure your maven to point to local repository
<?xml version="1.0" encoding="UTF-8"?> 
<settings> 
	<mirrors> 
		<mirror> 
			<id>local</id> 
			<name>Local mirror repository</name> 
			<url>http://localhost:8081/nexus/content/groups/public</url> 
			<mirrorOf>*</mirrorOf> 
		</mirror> 
	</mirrors>
	<servers> 
		<server> 
			<id>liferay</id> 
			<username>admin</username> 
			<password>ashwin@123</password> 
		</server> 
	</servers> 
</settings>

Now Goto to folder where you have liferay source

Open build.properties  and paste following in bottom  ( You can use other property file names . Just look at build-common.xml for more property filenames you can use)

 

maven.url=http://localhost:8081/nexus/content/repositories/liferay-ce-snapshots
gpg.passphrase=ashwin
gpg.keyname=Ashwin
lp.version=6.0.6
maven.repository.id=liferay
maven.version=${lp.version}-SNAPSHOT

 

Now Lets generate Key File using GPG which we downloaded before

LiferayGPG
LiferayGPG

 

Once you generate the key. you can now use it in deployment.

Now follow these steps

  1. C:\liferay-portal-src-6.0.6>set ANT_OPTS=”-Xmx1024m -XX:MaxPermSize=256m”
  2. ant jar
  3. ant -f build-maven.xml deploy-artifacts ( This will deploy all the Liferay Artifacs to maven repository you have just configured using nexus)
Now njoy using maven with liferay. Next articles would be to create portlets in Liferay and Using liferay to create portals

     

     

     

     

     

     

     

     

    jsoup HTMLParser and Parsing Dzone Links using CSS Selectors in Java

    I was working on a task to parse some of Amazon web-services. There are lots of ways to parse it Using DOM/SAX/Stax .  All of them require some amount of coding. I wanted a quick fix and i finally landed on to JSoup an opensource HTML Parser ( Other html parser i like is HTMLParser) . In this article i’m going to explain how i’m going to parse DZone HTML links in java.

    I’ll be retreiving description’s of all links in Dzone using the code

    Note: This is not the best way to read links from Dzone ( You can use rss feed’s instead).  This tutorial is to take you through css selectors for Java

    All DZone pagination queries looks like this

    http://www.dzone.com/links/?type=html&p=2

    i used an opensource java library to parse this and extract link text description (jsoup)

    Here is sample tags we have in dzone response

    <a name="link-613399">
    </a>
    
    <div class="linkblock frontpage " id="link-613399">
    	<div id="thumb_613399" class="thumb">
    		<a onmouseup="track(this, 'twitter4j_oauth_on_android', ''); "
    			href="http://www.xoriant.com/blog/mobile-application-development/twitter4j-oauth-on-android.html">
    			<img width="120" height="90"
    				src="http://cdn.dzone.com/links/images/thumbs/120x90/613399-1307624607000.jpg"
    				class="thumbnail" alt="Link 613399 thumbnail"
    				onmouseover="return OLgetAJAX('/links/themes/reader/jsps/nodecoration/thumb-load.jsp?linkId=613399', OLcmdExT1,
     300, 'bigThumbBody');"
    				onmouseout="OLclearAJAX(); nd(100);" />
    		</a>
    	</div>
    	<div id="hidden_thumb_613399">
    
    	</div>
    	<div class="tools">
    	</div>
    	<div class="details">
    		<div class="vwidget" id="vwidget-613399">
    			<a id="upcount-613399" href="#" class="upcount"
    				onclick="showLoginDialog(613399, null); return false">7</a>
    
    			<a id="downcount-613399" href="#"
    				onclick="showLoginDialog(613399, null); return false;" class="downcount">0</a>
    		</div>
    		<h3>
    			<a onmouseup="track(this, 'twitter4j_oauth_on_android', ''); "
    				href="http://www.xoriant.com/blog/mobile-application-development/twitter4j-oauth-on-android.html"
    				rel="bookmark"> Twitter4j OAuth on Android</a>
    		</h3>
    		<p class="voteblock">
    			<a href="/links/users/profile/811805.html">
    				<img width="24" height="24"
    					src="http://cdn.dzone.com/links/images/std/avatars/default_24.gif"
    					class="avatar" alt="User 811805 avatar" />
    			</a>
    		</p>
    		<p class="fineprint byline">
    			<a href="/links/users/profile/811805.html">RituR</a>
    			via
    			<a href="/links/search.html?query=domain%3Axoriant.com">xoriant.com</a>
    		</p>
    		<p class="fineprint byline">
    			<b>Promoted: </b>
    			Jun 08 / 17:27. Views:
    			520, Clicks: 266
    		</p>
    		<p class="description">
    			OAuth is an open protocol
    			which allows the users to share their private information and assets
    			like photos, videos etc. with another site...&nbsp;
    			<a href='/links/twitter4j_oauth_on_android.html'>more&nbsp;&raquo;
    			</a>
    		</p>
    		<p class="fineprint stats">
    			<a
    				href="http://twitter.com/home?status=RT+%40DZone+%22Twitter4j+OAuth+on+Android%22+http%3A%2F%2Fdzone.com%2FTBxR"
    				class="twitter">Tweet</a>
    			<a href="/links/twitter4j_oauth_on_android.html" class="comment">0
    				Comments</a>
    			<span class="linkUnsaved" id="save-link-613399"
    				onclick="showLoginDialog(613399); return false;">Save</span>
    			<span class="linkUnshared" id="share-link-613399"
    				onclick="showLoginDialog(613399); return false;">Share</span>
    			Tags:
    			<a href="/links/tag/mobile.html" class="tags" rel="tag">mobile</a>
    			,
    			<a href="/links/tag/standards.html" class="tags" rel="tag">standards</a>
    		</p>
    
    	</div>
    </div>

     

    To get description we have to get data from element “P” with class “description” which is actually present in DIV with class “details” Here is how we can do that in java

     

    /**
     * 
     */
    package com.linkwithweb.parser;
    
    import java.io.File;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    /****************************************************************
     * Description
     * jsoup elements support a CSS (or jquery) like selector syntax to find matching elements, that allows very powerful and robust queries.
     * 
     * The select method is available in a Document, Element, or in Elements. It is contextual, so you can filter by selecting from a specific element, or
     * by chaining select calls.
     * 
     * Select returns a list of Elements (as Elements), which provides a range of methods to extract and manipulate the results.
     * 
     * Selector overview
     * tagname: find elements by tag, e.g. a
     * ns|tag: find elements by tag in a namespace, e.g. fb|name finds <fb:name> elements
     * #id: find elements by ID, e.g. #logo
     * .class: find elements by class name, e.g. .masthead
     * [attribute]: elements with attribute, e.g. [href]
     * [^attr]: elements with an attribute name prefix, e.g. [^data-] finds elements with HTML5 dataset attributes
     * [attr=value]: elements with attribute value, e.g. [width=500]
     * [attr^=value], [attr$=value], [attr*=value]: elements with attributes that start with, end with, or contain the value, e.g. [href*=/path/]
     * [attr~=regex]: elements with attribute values that match the regular expression; e.g. img[src~=(?i)\.(png|jpe?g)]
     * : all elements, e.g. *
     * Selector combinations
     * el#id: elements with ID, e.g. div#logo
     * el.class: elements with class, e.g. div.masthead
     * el[attr]: elements with attribute, e.g. a[href]
     * Any combination, e.g. a[href].highlight
     * ancestor child: child elements that descend from ancestor, e.g. .body p finds p elements anywhere under a block with class "body"
     * parent > child: child elements that descend directly from parent, e.g. div.content > p finds p elements; and body > * finds the direct children of
     * the body tag
     * siblingA + siblingB: finds sibling B element immediately preceded by sibling A, e.g. div.head + div
     * siblingA ~ siblingX: finds sibling X element preceded by sibling A, e.g. h1 ~ p
     * el, el, el: group multiple selectors, find unique elements that match any of the selectors; e.g. div.masthead, div.logo
     * Pseudo selectors
     * :lt(n): find elements whose sibling index (i.e. its position in the DOM tree relative to its parent) is less than n; e.g. td:lt(3)
     * :gt(n): find elements whose sibling index is greater than n; e.g. div p:gt(2)
     * :eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1)
     * :has(seletor): find elements that contain elements matching the selector; e.g. div:has(p)
     * :not(selector): find elements that do not match the selector; e.g. div:not(.logo)
     * :contains(text): find elements that contain the given text. The search is case-insensitive; e.g. p:contains(jsoup)
     * :containsOwn(text): find elements that directly contain the given text
     * :matches(regex): find elements whose text matches the specified regular expression; e.g. div:matches((?i)login)
     * :matchesOwn(regex): find elements whose own text matches the specified regular expression
     * Note that the above indexed pseudo-selectors are 0-based, that is, the first element is at index 0, the second at 1, etc
     * See the Selector API reference for the full supported list and details.
     * 
     * @author Ashwin Kumar
     * 
     */
    public class HTMLParser {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		try {
    			File input = new File("input/dZoneLinks.xml");
    			Document doc = Jsoup.parse(input, "UTF-8",
    					"http://www.dzone.com/links/?type=html&p=2");
    
    			Elements descriptions = doc.select("div.details > p.description"); // get all description elements in this HTML file
    			/*
    			 * Elements pngs = doc.select("img[src$=.png]");
    			 * // img with src ending .png
    			 * 
    			 * Element masthead = doc.select("div.masthead").first();
    			 */
    			// div with
    
    			// Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
    			/**
    			 * Iterate over all descriptions and display them
    			 */
    			for (Element element : descriptions) {
    				System.out.println(element.ownText());
    				System.out.println("--------------");
    			}
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }

    Mavenized code has been checked in to svn at following location

    http://code.google.com/p/linkwithweb/source/browse/trunk/Utilities/HTMLParser

    Njoy parsing anything easily using jsoup

     

    PDF Generation Using Templates and OpenOffice and Itext in Java


    Recently i had a task where in i had to create PDF dynamically by merging data and template. I have been using lots of pdf libraries since i started my programming to do such tasks. The previous one i admired a lot was Jasper Reports. But after researching some more time i found out that IText with Openoffice Draw is a simplest way we can generate PDF forms.

    This visual tutorial first Explain’s how to Create PDF forms in Openoffice Draw

    Save PDF to folder where you have your program or just change path for input source in program

    Now here is java code which can convert this pdf to final pdf by filling in Values using IText

    /**
     *
     */
    package com.linkwithweb.reports;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    
    /**
     * @author Ashwin Kumar
     *
     */
    public class ITextStamperSample {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 * @throws DocumentException
    	 */
    	public static void main(String[] args) throws IOException,
    			DocumentException {
    		// createSamplePDF();
    		generateAshwinFriends();
    	}
    
    	/**
    	 *
    	 */
    	private static void generateAshwinFriends() throws IOException,
    			FileNotFoundException, DocumentException {
    		PdfReader pdfTemplate = new PdfReader("mytemplate.pdf");
    		FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);
    		stamper.setFormFlattening(true);
    
    		stamper.getAcroFields().setField("name", "Ashwin Kumar");
    		stamper.getAcroFields().setField("id", "1\n2\n3\n");
    		stamper.getAcroFields().setField("friendname",
    				"kumar\nsirisha\nsuresh\n");
    		stamper.getAcroFields().setField("relation", "self\nwife\nfriend\n");
    
    		stamper.close();
    		pdfTemplate.close();
    
    	}
    
    }
    
    

    Here is sample pom.xml

    <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.linkwithweb.reports</groupId>
      <artifactId>ReportTemplateTutorial</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>ReportTemplateTutorial</name>
      <description>ReportTemplateTutorial</description>
      <dependencies>
      	<dependency>
      		<groupId>com.lowagie</groupId>
      		<artifactId>itext</artifactId>
      		<version>2.1.7</version>
      	</dependency>
      </dependencies>
    </project>

     

    A pdf file with data filled in placeholders is created with name test.pdf


    OSGI for Beginners Using Maven with Equinox(HowTo)

    I have struggled to understand what OSGI really means for a long time. It has been around since a very long time but not many people are aware of it. It has been hyped as a very complex technology to understand. Here is my attempt to make it simple for any Java Developer. In my view if you understand concept of Interface , JNDI or EJB( Or registering some service in some form)  you will understand OSGI. In short OSGI is a Container where you can register your services through interfaces and those can be accessed any time. Another benefit of OSGI is that all these services can be Installed/Uninstalled/Started/Stopped at runtime (i.e Code can be hot deployed at runtime ) rather than normal requirement we have where we have restart J2EE server. Similar to J2ee containers (Tomcat, WebSphere, Jboss , Weblogic ) OSGI also has container like Equinox( Which is base for Eclipse), Apache Felix …etc

    In this article i’m going to explain OSGI with Eclipse Equinox container. Anyone who has Eclipse IDE installed on their machine has OSGI container also installed in Eclipse plugin’s folder.

    Name of OSGI container jar file looks like   org.eclipse.osgi_<version>.jar

    You can start OSGI like this

    java -jar org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console

    Attached is sample screenshot of how i started my OSGI container ( Its analogous to starting tomcat)

    Now that we started OSGI , let me start creation a HelloWorld OSGI Application using Maven. Lemme show you my project structure first and then display my pom.xml


    Now i’ll display my pom.xml. My pom.xml has 2 more profiles added to create 2 more new modules(MathService and MathServiceClient) which will be later explained in this article

    <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.linkwithweb.osgi</groupId>
    	<artifactId>HelloWorld</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>HelloWorld</name>
    	<dependencies>
    		<dependency>
    			<groupId>org.osgi</groupId>
    			<artifactId>org.osgi.core</artifactId>
    			<version>4.2.0</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<finalName>HelloWorld-${version}</finalName>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>2.3.1</version>
    				<configuration>
    					<source>1.5</source>
    					<target>1.5</target>
    				</configuration>
    			</plugin>
    
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-jar-plugin</artifactId>
    				<configuration>
    					<archive>
    						<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
    					</archive>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    	<profiles>
    		<profile>
    			<id>MathService</id>
    			<build>
    				<finalName>MathService-${version}</finalName>
    				<plugins>
    					<plugin>
    						<groupId>org.apache.maven.plugins</groupId>
    						<artifactId>maven-compiler-plugin</artifactId>
    						<version>2.3.1</version>
    						<configuration>
    							<source>1.5</source>
    							<target>1.5</target>
    						</configuration>
    					</plugin>
    
    					<plugin>
    						<groupId>org.apache.maven.plugins</groupId>
    						<artifactId>maven-jar-plugin</artifactId>
    						<configuration>
    
    							<excludes>
    								<exclude>**/*.xml</exclude>
    								<exclude>**/*.bsh</exclude>
    								<exclude>**/*.properties</exclude>
    							</excludes>
    							<archive>
    								<manifestFile>src/main/resources/MathService/META-INF/MANIFEST.MF</manifestFile>
    							</archive>
    						</configuration>
    					</plugin>
    
    				</plugins>
    			</build>
    		</profile>
    		<profile>
    			<id>MathServiceClient</id>
    			<build>
    				<finalName>MathServiceClient-${version}</finalName>
    				<plugins>
    					<plugin>
    						<groupId>org.apache.maven.plugins</groupId>
    						<artifactId>maven-compiler-plugin</artifactId>
    						<version>2.3.1</version>
    						<configuration>
    							<source>1.5</source>
    							<target>1.5</target>
    						</configuration>
    					</plugin>
    
    					<plugin>
    						<groupId>org.apache.maven.plugins</groupId>
    						<artifactId>maven-jar-plugin</artifactId>
    						<configuration>
    
    							<excludes>
    								<exclude>**/*.xml</exclude>
    								<exclude>**/*.bsh</exclude>
    								<exclude>**/*.properties</exclude>
    							</excludes>
    							<archive>
    								<manifestFile>src/main/resources/MathServiceClient/META-INF/MANIFEST.MF</manifestFile>
    							</archive>
    						</configuration>
    					</plugin>
    
    				</plugins>
    			</build>
    		</profile>
    	</profiles>
    
    </project>

    Now if you observe pom.xml there are 3 different MANIFEST.MF defined for 3 different OSGI bundle’s we create.  Saying so lemme explain you that OSGI bundles are same as Java jar file with its configuration defined in Manifest file of standard jar. OSGI defined few entries in manifest file which are related to OSGi which are read by container to activate the bundle, thus avoiding learning of any new metadata format we generally have for other frameworks

    Here is sample Manifest.MF i have defined for MathServiceClient

    Manifest-Version: 1.0
    Bundle-Name: MathServiceClient
    Bundle-Activator: com.linkwithweb.osgi.service.client.MathServiceClientActivator
    Bundle-SymbolicName: MathServiceClient
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework,com.linkwithweb.osgi.service

    If you observe above manifest file you can observer that all the entries except Manifest-Version are OSGI specific entries. These are the entries which define how to deploy a bundles what all it is dependent on and what are extension points it exposes for other services to consume.. etc

    Having explained this lemme first explain a HelloWorld bundle with its MANIFEST.MF and Activator class and its installation into Equinox OSGI Container

    /**
     * 
     */
    package com.linkwithweb.osgi;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    
    /**
     * @author Ashwin Kumar
     * 
     */
    public class HelloActivator implements BundleActivator {
    	public void start(BundleContext context) {
    		System.out.println("Hello World");
    	}
    
    	public void stop(BundleContext context) {
    		System.out.println("Goodbye All");
    	}
    }
    Manifest-Version: 1.0
    Bundle-Name: HelloWorld
    Bundle-Activator: com.linkwithweb.osgi.HelloActivator
    Bundle-SymbolicName: HelloWorld
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework

    Now run “mvn clean package” to build our bundle

    It will create HelloWorld-0.0.1-SNAPSHOT.jar in target folder and we can install that into Equinox to test. Here is the image which shows how to install and start our HelloWorld into Equinox

    install file:K:\Ashwin\OSGI\MavenProject\target\HelloWorld-0.0.1-SNAPSHOT.jar

    If you observe above screenshot you can use install command to install the bundle and use the start command on bundle id to start the bundle

    On start of Bundle start method of Activator class is called and while stopping bundle stop method of Activator is called and so you are seeing HelloWorld

    Congratulations you have learned basics of OSGI and you have just deployed your first bundle.

    Now lemme explain my second part of article which explains how to Expose and Consume services of modules

    Exposing and Consuming Services

    To explain this i’ll take a very simple example where i’ll publish a service which can add 2 numbers

    Here is the code which does that

    First we need to define an Interface which we are thinking of exposing to external bundles

    /**
     * 
     */
    package com.linkwithweb.osgi.service;
    
    /**
     * @author Ashwin Kumar
     * 
     */
    public interface MathService {
    
    	/**
    	 * @param a
    	 * @param b
    	 * @return
    	 */
    	public int add(int a, int b);
    }

    Now the implementation Class

    /**
     * 
     */
    package com.linkwithweb.osgi.service;
    
    /**
     * @author Ashwin Kumar
     *
     */
    public class MathServiceImpl implements MathService {
    
    	/* (non-Javadoc)
    	 * @see com.linkwithweb.osgi.service.MathService#add(int, int)
    	 */
    	public int add(int a, int b) {
    		// TODO Auto-generated method stub
    		return a+b;
    	}
    
    }

    Now Activator class which register’s this service with OSGI container

    /**
     * 
     */
    package com.linkwithweb.osgi.service;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    
    /**
     * @author Ashwin Kumar
     * 
     */
    public class MathServiceActivator implements BundleActivator {
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    	 */
    	public void start(BundleContext context) {
    		MathService service = new MathServiceImpl();
    		// Third parameter is a hashmap which allows to configure the service
    		// Not required in this example
    		context.registerService(MathService.class.getName(), service, null);
    		System.out.println("Math Service Registered");
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    	 */
    	public void stop(BundleContext context) {
    		System.out.println("Goodbye From math service");
    	}
    }

    Here is the manifest file which exposes service

    Manifest-Version: 1.0
    Bundle-Name: MathService
    Bundle-Activator: com.linkwithweb.osgi.service.MathServiceActivator
    Bundle-SymbolicName: MathService
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework
    Export-Package: com.linkwithweb.osgi.service

    If you observe this we are Exporting some packages so that they can be consumed later. Also all the package that we are thinking of importign have to be defined here

    Use the following command to build the jar file

    mvn -PMathService package

    and Here is image which displays how to Install and Run the service

    Now lemme explain how to implement consumer

    /**
     * 
     */
    package com.linkwithweb.osgi.service.client;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    
    import com.linkwithweb.osgi.service.MathService;
    
    /**
     * @author Ashwin Kumar
     * 
     */
    public class MathServiceClientActivator implements BundleActivator {
    	MathService service;
    	private BundleContext context;
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    	 */
    	public void start(BundleContext context) {
    		this.context = context;
    		// Register directly with the service
    		ServiceReference reference = context
    				.getServiceReference(MathService.class.getName());
    		service = (MathService) context.getService(reference);
    		System.out.println(service.add(1, 2));
    	}	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    	 */
    	public void stop(BundleContext context) {
    		System.out.println(service.add(5, 6));
    	}
    }

    And now manifest file

    Manifest-Version: 1.0
    Bundle-Name: MathServiceClient
    Bundle-Activator: com.linkwithweb.osgi.service.client.MathServiceClientActivator
    Bundle-SymbolicName: MathServiceClient
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework,com.linkwithweb.osgi.service

    Now here is how we create package and install

    mvn -PMathServiceClient package

    Source code have been checkedin to following location

    https://linkwithweb.googlecode.com/svn/trunk/osgi-tutorials/MavenProject

     

    Njoy creating OSGI bundles . Main benefit is you can redeploy your bundles/services at runtime.

    Mail me incase you have any doubts

    Smack , XMPP and GTalk

    I had to work on project which supports P2P file transfer and it is then when i thought that why not use an existing framework rather than building new one from scratch.

    I have worked on Smack/ Openfire  XMPP libraries around 6 years ago and Revisited the same for the above purpose

    Jabber /XMPP server’s support P2P File transfer protocol’s and follow Jingle Specification. So i thought to testing how to use smack api to build a Chat client in java. Next tutorial i’ll illustrate the same from Flex/Flash

    Here is sample code that can connect to GoogleTalk server and gets list of all friends.  This also shows example of how to Send a chat message to a given user.

    Here are few Terms you need to understand before you start using XMPP libraries

    Roster

    Roster is the name given to the list of contacts which appears in one’s chat client. In Google Talk terms, it is the list of friends you would see once you login. The elements of the roster are your contacts. Each of these items is identified by a unique identifier called JID (Jabber Identifier).

    Rosters are stored on the server so that the XMPP client may retrieve the same each time the user logs in.

    Message

    This is one of the three fundamental Stanzas in XMPP. (The other two are IQ and Presence.) Just as it is obvious, a Message Stanza is used for sending IMs (Instant Messages).

    IQ

    This is another very important Stanza. IQ stands for Info/Query. It is similar to traditional Request/Response paradigms such as HTTP. An IQ Stanza can be one of four types, “get”, “set”, “result” or “error”. The first two are requests or queries while the last two are responses or information. An IQ Stanza can wrap various kinds of elements. Let’s see one such example here.

    Juliet logs onto her XMPP client. The client sends an XMPP IQ Stanza of type “get” to gmail.com server requesting her roster.
    <iq from=”ashwin@gmail.com” type=”get” id=”roster_1″> <query xmlns=”jabber:iq:roster”/> </iq>

    Presence

    This is the third Stanza type specified by XMPP Core. In layman’s terms, Presence simply refers to the availability or status of a contact.

    Now that you are familiar with some basic terms you will come across the library, i’ll start pasting some working code which can be tested against gtalk

    Sample HelloWorld Code

    <pre>

    package com.linkwithweb.demos;
    
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.Roster;
    import org.jivesoftware.smack.RosterEntry;
    import org.jivesoftware.smack.SASLAuthentication;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    
    /**
     * @author Ashwin Kumar
     *
     */
    public class HelloWorld {
    	static JabberSmackAPI api = new JabberSmackAPI();
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		connectTOGTalk();
    	}
    
    	/**
    	 *
    	 */
    	private static void connectTOGTalk() {
    		// Create XMPP connection to gmail.com server
    		XMPPConnection connection = new XMPPConnection("gmail.com");
    
    		try {
    			// You have to put this code before you login(Required for Gtalk and not for Jabber.org)
    			SASLAuthentication.supportSASLMechanism("PLAIN", 0);
    			// Connect
    			connection.connect();
    
    			// Login with appropriate credentials
    			connection.login("madnesstestuser@gmail.com", "madnesstestuser");
    
    			// Get the user's roster
    			Roster roster = connection.getRoster();
    
    			// Print the number of contacts
    			System.out.println("Number of contacts: " + roster.getEntryCount());
    			api.setConnection(connection);
    			// Enumerate all contacts in the user's roster
    			for (RosterEntry entry : roster.getEntries()) {
    				System.out.println("User: " + entry.getUser());
    				if (entry.getUser().contains("pandumalladi")) {
    					Chat chat = connection.getChatManager().createChat(
    							entry.getUser(), api);
    					chat.sendMessage("Testing Hi");
    					api.fileTransfer(
    							"C:\\development\\Ashwin\\Jingle\\JitSI\\Test.txt",
    							entry.getUser());
    
    				}
    			}
    
    			connection.disconnect();
    		} catch (XMPPException e) {
    			// Do something better than this!
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 *
    	 */
    	public static void connectToJabber() {
    		ConnectionConfiguration cc = new ConnectionConfiguration("jabber.org",
    				5222, "jabber.org");
    		XMPPConnection connection = new XMPPConnection(cc);
    		try {
    			connection.connect();
    
    			// You have to put this code before you login
    			SASLAuthentication.supportSASLMechanism("PLAIN", 0);
    
    			// You have to specify your Jabber ID addres WITHOUT @jabber.org at the end
    			connection.login("your.jabber", "password", "resource");
    
    			// See if you are authenticated
    			System.out.println(connection.isAuthenticated());
    
    		} catch (XMPPException e1) {
    			e1.printStackTrace();
    		}
    	}
    }

    </pre>

    Sample utility Class used

    <pre>

    /**
     *
     */
    package com.linkwithweb.demos;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Collection;
    
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.Roster;
    import org.jivesoftware.smack.RosterEntry;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smackx.filetransfer.FileTransferManager;
    import org.jivesoftware.smackx.filetransfer.FileTransferNegotiator;
    import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
    
    /**
     * API With some Utitlity Methods
     * @author Ashwin Kumar
     *
     */
    public class JabberSmackAPI implements MessageListener {
    
    	XMPPConnection connection;
    	private FileTransferManager manager;
    
    	public void setConnection(XMPPConnection connection) {
    		this.connection = connection;
    		manager = new FileTransferManager(connection);
    	}
    
    	public JabberSmackAPI() {
    	}
    
    	public void login(String userName, String password) throws XMPPException {
    		ConnectionConfiguration config = new ConnectionConfiguration(
    				"localhost", 5222, "Work");
    		connection = new XMPPConnection(config);
    
    		connection.connect();
    		connection.login(userName, password);
    	}
    
    	public void fileTransfer(String fileName, String destination)
    			throws XMPPException {
    
    		// Create the file transfer manager
    		// FileTransferManager manager = new FileTransferManager(connection);
    		FileTransferNegotiator.setServiceEnabled(connection, true);
    		// Create the outgoing file transfer
    		OutgoingFileTransfer transfer = manager
    				.createOutgoingFileTransfer(destination);
    
    		// Send the file
    		transfer.sendFile(new File(fileName), "You won't believe this!");
    		try {
    			Thread.sleep(10000);
    		} catch (Exception e) {
    		}
    		System.out.println("Status :: " + transfer.getStatus() + " Error :: "
    				+ transfer.getError() + " Exception :: "
    				+ transfer.getException());
    		System.out.println("Is it done? " + transfer.isDone());
    	}
    
    	public void sendMessage(String message, String to) throws XMPPException {
    		Chat chat = connection.getChatManager().createChat(to, this);
    		chat.sendMessage(message);
    	}
    
    	public void displayBuddyList() {
    		Roster roster = connection.getRoster();
    		Collection<RosterEntry> entries = roster.getEntries();
    
    		System.out.println("\n\n" + entries.size() + " buddy(ies):");
    		for (RosterEntry r : entries) {
    			System.out.println(r.getName());
    		}
    	}
    
    	public void disconnect() {
    		connection.disconnect();
    	}
    
    	public void processMessage(Chat chat, Message message) {
    		if (message.getType() == Message.Type.chat)
    			System.out.println(chat.getParticipant() + " says: "
    					+ message.getBody());
    	}
    
    	public static void main(String args[]) throws XMPPException, IOException {
    		// declare variables
    		JabberSmackAPI c = new JabberSmackAPI();
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String msg;
    
    		// turn on the enhanced debugger
    		XMPPConnection.DEBUG_ENABLED = true;
    
    		// Enter your login information here
    		c.login("ashwin", "ashwin@123");
    
    		c.displayBuddyList();
    
    		System.out.println("-----");
    
    		System.out
    				.println("Who do you want to talk to? - Type contacts full email address:");
    		String talkTo = br.readLine();
    
    		System.out.println("-----");
    		System.out.println("All messages will be sent to " + talkTo);
    		System.out.println("Enter your message in the console:");
    		System.out.println("-----\n");
    
    		while (!(msg = br.readLine()).equals("bye")) {
    			c.sendMessage(msg, talkTo);
    		}
    
    		c.disconnect();
    		System.exit(0);
    	}
    
    }

    </pre>

    You can checkout the above mavenized project from SVN test it yourself. Have fun with Jabber

    https://linkwithweb.googlecode.com/svn/trunk/XMPP

    Derby Tutorial Mavenized with Unit Test and Loading Data from Excel

    Many applications requirement where they want to store some data at runtime and able to query using SQL. We wouldn’t like to install a full fledged database in such scenarios.
    Apache has come up with a light weight database named Derby which suits the purpose. I would like to publish usage of Apache derby in java and how to use it. This tutorial is targetted to any one who understands java

    This example rotates around a simple table person

    ID NAME FIRST_NAME BIRTH_DATE
    1 Jim Hammond 12/12/1974
    2 Robert Grass 2/6/1968
    3 John Kazyncksy 5/3/2000

    This data is loaded by program from an excel file to Derby Database

    As i’m using maven i’ll first define my pom here

    4.0.0
    com.ashwin.test.derby
    derby
    Derby Example
    1.0.0

    org.apache.maven.plugins
    maven-compiler-plugin

    1.5
    1.5

    org.apache.maven.plugins
    maven-surefire-plugin

    <!--

    Be sure that JAVA_HOME points to a JDK6 installation
    ${env.JAVA_HOME}/db/lib/derby.jar

    -->

    org.apache.maven.plugins
    maven-source-plugin

    attach-sources
    verify

    jar

    org.dbunit
    dbunit
    2.2.2
    test

    org.apache.derby
    derby
    10.6.2.1

    Now as we defined our POM. Now lets try to load some data into derby. As derby is inmemory database we dont need to start any db server before hand. I created a base class which return DB Connection url and driver class

    package com.linkwithweb.test.derby.dao;

    import org.dbunit.JdbcBasedDBTestCase;
    import org.dbunit.dataset.IDataSet;

    /**
    * An abstract Jdbc test case that factorizes the common methods.
    *
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public abstract class AbstractDaoTest extends JdbcBasedDBTestCase {

    /**
    * @see org.dbunit.JdbcBasedDBTestCase#getConnectionUrl()
    */
    @Override
    protected String getConnectionUrl() {
    return "jdbc:derby:myDB;create=true";
    }

    /**
    * @see org.dbunit.JdbcBasedDBTestCase#getDriverClass()
    */
    @Override
    protected String getDriverClass() {
    return "org.apache.derby.jdbc.EmbeddedDriver";
    }

    /**
    * @see org.dbunit.DatabaseTestCase#getDataSet()
    */
    @Override
    protected IDataSet getDataSet() throws Exception {
    return getConnection().createDataSet();
    }
    }

    Now let me create a class Table Creator which loads Data into the Database

    package com.linkwithweb.test.derby.dao;

    import java.sql.Statement;

    /**
    * Run once in order to create the tables.
    *
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public class TableCreator extends AbstractDaoTest {

    /**
    * Initializes the DB. Must be run once and only once to create structure.
    *
    * @throws Exception
    */
    public void initialize() throws Exception {

    Statement lStatement = getConnection().getConnection()
    .createStatement();

    lStatement
    .executeUpdate("CREATE TABLE PERSON (ID INT, NAME VARCHAR(60), FIRST_NAME VARCHAR(60), BIRTH_DATE DATE)");
    }

    /**
    * Calls the {@link #initialize()} method on a new instance.
    *
    * @param args
    * @throws Exception
    */
    public static void main(String[] args) throws Exception {

    new TableCreator().initialize();
    }
    }

    In the above code we have create a table person in our testDB

    Now letme create a GenericDAO which define any generic DAO contract

    package com.linkwithweb.test.derby.dao;

    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.Collection;

    /**
    * A DAO interface with common methods.
    *
    * @param
    * Object type of the DAO
    * @param
    * Object type of the unique key
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public interface IDao {

    /**
    * Returns the object denoted by this unique identifier in the back-end
    * system.
    *
    * @param pId
    * Unique identifier
    * @return Object
    * @throws SQLException
    * If an error occurs
    */
    T findById(K pId) throws SQLException;

    /**
    * Removes the object denoted by this unique identifier from the back-end
    * system.
    *
    * @param pId
    * Unique identifier
    * @throws SQLException
    * If an error occurs
    */
    void removeById(K pId) throws SQLException;

    /**
    * Returns all the object in the back-end system.
    *
    * @return Collection of objects
    * @throws SQLException
    * If an error occurs
    */
    Collection getAll() throws SQLException;
    }

    Now let me create Person DAO which acts on Person Table


    package com.linkwithweb.test.derby.dao;

    import java.io.Serializable;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    /**
    * An abstract DAO that knows how to get a connection to the db.
    *
    * @param
    * Object type of the DAO
    * @param
    * Object type of the unique key
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public abstract class AbstractDao implements
    IDao {

    /** Connection URL . */
    private static final String CONNECTION_URL;

    /** Connection login to the DB. */
    private static final String CONNECTION_LOGIN;

    /** Connection password to the DB. */
    private static final String CONNECTION_PWD;

    static {
    CONNECTION_URL = System.getProperty("com.linkwithweb.url");
    CONNECTION_LOGIN = System.getProperty("com.linkwithweb.login");
    CONNECTION_PWD = System.getProperty("com.linkwithweb.pwd");
    }

    /**
    * Gets a connection to the database.
    *
    *
    * @return Connection
    * @throws SQLException
    * If the DB can't be connected to
    */
    protected Connection createConnection() throws SQLException {

    return DriverManager.getConnection(CONNECTION_URL, CONNECTION_LOGIN,
    CONNECTION_PWD);
    }
    }

    package com.linkwithweb.test.derby.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Collection;

    import com.linkwithweb.test.derby.model.Person;

    /**
    * DAO for accessing the datas of a person.
    *
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public class PersonDao extends AbstractDao {

    /**
    * @throws SQLException
    * @see com.linkwithweb.test.derby.dao.IDao#findById(java.lang.Object)
    */
    public Person findById(Integer pId) throws SQLException {

    Connection lConnection = null;

    try {

    lConnection = createConnection();

    PreparedStatement lStatement = lConnection
    .prepareStatement("SELECT * FROM PERSON WHERE ID = ?");

    lStatement.setInt(1, pId);

    ResultSet lRs = lStatement.executeQuery();

    if (lRs.next()) {

    Person lPerson = new Person();

    lPerson.setId(lRs.getLong("ID"));
    lPerson.setName(lRs.getString("NAME"));
    lPerson.setFirstName(lRs.getString("FIRST_NAME"));
    lPerson.setBirthDate(lRs.getDate("BIRTH_DATE"));

    return lPerson;
    }

    } finally {

    if (lConnection == null) {

    lConnection.close();
    }
    }

    return null;
    }

    /**
    * @see com.linkwithweb.test.derby.dao.IDao#getAll()
    */
    public Collection getAll() {
    // TODO Auto-generated method stub
    return null;
    }

    /**
    * @see com.linkwithweb.test.derby.dao.IDao#removeById(java.lang.Object)
    */
    public void removeById(Integer pId) {
    // TODO Auto-generated method stub

    }
    }

    Now as we have all the classes defined now. Let us create a PersonDAOTest which loads data into our person table and Does simple unit test

    package com.linkwithweb.test.derby.dao;

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.SQLException;

    import org.dbunit.dataset.IDataSet;
    import org.dbunit.dataset.excel.XlsDataSet;
    import org.dbunit.operation.DatabaseOperation;

    import com.linkwithweb.test.derby.model.Person;

    /**
    * A test case for {@link PersonDao}.
    *
    * @author Ashwin
    * @since 16th Nov 2010
    */
    public class PersonDaoTest extends AbstractDaoTest {

    /** The DAO to be tested. */
    private PersonDao personDao;

    /**
    * @see org.dbunit.DatabaseTestCase#setUp()
    */
    @Override
    protected void setUp() throws Exception {

    super.setUp();

    // Creates the DAO to be tested
    personDao = new PersonDao() {

    /**
    * @see com.linkwithweb.test.derby.dao.AbstractDao#createConnection()
    */
    @Override
    protected Connection createConnection() throws SQLException {

    try {

    return getConnection().getConnection();

    } catch (Exception e) {

    throw new SQLException(e);
    }
    }
    };

    InputStream lStream = PersonDaoTest.class.getClassLoader()
    .getResourceAsStream("com/ashwin/test/derby/dao/person.xls");

    // Creates the initialization data
    IDataSet lDataSet = new XlsDataSet(lStream);

    DatabaseOperation.CLEAN_INSERT.execute(getConnection(), lDataSet);
    }

    /**
    * Test method for
    * {@link com.linkwithweb.test.derby.dao.PersonDao#findById(java.lang.Integer)}.
    *
    * @throws SQLException
    */
    public void testFindById() throws SQLException {

    Person lPerson = personDao.findById(1);

    assertNotNull(lPerson);
    }
    }

    Below is the project structure which will give you an overview of how it is

    C:\software\derby\src>tree /f
    C:.
    ├───main
    │ └───java
    │ └───com
    │ └───linkwithweb
    │ └───test
    │ └───derby
    │ ├───dao
    │ │ AbstractDao.java
    │ │ IDao.java
    │ │ PersonDao.java
    │ │
    │ └───model
    │ Person.java

    └───test
    ├───java
    │ └───com
    │ └───linkwithweb
    │ └───test
    │ └───derby
    │ └───dao
    │ AbstractDaoTest.java
    │ PersonDaoTest.java
    │ TableCreator.java

    └───resources
    └───com
    └───linkwithweb
    └───test
    └───derby
    └───dao
    person.xls

    DerbyHelloWorld
    Rename to .rar

    GWT Spring & Hibernate Mavenized Crud Sample

    Attached is the Mavenized Project which has sample CRUD operations

    Tested Configuration

    Using CATALINA_BASE: “C:\setup\Apache\apache-tomcat-6.0.26”
    Using CATALINA_HOME: “C:\setup\Apache\apache-tomcat-6.0.26”
    Using CATALINA_TMPDIR: “C:\setup\Apache\apache-tomcat-6.0.26\temp”
    Using JRE_HOME: “C:\setup\java\jdk1.5.0_21”

    Maven 2.2.1
    GWT 1.7.1

    Download this link extract using winrar
    MavenizedSample

    It has InstallMaven.txt which has command to install some thirdparty libraries
    It has InstallDB.txt which has commoand to import Oracle Dump

    Once the above configuration is done then Run
    mvn clean package

    This shall create war file as well as package in target folder. copy that deploy in tomcat.
    For any questions
    mail me at ashwin@linkwithweb.com