Skip to content

Instantly share code, notes, and snippets.

@ghusta
Last active December 22, 2015 00:09
Show Gist options
  • Save ghusta/6387753 to your computer and use it in GitHub Desktop.
Save ghusta/6387753 to your computer and use it in GitHub Desktop.
ServiceLocatorGenerics : a ServiceLocator for finding EJB 2 via JNDI, using Java 5 Generics. It makes it possible to invoke : MyEJBHome remoteHome = ServiceLocatorGenerics.getEjbRemoteHome("jndiName", MyEJBHome.class); without having to do a casting
package fr.husta.ejbutils.servicelocator;
public class ServiceLocatorException
extends Exception
{
private static final long serialVersionUID = 1L;
public ServiceLocatorException()
{
super();
}
public ServiceLocatorException(String message)
{
super(message);
}
public ServiceLocatorException(String message, Throwable throwable)
{
super(message, throwable);
}
public ServiceLocatorException(Throwable throwable)
{
super(throwable);
}
}
package fr.husta.ejbutils.servicelocator;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/**
* ServiceLocator for EJB 2 with Java 5 Generics.
* <br>
* See <a href="http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683">Java Effective 2nd Edition</a> :
* <ul>
* <li>Chapter 5 - Generics</li>
* <ul>
* <li>Item 27 : Favor generic methods.</li>
* </ul>
* </ul>
*
* @since J2EE 1.3
* @since Java 5
* @see http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
* @see https://forums.oracle.com/thread/1159595
*/
public class ServiceLocatorGenerics
{
private Context initialContext;
/**
*
* @throws NamingException
*/
public ServiceLocatorGenerics() throws NamingException
{
super();
initialContext = new InitialContext();
}
/**
*
* @param context
* @throws NamingException
*/
public ServiceLocatorGenerics(final Context context) throws NamingException
{
super();
this.initialContext = context;
}
/**
* Permet de simplifier l'ecriture de la recuperation d'un EJB Home par un ServiceLocator.
* <br>
* Exemple :
* <pre>
* ServiceLocatorGenerics serviceLocator = new ServiceLocatorGenerics();
* MyEJBHome remoteHome = serviceLocator.getEjbRemoteHome("test", MyEJBHome.class);
* </pre>
*
* @param jndiName JNDI name for EJB.
* @param ejbHomeClass
* @return
* @throws ServiceLocatorException
*/
@SuppressWarnings("unchecked")
public <E extends javax.ejb.EJBHome> E getEjbRemoteHome(String jndiName, Class<E> ejbHomeClass)
throws ServiceLocatorException
{
Object remoteInterface = null;
EJBHome ejbHome = null;
try
{
remoteInterface = initialContext.lookup(jndiName);
// test possible :
// only narrow if necessary
// if (java.rmi.Remote.class.isAssignableFrom(narrowTo))
ejbHome = (EJBHome) PortableRemoteObject.narrow(remoteInterface, ejbHomeClass);
if (ejbHome == null)
{
throw new ServiceLocatorException("Impossible d'obtenir l'interface home pour " + jndiName);
}
}
catch (NamingException ne)
{
throw new ServiceLocatorException(ne);
}
return (E) ejbHome;
}
/**
* Permet de simplifier l'ecriture de la recuperation d'un EJB Local Home par un ServiceLocator.
* <br>
* Exemple :
* <pre>
* ServiceLocatorGenerics serviceLocator = new ServiceLocatorGenerics();
* MyEJBLocalHome localHome = serviceLocator.getEjbLocalHome("testLocal", MyEJBLocalHome.class);
* </pre>
*
* @param jndiName JNDI name for EJB.
* @param ejbLocalHomeClass
* @return
* @throws ServiceLocatorException
*/
@SuppressWarnings("unchecked")
public <E extends javax.ejb.EJBLocalHome> E getEjbLocalHome(String jndiName, Class<E> ejbLocalHomeClass)
throws ServiceLocatorException
{
Object localInterface = null;
EJBLocalHome ejbLocalHome = null;
try
{
localInterface = initialContext.lookup(jndiName);
ejbLocalHome = (EJBLocalHome) localInterface;
if (ejbLocalHome == null)
{
throw new ServiceLocatorException("Impossible d'obtenir l'interface local home pour " + jndiName);
}
}
catch (NamingException ne)
{
throw new ServiceLocatorException(ne);
}
return (E) ejbLocalHome;
}
/**
* Acces generique a l'annuaire JNDI.
*
* @param jndiName
* @return
* @throws ServiceLocatorException Principalement en cas de NamingException.
* @see {@link javax.naming.Context#lookup(String)}.
*/
public Object getRemoteInterface(final String jndiName) throws ServiceLocatorException
{
Object remoteInterface;
try
{
remoteInterface = initialContext.lookup(jndiName);
}
catch (Exception ex)
{
throw new ServiceLocatorException(ex);
}
return remoteInterface;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment