The J2EE standard defines the specifications in which J2EE applications can be packaged in a standard, generic, and portable way. It defines these standards for Web applications, enterprise JavaBeans, resource adaptors, enterprise applications, and client applications. However, the J2EE specification defines only the application packaging standards and does not specify how a J2EE application should be deployed to a J2EE server. Any J2EE-compliant server can implement its own deployment behavior to give maximum flexibility to the J2EE developer. This section discusses the packaging of the most common J2EE applications: Web applications, EJBs, and enterprise applications. We also discuss the directory structure for assembling these applications as well as the deployment descriptors for each type of application. Applications that must be deployed to WebLogic Server should be packaged according to the J2EE standard. Packaging a J2EE application requires a specific directory structure containing Java classes, deployment descriptors, and other third-party JARs. Web ApplicationsA Web application consists of application files such as servlet classes, JSPs, static HTML pages, image files, and tag libraries. It also contains a standard J2EE deployment descriptor file named web.xml and a WebLogic-specific deployment descriptor named weblogic.xml . These deployment descriptor files together define various parameters for the Web container to configure the Web Application at runtime. The Web Application Directory StructureThe directory structure for assembling a Web application is defined by the Servlet specification, as follows :
A Web application can be deployed as a collection of files that use this directory structure (exploded directory format). The exploded directory deployment method is typically used during the development stages of a Web application. Alternatively, the Web application can be assembled in the preceding directory structure in order to stage the WAR file for the jar command. A file created with the Java jar tool bundles the files in a directory into a single Java archive file, maintaining the directory structure. The WAR file can then be deployed alone or packaged in an Enterprise Archive (EAR file) with other application components, including other Web applications and EJB components . This archive method of deployment is recommended for production environments. Web Application Deployment DescriptorsDeployment descriptors are XML documents that define the contents of the Web application and contain other parameters the Web container will use while configuring and deploying the application to WebLogic Server. The web.xml descriptor is a required descriptor as per the Servlet specification, and contains all the configuration parameters needed to deploy a Web application. For example, the web.xml descriptor contains parameters for
The weblogic.xml descriptor is an optional descriptor file. This file declares parameters that are specific to the WebLogic Server Web container. Some of the parameters this file contains are for
You can create deployment descriptors manually using an XML editor, or you can use WebLogic-specific Java-based utilities to automatically generate them for you, as discussed in the next section. Generating Web Application Deployment Descriptors AutomaticallyWebLogic Server includes a Java-based utility called WebInit that can be used to generate both the J2EE standard web.xml descriptor and the WebLogic-specific weblogic.xml descriptor. The utility should be pointed at the directory where you've assembled the Web application in the prescribed directory structure we previously discussed. To generate the deployment descriptors automatically, follow these steps:
The utility examines the objects present in the directory structure and then adds the appropriate elements to the deployment descriptors based on the servlet classes, lifecycle listeners, filters, JSPs, tag libraries, and other parameters it finds. The deployment descriptors are created on a best guess effort and therefore may be wrong. So, after generating them using the WebInit utility, they should be verified for consistency. If there are parameters that are incorrect or need to be changed, you can edit the deployment descriptor using the Administration Console, WebLogic Builder tool, or any other XML editor. To showcase this utility, let's say you have a simple HelloWorld Web application that's comprised of one servlet, which is contained in the following directory: D:\bea\user_projects\mydomain\applications\HelloApp The only missing files are the deployment descriptors inside the WEB-INF directory. The output of running the WebInit utility on the HelloApp directory is as follows: java weblogic.marathon.ddinit.WebInit HelloApp [ModuleInit]: Searching for class files [ModuleInit]: 1 classes found [ModuleInit]: found servlet class: 'objectmind.servlets.MyServlet' [ModuleInit]: Discovered module type for D:\bea\user_projects\mydomain\applications\HelloApp [ModuleInit]: Found Web components. Initializing descriptors [ModuleInit]: Writing descriptors filters=0 servlets=1 tags=0 At the end of the run, web.xml and weblogic.xml are created in the WEB-INF directory of the HelloApp directory. Packaging Web ApplicationsTo stage and package a Web application, follow these steps:
The Web application archive is now ready for deployment to the server. To deploy the Web application in an exploded format, you can copy the entire directory structure of the Web application to the < domainName> /applications directory. WebLogic Server will automatically deploy the Web application if it is running in development mode. Alternatively, you can also use the Administration Console to stage the application from the current directory and then deploy it. Application deployment is discussed in detail later in this chapter in the "Deploying Applications to WebLogic Server" section. Enterprise JavaBean ApplicationsEnterprise JavaBean Applications consist of three Java classes: the remote interface, the home interface, and the bean class. Besides these Java classes, EJB applications also contain deployment descriptors depending on the type of the bean. The EJB container uses these deployment descriptors to configure and deploy the bean at runtime. The Enterprise JavaBean Application Directory StructureEJBs are deployed as a JAR archive file or they can be bundled inside of an enterprise application (EAR file). As with Web applications, in order to be packaged correctly, EJB applications must follow a specific directory structure, which in this case is defined by the EJB Specification, as follows:
EJB Application Deployment DescriptorsWebLogic Server EJB applications contain the following deployment descriptor files:
Generating EJB Application Deployment Descriptors AutomaticallyWebLogic Server includes a Java-based utility called EJBInit, which creates the J2EE standard ejb-jar.xml deployment descriptor as well as the WebLogic-specific weblogic-ejb-jar.xml and weblogic-cmp-rdbms-jar.xml descriptors. The utility should be pointed at the EJB application's staging directory, which contains the compiled EJB classes placed in a directory structure that corresponds to their respective package names . To generate the deployment descriptors for the EJB automatically, follow these steps:
The EJBInit utility examines the classes in the staging directory structure and then adds the appropriate elements to the deployment descriptors based on the EJB classes it finds. The deployment descriptors are created on a best guess effort; therefore, they might be incorrect. For this reason, after the descriptors are generated using the EJBInit utility, they should be verified for consistency. If parameters are incorrect or need to be changed, you can edit the deployment descriptor using the Administration Console, the WebLogic Builder tool, or any other XML editor. For example, the results of running the EJBInit utility on a sample AccountEJB application, which contains a CMP entity bean are as follows: [ModuleInit]: SEARching for class files [ModuleInit]: 5 classes found [ModuleInit]: Discovered module type for D:\releases\70platformsp1\weblogic700\ samples\server\src\myexamples\deployment\containerManaged\build [ModuleInit]: Found EJB components. Initializing descriptors [ModuleInit]: Found EJBHome: examples.ejb20.basic.containerManaged.AccountHome [ModuleInit]: Bean class: examples.ejb20.basic.containerManaged.AccountBean [ModuleInit]: Setting prim-key-class to 'java.lang.String' [ModuleInit]: Added CMP field 'accountId' [ModuleInit]: Setting primkey-field to 'accountId' [ModuleInit]: Added CMP field 'balance' [ModuleInit]: Added CMP field 'accountType' [ModuleInit]: Creating weblogic rdbms descriptor [ModuleInit]: Adding Entity bean 'AccountBean' [ModuleInit]: [ModuleInit]: Creating relations [ModuleInit]: Writing descriptors [ModuleInit]: Building module with newly created descriptors [ModuleInit]: Finished building module [EJBJARCMBean] Writing descriptors At the end of the run, the ejb-jar.xml , weblogic-ejb-jar.xml , and weblogic-cmp-rdbms-jar.xml deployment descriptor files are created in the META-INF dir. Packaging EJB ApplicationsTo stage and package an EJB application, follow these steps:
You can run ejbc on this archive to generate the container classes depending on the parameters specified in the deployment descriptors. If you deploy the archive without running ejbc on it, WebLogic Server runs ejbc on the archive automatically to generate the container-specific classes. Enterprise ApplicationsAn enterprise application is an archive file that typically packages a Web application archive (WAR file) with one or more additional resources such as EJB applications ( .jar ), Java applications ( .jar ), and resource adapters ( .rar ) into a single deployable archive file ( .ear ). Besides these module archives, the EAR file also contains a deployment descriptor named application.xml , which defines the modules packaged in the enterprise archive file and the order in which they should be deployed to WebLogic Server. The format of application.xml is defined by the J2EE Specification. The Enterprise Application Directory StructureTo be packaged correctly, enterprise applications must follow a specific directory structure, which is defined by the J2EE Specification, as follows:
Enterprise Application Deployment DescriptorsAs briefly mentioned in the previous section, WebLogic Server Enterprise applications contain the application.xml and the weblogic-application.xml deployment descriptors. The application.xml file is a J2EE standard deployment descriptor for configuring an enterprise application. It contains information about the archive modules that comprise an enterprise application. You can also define security roles for your application within this descriptor file. The structure of an application.xml file that corresponds to the following enterprise application directory is listed in Listing 27.1: MyEAR/ (Document root of the Enterprise Application) webApp1.war (Web Application archive) webApp2.war (Another Web Application module) ejb1.jar (EJB archive) ejb2.jar (Another EJB module) myUtilityClasses.jar (Utility classes archive) META-INF/ (Contains the deployment descriptor files) application.xml (Standard Enterprise Application descriptor) weblogic-application.xml (WebLogic specific application descriptor) Listing 27.1 A Sample Structure of an application.xml File<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'> <application> <display-name>myEAR</display-name> <description>myEAR</description> <module> <ejb>ejb1.jar</ejb> </module> <module> <ejb>ejb2.jar</ejb> </module> <module> <web> <web-uri>webApp1.war</web-uri> <context-root>webApp1</context-root> </web> </module> <module> <web> <web-uri>webApp2.war</web-uri> <context-root>webApp2</context-root> </web> </module> <module> <java>myUtilityClasses.jar</java> </module> <security-role> <description> platinum customers</description> <role>platinumCustomerRole</role> </security-role> </application> Each component in application.xml is defined within a < module ></ module > element. Depending on the type of the module, it can be defined within the elements <ejb> , <web> , or <java> . When the application archive is deployed to WebLogic Server, the modules are deployed in the order in which they're defined in the application.xml file. The preceding descriptor also defines a security-role element that defines the name of the role that's used for authorization within the application. The role name can be mapped to WebLogic Server users and groups in weblogic-application.xml . The weblogic-application.xml descriptor file is WebLogic-specific and is required only if you want to configure application scoping or to map the security roles defined in the application.xml to WebLogic users and groups. Application scoping is a WebLogic-specific feature provided to configure resources for a particular application at the application level instead of configuring resources for the entire WebLogic Server. These resources include XML parsers, XML entity mappings, JDBC connection pools, security realms, and so forth. This helps in partitioning the resources between different applications deployed on the same WebLogic Server. For example, you might need to use a particular XML parser for one application and a different one for the other application. Application scoping enables you to configure this requirement. Another advantage of the scoping feature is that you can deploy an enterprise application archive directly onto another instance of WebLogic Server without having to configure the resources on that server. Packaging Enterprise ApplicationsTo stage and package an enterprise application, follow these steps:
The enterprise archive file is now ready for deployment to WebLogic Server. You can deploy it using the WebLogic Console, or by using the weblogic.Deployer utility, or by just auto-deploying it through the < domainName >/applications directory if WebLogic Server is configured to be in development mode. |