• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Context Factory !!!Accessing EJB from Standalone client program

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have deployed a EJB onto Websphere AppServer 5.0.
here is my code :

HOME :::::
package com.cognizant.sample.ejb.interfaces;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface BoxHome extends EJBHome {

public Box create() throws CreateException, RemoteException;

}

Remote Interface ::::
package com.cognizant.sample.ejb.interfaces;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Box extends EJBObject{

public int CalcArea (int length,int width) throws RemoteException;
public void printArea(int length,int width) throws RemoteException;
public int CalcVolume(int length ,int width,int depth) throws RemoteException;

}
BEAN ::::
package com.cognizant.sample.ejb.impl;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class BoxBean implements SessionBean {
public BoxBean() {
super();
}
public void setSessionContext(SessionContext arg0)
throws EJBException, RemoteException {

}
public void ejbRemove() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}
public void ejbPassivate() throws EJBException, RemoteException {

}

public int CalcArea (int length,int width) throws RemoteException{
return length * width;
}
public void printArea(int length,int width) throws RemoteException{
int area=length*width;
System.out.println("The area of a box of length " +length + " width" + width + " is "+ area);
}

public int CalcVolume(int length ,int width,int depth) throws RemoteException{
return length * width *depth;
}

}
I am trying to access it from a standalone java client program specifying the initial context factory class

STAND ALONE JAVA-CLIENT PROGRAM :
package com.cognizant.sample.ejb.clients;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.cognizant.sample.ejb.interfaces.*;
public class BoxClient {
public BoxClient() {
super();
}
public static void main(String[] args) {
try{

Properties props=new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
props.put(Context.PROVIDER_URL,"iiop://localhost:900");
Context ctx=new InitialContext(props);
Object obj=ctx.lookup("SAMPLEBOX");
BoxHome boxHome=(BoxHome)PortableRemoteObject.narrow(obj,BoxHome.class);
Box box=boxHome.create();

int area=box.CalcArea(1,2);
System.out.println("Area from client" +area);
box.printArea(2,3);
int volume=box.CalcVolume(2,3,4);
System.out.println("Volume from client" + volume);

}
catch(Exception ex){
ex.printStackTrace();
}


}
}

I am getting the following exception.
javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory. Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory
I included the jar file naming.jar in the class path.Then another exception was thrown asking for jndiprovider.properties. i included namingclient.jar.again another exception was thrown.this process is cascading.
Can anyone tell me how to write a standalone client program to access the beans in Websphere ?Is my program right ???
What initialcontext factory and info should i provide ?
AND MORE IMPORTANTLY,WHAT JARS SHOULD BE IN MY CLASSPATH and what properties files containg which info SHOULD BE IN MY CLASSPATH???
If anybody could explain the concept of the contextfactory,it would be very helpful.Pls reply asap.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic