Thursday, October 29, 2009

OpenWebBeans Injection Service in Embeddable Apache OpenEJB

Hi;

In this post, I would like to give a simple tutorial on how to use the OpenWebBeans Dependency Injection service in an embeddable EJB container (Apache OpenEJB within Apache Tomcat 6.X).

This tutorial shows how to deploy and run sample web application that is located at "
https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/samples/ejb-telephone/
".

Sample example simulates a simple telephone contact list. It has two main functionality

1* Add new telephone contact.
Adds a new telephone contact record into the database.

2* Show telephone contacts
Shows all contacts in a table.

Configuration Steps

1* If your system does not have Maven and Java Development Kit Version 6 (JDK 6).
Install maven and java 6 sdk into your system.
Configure your classpath so "mvn" and "java" commands can be run from any location.

2* Checkout OpenWebBeans development code from SVN trunk.

1.1)Checkout source code into the folder openwebbeans.
svn co https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/ openwebbeans

1.2)Compile and install it using maven command
cd openwebbeans;
mvn clean install;

If there is no errors, all jars are nstalled into respective "target" folders of the projects.

3* Download Tomcat 6.X version, (you can download Tomcat from http://tomcat.apache.org/download-60.cgi)

4* Download and configure OpenEJB. Look at the URL http://openejb.apache.org/tomcat.html for OpenEJB installation steps.

5* Copy "atinject-api-1.0.0-incubating-SNAPSHOT.jar" from "openwebbeans/atinject-api/target" to Tomcat "/lib" folder.

6* Copy "openwebbeans-api-1.0.0-incubating-SNAPSHOT.jar" from "openwebbeans/webbeans-api/target" to Tomcat "/lib" folder.

After completing the above configuration steps successfully, our OpenEJB+Tomcat embeddable container is ready to use.


OpenWebBeans Configuration

To use the OpenWebBeans in an OpenEJB+Tomcat, it requires some configuration. OpenWebBeans has lots of configuration parameters. You can override all of them with an properties file. (You can look at https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/readme/README_M3.txt for further details).

OpenWebBeans default properties can be overriden by using a "openwebbeans.properties" file locating at the classpath of an application.

1* Create a "META-INF/openwebbeans/openwebbeans.properties" in your web application classpath.(Its location will be "java/main/resources" in a mavenized web application.)

2* Add the following [key,value] pairs into the "openwebbeans.properties" file. This enables EJB functionality and resource injection service in the web project, for example injection of @PersistenceContext, @PersistenceUnit, @Resource etc.

#use embedded openejb metadata discovery
org.apache.webbeans.spi.deployer.UseEjbMetaDataDiscoveryService=true

#use resource service
org.apache.webbeans.resource.spi.ResourceService=org.apache.webbeans.spi.ee.openejb.resource.OpenEjbResourceServiceImpl


Maven Based Web Application Configuration

Web Application Configuration

Create a web application using maven archetype. Look at the code in the "ejb-telephone" sample.

It uses "tomcat-maven-plugin" to deploy the web application into a running tomcat instance.

The web application directory structure look like this;
telephone
   src
   main
      java
      resources
       META-INF
        openwebbeans
         openwebbeans.properties --> OWB specific file
         beans.xml
         persistence.xml --> JPA specific file
     WebContent
       WEB-INF
         beans.xml
         web.xml
         faces-config.xml --> JSF Specific file
     contact.xhtml
     index.html


Compile And Deploy

Source code of the telephone application can be found at the "openwebbeans/samples/ejb-telephone".

Tomcat plugin uses http://localhost:8080/manager application to deploy war file
into your embeddable EJB Tomcat container. There must be an tomcat-users.xml
file in the "conf" directory of the Tomcat server that contains manager role and username.

Example tomcat-users.xml file

<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="" roles="manager"/>
</tomcat-users>

>Start Tomcat server if not started
>cd Tomcat_Directory/bin;
>catalina.sh (or catalina.bat) run;
>mvn clean tomcat:deploy;

If everything goes well, hit the location in your favorite browser

Ejb Sample URL : http://localhost:8080/ejb-telephone.


Enjoy!

Sunday, October 25, 2009

Using JSF2 Ajax with OpenWebBeans

Hi;

I have created a simple JSF2 Ajax sample using OpenWebBeans. First update trunk and do following

Source Location is :

https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/samples/jsf2sample/

Run : mvn clean jetty:run

URL : http://localhost:8080/jsf2sample

Enjoy!

Sunday, October 18, 2009

Using OpenWebBeans in Java SE

You can use OpenWebBeans dependency injection service in Java SE (for example Java Swing) project. New sample project is added to SVN trunk that shows how to use OpenWebBeans in Swing applications.

Source of the module is under "trunk/samples/standalone-sample".

Firstly you run mvn:install from the "trunk" to install/update necessary modules.

Then;

>> cd samples/standalone-sample;
>> mvn clean package;

This creates a jar in "target/standalone-sample.jar"
>> unjar this into any folder
>> java -jar standalone-sample-1.0.0-incubating-SNAPSHOT.jar Boot

It shows simple login screen :)

Cheers;

OpenWebBeans Pass JSR-330 TCK

After struggling some weird errors, we have finally passed JSR-330 TCK test suite.

You can check out sources from "/trunk/atinject-tck" module. It depends on new module "trunk/webbeans-porting" module.

>>cd atinject-tck;
>>mvn test;

Or from "trunk/"
>>mvn test -Pjsr330tck.

SVN Location : https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/

Cheers;