Thursday, May 28, 2009

Pointer To Spring Stuff





Wednesday, May 27, 2009

How to fill VARRAY/TABLETYPE object in iBatis



Sample:

Step 1: Create DEMO_TABLE_TYPE as the following structure:
create or replace TYPE DEMO_TABLE_TYPE AS OBJECT
(
id NUMBER, name NVARCHAR2(35)
);

Step 2: Write demoProcedure as the following structure:
demoProcedure(
demoTType
) IS
BEGIN
INSERT INTO DEMOTABLE( ID, NAME) VALUES ( demoTType.ID,demoTType.NAME); END demoProcudre;

Step 3: create a sql map id in sql-map-xxx.xml like as follows:

<procedure id="demoProc" parameterClass="map">
<![CDATA[{call
DECLARE
demoTType DEMO_TABLE_TYPE := DEMO_TABLE_TYPE();
idx BINARY_INTEGER := 1;
BEGIN
]]>
<iterate property="list">
<![CDATA[
demoTType .extend(1);
demoTType (idxLF) := DEMO_TABLE_TYPE(
#demoTType[].id:NUMERIC#,
#demoTType[].name:VARCHAR#
);
idx := idx + 1;
]]>
</iterate>
<![CDATA[
demoProcedure(
demoTType
);
END}
]]>
</procedure>

Generic iBatis Error in Huge Volume Cetric Data

Error
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the BillIe.insertBillIssuingEntityOnEditCycle-InlineParameterMap.
--- Check the statement (update procedure failed).
--- Cause: java.sql.SQLException: ORA-06550: line 1, column 1:
PLS-00123: program too large (Diana nodes)

Solution:

Step 1: Create a store procedure which would insert/delete record from table

