Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Server-side Java: Create forward-compatible beans in EJB, Part 2

More key strategies for developing portable EJB 1.0 beans for EJB 1.1 servers

  • Print
  • Feedback

Page 6 of 6

List 10. Illustrative example of a EJB 1.0 to EJB 1.1 deployment descriptor converter
import java.io.*;
import javax.ejb.deployment.*;
public class DDConverter {
  public static void main(String [] args) {
    validateArgs(args);
    try {
      // read the serialized EJB 1.0 DeploymentDescriptor object.
      DeploymentDescriptor dd = readDD(args[0]);
      String XMLString = new String();
      // document heading
      XMLString += "<?xml version="1.0"?>
\n"+
        "<!DOCTYPE ejb-jar PUBLIC \"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN\""
        + "\"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd\">\n";
      // root element
      XMLString += "<ejb-jar>\n";
      XMLString += "   <enterprise-beans>\n";
      if (dd instanceof SessionDescriptor) {
        XMLString += "  <session>\n";
        XMLString += "    <ejb-name>"+dd.getBeanHomeName()+"</ejb-name>\n";
        XMLString += "    <home>"+dd.getHomeInterfaceClassName()+"</home>\n";
        XMLString += "    <remote>"+dd.getRemoteInterfaceClassName()+"</remote>\n";
        XMLString += "    <ejb-class>"+dd.getEnterpriseBeanClassName()+"</ejb-class>\n";
        if (((SessionDescriptor)dd).getStateManagementType()==SessionDescriptor.STATEFUL_SESSION) {
          XMLString +="      <session-type>Stateful</session-type>\n";
        } else {
          XMLString +="      <session-type>Stateless</session-type>\n";
        }
        ControlDescriptor [] controls = dd.getControlDescriptors();
        String mgmtType = "Container";
        for (int i = 0; i < controls.length; i++) {
          if (controls[i].getTransactionAttribute()==ControlDescriptor.TX_BEAN_MANAGED) {
            mgmtType = "Bean";
            break;
          }
        }
        XMLString += "    <transaction-type>"+mgmtType+"</transaction-type>\n";
        XMLString += "  </session>\n";
      } else {
        ;// DO ENTITY BEAN
      }
      // DO ENVIRONMENT PROPERTIES, TX ATTRIBUTES, SECURITY ROLES, etc., HERE
      XMLString += "  </enterprise-beans>\n";
      XMLString += "</ejb-jar>\n";
      // write the EJB 1.1 XML version of the DeploymentDescriptor
      writeXML(args[1], XMLString);
    } catch (Exception e) { e.printStackTrace(); }
  }
  // read the EJB 1.0 DeploymentDescriptor file
  public static DeploymentDescriptor readDD(String fileName) throws Exception {
    File inFile = new File(fileName);
    FileInputStream fis = new FileInputStream(inFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    DeploymentDescriptor dd = (DeploymentDescriptor)ois.readObject( );
    ois.close();
    fis.close();
    return dd;
  }
  // write the EJB 1.0 XML file
  public static void writeXML(String fileName, String XMLString) throws Exception {
    FileWriter fw = new FileWriter(fileName);
    fw.write(XMLString);
    fw.flush();
    fw.close();
  }
  // validate that the source file and target file arguments are included in the command line
  public static void validateArgs(String [] args) {
    if (args.length < 2) {
      System.out.println("\nERROR: ConverterDD did not run");
      System.out.println("Need to specify two arguments:");
      System.out.println("  1. The .ser file containing the EJB 1.1 deployment descriptor");
      System.out.println("  2. The name of the .xml file you want to create");
      System.out.println("Example:\n java DDConverter /dev/CustomerDD.ser /dev/ejb-jar.xml");
      System.exit(0);
    }
  }
}

The code above illustrates the fundamental differences between EJB 1.0's deployment descriptors and those of EJB 1.1. The EJB 1.1 XML deployment descriptors actually cover a lot more ground than the EJB 1.0 java.ejb.deployment package, so a converted deployment descriptor will require additional information not previously available in EJB 1.0.

Conclusion

Several changes in EJB 1.1 prevent beans developed in EJB 1.0 servers from porting to EJB 1.1 servers. Some of these changes affect the bean code while others are found in the new options offered by the XML deployment descriptor. In this article, we have focused on issues involving the portability of bean code, which is the topic that will cause the most grief for developers. By using the PortableContext or some similar construct, you can reduce the risk of forward-compatibility problems. The PortableContext can also be used to make beans portable across EJB vendors by creating custom implementations for specific brands of servers.

It is not necessary that you use the PortableContext developed in this article. This PortableContext is a good solution, but not the only solution. You may develop your own strategies based on the concepts developed in here, or you can simply use the PortableContext as defined. Proprietary idiosyncrasies may force you to modify the PortableContext with special vendor-specific implementation classes, or you may discover improvements to the general-purpose classes defined in this article. Either way, please relay your discoveries to me. I will keep a bulletin of proprietary extensions and improvements at my Website, EJBNow.com, for the entire EJB community. (See the Resources section below for the URL.)

The authors of this month's server-side Java computing articles will be holding a free online seminar on January 13 at 11:00 a.m. PST. Register to join at http://seminars.jguru.com.

About the author

Richard Monson-Haefel is an EJB expert for jGuru.com, and is the author of Enterprise JavaBeans (O'Reilly & Associates, 1999). Richard would like to thank Chris Raber of Gemstone, who was a technical reviewer of this article and a contributor in the development of the PortableContext. JavaWorld and jGuru have formed a partnership to help the community better understand server-side Java technology. Together, JavaWorld and jGuru are jointly producing articles, free educational Web events, and working together on the JavaWorld bookstore and Web-based training.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback

Resources

Server-side Java: Read the whole series -archived on JavaWorld