Integrating with EJBs


You can interact with Enterprise Java Beans (EJBs) using ColdFusion MX 7. There is no specific tag or function built into ColdFusion for getting a reference to an EJB. Instead, you use a series of <cfobject> or CreateObject() to instantiate and work with the standard Java classes responsible for locating and maintaining EJBs, such as javax.naming.Context and javax.naming.InitialContext. The specific steps are very similar to the steps you would take when connecting to EJBs in normal Java code; you just use the CFML-style syntax to do so.

Once you have an instance of an EJB, you access its methods and properties just like any other Java class object or Bean. You will see a simple example of calling an EJB method in the next listing. For details about working with the methods and properties of a Java object, refer to the "Using Java Class Objects" section at the beginning of this chapter.

A Simple Example

Listing 29.12 shows how to instantiate and work with a sample EJB called HelloBean. This sample EJB exposes just one method, getMessage(), which always returns the same "Hello, World!" type of message. This page simply interacts with the appropriate JNDI objects to create an instance of the EJB called myInstance, and then calls myInstance.getMessage() to obtain and display the text of the sample message (Figure 29.6).

Figure 29.6. This page displays a message from the sample EJB called HelloBean.


NOTE

This example assumes you are using Macromedia JRun as the EJB host/container. If you are using some other J2EE server to host your EJBs, you will need to alter a few of the lines (most probably the ones that set the INITIAL_CONTEXT_FACTORY and PROVIDER_URL).


Listing 29.12. EJBExample.cfmInstantiating and Working with an EJB
 <!---  Filename: EJBExample.cfm  Author: Macromedia, Inc. (adapted from the ColdFusion MX 7 documentation)  Purpose: Shows how to instantiate and use an Enterprise Java Bean (EJB) ---> <!--- Create the Context object to get at the static fields. ---> <cfset ctx = CreateObject("Java", "javax.naming.Context")> <!--- Create the Properties object and call an explicit constructor---> <cfset props = CreateObject("Java", "java.util.Properties")> <cfset props.init()> <!--- Specify the properties These are required for a remote server only ---> <cfset props.put(ctx.INITIAL_CONTEXT_FACTORY, "jrun.naming.JRunContextFactory")> <cfset props.put(ctx.PROVIDER_URL, "localhost:2908")> <!---   (You might add the following if security credentials need to be provided)   <CFSET prop.put(ctx.SECURITY_PRINCIPAL, "admin")>   <CFSET prop.put(ctx.SECURITY_CREDENTIALS, "admin")> ---> <!--- Create the InitialContext ---> <cfset initContext = CreateObject("Java", "javax.naming.InitialContext")> <!--- Pass the properties to the InitialContext constructor. ---> <cfset initContext.init(props)> <!--- Get reference to home object ---> <cfset home = initContext.lookup("HelloBean")> <!--- Create new instance of entity bean ---> <cfset myInstance = home.create()> <!--- Call a method in the entity bean ---> <cfset myMessage = myInstance.getMessage()> <html> <head><title>EJB Example</title></head> <body> <h2>EJB Example Page</h2> <!--- Display the value returned by the method ---> <cfoutput>   The following message was returned by the EJB:<br>   <b>#myMessage#</b><br> </cfoutput> </body> </html> <!--- Close the context. ---> <cfset init Context.close()> 

Refer to the JRun documentation or an EJB reference or tutorial for more information on the way the javax.naming.Context and javax.naming.InitialContext are used in this listing. You will find that Listing 29.12 is a fairly straightforward port of the kind of EJB-instantiation code typically used by Java programmers.

NOTE

The sample HelloBean EJB is provided in a JAR file called sample_hello_bean.jar from Macromedia, included with this chapter's listings for your convenience. You will need to deploy the EJB before running the example. If you are using Macromedia JRun 4, just copy the JAR to your server's deploy folder (perhaps JRun4/servers/default). For other J2EE servers, the specific steps for deploying an EJB will be different.

If you haven't used EJBs before and just want to see this example in action, download and install the free developer edition of JRun 4 from Macromedia's site. Once JRun is installed and running, copy the sample_hello_bean.jar file (included with this chapter's listings) to the servers/default folder (that's C:\JRun4\servers\default in a typical Windows installation), which should automatically deploy the EJB. Assuming that JRun and ColdFusion MX 7 are now running on the same server and using the default HTTP ports, the example should work as presented here.

This example is adapted fairly directly from the ColdFusion documentation. We don't do this often in this book, but in this case it makes a lot of sense. There really aren't many different ways to slice this particular problem. We did take the liberty of changing the <cfobject> tags in the documentation's listing to CreateObject() function calls, because the code reads better and will be more familiar to Java coders in this form.


Making It Easier with a Custom Tag