Step 2: Define some threshold value (Note: you must be ensure that threshold value won't encounter Diana nodes kind of problem) in java.

Step 3: Push records in batch mode

Pseudo Code Snippet


getSqlMapClient().startTransaction()
getSqlMapClient().startBatch()

//Check out how many records are there in the modelMap or List

totalRecord = modelList.size

toIndex = modelList.size

fromIndex =0

numberOfLoops = new Double(Math.ceil(totalRecord / THRESHOLD_VALUE)).intValue()

if(THRESHOLD_VALUE <= toIndex){
toIndex = new Double(THRESHOLD_VALUE).intValue()
}

for(int i = 0; i <>

modelSubList = modelList.subList(fromIndex , toIndex)

getSqlMapClient().insert("sql-map-id", modelSubList )

modelList.subList(fromIndex, toIndex).clear()

if(modelList.size() < THRESHOLD_VALUE){
toIndex = modelList.size();
}

}

getSqlMapClient().executeBatch()
getSqlMapClient().commitTransaction()

Quotation



  • FAIL FORWARD - FAILURE IS THE ONLY OPPORTUNITY TO BEGIN AGAIN MORE INTELLIGENTLY.



How to use Lucene Search in Struts 2.0

In Progress...

Monday, May 25, 2009

URLs About Servlet 3.0 Specification

1. Specification - http://jcp.org/en/jsr/detail?id=315
2. Good explanation in briefly - http://www.infoq.com/news/2007/06/servlet3
3. Good Presentation by Mark - http://people.apache.org/~markt/presentations/2009-04-01-Tomcat7Servlet3.pdf
4. http://today.java.net/pub/a/today/2008/10/14/introduction-to-servlet-3.html
5. http://blogs.sun.com/swchan/entry/servlet_3_0_annotations
6. http://blogs.sun.com/jluehe/
7. http://blogs.sun.com/swchan/
8. http://techtracer.com/2007/07/23/servlet-30-the-journey-begins-now/

Framework Stuff URL






Spring :
1. http://static.springframework.org/spring/docs/2.5.x/reference/orm.html
2. http://book.javanb.com/Professional-Java-Development-with-the-Spring-Framework/BBL0021.html
3. http://www.javalobby.org/java/forums/t44746.html - A Quick Journey Through Spring AOP
4. http://www.mularien.com/blog/2008/07/07/5-minute-guide-to-spring-security/ - 5 Minute Guide To Spring Security

OGNL:

1. http://www.ognl.org/2.6.9/Documentation/pdf/LanguageGuide.pdf
2. http://struts.apache.org/2.0.14/docs/ognl-basics.html

Struts 2.0:

1. http://struts.apache.org/2.0.6/docs/ui-tag-reference.html
2. http://struts.apache.org/2.x/docs/other-resources.html

iBatis :

1. http://ibatisnet.sourceforge.net/DevGuide.html
2. http://www.javalobby.org/java/forums/t75161.html
3. http://ibatisnet.sourceforge.net/DevGuide/ar01s04.html
4. http://opensource.atlassian.com/confluence/oss/display/IBATIS/Index

Sitemesh:

1. http://www.rkcole.com/articles/other/SiteMeshDecorators.html - Demo App
2. http://www.onjava.com/pub/a/onjava/2004/09/22/sitemesh.html - Article

Hibernate 3:

1. http://www.hibernate.org/hib_docs/reference/en/html/index.html

Freemarker:

1. http://freemarker.sourceforge.net/docs

Sunday, May 24, 2009

Eclipse Startup Error




When I start eclipse, sometimes it does not start and throws error message like "An error has occured. See the log file ..\workspace\.metadata\.log".

.log
!ENTRY org.eclipse.core.resources 2 10035 2009-05-25 10:49:07.164!MESSAGE A workspace crash was detected. The previous session did not exit normally.
!ENTRY org.eclipse.osgi 4 0 2009-05-25 10:49:07.383!MESSAGE An error occurred while automatically activating bundle org.eclipse.core.resources (39).!STACK 0org.osgi.framework.BundleException: Exception in org.eclipse.core.internal.compatibility.PluginActivator.start() of bundle org.eclipse.core.resources. at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:1010)
.....
Caused by: org.eclipse.core.internal.dtree.ObjectNotFoundException: Tree element /Test/Person.class not found. at org.eclipse.core.internal.dtree.AbstractDataTree.handleNotFound(AbstractDataTree.java:257) at org.eclipse.core.internal.dtree.DeltaDataTree.getData(DeltaDataTree.java:585) at org.eclipse.core.internal.dtree.DataDeltaNode.asBackwardDelta(DataDeltaNode.java:50) at org.eclipse.core.internal.dtree.NoDataDeltaNode.asBackwardDelta(NoDataDeltaNode.java:59) at org.eclipse.core.internal.dtree.DataDeltaNode.asBackwardDelta(DataDeltaNode.java:47) at org.eclipse.core.internal.dtree.DeltaDataTree.asBackwardDelta(DeltaDataTree.java:88)
..........
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:985) ... 28 moreRoot exception:org.eclipse.core.internal.dtree.ObjectNotFoundException: Tree element /Test/Person.class not found. at org.eclipse.core.internal.dtree.AbstractDataTree.handleNotFound(AbstractDataTree.java:257) at org.eclipse.core.internal.dtree.DeltaDataTree.getData(DeltaDataTree.java:585)
.........
at org.eclipse.core.launcher.Main.main(Main.java:952)
!ENTRY org.eclipse.osgi 4 0 2009-05-25 10:49:07.383!MESSAGE Application error!STACK 1java.lang.NoClassDefFoundError: org/eclipse/core/resources/IContainer at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:96)
..........
!ENTRY org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE The following is a complete list of bundles which are not resolved, see the prior log entry for the root cause if it exists:!
SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.core.filesystem.linux.x86_1.0.0.v20060603.jar [32] was not resolved.
!SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.core.filesystem.macosx_1.0.0.v20060603.jar [33] was not resolved.
!SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.jdt.launching.macosx_3.1.100.v20060605.jar [95] was not resolved.!
SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.swt.carbon.macosx_3.2.2.v3236.jar [179] was not resolved.!
SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar [180] was not resolved.!
SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.ui.carbon_3.2.0.I20060605-1400.jar [192] was not resolved.!
SUBENTRY 1 org.eclipse.osgi 2 0 2009-05-25 10:49:07.493!MESSAGE Bundle update@plugins/org.eclipse.update.core.linux_3.2.0.v20060605.jar [212] was not resolved.

How to Resolve
Delete the following files/folders:
1. \workspace\.metadata\.plugins\org.eclipse.core.resources\.snap
2. \workspace\.metadata\.plugins\org.eclipse.core.resources\.root\.markers.snap

Now start eclipse. It should start else problem is something else.

Friday, May 22, 2009

How to integrate: Struts 2+Spring+iBatis (NetBeans 6.5)





Assignment: Use Struts2/Spring/Hibernate framework to Authenticate user credential i.e. username and password. If Authentication is success then render result like “User logged successfully.” else remain in the same Login Input form.

Solution: To achieve the above assignment, I am using NetBeans 6.5 IDE and other mentioned framework and supporting libraries.

Framework:
MVC - Struts 2
Bussiness Layer - Spring 2.5
Persistance - iBatis
IDE - Netbeans 6.5
Plug-ins - Struts 2 – spring - plugin


Create Project and Add supporting Libraries:

Step 1:
Open NetBeans 6.5

Step 2:
Select File > New Project > Select the following Options
Categories: Java Web
Projects: Web Application

Step 3:
Once Java Web and Web Application are selected then Select Next (Ref. Figure 1)



Figure 1



Step 4:

Provide the following details in Name and Location Screen:
Project Name:
Project Location:

Step 5:

Select the following options in Server and Settings Screen:
Server : Tomcat 6.0 (For me it’s Tomcat 6.0)
Java EE Version : Java EE 5
Context Path : ssidemo


Step 6: Tick the check box for the following Frameworks:



Figure 2

Step 7:

IDE will add couple of jar files related spring/struts 2. Apart from that Add the following jar files in the Libraries:

1. struts2-spring-plugin-2.0.12.jar
2. commons-dbcp-1.2.1.jar
3. commons-pool-1.2.jar
4. commons-collections-3.0.jar
5. Java DB Driver - derby.jar
6. Java DB Driver - derbyclient.jar
7. Java DB Driver - derbynet.jar

(You can get 1….4 jar files by a quick search and download and 5, 6, 7 jar files is available in JDK/NetBeans)

Note:
Select Library folder and right click on it or other way and select “Add Library” and select “Java DB Driver” library to add To add jar files select “Add Jar/Folder” and select the jar files (1….4) to add

Configure Framework and Create Project Folder structure for the Assignment:

Project Folder Structure created by IDE:

Web Pages - It stores the entire files/resourced required for web app deployment
Configuration Files - All the config (.xml) files
Server Resources - Not in use for this exercise
Source Packages - Java Source Code
Test Packages - Not in use for this exercise
Libraries - All the jar/zip files
Test Libraries - Not in use for this exercise

Development Folder Structure used for this Assignment:

Struts Action - com.hcl.action
DAO - com.hcl.dao
Service - com.hcl.service
Model - com.hcl.model
JSP Pages - WEB-INF/jsp

Register Spring Details in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>


Do not worry about this web.xml; our IDE is smart enough to create it. But ensure that these are not missing.

Configure Hibernate / Datasource / Transaction / Struts Action with spring

<?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-2.0.xsd">
<!—REGISTER STRUTS ACTION -->
<bean id="action.Login" class="com.hcl.action.Login" scope="prototype">
<property name="daoService" ref="service.DAOServiceBean"/>
</bean>
<!—REGISTER IBATIS FACTORY BEAN -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:/sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!—REGISTER DATASOURCE -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/DEMODB" />
<property name="username" value="DEMODB" />
<property name="password" value="DEMODB" />
</bean>
<!—REGISTER DATASOURCE WITH IBATIS-->
<bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource" />
</bean>
 
<!—REGISTER DATASOURCE WITH TRANSACTION MANAGER -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<alias name="txManager" alias="transactionManager" />
<bean id="DAOIntfBean" class="com.hcl.dao.DAOImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="service.DAOServiceBean" class="com.hcl.service.DAOServiceImpl">
<property name="daoInft" ref="DAOIntfBean"/>
</bean>
</beans>

Little big than web.xml but logically very memorable.

Struts configuration

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action"/>
<include file="example.xml"/> <!—ADDED BY IDE NOT IN USE FOR THIS-->
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="Login" class="action.Login" method="doLogin">
<result name="login">/jsp/Login.jsp</result>
<result name="input">/jsp/Login.jsp</result>
<result name="error">/jsp/Login.jsp</result>
<result name="success">/jsp/Success.jsp</result>
</action>
</package>
</struts>


