r2978 - trunk/lima-swing/src/main/java/org/chorem/lima/service
Author: bpoussin Date: 2010-07-21 11:58:03 +0200 (Wed, 21 Jul 2010) New Revision: 2978 Url: http://chorem.org/repositories/revision/lima/2978 Log: service use lazy loading, and use only one instance by service Modified: trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2010-07-20 17:14:24 UTC (rev 2977) +++ trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2010-07-21 09:58:03 UTC (rev 2978) @@ -21,6 +21,7 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.Properties; +import java.util.HashMap; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -42,6 +43,7 @@ /** Single instance. */ protected static LimaServiceFactory instance; + protected HashMap<Class, Object> services = new HashMap<Class, Object>(); /** JNDI context used to look for EJB. */ protected static InitialContext ctx; @@ -86,19 +88,23 @@ } public <M> M getService(Class<M> serviceMonitorableClass){ - Object ejbHome = null; - String serviceName = serviceMonitorableClass.getSimpleName().replace("Monitorable", "ImplRemote"); - try { + M result = (M)services.get(serviceMonitorableClass); + if (result == null) { + Object ejbHome = null; + String serviceName = serviceMonitorableClass.getSimpleName().replace("Monitorable", "ImplRemote"); + try { ejbHome = ctx.lookup(serviceName); - } catch (NamingException eee) { - if (log.isErrorEnabled()) { - log.error("Can't lookup for service : " + serviceName, eee); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't lookup for service : " + serviceName, eee); + } } - } - InvocationHandler handler = new ServiceMonitorableHandler(ejbHome); - M result = (M) Proxy.newProxyInstance( + InvocationHandler handler = new ServiceMonitorableHandler(ejbHome); + M result = (M) Proxy.newProxyInstance( serviceMonitorableClass.getClassLoader(), new Class[]{serviceMonitorableClass}, handler); + services.put(serviceMonitorableClass, result); + } return result; }
participants (1)
-
bpoussin@users.chorem.org