| BUILDING BEANS |
|
|
||
| Writing web services with Enterprise Java Beans could be a lot less curdling with a little less wizardry. | ||||
by
Chris Amaru |
|
Initially Java was to be a language for building robust Internet applications. The tools available in the infancy of Java, however, were distinctly oriented towards building GUI applications for display in browsers. These tools were long on graphical components and short on data access and enterprise programming features. As a result, the first Java applets provided much more style than function and the notion of using Java applets on web sites came into disfavor. Only later did versions of the JDK introduce the construct of server-side applets dubbed servlets. Still, servlets were a primitive use of Java on the server and something else was needed. This came with the introduction of Java 2 and Enterprise Java Beans (EJB). With EJB, a true three-tier application could be built with Java, complete with business objects that encapsulated database access and abstracted business rules. |
|
|
||||||
|
Sun's Forte for Java Community Edition, which is built on the Open Source Netbeans project, aids in the development of server-side applications through servlets and Java Server Pages. This Java IDE, however, is short on enterprise features and does not provide for building an EJB. On the other hand, the new Forte for Java 3.0, Enterprise Edition (FFJEE) provides a robust environment for developing enterprise-class Java applications concentrating on the features for database access, web service creation, and EJB creation and deployment. To test FFJEE's enterprise-class features, we decided to build an EJB that modeled information about magazine subscribers, and a test web application that used that EJB. We also used that EJB to make a web service that could be used to retrieve information about subscribers over the web. We tested FFJEE with a number of different databases including MySQL, Microsoft SQL Server, DB2, and PointBase, a pure Java database that is included with both editions of FFJ 3.0. The database that seemed to work best was DB2, followed closely by PointBase. When using FFJEE with a database, understand that you must have a functional JDBC driver on your system for the database features to work. Since PointBase is a pure Java database, it includes a JDBC driver, as does DB2. Unfortunately, MySQL and SQL Server did not include JDBC drivers; you'll need third-party drivers to use them with FFJEE. |
|
To test any EJB that you create with FFJEE, you will need a Java application server of some sort. FFJEE provides deployment support for both the J2EE 1.2 Reference Edition application server and the Netscape iPlanet Application Server 6.0. We used the J2EE RI server and that worked fine. You can manually deploy your applications to other servers, but if you use one of the supported application servers, FFJEE does everything for you. |
|
Sun has very specific notions about how EJB should be used to provide a persistent view of data and the business methods that operate on that data. In this context, FFJEE has a wizard that creates an EJB from a database table or its schema. There are two ways to look at the schema of a database. One is to connect to the database in question and use that connection in the EJB Wizard. The other is to load the schema from the database and use the schema in the EJB Wizard. While both methods accomplish the same task, there are distinct differences between the two. In particular, we needed to create a class to represent the detail data of a subscriber, and that wizard only works with a schema. We selected the table that held our subscriber data and navigated through the wizard's dialog boxes. |
|
||||
|
A schema can hold the definitions for any number of database tables. You can browse the schema to look for fields, indexes and foreign keys within the tables. You can also use it to generate Java files for use with the Transparent Persistence feature of FFJEE. What you can't do is use the schema to browse the data of the table itself. To do this you must go to the Runtime tab of the Explorer and select the Databases entry in the tree. From there, you can perform various database functions on tables, views, and stored procedures. We had a lot of problems with the database browsing features of the product. For the most part, the features provided by the Database browser just don't seem to work. We could successfully access the database, but not without changing the SQL code generated by FFJEE. |
|
To create a schema we chose New/Databases/Database Schema from the context menu of a package we had previously created. This action exposed the first flaw we found in the software. When you use the context menu—especially the New menu, which is long—the menu first displays from the position where you right-clicked, downward. Then the IDE figures out that it can't fit the menu downward, so it re-displays the menu upward. It only does this after you move to a sub-menu, at which point it forgets where you were. We had no such problems with the wizard used to create an Entity bean from a table. You can create either a Bean Managed Persistence (BMP) entity bean or a Container Managed Persistence (CMP) entity bean. If you choose to create a BMP entity bean, you are responsible for all of the database access code for your bean. You must adhere to the EJB specifications, which can be quite a task. On the other hand, if you choose to create a CMP entity bean, the EJB container handles all of the database access code. You only have to implement a minimum number of interfaces to make your bean work. In most cases, this will be the preferred method, especially if you are using an RDBMS to store your data. The only time when we can see using Bean Managed Persistence is when using an unconventional method of data storage, such as XML. To minimize an already curdling exercise, we chose to create a CMP bean. When FFJEE queried for a database table to use as the basis for our entity bean, we chose our SUBSCRIBER table and were presented with the list of columns for our review. FFJEE automatically created the Java field names with the common method of using a lowercase character for the first character in the identifier, and uppercase characters for the start of other words within the identifier. For example, the database column named MEMBER_ID was changed to memberId. We don't have a problem with this as it is a common practice, but the Java field name was not the only thing changed. When you create a CMP entity bean, by default, FFJEE creates SQL statements for the CREATE and DROP of the table, as well as SELECT, INSERT, UPDATE and DELETE statements that correspond to the ejbLoad, ejbCreate, ejbUpdate and ejbDelete methods of the entity bean respectively. Unfortunately, these statements use the same names (case included) as the Java field names. Also, FFJEE expects that you will create a new table based on the old table's schema to hold any data added by your application. All of this is fine unless you actually want to use the old table. If this is the case, you must manually change the properties of your entity bean, in order not to create a new table when your application is deployed and not drop the existing table when your application is undeployed, which is all easily accomplished through a property sheet. If that were the end of it, we wouldn't have any complaints. The problem is that since FFJEE changed the column names in the property sheet to match the Java field names, you must now go back and manually edit the SQL statements to match the original table, including the case of the identifiers. A simple option to use the original table as the target of the SQL statements would make life much easier. Once the wizard completed its magic, we were left four new entries under the package we had created to hold our entity bean. The wizard created two interfaces, one Java class, and a logical bean that wraps it all together. This logical bean is very important. It allows you to create business methods and finder methods, as well as, create methods once and have the appropriate entries made in the physical class and interfaces. In particular, we invoked the context menu for the logical bean and chose New Finder Method from the menu. We entered the method name findByCompany, and the single String parameter, “companyName”, into the dialog box. A method signature was automatically created in the home interface for our bean. The only thing left for us to do was to create a SQL SELECT statement for our new finder method. In another case, we wanted a business method that would return a Java class that wrapped all of the bean's CMP fields for easy processing. Again, we invoked the logical bean's context menu and this time chose New Business Method from the menu. We called our business method getSubscriberDetail and had the method return an object of class SubscriberDetail, which we had created earlier. FFJEE automatically created a method signature in the remote interface of our bean and a method skeleton in the implementation of our bean. To complete the business method we just had to write the code in our bean's implementation. Being able to look at the logical bean in the IDE alleviates the need to remember what bean code has to be created when you add business, finder, or create methods. The IDE does it for you and you can concentrate on the implementation of the method. |
|
Having created a (hopefully) functional entity bean, how do you prove it actually works? It would be folly to start work on an application that uses an entity bean without verifying that it works. To do this, a test application is needed and FFJEE just happens to have a “New EJB Test Application” wizard that tests any entity bean created with FFJEE. The wizard walked us through a couple of dialog boxes and then created and deployed to the default J2EE application server an application that exercised all of the functionality of our Subscriber entity bean. To create the application that the wizard created automatically, we would have had to create a web module, an EJB module, and, most important, JSP pages to access the bean’s methods display the results. When it comes to testing an EJB, this feature alone is worth the price of admission. |
|
||||
|
OpenBench Labs now set out to build a web service to retrieve subscriber information by MEMBER_ID and COMPANY_NAME. Again, FFJEE helps with a wizard that can create web services from an EJB. Currently, a web service must use Sun's proprietary Enterprise Service Presentation (ESP) toolkit. An FFJEE module is under development to build Simple Object Access Protocol (SOAP) web services. The first step was to create an empty web service. Once you have an empty web service, you can add XML operations to it by pointing to an EJB and letting FFJEE do the work of building the operations. We told FFJEE to use our Subscriber EJB and it created XML operations for each method in our EJB. Since we only wanted to expose the finder methods through the web service, we deleted the XML operations it created for setCompanyName and Create. |
|
After we created the web service and added operations from our EJB, we had to package the EJB in an EJB module. Next we created a J2EE application for our web service and added both the EJB module and the web service to that application. We then deployed the web service on our application server. There we could execute the XML operations through the simple web page that FFJEE created for the web service. When executing XML operations via the web page, the automatically-generated JSP page translates the raw XML data returned into an HTML table. Alternatively, you can access the XML operations directly, in which case you get back XML data in raw format. Java applets access a web service in this manner. One thing we found in this exercise is that FFJEE is a real memory hog. We started running under SuSE 7.3 on a system with a mere 128MB of memory. That system was soon thrashing itself to death and only the quick insertion of an extra 512MB of RAM rescued it from a horrible fate. Nonetheless, this is a cheap price to pay to be able to create an EJB and web services without having to know the internals of J2EE deployment. |
|
||||
|
The
price of the Enterprise Edition, $1,995, is pretty steep, but well worth it
for the time you will save figuring out how to use the deployment tool that
comes with the iPlanet Application Server. The good news is that you can
download a free trial version of Forte for Java, Enterprise Edition and try
it out before you make a purchase commitment. We suggest you send away for
the Sun Open Network Environment (ONE) Starter Kit, which includes FFJEE,
FFJCE, the iPlanet Application Server, the iPlanet Web Server, and lots of
documentation about how to build web applications and web services based on
J2EE. It's only $10 for a four-CD set and it's a lifesaver if you don't have
a high-speed Internet connection. |