Here “action.Login” is a bean defined in ApplicationContext.xml

Configure SQL Queries with iBatis

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
maxSessions="64"
maxTransactions="8"
maxRequests="128"
/>
<sqlMap resource="UserDetails.xml"/>
</sqlMapConfig>

Here UserDetails.xml contains the queries used for my purpose.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
<resultMap id="model.UserInfo" class="com.hcl.model.UserInfo">
<result property="userName" column="USER_NAME"/>
<result property="password" column="PASSWORD"/>
</resultMap>
<select id="isUserExists" parameterClass="com.hcl.model.UserInfo" resultClass="Integer">
SELECT count(*)
FROM USER_INFO
WHERE USER_NAME=#userName# AND PASSWORD = #password#
</select>
</sqlMap>


It is not a best way to do; you have better way to do it. But for demo there is no problem.

Java Code

Login.java

package com.hcl.action;

import com.hcl.model.UserInfo;
import com.hcl.service.DAOServiceIntf;
import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {

private DAOServiceIntf DaoService;
private String userName;
private String password;


public Login() {
}

public String doLogin() throws Exception {
UserInfo userInfo = new UserInfo();
userInfo.setPassword(password);
userInfo.setUserName(userName);
if(DaoService.isUserExists(userInfo)){
return ActionSupport.SUCCESS;
}else{
return ActionSupport.INPUT;
}

}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public DAOServiceIntf getDaoService() {
return DaoService;
}

public void setDaoService(DAOServiceIntf DaoService) {
this.DaoService = DaoService;
}
}

Login-validation.xml – Way to do Validation in Struts 2

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Login name is required</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Password is required</message>
</field-validator>
</field>
</validators>

UserInfo.java

package com.hcl.model;

public class UserInfo {

private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

DAOIntf.java (DAO Interface)

package com.hcl.dao;

import com.hcl.model.UserInfo;

public interface DAOIntf {
public boolean isUserExists(UserInfo userInfo);
}


DAOImpl.java (DAO Implementation)

package com.hcl.dao;
import com.hcl.model.UserInfo;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class DAOImpl extends SqlMapClientDaoSupport implements DAOIntf {

public boolean isUserExists(UserInfo userInfo) {
Integer userCount;
boolean result = false;
try{
userCount =(Integer) getSqlMapClientTemplate().queryForObject("isUserExists",userInfo);
}
catch(Exception ex){
ex.printStackTrace();
System.out.println(ex);
return false;
}
if (userCount != null && userCount > 0) {
result = true;
} else{
result = false;
}
return result;
}
}

DAOServiceIntf.java (Service Interface)

package com.hcl.service;

import com.hcl.model.UserInfo;

public interface DAOServiceIntf {
public boolean isUserExists(UserInfo userInfo);
}

DAOServiceImpl.java (Service Implementation)

package com.hcl.service;

import com.hcl.dao.DAOIntf;
import com.hcl.model.UserInfo;

public class DAOServiceImpl implements DAOServiceIntf {

private DAOIntf daoInft;

public boolean isUserExists(UserInfo userInfo){
return daoInft.isUserExists(userInfo);
}

public DAOIntf getDaoInft() {
return daoInft;
}

public void setDaoInft(DAOIntf daoInft) {
this.daoInft = daoInft;
}
}


Result (JSP) Pages

Login.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<s:form name="LoginForm" action="Login" method="post" >
<table>
<tr>
<td>
<s:text name="User Name :"/>
</td>
<td>
<s:textfield name="userName" />
</td>
</tr>
<tr>
<td>
<s:text name="Password :"/>
</td>
<td>
<s:password name="password"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<s:submit name="Login" value="Login" align="center"/>
</td>
</tr>
</table>
</s:form>
</body>
</html>

Success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
User <s:property value="userName"/> logged successfully.
</body>
</html>

Deployment Folder Structure ( NetBeans 6.5 IDE takes care most of the file location. )

Java Source Code: No need to place class/jar files in WEB-INF/classes or WEB-INF/lib. IDE takes care about it.
xml Files:



If everything goes fine then: URL To Access web Application:

http://localhost:8080/ssi/jsp/Login.jsp