Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

This article describes how to develop a Struts1 web application for Geronimo. Besides Struts1 technology, you can also find sample code about usages of JPA and Security annotations.

...

Because of a problem described in GERONIMO-5020, this sample application is not available as a geronimo plugin for the time being.

This article doesn't intend to serve as a tutorial of developing a Struts1 application. For informations about Struts1 development, see Struts1 web site.

...

This article is organized into the following sections.

Table of Contents

Application overview

Assuming you have accumulated lots of archival data CDs from your computer, each of them includes rich of information you ever collected. The collection of those archived CDs is your valuable knowledge asset, and certainly you want to manage them carefully and orderly for easy references in the future. The DataCDInfo sample application is about to assist you with this task.

...

  • Two JPA entity beans: DataCDBean and OwnerBean, which represents Data CD records and Owner records respectively. The relationship bewteen OwnerBean and DataCDBean is 1...*, one owner could have multiple Data CDs.
  • The DataCDInfoJTAImpl is a stateless session bean which implements the business logic of DataCDInfo application, including login, registration/unregistration of the owner, and add/update/removal of data CD records. DataCDInfoLocal and DataCDInfoRemote is the local and remote business interface respectively.
  • The DataCDInfoAdmin is a stateful session, in which there is an EXTENDED persistence context. By default, a container-managed persistence context is of type TRANSACTION. The EXTENDED persistence context can only be initiated within the scope of a stateful session bean.
  • The DataCDInfoAdmin defines two roles "superadmin" and "admin" with security annotation @RolesAllowed. In the code, the role "superadmin" can access all three methods, while the role "admin" can only access "listOwners" method. Another way to define the access is via EJB deployment descriptor ejb-jar.mxl. The configurations in ejb-jar.xml file will override the ones in code already.
    As shown below, the role "admin" also has access to method "listAllDataCDs" besides the method "listOwners" defined in the code because of configuration in its ejb-jar.xml. Code Blockxmltitleejb-jar.xmlborderStylesolidxml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"> <display-name>DataCDInfo Enterprise Bean Definitions</display-name> <enterprise-beans> <session> <ejb-name>ejb/DataCDInfoJTAImpl</ejb-name> <business-local>org.apache.geronimo.samples.datacdinfo.core.DataCDInfoLocal</business-local> <business<business-remote>org.apache.geronimo.samples.datacdinfo.core.DataCDInfoRemote</business-remote> <ejb-class>org.apache.geronimo.samples.datacdinfo.core.DataCDInfoJTAImpl</ejb-class> <!-- Stateful|Stateless --> <session-type>Stateless</session-type> <!-- Who manages transanction? Bean|Container --> <transaction-type>Container</transaction-type> </session> <session> <ejb-name>ejb/DataCDInfoAdmin</ejb-name> <business-local>org.apache.geronimo.samples.datacdinfo.core.DataCDInfoAdminLocal</business-local> <ejb-class>org.apache.geronimo.samples.datacdinfo.core.DataCDInfoAdmin</ejb-class> <!-- <!-- Stateful|Stateless --> <session-type>Stateful</session-type> <!-- Who manages transanction? Bean|Container --> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <method-permission> <role<role-name>superadmin</role-name> <method> <ejb-name>ejb/DataCDInfoAdmin</ejb-name> <method-name>*</method-name> </method> </method-permission> <!-- In code, role "admin" only has right to access listOwners method but via this xml definition, the role could also access listAllDataCDs method --> <method-permission> <role-name>admin</role-name> <method> <ejb-name>ejb/<method> <ejb-name>ejb/DataCDInfoAdmin</ejb-name> <method-name>listAllDataCDs</method-name> </method> </method-permission> </assembly-descriptor> </ejb-jar>
  • A persistence unit is defined via META-INF/persistence.xml as shown below. Code Blockxmltitlepersistence.xmlborderStylesolidxml <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="DataCDInfoUnit" transaction-type="JTA"> <description>DataCDInfo Persistence Unit Definition</description> <jta-data-source>jdbc/DataCDInfoDS</jta-data-source> <non-jta-data-source>jdbc/NoTxDataCDInfoDS</non-jta-data-source> <class>org.apache.geronimo.samples.datacdinfo.beans.OwnerBean</class> <class>org.apache.geronimo.samples.datacdinfo.beans.DataCDBean</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="openjpa.Sequence" value="table(Table=OPENJPASEQ, Increment=1)"/> <!--<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>--> <!--<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>--> <property name=<property name="openjpa.Log" value="File=/tmp/org.apache.openjpa.log, DefaultLevel=WARN, Tool=INFO, Runtime=TRACE, SQL=TRACE"/> </properties> </persistence-unit> </persistence>

...

If the persistence context requires some non-transactional operations, such as table sequence generation, you have to define a non-jta-data-source as well. Otherwise, you will encounter an exception like org.apache.openjpa.persistence.RollbackException: The transaction has been rolled back.

...

The maven project layout of the EJB module as follows:

...

...

The Web Module

All Struts1 objects are in the Web module. A typical Struts1 web application uses a configuration file to initialize its resources. Those resources include ActionForms to collect input from users, ActionMappings to direct input to server-side Actions, and ActionForwards to select output pages.(Quoted from Struts1 documentation).

The DataCDInfo application web module consists of:

  • Struts1 ActionForm: DataCDForm and OwnerForm.
    • Those two ActionForm classes extend Struts1 ValidatorForm in order to utilize the convenient validation feature provided by Struts1.
    • You might have noticed that these two classes are very similar to the JPA entity beans. The design is one of requirements in Struts1, so that the view model is separated from the backend business model. To convey data between Struts1 form view and business logic beans, you can use org.apache.commons.beanutils.PropertyUtils method.
  • Struts1 Action: DataCDActions and OwnerActions
    • Those two Action classes extend Struts1 MappingDispatchAction, so that business related actions could be in the same Action class. For more details, check the API doc of MappingDispatchAction.
    • Those two Action classes wrap form data and call the corresponding business operations to persist the data into database.
  • Struts1 resource files: DataCDInfoResource.properties and several DataCDInfoResources_LANG.properties
    • Struts1 uses the standard globalization way that Java language provides to present messages for different locale.

The Web Module

All Struts1 objects are in the Web module. A typical Struts1 web application uses a configuration file to initialize its resources. Those resources include ActionForms to collect input from users, ActionMappings to direct input to server-side Actions, and ActionForwards to select output pages.(Quoted from Struts1 documentation).

The DataCDInfo application web module consists of:

  • Struts1 ActionForm: DataCDForm and OwnerForm.
    • Those two ActionForm classes extend Struts1 ValidatorForm in order to utilize the convenient validation feature provided by Struts1.
    • You might have noticed that these two classes are very similar to the JPA entity beans. The design is one of requirements in Struts1, so that the view model is separated from the backend business model. To convey data between Struts1 form view and business logic beans, you can use org.apache.commons.beanutils.PropertyUtils method.
  • Struts1 Action: DataCDActions and OwnerActions
    • Those two Action classes extend Struts1 MappingDispatchAction, so that business related actions could be in the same Action class. For more details, check the API doc of MappingDispatchAction.
    • Those two Action classes wrap form data and call the corresponding business operations to persist the data into database.
  • Struts1 resource files: DataCDInfoResource.properties and several DataCDInfoResources_LANG.properties
    • Struts1 uses the standard globalization way that Java language provides to present messages for different locale.
    • The sample includes message resources files for both en_US and zh_CN locales. You can extends the locale support by adding additional locale resource files to the resources directory, and then run a new build for later deployment.

...

  • DataCDInfoAdminServlet – A servlet used to call security-controlled business methods defined in DataCDInfoAdmin stateful session bean.
  • admin/. – The presentation files of DataCDInfo admin operations
  • auth/. – The files used to FORM authentication. By default, the DataCDInfo application uses BASIC authentication. If you want to see what a FORM authentication looks like, you can modify web.xml as follows: Code Blockxmltitle xmlpart of web.xmlborderStylesolidxml ... <login-config> <auth-method>FORM</auth-method> <realm-name>geronimo-admin</realm-name> <form-login-config> <form-login-page>/auth/logon.html</form-login-page> <form-error-page>/auth/logonError.html</form-error-page> </form-login-config> </login-config> <!-- <login-config> <auth-method>BASIC</auth-method> <realm-name>geronimo-admin</realm-name> </login-config> --> ...

The maven project layout of the Web module as follows:

...

borderColor#FFFFFF
bgColor#FFFFFF
borderStylesolid

...

  • . <login-config> <auth-method>FORM</auth-method> <realm-name>geronimo-admin</realm-name> <form-login-config> <form-login-page>/auth/logon.html</form-login-page> <form-error-page>/auth/logonError.html</form-error-page> </form-login-config> </login-config> <!-- <login-config> <auth-method>BASIC</auth-method> <realm-name>geronimo-admin</realm-name> </login-config> --> ...

The maven project layout of the Web module as follows:

...

The EAR module

The EAR module contains database creation scripts and the application deployment plan. The application deployment plan will override the duplicate configurations defined in the EJB module and Web module.

In the application deployment plan, there are definitions about the web context root and the security realm used to authenticate the admin operations.

...

The DataCDInfo application uses the default geronimo security realm geronimo-admin, which is a .properties file-based realm. To enable "superadmin" role used by this application, these .properties files in /var/security must be modified before starting Geronimo server:

...

...

...

The plain text password will be encrypted when the geronimo server restarts.

Two datasources are defined in the deployment plan. jdbc/DataCDInfoDS is for JTA scenarioa, and jdbc/NoTxDataCDInfoDS for non-JTA.

...

To map the application security roles to Geronimo security roles, the deployment plan must include configurations as below:

...

...

Besides the default deployment plan, there is another plan for MySQL database. You can enable the database by using deploy command:

...

deploy deploy DataCDInfo-JTA-ear-2.2.ear geronimo-application-mysql.xml

...

You must install proper mysql jdbc driver into Geronimo repository first before deploying the DataCDInfo application with MySQL deployment plan geronimo-application-mysql.xml.

...

The maven project layout of the EAR module as follows:

...

...

Run Application

Using geronimo admin console to deploy the application.

...