Clover coverage report -
Coverage timestamp: Sat Jul 7 2007 16:41:13 CEST
file stats: LOC: 120   Methods: 4
NCLOC: 65   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ServiceLocator.java 25% 54.5% 75% 53.7%
coverage coverage
 1    /**
 2    * JTRunner is free software; you can redistribute it and/or modify it under the
 3    * terms of the GNU General Public License as published by the Free Software
 4    * Foundation; either version 2, or (at your option) any later version.
 5    */
 6   
 7    package jtr.enterprise;
 8   
 9    import java.lang.reflect.Method;
 10    import java.lang.reflect.InvocationTargetException;
 11    import java.util.Hashtable;
 12   
 13    import javax.naming.Context;
 14    import javax.naming.NamingException;
 15    import javax.naming.InitialContext;
 16    import javax.rmi.PortableRemoteObject;
 17   
 18    import jtr.config.enterprise.EnterpriseConfig;
 19    import org.apache.log4j.Logger;
 20   
 21    /**
 22    * This helper class provides a facility for looking up enterprise java beans.
 23    *
 24    * @author Francesco Russo (frusso@dev.java.net)
 25    * @version 4.0
 26    * @since 1.0
 27    */
 28    public class ServiceLocator {
 29    /**
 30    * This method takes care of looking up the EJB registered under the
 31    * <code>ejbName</code> input parameter, of narrowing it to the desired
 32    * home class, and finally invokes the create on that home instance.
 33    *
 34    * @param ejbName
 35    * The JNDI name of the EJB
 36    * @param clazz
 37    * The class the retrieved instance should be narrowed to
 38    * @param environment
 39    * The enterprise configuration to be used for instantiating
 40    * the initial context
 41    * @return Object
 42    * The remote reference or <code>null</code>
 43    * @throws LocatorException
 44    * @deprecated
 45    */
 46  0 @Deprecated
 47    public static <T> T getEJBObject(String ejbName, Class clazz, Hashtable environment) throws LocatorException {
 48  0 Object ref = null;
 49  0 try {
 50  0 logger.debug("Getting remote object " + ejbName + "...");
 51  0 ref = getInitialContext(environment).lookup(ejbName);
 52  0 ref = PortableRemoteObject.narrow(ref, clazz);
 53  0 T newRef = (T) invokeCreate(ref);
 54  0 logger.debug("... done");
 55  0 if(newRef==null)
 56  0 return (T)ref;
 57    else
 58  0 return newRef;
 59    } catch (Exception e) {
 60  0 String msg = "Remote object location failed for " + ejbName;
 61  0 logger.fatal(msg, e);
 62  0 throw new LocatorException(msg, e);
 63    }
 64    }
 65   
 66    /**
 67    * This method takes care of looking up the EJB registered under the
 68    * <code>ejbName</code> input parameter, of narrowing it to the desired
 69    * home class, and finally invokes the create on that home instance.
 70    *
 71    * @param ejbName
 72    * The JNDI name of the EJB
 73    * @param clazz
 74    * The class the retrieved instance should be narrowed to
 75    * @param environment
 76    * The enterprise configuration to be used for instantiating
 77    * the initial context
 78    * @return Object
 79    * The remote reference or <code>null</code>
 80    * @throws LocatorException
 81    */
 82  19994 public static <T> T getEJBObject(String ejbName, Class clazz, EnterpriseConfig environment) throws LocatorException {
 83  19994 Object ref = null;
 84  19998 try {
 85  19998 logger.debug("Getting remote object " + ejbName + "...");
 86  19998 ref = getInitialContext(environment.toHashtable()).lookup(ejbName);
 87  19994 ref = PortableRemoteObject.narrow(ref, clazz);
 88  19992 T newRef = (T) invokeCreate(ref);
 89  19996 logger.debug("... done");
 90  19995 if(newRef==null)
 91  19995 return (T)ref;
 92    else
 93  0 return newRef;
 94    } catch (Exception e) {
 95  4 String msg = "Remote object location failed for " + ejbName;
 96  4 logger.fatal(msg, e);
 97  4 throw new LocatorException(msg, e);
 98    }
 99    }
 100   
 101  19999 private static Context getInitialContext(Hashtable env) throws NamingException {
 102  20000 return new InitialContext(env);
 103    }
 104   
 105  19993 private static <T> T invokeCreate(Object obj) throws SecurityException, InvocationTargetException, IllegalArgumentException, IllegalAccessException {
 106  19994 try {
 107  19994 Method createMethod = obj.getClass().getDeclaredMethod(CREATE_METHOD_SIGNATURE, new Class[0]);
 108  0 return (T) createMethod.invoke(obj, new Object[0]);
 109    } catch(NoSuchMethodException e) {
 110  19984 String msg = "The provided instance of type "+obj.getClass()+", does not have a "+CREATE_METHOD_SIGNATURE+" method.\n"+
 111    "Maybe this is because it is already a remote interface obtained looking up an EJB3 bean."+
 112    "Follows the caugth exception just for your convenience:\n";
 113  19992 logger.warn(msg,e);
 114  19996 return null;
 115    }
 116    }
 117   
 118    private static String CREATE_METHOD_SIGNATURE = "create";
 119    private static Logger logger = Logger.getLogger(ServiceLocator.class);
 120    }