What developers can learn from this article
This article describes how to use the new OSGi development tools included with IBM® Rational® Application Developer Version 8.0 to convert existing Java Enterprise Edition (JEE) applications to OSGi applications. (OSGi meant "Open Service Gateway initiative" before the scope expanded beyond service gateways.) These tools, combined with the WebSphere Application Server Version 7 Feature Pack for OSGi Applications, provide the support to develop and deploy OSGi enterprise applications with capabilities similar to that of Java EE applications.
An OSGi application can consist of the same modules deployed in a Java EE project, but includes OSGi metadata that enables the modules to be loaded as OSGi bundles, thereby benefiting from the bundle management mechanisms provided by the OSGi framework. There are several reasons why Java EE developers might choose to move toward OSGi for enterprise application development:
- Applications can be deployed as bundles that contain only the application-specific content, along with the metadata that references any dependent, shared libraries. As a result, the application space is reduced, because it leverages the single copies of common libraries managed by the bundle repository. This capability removes the tendency of Java EE applications to have the same JAR (Java Archive) files embedded within many enterprise applications.
- At development time, the OSGi development tools included with Rational Application Developer v8.0 enforce OSGi visibility rules to ensure that package dependencies are explicitly declared, which minimizes potential runtime issues when deployed.
- Multiple versions of classes can be loaded simultaneously in the same application using standard OSGi mechanisms.
- Applications can use their own versions of common utility classes, so you do not need to configure application Java EE classloader policies (for example, PARENT_LAST mode).
- Deployed applications can be dynamically updated at the bundle level without redeploying the entire enterprise archive.
- Single converted OSGi bundle containing JPA
- Multiple converted OSGi bundles separating web and data persistence layers
- Java EE web project (.war) with converted JPA OSGi bundle
- OSGi project conversion tools to convert existing modules to OSGi bundles
- OSGi manifest editors
- OSGi application and bundle-creation wizards
- OSGi deployment to IBM WebSphere Application Server
Before proceeding with the steps that follow, you need the following configuration installed and launched on your workstation:
- IBM Rational Application Developer v8.0 with the "OSGi Development Tools" feature already installed
- IBM WebSphere Application Server v7.0 with the OSGi Applications feature pack already installed
Begin by importing the "JPA JSF Employee List" sample application:
- Select Help menu > Help Contents.
- In the Help window's Contents section, expand Samples > Faces and select JPA JSF Employee List application.
- Click Import the Sample and an Import Projects window will launch.
- In the Import Projects window, click Finish.
- (Optional) Switch to the web perspective when you are prompted.
EmployeeListSample.zip
Verify the JPA JSF Employee List sample application functions as expected:
- After it is imported, the EmployeeListWeb application will have an error on
the
persistence.xml
file. This error will appear in the Errors view. Double-click the error to openpersistence.xml
file. - Locate the ConnectionURL property in the persistence.xml
file:
<property name="openjpa.ConnectionURL" value="jdbc:derby:<workspace_location>\EmployeeListWeb\WebContent\sample" />
- Change the <workspace_location> text to the location of your
workspace, for instance:
<property name="openjpa.ConnectionURL" value="jdbc:derby:c:\osgiDevWorksTutorial1\EmployeeListWeb\WebContent\sample" />
- Run
index.jsp
inside the EmployeeListWeb application on a WebSphere Application Server v7.0. In the Web Content folder in EmployeeListWeb, right-click the index.jsp file, and select Run > Run on Server. - Select WebSphere Application Server v7.0 and click Finish.
- The sample site should launch in the internal browser at this
location:
http://localhost:<port>/EmployeeListWeb/index.faces
- The site should be functional. For instance, you should be able to click List all employee records and see a list of employees.
- Remove the application from the server before proceeding to the next section.
First, we will demonstrate the example where an existing Web Application containing JPA is converted to an OSGi Web Application Bundle and deployed with an OSGi Application. Now that the sample application is working, we will use the OSGi tools to convert the sample JEE web project to an OSGi application containing a single OSGi bundle with web and JPA support.
Convert the sample application to an OSGi bundle
- Right click EmployeeListWeb and from the context menu select Configure > Convert to OSGi Bundle Project.
Figure 1. Project conversion to OSGi warning
- Select OK to confirm the conversion from a JEE web project to an OSGi bundle.
- The following will occur:
- A bundle manifest has been created at
WebContent/META-INF/manifest.mf
. - In the EmployeeListWeb project's properties, an OSGi Bundle facet has been added.
- The EmployeeListWeb project is no longer a module in EmployeeListWebEAR
- A bundle manifest has been created at
Figure 2. Converted EmployeeListWeb project, now a bundle
- The bundle manifest automatically adds JSF project JARs (contained in
WebContent/WEB-INF/lib
) to thebundle-classpath
and adds the necessary java class imports to the list of import packages
Before conversion:
Listing 1. Web project manifest
Manifest-Version: 1.0 Ignore-Scanning-Archives: jsf-ibm.jar, icu4j_3_4_1.jar |
After conversion:
Listing 2. Web bundle manifest after conversion
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: EmployeeListWeb Bundle-SymbolicName: EmployeeListWeb Bundle-Version: 1.0.0.qualifier Bundle-ClassPath: WEB-INF/classes, WEB-INF/lib/icu4j_3_4_1.jar, WEB-INF/lib/jsf-ibm.jar, WEB-INF/lib/sitelib.jar, WEB-INF/lib/web-jpa.jar Ignore-Scanning-Archives: jsf-ibm.jar, icu4j_3_4_1.jar Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Meta-Persistence: Web-ContextPath: /EmployeeListWeb Import-Package: javax.faces.application, javax.faces.validator, org.apache.commons.logging;resolution:=optional, javax.faces.convert, javax.faces.render, javax.servlet.jsp;version="2.0", javax.persistence;resolution:=optional, javax.servlet.http;version="2.5", javax.faces.event, javax.faces.component.html, javax.servlet;version="2.5", javax.faces.component, javax.servlet.jsp.tagext;version="2.0", javax.servlet.jsp.el;version="2.0", javax.faces.el, javax.faces.model, javax.faces.lifecycle, javax.faces.webapp, javax.faces, javax.faces.context, javax.el;version="2.0";resolution:=optional |
Create the OSGi application
In order to deploy the OSGi bundle to WebSphere Application Server, first you need to create an OSGi application to contain the OSGi bundle for deployment.
- Select File > New > Other.
- In the New Project dialog, expand OSGi and select OSGi Application Project. Click Next.
- Enter a project name, for instance
EmployeeListApp
, verify that WebSphere Application Server v7.0 is set as the runtime target, and click Finish.
Figure 3. OSGi Application Manifest editor
- In the Contained Bundles section, click Add.
- Select EmployeeListWeb 1.0.0 and click OK. The warning will resolve.
- Save and close the EmployeeListApp manifest editor.
Data sources need to be manually created on WebSphere Application Server before deploying the OSGi application. In the imported sample JEE application, Rational Application Developer includes the data source when publishing the EAR to the WebSphere Application Server. However, for the OSGi variant, Rational Application Developer Version 8.0 does not currently support publishing data sources when deploying OSGi applications. Additionally, both a JTA and non-JTA datasource must be specified to make this application portable across JPA providers.
- If WebSphere Application Server v7.0 is not started, start it.
- In the Servers view, right click WebSphere Application Server v7.0 and, from the drop-down menu, select Administration > Run Administrative Console to launch the console.
- In the Administrative Console, on the left side, expand Resource > JDBC and then click JDBC Providers.
- Click the existing Derby JDBC Provider resource to verify that t has been created by default on your application server.
- Click the Data sources link.
- Click New to create a new data source.
- Create the new JTA data source with the following properties:
- In Step 1 of the Data Source wizard, set JNDI Name to
jdbc/sample
. - In Step 2 of the Data Source wizard, set the Database Name to the location of the sample database in your workspace, for example: c:\osgiDevWorksTutorial1\EmployeeListWeb\WebContent\sample
- Accept the default settings for all other entries, and finish the new Data Source wizard.
- In Step 1 of the Data Source wizard, set JNDI Name to
- Create the new non-JTA data source with the following properties:
- In Step 1 of the Data Source wizard, set JNDI Name to
jdbc/sample-nonjta
. - In Step 2 of the Data Source wizard, set the Database Name to the location of the sample database in your workspace, for example: c:\osgiDevWorksTutorial1\EmployeeListWeb\WebContent\sample
- Accept the default settings for all other entries, and finish the new Data Source wizard.
- Open the non-jta data source configuration page, click WebSphere Application Server data source properties, select the checkbox Non-transactional data source, and save your changes.
- In Step 1 of the Data Source wizard, set JNDI Name to
- Test the data source connection to verify that it has been created successfully.
Now that the data source is set up in the WebSphere server, the OSGi bundle needs to be set up to access it. To do this, set up EmployeeListWeb bundle to access data source on server by modifying
persistence.xml
in the
EmployeeListWeb project to point to a data source defined on the server. Follow
these steps:- In the Enterprise Explorer view, expand EmployeeListWeb > JPA Content, double click persistence.xml to open it in the Persistence XML Editor.
- Click to the Design tab if you see the
persistence.xml
source. - In the Overview section on the left side of the editor, expand Persistence and click Persistence Unit (EmployeeListWeb).
- Set the Details section:
- JTA Data Source:
jdbc/sample
- Non JTA Data Source:
jdbc/sample-nonjta
- JTA Data Source:
- In the Overview, expand Persistence > Persistence Unit > Properties.
- Remove the three properties by highlighting the property and clicking the Remove button.
- Add a property by selecting Properties, click the Add button, select Property and press OK.
- Set the properties:
- Name:
openjpa.jdbc.Schema
- Value:
SAMP
- Name:
- Click to the Source view.
Listing 3. Persistence.xml after modification
<?xml version="1.0" encoding="UTF-8" ?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="EmployeeListJPA"> <jta-data-source>jdbc/sample</jta-data-source> <non-jta-data-source>jdbc/sample-nonjta</non-jta-data-source> <class>employee.list.Department</class> <class>employee.list.Employee</class> <properties> <property name="openjpa.jdbc.Schema" value="SAMP" /> </properties> </persistence-unit> </persistence> |
- Save and close the Persistence XML editor.
Now everything is set up to deploy and run the EmployeeListWeb sample application after conversion to an OSGi application and contained bundle. Because everything is contained in a single web bundle, the JPA runtime will work as it would in any JEE module created in Rational Application Developer.
- Publish EmployeeListApp to the WebSphere Application Server v7.0. Notice that the contained bundle is displayed beneath the OSGi application.
Figure 4. Add and Remove to server dialog
Note:
EmployeeListWebEAR
will still exist
as a project in your workspace and will contain errors saying that the
EmployeeListWeb.war
file cannot be found. You can either ignore or
delete the EAR file.- Run
index.jsp
inside the EmployeeListWeb application on WebSphere Application Server v7.0. In the Web Content folder in EmployeeListWeb, right-click index.jsp and select Run > Run on Server. - Select WebSphere Application Server v7.0, and click Finish.
- The sample site should launch in the internal browser at this
location:
http://localhost:<port>/EmployeeListWeb/index.faces
Figure 5. Example page, which lists all employees
- Remove the application from the server and delete all projects in your workspace before proceeding to the next section. Alternative: Keep the existing variation in the current workspace and continue with the next variation in a new Rational Application Developer workspace.
In this example, the persistence layer will be contained in its own OSGi JPA bundle and accessed from the OSGi web bundle. In general it is better to have the JPA persistence layer in its own bundle (and hence project). That way, JPA entities are managed in one place and can be used as a data layer by many projects, rather than each project containing its own JPA entities and manager beans. When separating web and persistence concerns it is desirable to have an application container continue to manage the lifecycle of JPA resources such as EntityManagers. In an OSGi application this can be achieved using POJO Blueprint components, whose JPA resources may be injected into them by their container.
OSGi modules, like Java EE modules, may choose to manage their own JPA resources and the JSF JPA Employee List sample we are building from uses unmanaged JPA in its Web module. The focus of this section of the article is to describe how to use application-managed JPA to provide the same capabilities to an application converted to OSGi bundles.
Import the sample modified to split JPA
Import the EmployeeListSample-Part2Begin.zip file (see Downloads) to your Rational Application Developer workspace as an existing project:
- Select File > Import.
- Select General > Existing Projects into Workspace.
- In the Import dialog window, select the Select Archive File radio button.
- Click Browse and locate the
EmployeeListSample-Part2Begin.zip
file. Select the file and click Open. You should see the full path to the .zip file in the field beside the Browse button. - Click Finish.
persistence.xml
file in the
EmployeeListJPA project has already been set up to use the jdbc/sample data
source on the server you created in the section above. Assuming the data source
is configured on the server to access the sample database contained in the
EmployeeListWeb project and you are using the sample workspace location, the
project is ready to deploy and test by running index.jsp
on your
server like was done above.Convert projects to OSGi bundles
First, convert the JPA project to an OSGi JPA bundle:
- Right click the EmployeeListJPA project and, from the drop-down menu, select Configure > Convert to OSGi Bundle Project.
Figure 6. Project conversion to OSGi warning
- Select OK to confirm the conversion from JPA project to OSGi bundle.
- In
src/META-INF
, there is now a bundle manifest file,MANIFEST.MF
. - An OSGi Bundle facet has been added to this project. This makes the JPA project a JPA bundle.
- In the
MANIFEST.MF
file:- The entity and entity manager bean packages have been added as export-packages. This makes it so they are available to import by other bundles and, in this case, so the JPA entities can be accessed and data manipulated.
- Necessary imports for the existing classes have been added to the import-packages.
Listing 4. JPA bundle manifest after conversion
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: EmployeeListJPA Bundle-SymbolicName: EmployeeListJPA Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Meta-Persistence: Export-Package: employee.list.controller, employee.list Import-Package: javax.persistence;resolution:=optional, javax.faces.model;resolution:=optional |
Convert the web project
Next, convert the web project to an OSGi web bundle:
- Right-click EmployeeListWeb and, from the drop-down menu, select Configure > Convert to OSGi Bundle Project.
Figure 7. Project conversion to OSGi warning
- Select OK to confirm the conversion from JEE web project to an OSGi bundle.
- In the EmployeeListWeb project's properties, an OSGi Bundle facet has been added.
- A bundle manifest has been created at
WebContent/META-INF/manifest.mf.
- The EmployeeListWeb project is no longer a module in EmployeeListWebEAR
EmployeeListWebEAR will still exist as a project in your workspace and will contain errors saying "A Java EE Enterprise Application must contain one or more modules."
- The bundle manifest is listed in the EmployeeListWeb project. You can double-click Manifest: EmployeeListWeb to open the Bundle Manifest Editor.
- The bundle manifest automatically adds JSF project JAR files (contained in
WebContent/WEB-INF/lib
) to thebundle-classpath
. - The bundle manifest automatically adds Java class imports to the list of import packages, including imported entity classes that are resolved to the exports from the JPA bundle.
Listing 5. Web bundle manifest after conversion
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: EmployeeListWeb Bundle-SymbolicName: EmployeeListWeb Bundle-Version: 1.0.0.qualifier Bundle-ClassPath: WEB-INF/classes, WEB-INF/lib/icu4j_3_4_1.jar, WEB-INF/lib/jsf-ibm.jar, WEB-INF/lib/sitelib.jar, WEB-INF/lib/web-jpa.jar, WEB-INF/lib/EmployeeListJPA.jar Ignore-Scanning-Archives: jsf-ibm.jar, icu4j_3_4_1.jar Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Web-ContextPath: /EmployeeListWeb Import-Package: javax.faces.application, javax.faces.validator, org.apache.commons.logging;resolution:=optional, javax.faces.convert, javax.faces.render, javax.servlet.jsp;version="2.0", javax.servlet.http;version="2.5", javax.faces.event, employee.list.controller;resolution:=optional, javax.faces.component.html, javax.servlet;version="2.5", javax.faces.component, javax.servlet.jsp.tagext;version="2.0", javax.servlet.jsp.el;version="2.0", javax.faces.el, javax.faces.model, javax.faces.lifecycle, javax.faces.webapp, javax.faces, employee.list;resolution:=optional, javax.faces.context, javax.el;version="2.0";resolution:=optional |
The Bundle Manifest editor also provides visual representation of the manifest. Figure 8 shows an example of the Imported Packages from EmployeeListWeb's bundle manifest.
Figure 8. Bundle manifest editor Dependencies page
Finally, you need to remove the project dependency between the EmployeeListWeb and EmployeeListJPA projects. When the projects were JEE projects there was a dependency between EmployeeListWeb and EmployeeListJPA defined in the Web project's properties so JPA entities could be resolved by the web project. As the projects were converted to bundles the entity classes were added as export-packages in the JPA bundle and import-packages in the Web bundle. This duplicate dependency between projects, as an OSGi and Plug-in dependency, needs to be resolved. Follow these steps:
- Right-click EmployeeListWeb, and select Properties.
- In the Properties dialog window, go to the Deployment Assembly page.
- Select the EmployeeListJPA.jar entry and click Remove.
- Click OK in the Properties dialog window to accept this change.
Modify JPA Manager Beans
Inside the EmployeeListJPA bundle, the JPA manager beans, which are essentially facade beans for the JPA entity beans, need to be modified for the OSGi environment. In this OSGi example, where application-managed JPA entity manager beans will be used, we need to obtain an application-managed persistence unit via an OSGi service lookup. In a managed JPA example, a Java EE application's persistence unit can be injected by the container through the
@PersistenceUnit
annotation. Similarly, an OSGi application making
use of blueprint managed JPA beans, would inject the persistence units into its
blueprint component. A practical application and example of a managed JPA Java
EE application moving to OSGi blueprint container managed beans is outside the
scope of this article.In the application-managed JPA example we are exploring, if the OSGi service registry lookup was not added to the JPA entity manager bean, WebSphere would not be able to resolve the persistence unit, and you would see the error shown in Listing 6 at runtime as your web page tried to access JPA data and lookup the persistence unit.
Listing 6. Error if @PersistenceUnit is used in the JPA manager bean
Error 500: javax.servlet.ServletException: javax.faces.el.EvaluationException: javax.el.ELException: <openjpa-2.0.0-r422266:935683 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or Data source class name must be specified in the ConnectionDriverName property. |
In addition, because the original sample application creates unmanaged EntityManagerFactory instances, the persistence-unit transaction-type defaults to RESOURCE_LOCAL rather than JTA. Because we are now using application-managed JPA, which has a different default, we now need to specify this in the persistence.xml file. See Listing 7 below for what the persistence.xml file looks like after transaction-type="RESOURCE_LOCAL" (in bold) is added to the persistence.unit.
Listing 7. Perstistence.xml after adding type="RESOURCE_LOCAL"
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="EmployeeListJPA" transaction-type="RESOURCE_LOCAL"> <jta-data-source>jdbc/sample</jta-data-source> <non-jta-data-source>jdbc/sample-nonjta</non-jta-data-source> <class>employee.list.Department</class> <class>employee.list.Employee</class> <properties> <property name="openjpa.jdbc.Schema" value="SAMP"/> </properties> </persistence-unit> </persistence> |
To modify the JPA manager beans:
- Open the Employee entity manager bean,
EmployeeManager.java, which you can find at
EmployeeListJPA/src/employee/list/controller
. - In the EmployeeManager class, locate the getEntityManager() method.
- In the getEntityManager() method, replace the contents with the code in Listing 8.
Listing 8. New getEntityManager() method contents
try { emf = (EntityManagerFactory) new InitialContext().lookup("osgi:service/javax.persistence.EntityManagerFactory/ (osgi.unit.name=EmployeeListJPA)"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return emf.createEntityManager(); |
- Correct the error on the emf variable by selecting the "create field emf" quickfix (to add the field declaration: private EntityManagerFactory emf;).
- Quickfix the errors on InitialContext() and NamingException, too, and add the necessary imports to the class.
- Repeat Steps 5 through 9 for the DepartmentManager.java JPA manager bean,
also found in the same package at
EmployeeListJPA/src/employee/list/controller
.
In order to deploy the OSGi bundle to WebSphere Application Server, you need to create an OSGi application to contain the OSGi bundle for deployment first.
- Select File > New > Other.
- In the New Project dialog window, expand OSGi and select OSGi Application Project.
- Click Next.
- Enter a Project Name, for example
EmployeeListApp
. - Verify that WebSphere Application Server v7.0 is set as the target runtime, and click Finish.
Figure 9. New OSGi application wizard
- EmployeeListApp will be listed in the Enterprise Explorer view. Expand it and double- click Manifest: EmployeeListApp to open the OSGi Application Manifest editor. There will be a warning on the manifest until a bundle is added in the next step.
- In the Contained Bundles section, click Add.
- Select EmployeeListWeb 1.0.0 and EmployeeListJPA 1.0.0, and click OK. The warning will resolve.
Figure 10. OSGi Application Manifest Editor overview page
Note:
Bundles can also be added to OSGi applications in the new OSGi application wizard.
- Save and close the EmployeeListApp manifest editor.
The data source needs to be created on your IBM WebSphere Application Server. See the Create a data source on the server section for how to create the Derby data source on the server, pointing to the sample database in the EmployeeListWeb project. If you are using the same server profile, you can reuse the data source created on the server in the previous section for this configuration.
Everything is now set up to deploy and run the EmployeeListWeb sample application when it is configured as an OSGi web bundle to access JPA data from another OSGi bundle contained in the OSGi application.
- Publish EmployeeListApp to WebSphere Application Server v7.0. Notice that the contained bundles are displayed beneath the OSGi application (see Figure 11).
Figure 11. Add and Remove to Server dialog window
Note:
EmployeeListWebEAR will still exist as a project in your workspace and will contain an error "A Java EE Enterprise Application must contain one or more modules." You can either delete or ignore this EAR file
- Run
index.jsp
inside the EmployeeListWeb application on WebSphere Application Server v7.0. In the Web Content folder in EmployeeListWeb, right-click the index.jsp file and select Run > Run on Server. - Select WebSphere Application Server v7.0 and click Finish.
- The sample site should launch in the internal browser
here:
http://localhost:<port>/EmployeeListWeb/index.faces
. - The site should be functional. For instance, you should be able to click List all employee records and see a list of employees.
Figure 12. Example page listing all employees, with web and JPA as separate bundles
- Remove the application from the server and delete all projects from your workspace before proceeding to the next section.
OSGi applications can include dynamic web projects as JEE modules. This gives Rational Application Developer users the option to use web projects as they begin to develop OSGi applications. The following steps will show how to use the "JSF JPA Employee List Sample" with the OSGi application including a dynamic web project in the OSGi application deployment.
Organize your application modules
To organize the EmployeeListApp to use a dynamic web project rather than a Web Application Bundle for the web front end in the OSGi application, follow these steps:
- Import the results from the second configuration variation (the Single converted OSGi bundle containing JPA section of this article), or use the EmployeeListSample-Part2Complete.zip file (from the Download section), exception for the EmployeeListWeb project. Do not import the EmployeeListWeb bundle with the other resource. You will import a web project in later steps.
- EmployeeListJPA
- EmployeeListApp
- There will be a warning on EmployeeListApp: "Bundle EmployeeListWeb with
version 1.0.0 cannot be resolved." Resolve the warning:
- Expand EmployeeListApp.
- Open the OSGi Application Manifest editor by double-clicking Manifest: EmployeeListApp.
- In the Contained Bundles section, select EmployeeListWeb 1.0.0 and click Remove.
- Save and close the manifest editor.
- Import the
EmployeeList-Part2Begin.zip file (see the Download section):
- File > Import.
- Select General > Existing Projects into Workspace.
- In the Import dialog window, choose the Select Archive File radio button.
- Click the Browse button, and locate the EmployeeList-Part2Begin.zip file.
- Select only the EmployeeListWeb project to import.
- Click Finish.
- There will be a new web project in your workspace: EmployeeListWeb.
- In the EmployeeListWeb project properties, go to the Deployment Assembly page.
- Remove the EmployeeListJPA.jar entry from this page.
- In the EmployeeListWeb project properties, go to the Java Build Path page and click the Libraries tab.
- Click the Add Class Folder button.
- Select to add the src folder under the EmployeeListJPA
project. This will add the following folder entry to the build path:
EmployeeListJPA/src
(class folder). - Click OK to save the changes to the project properties.
- Expand EmployeeListApp in the Enterprise Explorer view.
- Open the OSGi Application Manifest editor by double-clicking Manifest: EmployeeListApp.
- In the OSGi Application Manifest editor, expand the Dynamic web projects section, and click Add.
- In the Select a dynamic web project dialog window, select
EmployeeListWeb and click OK. This will add
the following to the manifest file:
Application-WebModules: EmployeeListWeb
Figure 13. OSGi Application Manifest editor showing web project
Create data source on WebSphere and deploy
OSGi applications containing dynamic web project modules can be successfully deployed to WebSphere Application Server only when the server is configured to "Run server with resources on server." This is not the default setting. To set this configuration:
- Open the Servers view.
- Double-click WebSphere Application Server v7.0 to open the Server Overview editor.
- Expand the Publishing setting of the WebSphere Application Server section.
- Select the publishing setting, Run server with resources on Server (Figure 14).
Figure 14. Publish settings to use resource on server
- Save and close the Server Overview editor.
Deploy and run the OSGi Application, EmployeeListApp
Now everything is set up to deploy and run the EmployeeListWeb sample application when it is configured as an OSGi application with an OSGi JPA bundle and a dynamic web project.
- Publish EmployeeListApp to the WebSphere Application Server v7.0. Notice the contained bundle and JEE web project are displayed beneath the OSGi application:
Figure 15. Add and Remove Server dialog window
Run
index.jsp
inside the EmployeeListWeb
application on WebSphere Application Server v7.0:- In the Web Content folder in EmployeeListWeb, right-click the index.jsp file, and select Run > Run on Server.
- Select WebSphere Application Server v7.0, and click Finish.
- The sample site should launch in the internal browser at this
location:
http://localhost:<port>/EmployeeListWeb/index.faces
- The site should be functional. For instance, you should be able to click List all employee records and see a list of employees.
Figure 16. Example page that lists all employees, with web module in OSGi app
This article demonstrated how Java EE developers can leverage the capability provided by IBM Rational Application Developer OSGi development tools by converting their existing Java EE applications to OSGi-based apps. You have seen three ways to convert and organize a simple dynamic web and JPA application, using application-managed JPA, as an OSGi application:
- Single converted OSGi web bundle that contains a JPA
- Multiple converted OSGi bundles, separating web and data persistence layers
- Java EE web module (dynamic web project) with converted JPA OSGi bundle
- OSGi project conversion wizard to convert the dynamic web project to an OSGi web application bundle (WAB) and the JPA project to an OSGi bundle
- OSGi bundle manifest editor to describe the bundle metadata, including bundle dependencies
- OSGi application project, which creates the Enterprise Bundle Archive (EBA), replacing the Java EE EAR file.
- Enable JPA access within an OSGi context, using JNDI to perform OSGi service registry lookup
- Server integration, supporting seamless publication and deployment of OSGi applications in a manner very similar to that of Java EE applications
- Import/Export support for Enterprise Bundle Archive (EBA) to share OSGi applications or export for manual deployment into WebSphere Application Server
- Plug-in Development Environment (PDE) project conversion to OSGi
- OSGi composite bundles
- Blueprint container with a dependency injection model
- OSGi life cycle management that enables applications to be installed, updated, started, stopped, and uninstalled without a server restart
Many thanks to Tim DeBoer, Zina Mostafia, and Jim Zhang for their technical and editorial contributions to this article.
Description | Name | Size | Download method |
---|---|---|---|
Employee List Sample | EmployeeListSample.zip | 788KB | HTTP |
Employee List Sample - Part 2 Begin | EmployeeListSample-Part2Begin.zip | 5445KB | HTTP |
Employee List Sample - Part 2 Complete | EmployeeListSample-Part2Complete.zip | 5439KB | HTTP |
Resources
Learn
- Find out more about OSGI and the Blueprint
specification:
- Building OSGi applications with the Blueprint Container specification, an IBM® developerWorks® article by Jarek Gawor (November 2009)
- Developing enterprise OSGi applications for WebSphere Application Server, a developerWorks article by Ian Robinson and Holly Cummins (July 2010)
- Best practices for developing and working with OSGi applications, a developerWorks article by Graham Charters, Jeremy Hughes, Tim Mitchell and others (July 2010)
- About the OSGi Service Platform, a technical white paper from the OSGi Alliance (June 2007)
- Find out more about Rational Application
Developer:
- Browse the Rational Application Developer for WebSphere Software page on developerWorks for links to technical articles and many related resources.
- Explore the Information Center.
- Visit the Rational software area on
developerWorks for technical resources and best practices for Rational
Software Delivery Platform products.
- Stay current with developerWorks technical events and webcasts focused on a variety of IBM products and IT industry topics.
- Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM products and tools, as well as IT industry trends.
- Follow developerWorks on Twitter.
- Watch developerWorks on-demand demos, ranging from product installation and setup demos for beginners to advanced functionality for experienced developers.
- Check the Rational training and certification catalog, which includes many types of courses on a wide range of topics. You can take some of them anywhere, any time, and many of the "Getting Started" ones are free.
- Try Rational
Application Developer for WebSphere Software, free
- Evaluate IBM software in the way that suits you best: Download it for a trial, try it online, use it in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement service-oriented architecture efficiently.
- Join the Development
Tools forum to ask questions and participate in discussions.
- Get involved in the My developerWorks community. Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups such as the Rational Café, and wikis.
댓글 없음:
댓글 쓰기