Tuesday, September 30, 2008

Studies --> java --> Ejb -> Simple build file to create EAR

This is simple example to create a ear file through build. We need to create some folder structures .

1). build.xml





















































































2). build.bat
@rem build\build.bat@echo off
call .\setenv.bat:: let's just make sure WSROOT is valid, (if is not, an error message was already given)if not exist %WSROOT%\jmetro\build\build.bat goto done
@rem Get command line arguments. Supports variable number of argumentsset CMD_LINE_ARGS=%1if ""%1""=="""" ( echo WSROOT=%WSROOT% echo JAVA_HOME=%JAVA_HOME% echo ANT_HOME=%ANT_HOME% %JAVA_HOME%\bin\java -version goto doneArgs)shift
:copyPropertiesFileif "%TARGET_APPSERVER%"=="" ( set TARGET_APPSERVER=weblogic)@copy /y ..\config\%TARGET_APPSERVER%\build.properties build.properties
:setupArgsif ""%1""=="""" goto doneArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setupArgs
:doneArgscall %ANT_HOME%\bin\ant %CMD_LINE_ARGS%echo ant_home = %ANT_HOME%:done

3) setenv.bat
@rem Sets environment variables@echo off:: WSROOT should point to the parent directory of jmetro and jmetrotest:: set WSROOT=G:\MyProject <-- this use to be the original WSROOTif "%WSROOT%" == "" ( for /f "tokens=1,2 delims=\" %%i in ('cd') do set WSROOT=%%i\%%j)
if exist %WSROOT%\builda\build.bat goto done
:: this is for backwards compatibility, where WSROOT used to point to:: the parent of the parent of jmetro and jmetrotestif exist %WSROOT%\metrodev\jmetro\build\build.bat goto oldStyle
echo There seems to be a problem with your environment variable WSROOTecho Make sure it points to the parent directory of jmetro and jmetrodevgoto done
:oldStyleset WSROOT=E:\metrodev\tools
:done
set JMTOOLS=%WSROOT%\tools
set ANT_HOME=%JMTOOLS%\antset JAVA_HOME=%JMTOOLS%\jvm\jdk150
set XMLBEANS_HOME=%JMTOOLS%\xmlbeans-2.0.0set XMLBEANS_LIB=%XMLBEANS_HOME%\lib
set CLASSPATH=%XMLBEANS_LIB%;%CLASSPATH%
:: Make sure our tools are the first ones in the pathfor %%i in (java.exe) do set java.exe=%%~$PATH:iif /i "%java.exe%" neq "%JAVA_HOME%\bin\java.exe" ( echo Adding %JAVA_HOME%\bin;%ANT_HOME%\bin;%JMTOOLS%\misc to PATH path %JAVA_HOME%\bin;%ANT_HOME%\bin;%JMTOOLS%\misc;%path%)


In above .bat file some properties are unncessary. we need to cpoy the tools directory with
ant , jvm, perl and xml beans 2.0. I think except ant all are not necessary.

Monday, September 29, 2008

EJB--> BMP AND CMP

This link will give the what to do and not do in CMP and BMP.
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP3.html

The below link explains how to create an J2EE application :
http://www.roseindia.net/jboss/sessionbeanservlet.shtml

Studies --> Java --> Ejb --> calling entity from session.

com.ejb.entity package
1). NamedQueryA remote interface.
package com.ejb.entity;
import java.rmi.RemoteException;import java.util.Collection;
/** * NamedQuery entity bean interface * @author KCN Reddy * @version 1.0 * @since 1.0 */public interface NamedQueryA extends javax.ejb.EJBLocalObject { public void setBName(String bname); public String getBName(); public void setBColor(String bcolor); public String getBColor();
public void setBQuantity(int bquantity); public int getBQuantity(); public long calicul(long a ,long b) ; }

2). NamedQueryA home
package com.ejb.entity;
import javax.ejb.CreateException;import javax.ejb.FinderException;

