|
Ken,
Your timing is impecable. I just got done
figuring out how to get a client program (static void main) up and running about
four weeks ago. The message below is the message I sent another individual
that was having problems as well. He's said that the document I put
together worked perfectly the first time around.
Everything I've detailed below is basically doing
the 'CabinBean' example from O'Reilly publishings 'Enterprise JavaBeans'
book. I'd be happy to fill in the blanks if you get stuck on
anything.
Let me know how it works for you:
ejb-jar.xml
=========================================================================== There seems to be some issues with security role
mapping, that I'm still
trying to figure out. I had to remove the <method-permission> tags from this file or else I'd get an exception that admin was not allowed to execute any of the methods on this bean. I'm working on setting up a <method-permission> for admin, but am having some difficulty. It works if you leave those out though. Also, make note that the ejb-name is
'CabinBean'. This comes into play
later. <?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems,
Inc.//DTD Enterprise
JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd"> <ejb-jar>
<enterprise-beans> <entity> <description> This Cabin enterprise bean entity represents a cabin on a cruise ship. </description> <ejb-name>CabinBean</ejb-name> <home>com.titan.cabin.CabinHome</home> <remote>com.titan.cabin.Cabin</remote> <ejb-class>com.titan.cabin.CabinBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>com.titan.cabin.CabinPK</prim-key-class> <reentrant>False</reentrant>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field> <cmp-field><field-name>deckLevel</field-name></cmp-field> <cmp-field><field-name>ship</field-name></cmp-field> <cmp-field><field-name>bedCount</field-name></cmp-field> </entity> </enterprise-beans>
<assembly-descriptor>
<security-role> <description> This role represents everyone who is allowed full access to the cabin bean. </description> <role-name>everyone</role-name> </security-role> </assembly-descriptor> </ejb-jar>
application.xml
=========================================================================== <?xml version="1.0"?> <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN" "http://java.sun.com/j2ee/dtds/application_1_2.dtd"> <application>
<display-name>Cabin</display-name> <module> <ejb>cabin.jar</ejb> </module> <security-role> <role-name>users</role-name> </security-role> </application> application-client.xml
=========================================================================== This is required for the application client
'Client_1.class'. This exists
in a META-INF directory where the Client_1.class is being executed from. Notice the ejb-ref-name is 'ejb/CabinBean'. This should be the same as the ejb-name in ejb-jar.xml, with the 'ejb/' in front of it. <?xml version="1.0"?>
<!DOCTYPE application-client PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.2//EN" "http://java.sun.com/j2ee/dtds/application-client_1_2.dtd"> <application-client> <display-name>Cabin client</display-name> <description>The cabin client program for testing</description> <ejb-ref> <ejb-ref-name>ejb/CabinBean</ejb-ref-name> <ejb-ref-type>Entity</ejb-ref-type> <home>com.titan.cabin.CabinHome</home> <remote>com.titan.cabin.Cabin</remote> </ejb-ref> </application-client> Client_1.class
========================================================================== This is the client program from O'Reilly publishing
'Enterprise JavaBeans'
book. Notice a couple things: (1) Instead of using a function called
'getInitialContext', I just
instantiate new InitialContext(). You of course need a jndi.properties file that can be found in the classpath upon startup with the required information. I'll provide this a bit later. (2) When I do the lookup. I
do:
Object ref =
jndiContext.lookup("java:comp/env/ejb/CabinBean");
Where as the book does:
Object ref =
jndiContext.lookup("java:comp/env/ejb/CabinHome");
It SHOULD be the ejb-name you supplied in the
ejb-jar.xml file. This was
one of the big reasons it could never find my bean. import com.titan.cabin.CabinHome;
import com.titan.cabin.CabinPK; import com.titan.cabin.Cabin; import javax.naming.InitialContext;
import javax.naming.Context; import javax.naming.NamingException; import java.rmi.RemoteException; import java.util.Properties; /**
* * @author joeman * @version */ public class Client_1 { public static void main(String argv[])
{
try { Context jndiContext = new InitialContext(); Object ref = jndiContext.lookup("java:comp/env/ejb/CabinBean"); CabinHome home = (CabinHome)javax.rmi.PortableRemoteObject.narrow(ref, CabinHome.class); Cabin cabin_1 = home.create(1); 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, "com.evermind.server.ApplicationClientInitialContextFactory"); // p.put(Context.PROVIDER_URL, "ormi://localhost:1024/cabin"); // return new javax.naming.InitialContext(p); // } }
jndi.properties
========================================================================== This file exists in the same directory where
Client_1.class is being
executed. Notice that I specify the ApplicationClientInitialContextFactory. Also, since it is executing outside the ejb-container, i have to specify the provider which is 'ormi://localhost/<application name>. The application name is the name you've given the application in server.xml: <application name="cabin" path="../applications/cabin.ear" /> java.naming.factory.initial=com.evermind.server.ApplicationClientInitialCont
extFactory java.naming.provider.url=ormi://localhost/cabin java.naming.security.principal=<admin account> java.naming.security.credentials=<admin password> I start the orion web server with the following
command:
java -jar -cp %CLASSPATH%;d:\servers\orion\;d:\servers\orion\config\ orion.jar I start the client program using the following command: java -cp d:\development\projects\ejbtemp\;d:\servers\orion\orion.jar;%CLASSPATH% Client_1 The client program needs to have the orion.jar file in the
classpath,
because the jndi.properties file specifies the com.evermind.server.ApplicationClientInitialContextFactory which exists in Orion.jar. Also, I have a jndi.properties file that exists in my orion\config\
path.
You'll notice above, that I specify this path in the classpath when I start the server. There's been some debate as to whether this is needed or not. Doesn't seem to harm anything, but most people have told me that I don't need that. I'm a bit confused on how the web-applications specify their jndi.properties then. Maybe you can shed some light on that. Anyways, the jndi.properties file in the config directory is as follows: java.naming.factory.initial=com.evermind.server.ApplicationClientInitialCont
extFactory java.naming.provider.url=ormi://localhost/ java.naming.security.principal=<admin account> java.naming.security.credentials=<admin password> Joe
|
- Client program for simple EJB tutorial Ken Cooper
- Re: Client program for simple EJB tutorial Joe Antkowiak
- Re: Client program for simple EJB tutorial John D. Ware
- Re: Client program for simple EJB tutorial Ray Harrison
- RE: Client program for simple EJB tutorial Eric Hodges
- RE: Client program for simple EJB tutorial Fyffe Carl
