PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder

PowerBuilder: Article

Accessing Jaguar Properties in PowerJ Pt. 2

Accessing Jaguar Properties in PowerJ Pt. 2

In the May issue of PBDJ (Vol. 7, issue 5) we looked at how to initialize a Jaguar component using the Jaguar component properties, focusing on a PowerBuilder component using the ContextKeyword object. In this article we'll look at a PowerJ / Java component and the Jaguar::Repository. The Jaguar::Repository interface discussed in this article can be used from PowerBuilder as well.

The Jaguar component we'll build will be written in Java using PowerJ 3.5. It will be written as a stateless session bean, an Enterprise JavaBean (EJB) component type used to develop components that model business processes. This article won't cover EJB since that's outside the scope of using Jaguar component properties to initialize a component.

It's easy to create a stateless session bean in PowerJ using wizards. Create a new target in PowerJ using the EJB 1.0 Wizard as shown in Figure 1. Our session bean will be named BTFBankAccountBean and will be used to retrieve a list of bank accounts from the BTFBank database. The code to retrieve the accounts is shown in Listing 1. For the purposes of this article it's assumed that the reader has some knowledge of Java and JDBC so the function getAccounts isn't covered in detail here. The function uses an instance variable JagCache to store the name of the database connection cache.

The BTFBank database is a Sybase ASA (Adaptive Server Anywhere) database. Using the Jaguar Manager a connection cache must be set up to connect to the ASA database using the JDBC-ODBC bridge driver included with the Sun JDK. The connection cache name is BTFBank_JDBC and it must have the "access connection cache by name" option enabled in order for the code sample to work as shown. The component will use a Jaguar component property to get the name of the connection cache as shown in Figure 2.

After the component has been written and deployed to Jaguar CTS, the component property is set for it using the Jaguar Manager. This was covered in the last article. The rest of this article will focus on the Jaguar::Repository.

The Jaguar::Repository Interface
The Jaguar::Repository interface is provided with Jaguar CTS to allow developers to access and manipulate the Jaguar server's repository programmatically. Normally an administrator will use the Jaguar Manager to make changes to the Jaguar repository. However, the Jaguar::Repository interface allows a third-party software vendor to build utilities to complement the Jaguar Manager and allows other IDE vendors to build wizards to deploy components into Jaguar. We'll look at using the interface from a Jaguar component to read values from its own properties to initialize itself.

When dealing with the Jaguar repository we're working with entities. Jaguar has several "well-known" or defined entities, listed in Table 1. These entity types are used to tell Jaguar what type and how much information to return based on a method call.

The interface has several functions that deal with the Jaguar repository. Table 2 lists them all with a brief description. In this article we'll be focusing on the lookup function that allows a Jaguar entity to retrieve all of its properties.

The lookup method of the Repository component retrieves the properties for a Jaguar entity and has the following method signature:

Jaguar::Properties lookup (in string entityType, in string entityName) throws Jaguar::LookupError
The entityType argument takes one of the entity values specified in Table 1. The entityName argument specifies the name of the Jaguar entity. The name of the entity is expected to be of the same type as specified in the entityType argument; otherwise Jaguar won't be able to find it. In order to tell the Jaguar CTS server to return all the component properties for a component, the entityType argument should pass in the value "Component" and the entityName argument should be the name of the component using the Jaguar Package/Jaguar Component name syntax. If the BTFBankAccountBean component is deployed to the PBDJ package on Jaguar CTS with the name BTFBankAccount, the entityName is PBDJ/BTFBankAccount. The name used here is not dependent on the type of Jaguar component being accessed and should always be the Jaguar Package/Jaguar Component name syntax for both an EJB and a CORBA component deployed to Jaguar.

The call to the lookup method, assuming we've already gotten a remote object reference - JagRep - to the Jaguar Repository component, would be as follows:

com.sybase.jaguar.system.Property JagProp[];
JagProp = JagRep.lookup("Component", "PBDJ/BTFBankAccount");