/** * NamedQuery home interface * @author KCN Reddy * @version 1.0 * @since 1.0 */public interface NamedQueryAHome extends javax.ejb.EJBLocalHome {
//public NamedQuery create(NamedQueryData nqData) throws CreateException; public NamedQueryA create(String bname,String bcol,int quantity) throws CreateException;
// public NamedQueryA findByPrimaryKey(NamedQueryAPK nqPK) throws FinderException; public NamedQueryA findByPrimaryKey(String nqPK) throws FinderException;}

3). Bean class
package com.ejb.entity;
import java.rmi.RemoteException;import java.util.Collection;import javax.ejb.CreateException;import javax.ejb.EntityBean;import javax.ejb.EntityContext;import javax.ejb.FinderException;import javax.ejb.RemoveException;

/** * NamedQuery information entity bean class * @author KCN Reddy * @version 1.0 * @since 1.0 */public abstract class NamedQueryABean implements EntityBean {
EntityContext entityContext; String bname; String bcolor; int bquantity; public java.lang.String ejbCreate(String bname,String bcolor,int quantity) throws CreateException{ this.bname = bname; this.setBColor(bcolor); this.setBName(bname); this.setBQuantity(quantity); return bname; } public void ejbPostCreate(String bname,String bcolor,int quantity) throws CreateException { // since there are no relations, this is empty. /** @todo Complete this method*/ }
public void ejbRemove() throws RemoveException {/** @todo Complete this method*/}
public abstract void setBName(String bname); public abstract String getBName(); public abstract void setBColor(String bcolor); public abstract String getBColor(); public abstract void setBQuantity(int bquantity); public abstract int getBQuantity();
public void ejbLoad() {/** @todo Complete this method*/}
public void ejbStore() {/** @todo Complete this method*/}
public void ejbActivate() {/** @todo Complete this method*/}
public void ejbPassivate() {/** @todo Complete this method*/}
public void unsetEntityContext() { this.entityContext = null; }
public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public long calicul(long a,long b) { System.out.println("------------caliculating---------hai------------"); return a; } }

4). NamedQueryAPK class

package com.ejb.entity;
import java.io.Serializable;

public class NamedQueryAPK implements Serializable {
public String bName ;
public NamedQueryAPK() {}
public NamedQueryAPK(String bName) {
this.bName = bName;
}
public boolean equals(Object obj) {
if (obj != null) {
if (this.getClass().equals(obj.getClass())) {
NamedQueryAPK that = (NamedQueryAPK) obj;
return this.bName.equals(that.bName);
}
}
return false;
}
public int hashCode() {
return (bName).hashCode();
}
}

com.ejb. session package.

1). Remote interface.
package com.ejb.session;
import javax.ejb.*;import java.rmi.RemoteException;
public interface FirstSession extends EJBObject{ public long caliculate(long a ,long b) throws RemoteException; public String insertItem(String bname, String bcolor,int bquantity) throws RemoteException; }
2).Home interface.
package com.ejb.session;
import javax.ejb.*;import java.rmi.RemoteException;
public interface FirstSessionHome extends EJBHome { public FirstSession create()throws RemoteException, CreateException;
}
3).Bean class
package com.ejb.session;
import java.rmi.RemoteException;import java.util.Properties;
import javax.ejb.*;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;
import com.ejb.entity.NamedQueryA;import com.ejb.entity.NamedQueryAHome;
public class FirstSessionBean implements SessionBean { private SessionContext sessionContext; Properties p = new Properties();
public String insertItem(String bname,String bcolor,int bquantity) throws RemoteException{ System.out.println("inside insert Item method of session bean"); long value =0; try { InitialContext ectx = new InitialContext(p); NamedQueryAHome nHome = (NamedQueryAHome)(ectx.lookup("java:comp/env/ejb/NamedQueryA")); NamedQueryA nQuery = nHome.create(bname,bcolor,bquantity); //System.out.println(" After getting the named query object "+nQuery.getBName()); //value = nQuery.calicul(12,23); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (CreateException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""+value; } public void ejbCreate(){ } public void ejbRemove(){ } public void ejbActivate(){ } public void ejbPassivate(){ } public void setSessionContext(SessionContext sessionContext){ this.sessionContext = sessionContext; } public long caliculate(long a,long b) throws RemoteException{ System.out.println("------------caliculating---------hai------------"); return a; } }

META-INF.
1) ejb-jar.xml




EJB1



session bean
FirstSession
FirstSession
com.ejb.session.FirstSessionHome
com.ejb.session.FirstSession
com.ejb.session.FirstSessionBean
Stateless
Container

ejb/NamedQueryA
Entity
com.ejb.entity.NamedQueryAHome
com.ejb.entity.NamedQueryA
NamedQueryA



This is description
NamedQueryA
NamedQueryA
com.ejb.entity.NamedQueryAHome
com.ejb.entity.NamedQueryA
com.ejb.entity.NamedQueryABean
Container

java.lang.String
false
2.x
NamedQueryA
BName
BColor
BQuantity
BName





This role represents everyone who is allowed full access
to the cabin bean.

everyone


everyone

NamedQueryA
*




2).jbosscmp-jdbc.xml






java:/JMetroJDBCDataSource
MS SQLSERVER2000
false
false



NamedQueryA
java:/JMetroJDBCDataSource
MS SQLSERVER2000
MYBEER

BName
BName


BColor
BColor


BQuantity
BQuantity





Client FILE:
1).
package com.ejb.session;
import java.util.Properties;
import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;
public class Client { public static void main( String[] args){ try{ Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); p.put(Context.PROVIDER_URL, "jnp://localhost:1099"); p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); InitialContext ctx = new InitialContext(p); Object obj = ctx.lookup("FirstSession"); FirstSessionHome ejbHome = ( FirstSessionHome ) PortableRemoteObject.narrow(obj, FirstSessionHome .class); FirstSession sessionObj = ejbHome.create(); System.out.println("---------------"); String aaa = sessionObj.insertItem("Bhaskar","Yellow",2); System.out.println("----------k-------------"+aaa); }catch(Exception e){ e.printStackTrace(); } } }

jar files i added are:
1). jboss 4.0.5ga/server/default/lib : jboss-j2ee.jar, javax.servlet.jar,javax.servlet.jsp.jar.
2).jboss 4.0.5ga/client/lib: jbossall-client.jar.

* I used eclipse europa to crate a project. And i create a table in the database MYBEER.
QUERY IS
create table MYBEER( BName char(30) primary key, BColor char(30), BQuantity int) .

Thursday, September 25, 2008

EJB -> Errors

1).While working with entity bean I got entity does not found the object 'CABIN' (table name)
this is my mistake , i did not create the table in the proper database .

2)Now I got the following error.
--------------------------------

javax.ejb.CreateException: Error checking if entity exists:java.sql.SQLException: Line 1: Incorrect syntax near 'WHERE'.
at org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand.beforeInsert(JDBCInsertPKCreateCommand.java:105)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.execute(JDBCAbstractCreateCommand.java:150)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:587)

Sol: This error gone when i change the cabinPK class variable to primary key filed.

previously it has id as field , now i changed to the primary key filed name as metioned in the bean class. And I changed the method names to plane like previously i has getRsCardNo

i changed that to getCardno.

Solution:

CMP:(container managed persistance).

Actually container will find our entity bean by using primay key , In ejb-jar.xml I metioned the primary class name as NamedQueryAPK class . But there is no column with that class type.

So I changed the primay key class as java.lang.String and primary key field as BName( stiring and one of the column of the table) . Now the container is able to find the entity and created a row in the table.

Note: One thing that i observed is , when i call a entity bean directly from the client that time I gabe the primary key class as CabinPK but container does not through any error. But this time( calling entity bean from session bean) it thrown create exception.

Wednesday, September 24, 2008

Studies --> java --> ejb -- > entity --> simple Example

I created a simple entity bean which inserts a row in a table .

1). Remote interface.

package com.titan.cabin;
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName(String str) throws RemoteException;
public int getDeckLevel() throws RemoteException;
public void setDeckLevel(int level) throws RemoteException;
public int getShip() throws RemoteException;
public void setShip(int sp) throws RemoteException;
public int getBedCount() throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}

2). Home interface
package com.titan.cabin;
import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.FinderException;
public interface CabinHome extends javax.ejb.EJBHome {
public Cabin create(int i,String n,int a, int b,int id) throws CreateException, RemoteException;
public Cabin findByPrimaryKey(CabinPK pk) throws FinderException, RemoteException;
}

3). primary key class

package com.titan.cabin;
public class CabinPK implements java.io.Serializable {
public int id;
String cname = null;
public CabinPK(){}
public CabinPK(int id) { this.id = id; }
public boolean equals(Object obj) {
if (obj != null) {
if (this.getClass().equals(obj.getClass())) {
CabinPK that = (CabinPK) obj;
return this.id == that.id;
}
}
return false;
}
public int hashCode() {
return (""+id).hashCode();
}
}

4). Bean class
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.EntityBean {
public int id; public String name;
public int deckLevel;
public int ship;
public int bedCount;
public CabinPK ejbCreate(int i,String a, int b, int c,int d) {
this.id = i;
this.setName(a);
this.setDeckLevel(b);
this.setShip(c);
this.setBedCount(d);
//this.id = id;
//return null;
return new CabinPK(id);
}
public void ejbPostCreate(int i,String a, int b, int c,int d) { // Do nothing. Required. } public String getName() { return name; }
public void setName(String str) { name = str; }
public int getShip() { return ship; }
public void setShip(int sp) { ship = sp; }
public int getBedCount() { return bedCount; }
public void setBedCount(int bc) { bedCount = bc; }
public int getDeckLevel() { return deckLevel; }
public void setDeckLevel(int level ) { deckLevel = level; }
public void setEntityContext(EntityContext ctx) { // Not implemented. }
public void unsetEntityContext() { // Not implemented. }
public void ejbActivate() { // Not implemented. }
public void ejbPassivate() { // Not implemented. }
public void ejbLoad() { // Not implemented. }
public void ejbStore() { // Not implemented. }
public void ejbRemove() { // Not implemented. }
}

5). ejb-jar.xml







This Cabin enterprise bean entity represents a cabin on
a cruise ship.

CabinBean
com.titan.cabin.CabinHome
com.titan.cabin.Cabin
com.titan.cabin.CabinBean
Container
com.titan.cabin.CabinPK
False
id
name
deckLevel
ship
bedCount





This role represents everyone who is allowed full access
to the cabin bean.

everyone


everyone

CabinBean
*




CabinBean
*

Required




6). jbosscmp-jdbc.xml







java:/JMetroJDBCDataSource
MS SQLSERVER2000
false
false



CabinBean
java:/JMetroJDBCDataSource
MS SQLSERVER2000
CABIN

id
ID


name
NAME


deckLevel
DECK_LEVEL


ship
SHIP_ID


bedCount
BED_COUNT






7). Client.java

package com.titan.cabin;
import com.titan.cabin.CabinHome;import com.titan.cabin.Cabin;
import com.titan.cabin.CabinPK;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;
public class Client_1 {
public static void main(String [] args) {
try {
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CabinBean");
CabinHome home = (CabinHome) // EJB 1.0:Use Java cast instead of narrow( ) PortableRemoteObject.narrow(ref,CabinHome.class);
Cabin cabin_1 = home.create(1,"MyName",2,2,3);
// cabin_1.setName("Master Suite");
// cabin_1.setDeckLevel(1);
// cabin_1.setShip(1);
// cabin_1.setBedCount(3);
// // CabinPK pk = new CabinPK();
// pk.id = 1;
// // Cabin cabin_2 = home.findByPrimaryKey(pk);
// System.out.println(cabin_2.getName());
// System.out.println(cabin_2.getDeckLevel());
// System.out.println(cabin_2.getShip());
// System.out.println(cabin_2.getBedCount());
} catch (java.rmi.RemoteException re)
{re.printStackTrace();
} catch (javax.naming.NamingException ne){ne.printStackTrace();
} catch (javax.ejb.CreateException ce){ce.printStackTrace();
} //catch (javax.ejb.FinderException fe){fe.printStackTrace();
}
}

public static Context getInitialContext() throws javax.naming.NamingException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
// ... Specify the JNDI properties specific to the vendor. return new javax.naming.InitialContext(p);
}
}


8). You need to create a table
create table CABIN ( ID int primary key, SHIP_ID int, BED_COUNT int, NAME char(30), DECK_LEVEL int)

Note: you need to create this table in the data base i.e refred by application server.
for example in jboss we will refer the database in jboss-ds.xml. So see the datasource name in the jboss-ds.xml and create the table in that.

keep the ejb-jar.xml and jbosscmp-jdbc.xml in web-inf folder and deploy the jar in server.

--------------------------
1) Client to fet the record from the table
package com.titan.cabin;
import com.titan.cabin.CabinHome;import com.titan.cabin.Cabin;import com.titan.cabin.CabinPK;
import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;import java.util.Properties;
public class Client_1 {
public static void main(String [] args) {
try {
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CabinBean");
CabinHome home = (CabinHome) // EJB 1.0:Use Java cast instead of narrow( ) PortableRemoteObject.narrow(ref,CabinHome.class);
// Cabin cabin_1 = home.create(1,"MyName",2,2,3);
// cabin_1.setName("Master Suite");
// cabin_1.setDeckLevel(1);
// cabin_1.setShip(1);
// cabin_1.setBedCount(3);
CabinPK pk = new CabinPK();
pk.id = 1;
Cabin cabin_2 = home.findByPrimaryKey(pk);
System.out.println(cabin_2.getName());
System.out.println(cabin_2.getDeckLevel());
System.out.println(cabin_2.getShip());
System.out.println(cabin_2.getBedCount());
} catch (java.rmi.RemoteException re){re.printStackTrace();} catch (javax.naming.NamingException ne){ne.printStackTrace();} //catch (javax.ejb.CreateException ce){ce.printStackTrace();} catch (javax.ejb.FinderException fe){fe.printStackTrace();} }

Tuesday, September 23, 2008

Studies: Java --> ejb --> session --> simple exmaple

I don`t know much about ejbs , but I am eager to lean them . I started my journey with simple ejb exmaple.

HelloWorld session bean example.

1) Remote interface:

package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface HelloWorldSession extends EJBObject {
int businessMethod(int a, int b) throws RemoteException;
}

2) Home Interface:

package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.CreateException;import javax.ejb.EJBHome;
public interface HelloWorldHome extends EJBHome {
HelloWorldSession create()throws RemoteException,CreateException;
}

3).Bean class :
package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;
public class HelloWorldSessionBean implements SessionBean { private SessionContext sessionContext;
public HelloWorldSessionBean() {
super();
}
public int businessMethod(int a, int b) throws RemoteException{
return a+b;
}
public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
this.sessionContext = sessionContext;
}
public void ejbCreate() {}
public void ejbRemove(){}
public void ejbActivate() {}
public void ejbPassivate() {}
}

4). ejb-jar.xml







HelloWorldBean
com.ejb.session.HelloWorldHome
com.ejb.session.HelloWorldSession
com.ejb.session.HelloWorldSessionBean
Stateless
Container





HelloWorldBean
*

Required




5). Client file:
package com.ejb.session;
import java.util.Properties;
import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;
public class SessionClient {
public SessionClient() { super(); // TODO Auto-generated constructor stub }
/** * @param args */
public static void main(String[] args) {
try{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); InitialContext ctx = new InitialContext(p);
Object obj = ctx.lookup("HelloWorldBean");
HelloWorldHome ejbHome = ( HelloWorldHome ) PortableRemoteObject.narrow(obj, HelloWorldHome .class);
HelloWorldSession sessionObj = ejbHome.create();
int k = sessionObj.businessMethod( 10,20);
System.out.println("----------Result is-------------"+k);
}catch(Exception e){
e.printStackTrace();
}
}
}

put ejb-jar.xml in META-INF folder.

test

Usefull link: http://www.unix.com.ua/orelly/java-ent/ebeans/ch06_01.htm