Ouertani Slim was born in Tunisia in 1981. Now he is a software engineer since 2004 and he is Java 6 and Spring certified with 100% and 94% respectively. He is very interested in Java technology, Scala and open source projects. He believes that programming is one of the hardest jobs and most beautiful in the world. Slim has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

JEE 6 Environmental Enterprise Entries and Glassfish

06.20.2012
| 4291 views |
  • submit to reddit

Purpose

This tutorial is a supplement to the article of oracle published here.

After reading this later, I decided to share some tips about this EE environment configuration.

Introduction

Two years ago, we decided to move to JEE 6 for our enterprise solutions and take a lot of fun with new EJB 3.1 features and annotations. Glassfish has made ​​our lives easier, it was very easy to declare variables using it's administration console.


Less complicated than using ejb-jar.xml are two methods using Glassfish server and resources :

I- Old fashion with resource per param (similar to oracle tutorial)

1- Declare an EJB with toBeInjected resource :
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author slim
 */
@Stateless
@LocalBean
public class MyEjbBean {
  @Resource(lookup="resName")
  protected String toBeInjected; 
 
 
  ////
   
}


2 - Open admin console ( localhost:4848 by default ) -> Resources -> Custom Resources -> New -> and fill the informations as below
Glassfish Jndi

At first glance, it looks simple and fast forward but problems start after just after a first project.

  • Problems ! Why ?

We achieved more than 20 variables and some resource names begin to confuse them. I was very hard for our operations team to catch the name of the correct setting and update its value.

It was necessary to group parameters resources into a single resource by project.

Next Fashion : Grouping resources

In this section I will use scala (similar class can be translated easy to java).

1 - First of all, we need to create our single resource class as simple bean

package me.slim.ouertani
class Ressource {
  var param1 : String = _
  var parma2 : String = _ 
}

2 - Next, let's create a resource factory class :

package me.slim.ouertani
class RessourceFactory extends ObjectFactory {
 
  override def getObjectInstance(obj: Object, name: javax.naming.Name, nameCtx: javax.naming.Context, environment: Hashtable[_, _]): Object = {
    val ressource = new Ressource();
    val reference = obj.asInstanceOf[Reference];
    val attributes = reference.getAll();
    while (attributes.hasMoreElements()) {
      val refAddr = attributes.nextElement().asInstanceOf[RefAddr];
      init(ressource, refAddr.getType(), refAddr.getContent());
    }
    return ressource;
  }

  private[this] def init(ressource: Ressource, tipe: String, content: Object) {
    tipe match {  

      case "param1" => ressource.param1 = content.toString()
      case "param2" => ressource.param2 = content.toString()
    
    }
  }
}
The purpose of object factory is to read injectable parameters and populate a resource class

3 - Back to Glassfish admininstration console, create a single resource ( see screenshot below) :
  1. Jndi Name => the resource name to be used by project for example
  2. Resource Type => check second radio button, and fill the resource full name ( with package)
  3. Factory class => Our RessourceFactory class full name ( with package)
  4. Add two (or more) properties to be injected in our resource class.

 

Glassfish JNDI

4- At the end we inject our resource in EJB class :

@Stateless
@LocalBean
class MyEjbBean {
  @Resource(lookup="globalRes")
  protected var res:Ressource= _
 
  /// res.param1
}

Conclusion

Having a single resource per project facilitates business management and thanks to Glassfish administration console managing JNDI became easier.
Published at DZone with permission of its author, Slim Ouertani.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)