The lookup method returns a Jaguar::Properties return value that's a sequence of Jaguar::Property data types. A sequence is the CORBA IDL representation of a single dimensional Java array. The Jaguar::Property data type is a structure that defines the properties of the entity as name-value pairs. It has the following definition:

struct Property
string name;
string value;
Once the array of structures is returned by the call to the lookup function, the component can search the list of properties for the ones it needs to initialize itself. We'll look at this in more detail.

Getting a Remote Reference to Jaguar::Repository
For a Jaguar component to use any other Jaguar component it must have a stub that defines the interface of the component it wishes to use. The com.sybase.jaguar.system.Repository component that implements the Jaguar repository interface is a CORBA component provided with Jaguar CTS. To use this component in PowerJ, generate the Java stubs. This can be done using the Jaguar Manager or the PowerJ tool. From PowerJ choose the Component | Add Jaguar Component menu option. Specify the host and port of the Jaguar CTS server and select the Repository object under the Jaguar package. The Helper, Holder, Jaguar::Properties and Jaguar::Property data types that are needed are also generated with the Java stubs by Jaguar.

Before accessing the Repository object deployed to the "Jaguar" package we need to initialize an ORB. The ORB class allows a client application or a component to communicate with a server-side CORBA ORB/application server. The ORBClass must be set to com.sybase.CORBA.ORB to communicate with Jaguar CTS. To access the component in Jaguar we can take advantage of the string_to_object function on the ORB and tell Jaguar to give us an object reference to the Jaguar/Repository object using just the component's Jaguar package and name. Jaguar will return the IOR for the component on the same Jaguar server that the Jaguar component that made the call is running on, which is exactly what we want. The code sample to do this is as follows:

import com.sybase.jaguar.system.*;
Repository JagRep = null;
java.util.Properties props = new java.util.Properties();
props.put("org.omg.CORBA.ORBClass", "com.sybase.CORBA.ORB");
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init((String[])null, props);
JagRep = RepositoryHelper.narrow(orb.string_to_object("Jaguar/Repository"));
Once the component has a remote object reference, it needs to narrow or cast it to the appropriate interface using a Helper class that's generated with the Java stubs. After this is accomplished, the component can invoke the lookup method.

Using the Property Value
Once the call to the lookup method is made, the Jaguar component can search through the array of structures for a particular Jaguar component property and get its value. In our example the property name is BTFBank.database.cachename. To properly initialize our session bean we should place the code to initialize the ORB, get the remote reference to the Jaguar/Repository object, invoke the lookup method and search for the Jaguar property in the ejbCreate method of the Bean class. This method is called when the component instance is instantiated and ensures that all the values we need to initialize the component can be looked up before any other methods are invoked. The code shown in Listing 2 puts the code together in a single function, ejbCreate.

The value for the database connection cache name, stored in the instance variable JagCache, can be used by the rest of the methods on the session bean - including getAccounts - to access the proper Jaguar database connection cache. Should the name of the connection cache need to change, the Jaguar property value can be modified in the Jaguar Manager and the component instance can be refreshed so the new value is used.

This concludes a two-part series on how a component can easily access its own component properties in Jaguar CTS and use the values to initialize itself, allowing information such as database connection cache names, Jaguar server URLs and turning debugging messages on and off.

While this article has touched on the Jaguar repository and the Jaguar::Repository interface, I recommend my new book, Taming Jaguar, coauthored by Jason Weiss and published by Manning Publications, a chapter of which is dedicated to this topic. Additionally, this book covers EJB development in Jaguar and PowerJ, also mentioned in this article.

More Stories By Mike Barlotta

Background Information: Michael Barlotta is the Director of Technology at AEGIS.net Inc (www.AEGIS.net). Mike is a Sun Certified Java Programmer and is recognized as an expert on the Sybase application server EAServer (Jaguar CTS), mentoring clients and speaking at conferences on the topic. He is the author of several books including Taming Jaguar and Jaguar Development with PowerBuilder 7 (both by Manning Publications). Be sure to check out Mike’s Web site www.TamingJaguar.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.