[ Team LiB ] |
The Inventory Item Example—COM Client Accessing an EJBIn the inventory item example, we'll expose an Inventory Item EJB and access it through a Visual Basic COM client. The Item EJB can be found in the c:\ch33\jCOM\InventoryItem\earlybound directory. Make sure that it's deployed and available on WebLogic Server. We will access this bean in a late bound and then an early-bound fashion. The main difference between the early-bound and late-bound examples is that for early bound access, an additional task is required: creating and deploying a type library and wrapper classes for the bean. The example presumes that you have both WebLogic and the COM client on a Microsoft Windows machine. You'll need Visual Basic 6.0 and the Microsoft MIDL compiler available on your machine as well. The Microsoft MIDL compiler comes bundled with the Microsoft Platform SDK and can be found on Microsoft's Web site. The code files for this example can be found on the accompanying CD-ROM at the back of the book. First, copy the InventoryItem sample directory from the ch33\jCOM\InventoryItem directory on the accompanying CD-ROM and place it on your machine at C:\ch33\jCOM\InventoryItem\. Next, we'll set up everything that's required on WebLogic Server to expose the Inventory Item EJB. WebLogic Server SetupThere are a few things that we will need to do in order to set up the WebLogic Server correctly for this example. This will be done through the WebLogic Server's administrator's console. First, make sure that the Inventory Item EJB is deployed and is targeted to the examples server. To enable COM calls on WebLogic Server, do the following:
Next, we'll need to administer the security settings on WebLogic Server to allow access to our objects. Additional jCOM-specific security settings are required because the COM client will be accessing our objects remotely.
NOTE You can find more in-depth information about granting and revoking access to classes in the BEA electronic documents found at: http://e-docs.bea.com/wls/docs81/secintro/index.html. Next, register the Java virtual machine where the COM client can find the EJB with the following command: regjvmcmd.exe examplesServer localhost[7001] Change the server name and port number where applicable. For more details on regjvmcmd, refer to "Working with jCOM Tools," earlier in this chapter. Everything we've done thus far is required for both early and late binding. Now we get to the specific differences between the late and early approaches. The Inventory Item Visual Basic Client Accessed with Late BindingThis example demonstrates the capability for a Visual Basic client program to access an Enterprise JavaBean using the late binding method. Step-by-step instructions are provided. Creating the VB ClientOpen the VB project InventoryItemLateBound.vbp located in the c:\ch33jCOM\InventoryItem\LateBound directory you created from the files located on the accompanying CD at the back of the book. Open the form code and the first thing you'll notice is the manner in which a handle to the Item bean is obtained. It is similar to any other VB object creation: 'Bind the EJB ItemHome object via JNDI Set mobjHome = CreateObject("examplesServer:jndi: jcom-inventoryitem-itemHome") Here you reference the server where the bean resides and the JNDI name it was deployed with. As you can see in Listing 33.2, coding occurs as usual with Visual Basic, but because we do not have access to a type library for the item bean, everything must be referenced as an object. Here you can clearly see the tradeoff you're making. Late binding will get you up and running quickly, but you program somewhat in the dark. Listing 33.2 Visual Basic Bank Account Client Code ListingPrivate Sub ListItem() Dim objItem As Object Dim title As String 'Handle errors On Error GoTo ErrOut Set objItem = mobjHome.Create() title = objItem.getItem() MsgBox ("EJB Accessed:: " + title) Exit Sub Run the code by pressing F5. As depicted in Figure 33.11, you should see a message box that displays the results of your EJB method call. Figure 33.11. Inventory Item Visual Basic client.The Inventory Item Visual Basic Client Accessed with Early BindingEarly binding requires the creation of a type library that is registered in the client machine's Registry and associated wrapper classes that are compiled and placed in the classpath of WebLogic Server. This can be a somewhat labor-intensive task, so I highly recommend the use of a build tool such as Ant to expedite the process. If you haven't worked with Ant before, now is as good a time as any to start. In the C:\ch33\jCOM\InventoryItem\earlybound directory, you'll find the Ant build script build.xml as well as everything else you need to accomplish early binding. As we go through this process, open each of the files and inspect them. The goal here is for you to later be able to copy and alter these files to suit your own implementations. Follow these steps to invoke the Inventory Item Visual Basic client with early binding:
Creating the VB ClientOpen up the VB project InventoryItemLateBound.vbp located in the c:\ch33\\jCOM\InventoryItem\earlybound directory you created from the files located on the accompanying CD at the back of the book. From the Projects menu, select References. Make sure that the itemTLB is available for the project as in Figure 33.14. Figure 33.14. Visual Basic references.Open up the form code. Look through the code and notice the differences between this and the late-bound version. The distinction here being that you can now see the methods signatures and attributes of the Item bean as you interact with it. The nomenclature of the code is clearer because we no longer have to refer to everything simply as object. Run the code by pressing F5, and you should see the same results as shown in the early-bound version you saw earlier. |
[ Team LiB ] |