r3357 - trunk/lima-business/src/main/java/org/chorem/lima/service
Author: echatellier Date: 2012-04-11 15:25:39 +0200 (Wed, 11 Apr 2012) New Revision: 3357 Url: http://chorem.org/repositories/revision/lima/3357 Log: Move to jee 6 api to init ejb container. Modified: trunk/lima-business/src/main/java/org/chorem/lima/service/LimaServiceFactory.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/service/LimaServiceFactory.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2012-04-06 14:32:57 UTC (rev 3356) +++ trunk/lima-business/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2012-04-11 13:25:39 UTC (rev 3357) @@ -33,8 +33,7 @@ import java.util.Map; import java.util.Properties; -import javax.naming.Context; -import javax.naming.InitialContext; +import javax.ejb.embeddable.EJBContainer; import javax.naming.NamingException; import org.apache.commons.io.IOUtils; @@ -53,17 +52,26 @@ /** Single instance. */ private static LimaServiceFactory instance; - protected Map<Class<?>, Object> services = new HashMap<Class<?>, Object>(); + /** Service cache. */ + protected Map<Class, Object> services = new HashMap<Class, Object>(); - /** JNDI context used to look for EJB. */ - protected static InitialContext ctx; + /** EJB container. */ + protected static EJBContainer container; - /** Init openejb jndi context. */ + /** EJB service namespace. */ + protected static final String NAMESPACE = "java:global/lima-business/"; + + /** + * Init openejb container. + * + * @param config configuration + */ public static void initFactory(ApplicationConfig config) { // TODO EC-20100407 maybe put all options in LimaConfig and allow user to configure it (remove getFlatOptions use) // TODO EC-20120113 i don't understand this comment anymore :( - Properties props = config.getFlatOptions(); + //Properties props = config.getFlatOptions(); + Properties props = config.getOptions(); // transmission des options de logging a openejb // sinon, il utilise son propre pattern interne @@ -81,31 +89,18 @@ IOUtils.closeQuietly(log4jFile); } - // Context.INITIAL_CONTEXT_FACTORY is a mandatory option - // containsKey() does'nt work :( - if (!props.containsKey(Context.INITIAL_CONTEXT_FACTORY)) { - throw new IllegalStateException( - "Application configuration is missing mandatory property " - + Context.INITIAL_CONTEXT_FACTORY); - } - - try { - ctx = new InitialContext(props); - } catch (NamingException eee) { - if (log.isErrorEnabled()) { - log.error("Can't initialize initial context", eee); - } - } - + // see http://openejb.apache.org/embedded-configuration.html + // http://openejb.apache.org/properties-listing.html + // for embedded configuration + container = EJBContainer.createEJBContainer(props); } public static <M> M getService(Class<M> serviceMonitorableClass) { LimaServiceFactory factory = getInstance(); - Map<Class<?>, Object> services = factory.getServices(); + Map<Class, Object> services = factory.getServices(); M result = (M) services.get(serviceMonitorableClass); if (result == null) { result = factory.newService(serviceMonitorableClass); - services.put(serviceMonitorableClass, result); } return result; @@ -131,22 +126,10 @@ } /** - * Destroy openejb jndi context. - * <p/> - * Code taken from openEJB faq : - * http://openejb.apache.org/faq.html - * - * @throws Exception when trying to destroy a non existent application + * Close openejb container. */ - public static void destroy() throws Exception { - - /* not available in openejb 4 - destroy code (only in embedded mode) - Assembler assembler = SystemInstance.get().getComponent(Assembler.class); - for (AppInfo appInfo : assembler.getDeployedApplications()) { - assembler.destroyApplication(appInfo.jarPath); - } - OpenEJB.destroy();*/ + public static void destroy() { + container.close(); } /** @@ -163,16 +146,16 @@ return instance; } - protected Map<Class<?>, Object> getServices() { - return getInstance().services; + protected Map<Class, Object> getServices() { + return services; } protected <M> M newService(Class<M> serviceMonitorableClass) { M result; Object ejbHome; - String serviceName = serviceMonitorableClass.getSimpleName().replace("Monitorable", "ImplRemote"); + String serviceName = serviceMonitorableClass.getSimpleName().replace("Monitorable", "Impl"); try { - ejbHome = ctx.lookup(serviceName); + ejbHome = container.getContext().lookup(NAMESPACE + serviceName); } catch (NamingException eee) { throw new RuntimeException( "Can't lookup for service : " + serviceName, eee);
participants (1)
-
echatellier@users.chorem.org