Thursday, February 9, 2012

Sample Hibenrnate Spring configuration


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/properties/db.properties
</value>
</list>
</property>

<bean id="hibernateConnProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.connection.driver_class">${driverClassName}
</prop>
<prop key="hibernate.connection.username">${username}</prop>
<prop key="hibernate.connection.password">${password}</prop>
<prop key="hibernate.connection.url">${url}
</prop>
<prop key="hibernate.dialect">${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.c3p0.minPoolSize">${hibernate.c3p0.minPoolSize}
</prop>
<prop key="hibernate.c3p0.maxPoolSize">${hibernate.c3p0.maxPoolSize}
</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}
</prop>
<prop key="hibernate.c3p0.max_statement">${hibernate.c3p0.max_statement}
</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">${hibernate.c3p0.testConnectionOnCheckout}
</prop>
</props>
</property>
</bean>


<bean id="dataSourceService" class="org.apache.commons.dbcp.BasicDataSource"
init-method="createDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!--
<value>classpath*:/hibernate/NamedQueries.hbm.xml</value>
<value>/hibernate/BMC_CM_ORIGINAL_REQ.hbm.xml</value>
-->
<util:list id="ormResources">
<value>classpath*:/hibernate/NamedQueries.hbm.xml</value>
</util:list>

<bean id="mapBean" class="SORMappings">
<property name="mappingResources" ref="ormResources" />
</bean>

<util:property-path id="mappingLocations" path="mapBean.mappingResources" />

<bean id="sorSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceService" />
<property name="hibernateProperties" ref="hibernateConnProperties" />
<property name="mappingLocations" ref="mappingLocations" />
</bean>

<bean id="mydao" class="dao">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>


<context:annotation-config />

</beans>



public class dao extends HibernateDaoSupport {

@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)

}

Work around to insert XML to database Oracle OCI & Hibernate

The workaround is using a PL/SQL embedded in Java

String XmlData = ((String)cr.get("xml")).replace('\'', '`');
StringBuffer xmlSP = new StringBuffer();
xmlSP/*.append("DECLARE ")
.append("\n")
.append(" l_xml CLOB := ?;")
.append("\n")*/
.append(" BEGIN \n")
.append(" INSERT INTO xml_data")
.append("\n")
.append(" (id, xml)")
.append("\n")
.append(" VALUES ")
.append("\n")
.append(" (my_seq.nextval, ?);")
.append("\n")
.append("END;");

logger.info(xmlSP.toString());
Connection con = session.connection();
try {
CallableStatement stmt = con.prepareCall(xmlSP.toString());
Connection poolConn = stmt.getConnection();
//Convert the poolConnection to OracleConnection.
OracleConnection oracleConn= (OracleConnection) ((DelegatingConnection) poolConn).getInnermostDelegate();
//Convert the input request string to XMLTYPE object.
oracle.xdb.XMLType requestXMLTYPE = oracle.xdb.XMLType.createXML(oracleConn, XmlData);
stmt.setObject(1, requestXMLTYPE);
boolean retCode = stmt.execute();
logger.info("Inserted data into bmc_cm_original_req");
} catch (SQLException e) {
logger.error("Sql Exception while inserting into bmc_cm_original_req " + e.getMessage());
} catch (Exception e) {
logger.error("Sql Exception while inserting into bmc_cm_original_req " + e.getMessage());
}

Wednesday, February 8, 2012

Testing maven Generated WSDL2JAVA JAR

Here is a sample test

@Test
public void testAxis14CcpClient () throws ServiceException {

ccpWebServicesImplServiceLocator.setEndpointAddress(
new javax.xml.namespace.QName(
"http://webservices.com",
"XXXServicesImpl"), "http://XXXcom:80/ServicesImpl");

XXXWebServicesImpl_PortType serviceimpl = ccpWebServicesImplServiceLocator
.getXXXWebServicesImpl();

((javax.xml.rpc.Stub) serviceimpl)._setProperty(
"javax.xml.rpc.session.maintain", Boolean.TRUE);
((javax.xml.rpc.Stub) serviceimpl)._setProperty(XXXWebServicesImpl_BindingStub.USERNAME_PROPERTY,"UUUU");
((javax.xml.rpc.Stub) serviceimpl)._setProperty(XXXWebServicesImpl_BindingStub.PASSWORD_PROPERTY,"ddddd");
String o;
try {
o = serviceimpl.myMethod("X");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Tuesday, February 7, 2012

Generate SOAP WSDL2JAVA using maven

Find the WSDL file for which you need to generate the client stub.
Keep your WSDL in the main/resources directory as a best practice.

http://mojo.codehaus.org/wsdl2java-maven-plugin/howto.html

Prepare the maven pom.xml

Here is an Example
<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>soap.wsdlclient</groupId>
<artifactId>soap.wsdlclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>restapp.wsdlclient</name>
<dependencies>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
</plugin>
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/resources</sourceDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>


Run mvn install

This will generate the client code. You may not have to alter the code.
publish this as a jar and use in other java projects.

Using MockStrutsTestCase

Here is how you can test struts action with MockStrutsTest

Create your struts configuration



<struts-config>
<action-mappings>
<action path="/test" type="my.demo.MyAction" >
<forward name="viewCapablity" redirect="false" path="/WEB-INF/jsp/view.jsp"></forward>
<forward name="viewCapablity1" redirect="false" path="/WEB-INF/capability/jsp/viewCapability1.jsp"></forward>
</action>
<action path="/benny" type="com.cisco.cssd.struts.demo.MyAction" >
<forward name="viewCapablity" redirect="false" path="/WEB-INF/jsp/view.jsp"></forward>
<forward name="viewCapablity1" redirect="false" path="/WEB-INF/capability/jsp/viewCapability1.jsp"></forward>
</action>
</action-mappings>
</struts-config>

Develop your struts action classes

package my.struts.demo;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyAction extends Action {

public ActionForward execute(ActionMapping aMapping, ActionForm aForm,
HttpServletRequest aRequest, HttpServletResponse aResponse) {

String type = (String) aRequest.getParameter("type");

System.out.println(type);

return aMapping.findForward("viewCapablity");

}
}


write mock tests
package com.cisco.cssd.struts.demo;

import org.junit.Test;

import servletunit.struts.MockStrutsTestCase;

public class TestMyAction extends MockStrutsTestCase {

public TestMyAction(String testName) {
super(testName);
// TODO Auto-generated constructor stub
}

public void setUp() throws Exception {
super.setUp();
setServletConfigFile("/WEB-INF/web.xml");
setConfigFile("my/struts/demo/struts-config.xml");
setInitParameter("validating", "false");
setRequestPathInfo("/test.do");
}

@Test
public void testing () {
System.out.println("testing ...");
addRequestParameter("type", "MY demo type");
actionPerform ();
verifyForward("viewCapablity");
verifyForwardPath("/WEB-INF/jsp/view.jsp");


}

}


finally integrate with the final product.