Although the code in Listing 29.12 is a straightforward port of the Java code that you would typically use to deploy an EJB, it's not particularly simple or ColdFusion-like. This chapter includes code for a custom tag called <CF_UseEJB> that makes it easier to work with EJBs; this tag need merely be located and created in the typical fashion. You can use <CF_UseEJB> as is if you wish, or you can adapt the idea to suit your own needs. As presented here, the tag supports the four attributes listed in Table 29.7.

Table 29.7. <CF_UseEJB> Custom Tag Syntax

ATTRIBUTE

DESCRIPTION

EJBName

Required. The name of the EJB that you wish to work with, as it is known by the JNDI naming service.

Variable

Required. A variable name for the instantiated EJB object.

InitialContextFactory

Optional. The name of the appropriate initial context factory. As presented here, this attribute defaults to jrun.naming.JRunContextFactory, which means that the tag will automatically try to connect to JRun's naming implementation by default.

ProviderURL

Optional. The URL used to connect to the JNDI naming service. As presented here, this attribute defaults to localhost:2908, which means that you don't need to provide this attribute if JRun is installed on the same server as ColdFusion and is using the default port of 2908.

SecurityPrincipal

Optional. The security principal information (in most cases, some kind of username), if any, that is needed to connect to the naming service.

SecurityCredentials

Optional. The security credentials information (in most cases, some kind of password), if any, that is needed to connect to the naming service.


Listing 29.13 shows the code used to create the <CF_UseEJB> custom tag. Again, feel free to adapt this code to suit your needs, or to use it as a starting point for some other type of EJB-related abstraction. For instance, you might decide to redesign this code as a CFC rather than a custom tag.

Listing 29.13. UseEJB.cfmA Custom Tag to Make It Easier to Work with EJBs
 <!---  Filename: UseEJB.cfm  Author: Nate Weiss (NMW)  Purpose: Creates a custom tag to ease the task of getting an EJB instance ---> <cfsilent>   <!--- Tag Attributes --->   <cfparam name="ATTRIBUTES.EJBName" type="string">   <cfparam name="ATTRIBUTES.Variable" type="variableName">   <cfparam name="ATTRIBUTES.InitialContextFactory" type="string"     default="jrun.naming.JRunContextFactory">   <cfparam name="ATTRIBUTES.ProviderURL" type="string"     default="localhost:2908">   <!--- Create the Context object to get at the static fields --->   <cfset ctx = CreateObject("Java", "javax.naming.Context")>   <!--- Create the Properties object and call an explicit constructor --->   <cfset props = CreateObject("Java", "java.util.Properties")>   <!--- Specify the properties to pass to the initial context --->   <cfset props.put(ctx.INITIAL_CONTEXT_FACTORY, ATTRIBUTES.InitialContextFactory)>   <cfset props.put(ctx.PROVIDER_URL, ATTRIBUTES.ProviderURL)>   <!--- If a SecurityPrincipal attribute was provided --->   <cfif IsDefined("ATTRIBUTES.SecurityPrincipal")>     <cfset prop.put(ctx.SECURITY_PRINCIPAL, "admin")>   </cfif>   <!--- If a SecurityCredentials attribute was provided --->   <cfif IsDefined("ATTRIBUTES.SecurityCredentials")>     <cfset prop.put(ctx.SECURITY_CREDENTIALS, "admin")>   </cfif>   <!--- Create the InitialContext --->   <cfset initContext = CreateObject("Java", "javax.naming.InitialContext")>   <!--- Pass the properties to the InitialContext constructor. --->   <cfset initContext.init(props)>   <!--- Get reference to home object --->   <cfset home = initContext.lookup(ATTRIBUTES.EJBName)>   <!--- Create new instance of entity bean --->   <cfset instance = home.create()>   <!--- Return the completed instance --->   <cfset "CALLER.#ATTRIBUTES.Variable#" = instance> </cfsilent> 

Listing 29.14 shows how the new <CF_EJB> custom tag can be used in a ColdFusion page. Assuming that the EJB is being hosted by a JRun server running on the same physical machine as ColdFusion MX 7, and assuming that no security credentials (such as a user name and password) need to be provided, the EJB can now be instantiated using just the EJBName and Variable attributes (add the other attributes from Table 29.7 as needed).

When visited with a browser, this page displays the same message as the first example in this section (see Figure 29.6).

Listing 29.14. UseEJBDemo.cfmUsing the <CF_UseEJB> Custom Tag
 <!---  Filename: UseEJBDemo.cfm  Author: Nate Weiss (NMW)  Purpose: Instantiates an EJB via <CF_UseEJB> ---> <!--- Create an instance of the HelloBean EJB ---> <CF_UseEJB   EJBName="HelloBean"   Variable="mySimple"> <html> <head><title>EJB Example</title></head> <body> <h2>EJB Example Page</h2> <!--- Display the value returned by the method ---> <cfoutput>   The following message was returned by the EJB:<br>   <b>#mySimple.getMessage()#</b><br> </cfoutput> </body> </html> 



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

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