|
Many J2EE 1.2-based applications and components are emerging in the marketplace as the J2EE platform matures. Application portability is one of the most important benefits offered by the J2EE platform. Through the J2EE Java Pet Store sample application, the J2EE Blueprints team has developed a set of best practices for ensuring application portability across J2EE-compatible application servers. This article is the first in a series of J2EE application and component portability recommendations for the J2EE development community. This series will focus J2EE application portability in light of maturing J2EE technologies, especially J2EE 1.3 and EJB 2.0. This first article presents several portability guidelines for the J2EE platform, version 1.2, but includes references to the upcoming version 1.3 where appropriate. This article assumes that the reader is familiar with basic J2EE 1.2 platform terminology. What Is Portability?Portability is an important goal for application developers who want to maintain freedom of choice in technology. It's important to understand how J2EE application portability fits into the Java portability picture. Java technology provides several levels of portability, each supported by a particular specification:
This series focuses on J2EE application portability tips and guidelines. The J2EE Platform Specification defines a contract between J2EE product providers, who create J2EE servers, APIs, and so on, and J2EE component and application developers, who use those J2EE products to create solutions. Both sides of the contract must understand their responsibilities in order to realize the portability benefits of the J2EE platform. This article provides some detailed tips for ensuring that your J2EE components and applications will be portable between different J2EE vendors' products. J2EE Product CompatibilityJ2EE product providers produce Web and application servers, servlet and EJB containers, databases, platform APIs, and development tools. The J2EE Platform Specification defines the behavior of each type of J2EE product, so J2EE component and application developers can depend on consistent results. Several tools are available to help J2EE product providers create products that meet the platform specification requirements. The J2EE Compatibility Test Suite (CTS) helps J2EE product providers check that their products meet the requirements of the J2EE platform specification. The J2EE Compatibility Test Suite is a standard set of over 5,000 test cases derived from the J2EE specification requirements. Products that pass the CTS are considered J2EE-compatible. The tests check specific requirements for each of the J2EE technologies. For example, the EJB container tests check that the EJB instance life cycle behaves as defined in the EJB specification. The J2EE Reference Implementation (RI) provides J2EE server developers with an example implementation of a J2EE application server. The J2EE RI demonstrates that the specifications can indeed be implemented. The RI also gives application server vendors a working server they can use to check the correctness of their implementation. The J2EE RI is required to pass 100% of the CTS, since its purpose is to demonstrate the complete J2EE specification. Application Developer ToolsGood tools play an important role in helping the J2EE application developer write portable applications. Providing powerful, easy-to-use tools is one of the ways that product providers can serve their customers, and thereby gain a competitive advantage in the marketplace. Several kinds of application development tools provide support for application portability. Code generation tools generate code for various parts of an application, based on higher-level specifications provided by the developer. Verification tools ensure the correctness of deployment descriptors. Sample code and references help developers understand how to use application components. Let's look at an example of each of these types of tools.
Code generation tools automatically generate source code based on what the developer
has specified or has already coded. Such tools improve portability by creating code
that adheres to J2EE specifications. For example, a tool called EJBGen (version 1.20 - an EJB 2.0 code
generator) automatically generates the all of the needed files (Home, Remote, Local
interfaces, and the deployment descriptor) based on an existing Bean class. In
addition to eliminating mindless coding, EJBGen improves portability and code
quality by ensuring that the methods of the
Verification tools ensure that the contents of a deployment descriptor (such as A good code example is worth a thousand words. Samples and references provide good examples to demonstrate how individual J2EE components, applications, and servers all come together. Many code snippets and tutorials for particular technologies are only a Web search away, but developers need to understand how to use these technologies together: they need to be able to see "the big picture". The Sun J2EE Blueprints program provides developers with a free sample application, the Java Pet Store. The Java Pet Store is an end-to-end, B2C, e-commerce application that gives the J2EE application developer an example from which to learn. The J2EE Blueprints program also offers design discussion explaining best practices for J2EE application design, and provides a set of design patterns for building robust, scalable, portable J2EE applications. The Java Pet Store sample application can be downloaded for free from Java 2 Platform, Enterprise Edition Blueprints. J2EE Application Portability TipsThe rest of this article presents several specific technology usage tips for maximizing J2EE application portability. Each tip may also have other benefits, but the focus is on portability between J2EE product providers' products. This section assumes a working knowledge of J2EE 1.2 technology. Know When (and When Not) to Use Serializable FieldsAs an Enterprise JavaBean provider, you are responsible for ensuring that all of a bean's instance fields are serializable, so that the state of the bean can be preserved when the bean is passivated.
While fields of primitive types like
For example, consider an entity bean called
Since the You must mark all fields of nonserializable types as transient. Fields marked as transient are ignored by the JVM's serialization machinery.
For example, a database connection, represented by
For more information on serialization and passivation, refer to Section 6.4.1 of the EJB 1.1 Specification (available for download from the resource list). Close Database Connections Cleanly
When retrieving items from a database, open a connection through a
What happens if you try to do this the other way around? Some JDBC 2.0 driver implementations
will throw an exception if you try to close a Be careful When Writing Deployment DescriptorsIf your J2EE-compatible application server provides tools to construct deployment descriptors for you, you should use those tools. Hand-written deployment descriptors are prone to errors such as misordering of elements, duplicate element entries, and simple typos. If you still want (or need) to write your own descriptors, many application servers provide tools that validate a deployment descriptor against a DTD, ensuring that the descriptor's contents are both well formed and valid. The verifier tool shipped with the J2EE Reference Implementation will catch only some errors in a descriptor, but not all of them. You can supplement the verifier with a small program that uses a validating parser to check a descriptor's validity against its DTD:
Use Valid Encodings for Deployment DescriptorsEnsure that the static deployment descriptors for your Web and EJB components include a valid encoding declaration. For example, if you're using encoding ISO-8859-1, the deployment descriptor should begin with the following line:
In an encoding declaration, the value Refer to Section 4.3.3, "Character Encoding in Entities," of the W3C XML 1.0 Recommendation for more details on XML character encodings. Using Methods Correctly Is Vital
When an entity bean is activated through a callback to
Similarly, when an entity bean is passivated through a callback to
Even if you tried accessing a resource from For more information on these methods, refer to Sections 9.1.5 and 9.1.6 of the EJB 1.1 Specification. Throwing Exceptions from Entity Beans
The EJBGen tool helps developers write EJBs, and in particular ensures that EJB method
signatures express the appropriate thrown exceptions. If you do not use such a tool,
then you should double check the EJB method signatures, especially the thrown exceptions,
in your component's
Here is a summary of the EJB 1.1 Specification exception requirements for Remote Interface:
Home Interface - Session:
Home Interface - Entity:
The J2EE RI 1.2.1 verifier tool checks for method signature exceptions. PackagingHow to package an application seems to be an area of confusion among developers in general.
Packaging is an important portability consideration. Packaging requirements and options
are described in the Servlet 2.2, EJB 1.1, and J2EE 1.2 specifications. The Servlet and
EJB specifications do a good job of describing what each application component package
( However, some concrete examples describing how you can package your application make the specifications more comprehensible. If you don't understand the specifications regarding application packaging, your application might not be portable. The following scenarios present increasingly complex deployment situations, describe options for packaging them, and provide some considerations and notes on deployment for each scenario. Keep in mind that war file and ear file formats are different. Scenario #1:
Single ear, war references EJB
Packaging Options:
War File Notes:
Scenario #2:
Single ear, EJB references another EJB appA1.ear:
Packaging Options:
EJB Jar File Notes:
Scenario #3:
Single ear, EJB references util classes
Packaging Options:
Utility and Implementation-Specific Notes:
For example:
Scenario #4:
Inter-ear References
Packaging Options:
Scenario #5:
multi-ear, utility classes
util.jar ConclusionThis article has presented some tips that should help you create more portable J2EE applications. The J2EE Blueprints team is interested in hearing about your experiences with the J2EE platform. Please send feedback and comments to j2eeblueprints-interest@sun.com. Special thanks to the J2EE development community for their continued commitment to the J2EE platform and for developing J2EE applications and components. RESOURCES:
About the AuthorElizabeth Blair is a staff engineer with Sun Microsystems where she leads the Java 2 Platform, Enterprise Edition Blueprints implementation team. She contributed to the recent Java Pet Store sample application with an emphasis on the tiers for EIS and EJB architecture. In the past, Liz has worked on the Compatibility Test (CTS) suite for the J2EE platform, the J2SE platform, and the WABI (Windows Applications Binary Interface) projects. | |||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||