EAServer 6.1 .NET Client Support
The EAServer .NET client is one of the most important features in EAServer 6.1. This new feature enables .NET to communicate with EAServer components using IIOP. It provides underlying marshaling support for PowerBuilder's WinForm applications with EAServer and it's also a way to interoperate between .NET and J2EE distributed objects.
In this article, we'll introduce EAServer .NET client and illustrate how to invoke EJB in EAServer from ASP.NET and PowerBuilder WinForm application.
Note: The ${EAServer6.1} in this article refers to the EAServer 6.1 installed directory.
Overview of .NET Client
The EAServer 6.1 .NET client contains the following components:
• NetCompiler - To generate C# stubs for existing components inside EAServer. You can invoke NetCompiler with the batch file, netcc.bat, located in ${EAServer6.1}\bin.
To generate stubs for EJB deployed at EAServer 6.1, use the following command:
${EAServer6.1}\bin\netcc.bat ejbjar-${ejb_package_name}
• .NET Runtime Assemblies To provide runtime support for C# stubs to communicate with EAServer such as managing IIOP, SSL, compression, and so forth. The .NET runtime assemblies are located in the ${EAServer6.1}\lib directory. The two libraries of interest in the runtime assemblies are:
- com.sybase.iiop.net.dll - Provides basic runtime support such as marshaling, managing connection, SSL, compression, and so forth
- com.sybase.ejb.net.dll - Provides runtime support for invoking EJB using .NET
Preparing EJB for the C# Client
To begin with, we need to prepare an EJB for our sample. You can create the EJB using your favorite IDE. In this case, I created the EJB with Netbeans5.5. The EJB's remote interface has two methods:
• String sayHello(String yourName) throws RemoteException
• ResultSet getResultSet(String sql) throws RemoteException
The first method just concatenates yourName with the word "Hello." The second method uses the SQL statement provided to query the default connection cache of EAServer and sends back a java.sql.ResultSet of the query result to the client.
After obtaining the ejbjar, you need to deploy it to EAServer. You can deploy the ejbjar using EAServer's Web-based Management Console.
Generating the C# stubs with NetCompiler
The EJB package name is dotnetsample (the package name is lower case), which is the same as your ejbjar's filename without an extension. Now you're ready to execute the following using the NetCompiler to generate C# stubs:
${EAServer6.1}\bin>netcc.bat ejbjar-dotnetsample
NetCompiler generates both the C# stubs source files and the compiled assemblies. Normally, we don't have to worry about the C# source files since these *.cs files will be compiled and packed inside the output assembly as follows:
${EAServer6.1}\deploy\assemblies\dotnetsample.client.dll
There are two classes inside the assembly that we need to take note of:
• com.sybase.net.sample.MyEJBRemote C# remote interface
• com.sybase.net.sample.MyEJBRemoteHome C# remote home interface
The C# remote interface and remote home interface names are identical to Java. The interfaces are used for EJB remote stub and home stub.
Calling EJB from ASP.NET
In this example, we'll illustrate how to call EJB inside ASP.NET using Microsoft Visual Studio 2005.
Here are the steps:
1. Create an ASP.NET solution by selecting File | New | Web Site | ASP.NET Web Site.
This creates a simple Web application.
Next, we need to include .NET runtime assemblies and EJB's stub assembly in the ASP.NET project folder.
2. Select WebSite | Add Existing Item ...
3. Select following assemblies in your file system and add them to your project folder:
• com.sybase.ejb.net.dll
• com.sybase.iiop.net.dll
• dotnetsample.client.dll
Both com.sybase.ejb.net.dll and com.sybase.iiop.net.dll are located in ${EAServer6.1}\lib; dotnetsample.client.dll is the EJB's stub assembly we just created in ${EAServer6.1}\deploy\assemblies.
When done, the assemblies' directory structure appears as shown in Figure 1.
Adding a TextBox, Button & Label in the Web Page(Figure 2)
Drop a TextBox, Button, and Label in Default.aspx and modify the codes in Default.aspx.cs (see Listing 1).
Quick points in the code:
1. We import the following namespaces:
• com.sybase.ejb.net To cater to the .NET runtime API.
• com.sybase.net.sample To cater to the stubs generated from the EJB
2. We create EjbProvider, which acts like an EAServer profile. We define a URL, user name, and password in EjbProvider.
3. We use EjbConnection to get the home interface (com.sybase.net.EchoRemoteHome). This method will look up the server's naming context and get a stub from the server using the connection information from EjbProvider.
4. We use home.create() to get the remote interface and call its business method "hello." This is exactly like what we do in a Java EJB client.
5. In Button1_Click, we call the sayHello method with our input and display the result in Label1. We type "Freeman" and get "Hello Freeman" in our Web page (see Figure 3).
Now let's create a complicated one to see what we can do with ResultSet.
Create a Web page containing a TextBox, a Button, and a GridView. (See Figure 4 for the Web page layout design). Edit Default2.aspx.cs (see Listing 2) to include these controls.
Listing 2 is similar to Listing 1. The difference is that it includes DataTable. This means that it will get a DataTable from the server and display the results using GridView.
Take a closer look at the generated stub interface: MyEJBRemote.cs (see Listing 3).
The Java type, java.sql.ResultSet, is mapped to System.Data.DataTable. This implies that the ResultSet generated from the server will be unmarshaled as DataTable at C# side. This lets us use GridView and these kinds of .NET facilities with ease to display the search result.
When done, launch the Web page in the Web browser. In this example, we'll launch this Web page using Mozilla Firefox. Enter the table name of your database you want to view. Next, click the Execute button. The table's contents are displayed inside the GridView. (See Figure 5.)
Calling EJB from PowerBuilder.NET
Here comes the PowerBuilder! We could also use PowerBuilder 11.0 as the .NET client to call EAServer 6.1. We'll create a small example to illustrate this.
First, create a PowerBuilder project by selecting File | New... | Target | .NET Windows Forms Application. Inside the target, we create a new Window. (See Figure 6.)
Second, we'll call the remote EJB inside a .NET block in PowerScript. Unlike ASP.NET, I wrapped the complexity of getting ejbReference inside a C# class: ClientProxy.cs (see Listing 4).
Obtaining the EJB reference code is similar to ASP.NET. Use the "csc" to compile the code into a ClientProxy.dll:
csc /target:library ClientProxy.cs /r:com.sybase.iiop.net.dll
/r:com.sybase.ejb.net.dll /r:dotnetsample.client.dll
Hence, we can import it into PowerBuilder IDE. Likewise to ASP.NET, we need to reference the .NET assemblies inside PowerBuilder project. This can be done by setting the assemblies in PowerBuilder's "target" property's | .NET Assemblies tab. In PowerBuilder .NET, we have to include four assemblies. The additional assembly to include is ClientProxy.dll, which contains a ClientProxy class. (See Figure 7.)
Third, we'll append the Button sayHello's click method in the code:
We put our code inside the .NET Block:
#IF Defined PBDOTNET Then
#END IF
The syntax in the .NET Block is identical to PowerScript. But PowerBuilder's "pb2cs" will parse all .NET target types inside the .NET Block. The example uses EAServer information, namely the URL, username, and password from the GUI. It then takes sle_yourname's input as a parameter and calls the sample EJB. After that, it will send a Message Box to show the result.
For the second Button, we call the getResultSet method to get the DataTable. After that, we use .NET's DataTable.WriteXML method to serialize the table data inside an XML file.
When done, right-click the target properties and select Deploy (see Figure 8). The PowerBuilder IDE will generate an assembly "mywinform.exe." This assembly can be executed standalone.
When this article was written, PowerBuilder 11.0 .NET Windows Forms application couldn't generate .NET stubs for non-visual objects (NVO) deployed at EAServer. Since PowerBuilder NVO is wrapped as an EJB in EAServer 6.x, this method could also be used to call the PowerBuilder NVO by PowerBuilder .NET WinForm. In future PowerBuilder releases, it may be able to generate .NET stubs for the NVO deployed at EAServer directly using the PowerBuilder IDE. Hence, we should be able to use PowerBuilder's instead.
Conclusion
The EAServer .NET client lets you to invoke EAServer 6.1 components using the .NET client. Calling an EJB from ASP.NET and PowerBuilder .NET WinForm illustrate how this can be done. But inside EAServer 6.x, other components are wrapped as EJBs including C++ components and PowerBuilder NVOs. So, if you want to call your component rather than the EJB, you can easily find the EJB wrapper in EAServer 6.x and use the procedures as shown in the ASP.NET and PowerBuilder .NET WinForm.
The .NET client in EAServer 6.1 can also work with the JMS in our .NET program, which wasn't covered in this article. For more information, refer to the EAServer 6.1 documentation at Sybase Product Manuals Web site http://sybooks.sybase.com/nav/base.do
© 2008 SYS-CON Media