3.2. Running Examples

Each example has its own build.xml file; this allows each example to be run independently.

3.2.1. Stateless Session Bean

The build.xml file for this example is located in the examples/statelessbean folder.

3.2.1.1. Description

This example is a stateless session bean. It contains a helloWorld() method that displays text on the server side. Additionally, it demonstrates the use of EJB3 annotation, such as @Stateless.

The trace() method is annotated with @AroundInvoke EJB3 annotation. This method will be called at each call on a business method. The business methods are defined in the interface implemented by the SessionBean class.

The signature of the method annotated by @AroundInvoke when it is defined in the bean class, must follow this signature:

(private|protected|public) Object methodName(InvocationContext invocationContext)
    throws Exception;
[Note]Note

As a new feature of the EJB3, the bean's interface does not need to extend the Remote interface.

3.2.1.2. Running the Server

If the server is not available, it must be run by following the steps described in Chapter 3, "Running the EasyBeans Server."

3.2.1.3. Deploying the Bean

The stateless session bean must be deployed. If the bean has been installed in the ejb3s folder, this is done automatically.

On the server side, the following output should be seen:

     [java] INFO: Creating container for archive /home/benoitf/workspace/easybeans/ejb3s/stateless.jar.
     [java] INFO: Analyze elapsed during : 95 ms
     [java] INFO: Binding bean XXX with interface XXX into registry with jndi name XXX
     [java] INFO: Enhancement elapsed during : 105 ms
     [java] INFO: Container started in : 274 ms

Once this information is displayed on the screen, the container is ready to receive client calls.

3.2.1.4. Running the Client

Once the container has been started, the client can be launched.

Run the client with the following ant command: ant run.client

If the client runs successfully, the following output is displayed:

     [java] Calling helloWorld method...
     [java] Add 1 + 2...
     [java] Sum = '3'.
[Note]Note

In the client's code, the use of the PortableRemoteObject.narrow() call is no longer required.

3.2.2. Stateful Session Bean

The build.xml file for this example is located in the examples/statefulbean folder.

3.2.2.1. Description

This is an example of a stateful session bean using the SessionSynchronization interface.

It uses the @Stateful annotation and uses the default transaction model, which is REQUIRED.

3.2.2.2. Running the Server

If the server is not available, it must be run by following the steps described in Chapter 3, "Running the EasyBeans Server."

3.2.2.3. Deploying the Bean

The stateful session bean must be deployed. It is done automatically if the bean has been installed in the ejb3s folder.

On the server side, the following output should be seen:

     [java] INFO: Creating container for archive /home/benoitf/workspace/easybeans/ejb3s/stateful.jar.
     [java] INFO: Analyze elapsed during : 89 ms
     [java] INFO: Enhancement elapsed during : 76 ms
     [java] INFO: Binding bean XXX with interface XXX into registry with jndi name XXX
     [java] INFO: Container started in : 251 ms

Once this information is displayed on the screen, the container is ready to receive client calls.

3.2.2.4. Running the Client

Once the container has been started, the client can be launched.

Run the client with the following ant command: ant run.client

If the client runs successfully, the following output is displayed:

     [java] Start a first transaction
     [java] First request on the new bean
     [java] Second request on the bean
     [java] Commit the transaction
     [java] Start a second transaction
     [java] Buy 50 amount.
     [java] Rollback the transaction
     [java] after rollback, value = 30
     [java] Request outside any transaction
     [java] Check that value = 30
     [java] ClientStateful OK. Exiting.

3.2.3. Entity Bean

The build.xml file for this example is located in the examples/entitybean folder.

3.2.3.1. Description

This is an example of an entity bean. It describes how to use the new Java Persistence Model of an EJB3 persistence provider. To access EJB3 entities that are POJO, a stateless session bean is used. It is a facade bean.

The Entity class is a POJO class annotated with @Entity. The entities class is managed by the persistence provider.

Currently, the persistence provider is supplied by the Hibernate product, but the ObjectWeb Speedo product should be available soon. Users will have the choice between providers.

This example uses the @Stateful annotation and uses the default transaction model, which is REQUIRED.

The example shows an entity bean using EJB3 Hibernate-prototype persistence provider.

3.2.3.2. Running the Server

If the server is not available, it must be run following the steps described in Chapter 3, "Running the EasyBeans Server."

3.2.3.3. Deploying the Bean

The entity bean must be deployed. It is done automatically if the bean has been installed in the ejb3s folder.

