DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > JEE 6 Environmental Enterprise Entries and Glassfish

JEE 6 Environmental Enterprise Entries and Glassfish

Slim Ouertani user avatar by
Slim Ouertani
·
Jun. 20, 12 · Java Zone · Interview
Like (0)
Save
Tweet
5.78K Views

Join the DZone community and get the full member experience.

Join For Free

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.
GlassFish

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing Between GraphQL Vs REST
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • Why Is Software Integration Important for Business?
  • After COVID, Developers Really Are the New Kingmakers

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo