Working with EJBs


To ColdFusion MX, Enterprise Java Beans are just another Java object, but there are several very specific things you must do to get ColdFusion to work with EJBs. You first need to make sure you have registered the EJB JAR file in the Administrator.

To communicate with the EJB, you need to use CFOBJECT to get the Java Naming and Directory Interface (JNDI) naming-context class. You will use fields from this class to define the information you use to locate the EJB. Because you only use fields, you do not initialize the object. Next you will need to load the Java Hashtable class to contain the context object properties via CFOBJECT. Initialize the Hashtable object using the ColdFusion init method. You will need to set your Hashtable to contain the properties required to create an initial JNDI naming context. These properties include INITIAL_CONTEXT_FACTORY and PROVIDER_URL. You might need to provide the SECURITY_PRINCIPAL and SECURITY_CREDENTIALS values as well; they might be required for secure access to the naming context.

Next load the JNDI InitialContext class. Call the init method with the Hashtable object values to initialize the InitialContext object. Call the InitialContextext object lookup method to get a reference to the home interface for the bean you want. Specify the JNDI name of the bean as the lookup argument. Call the create method of the bean's home object to create a new instance of the bean. If you are using Entity beans, you typically use a finder method instead. A finder method locates one or more existing entity beans.

Now you can use the bean's methods as required by your application. When finished, call the context object's close method to close the object.

The following code shows this process using a simple Java Entity bean. It calls the bean's getMessage method to obtain a message.

 <html>  <head>    <title>cfobject Test</title>  </head>  <body>  <H1>cfobject Test</H1>  <CFOBJECT    action=create    name=ctx    type="JAVA"    >  <CFOBJECT    action=create    name=prop type="JAVA"    >  <cfset prop.init()>  <cfset prop.put(ctx.INITIAL_CONTEXT_FACTORY, "jrun.naming.JRunContextFactory")>  <cfset prop.put(ctx.PROVIDER_URL, "localhost:2918")>  <CFOBJECT    action=create    name=initContext    type="JAVA"    >  <cfset initContext.init(prop)>  <cfset home = initContext.lookup("Simple")>  <cfset mySimple = home.create()>  <cfset myMessage = mySimple.getMessage()>  <cfoutput>  #myMessge#<br>  </cfoutput>  <cfset initContext.close()>  </body>  </html> 

Working with EJBs from ColdFusion is by no means an easy task; but if you can get the JNDI initial context, you are in the home stretch. If you do not have a good understanding of building, deploying, and using EJBs, you will find that you will need to consult your J2EE application server's manuals to get specific information on JNDI values and properties. Armed with that information, you should be able to call your EJB from ColdFusion.



Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net