On the server side, the following output should be seen:

     [java] INFO: Creating container for archive /home/benoitf/workspace/easybeans/ejb3s/entitybean.jar.
     [java] INFO: Analyze elapsed during : 95 ms
     [java] INFO: Enhancement elapsed during : 102 ms
     [java] INFO: No persistence provider was set, set to value org.hibernate.ejb.HibernatePersistence.
     [java] INFO: Hibernate 3.1.1
     [java] INFO: Using provided datasource
     [java] INFO: RDBMS: HSQL Database Engine, version: 1.8.0
     [...]
     [java] INFO: Binding bean XXX with interface XXX into registry with jndi name XXX
     [java] INFO: Container started in : 2010 ms

Once this information is displayed on the screen, the container is ready to receive client calls.

3.2.3.4. Running the Client

Once the container has been started, the client can be launched.

The client is run with the following ant command: ant run.client

If the client runs successfully, the following output is displayed:

     [java] Employee with id 1 = Florent
     [java] Employee with id 2 = Whale

3.2.3.5. Properties for the persistence

These properties are defined in the META-INF/persistence.xml file.

3.2.3.5.1. JDBC Dialect

By default, the dialect used to communicate with the database is set to HSQL as it is embedded in EasyBeans.

This dialect configuration is done with the following properties:

<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="toplink.target-database" value="HSQL"/>

3.2.3.5.2. Database (tables)

By default, the tables are created and database is empty after loading the entity beans.

This configuration is done with the following properties:

<property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="toplink.ddl-generation" value="drop-and-create-tables"/>

In order to keep data in database, this property should be changed.

3.2.4. Security example

The build.xml file for this example is located in the examples/security folder.

3.2.4.1. Description

This example illustrates the use of different Java EE 5.0 annotations which are linked to the security part.

The annotations used by the example are:

  • @DeclareRoles which is used to declare the roles used by an EJB component.

  • @RolesAllowed which list the authorized roles in order to call a method.

  • @DenyAll which deny the call to the method (for every roles).

  • @RunAs which set a new identity when calling other EJBs.

3.2.4.2. Running the Server

If the server is not available, it must be run following the steps described in Chapter 3, "Running the EasyBeans Server."

3.2.4.3. Deploying the Bean

The security bean example must be deployed. It is done automatically if the bean has been installed in the ejb3s folder.

On the server side, the following output should be seen:

  [java] 10/16/06 5:24:50 PM (I) ContainersMonitor.scanNewContainers : Creating container for archive /home/benoitf/workspace/easybeans/ejb3s/security.jar.
  [java] 10/16/06 5:24:50 PM (I) JContainer3.start : Analyze elapsed during : 124 ms
  [java] 10/16/06 5:24:50 PM (I) JContainer3.start : Enhancement elapsed during : 99 ms
  [java] 10/16/06 5:24:50 PM (I) JContainer3.start : Container started in : 363 ms
  [java] 10/16/06 5:24:50 PM (I) ContainersMonitor.scanNewContainers : Creating container for archive /home/benoitf/workspace/easybeans/ejb3s/mdb.jar.
  [java] 10/16/06 5:24:50 PM (I) JContainer3.start : Analyze elapsed during : 4 ms
  [java] 10/16/06 5:24:51 PM (I) JContainer3.start : Enhancement elapsed during : 287 ms
  [java] 10/16/06 5:24:51 PM (I) JContainer3.start : Container started in : 544 ms

Once this information is displayed on the screen, the container is ready to receive client calls.

3.2.4.4. Running the Client

Once the container has been started, the client can be launched.

The client is run with the following ant command: ant run.client

If the client runs successfully, the following output is displayed on the client's side:

     run.client:
     [java] Oct 16, 2006 5:27:03 PM org.objectweb.carol.util.configuration.ConfigurationRepository init
     [java] INFO: No protocols were defined for property 'carol.protocols', trying with default protocol = 'jrmp'.
     [java] Calling methods that everybody can call...
     [java] Call a bean with run-as in order to have 'admin' role...
     [java] Access denied as expected (method is denied)

And on the server:

     [java] someRolesAllowed() called
     [java] -> Caller is 'Principal[EasyBeans/Anonymous]'.
     [java] for run-as bean, caller is Caller is 'Principal[EasyBeans/Anonymous]
     [java] onlyAdminAllowed() called
     [java] -> Caller is 'Principal[admin]'.
     [java] someRolesAllowed() called
     [java] -> Caller is 'Principal[admin]'.