Author: echatellier Date: 2010-03-23 16:31:34 +0100 (Tue, 23 Mar 2010) New Revision: 2812 Log: Ajout d'une factory pour recuperer les services via OpenEJB (en mode local pour l'instant) Added: trunk/lima-swing/src/main/java/org/chorem/lima/service/ trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java Added: trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java 2010-03-23 15:31:34 UTC (rev 2812) @@ -0,0 +1,191 @@ +/* *##% + * Copyright (C) 2010 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.chorem.lima.service; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.openejb.OpenEJB; +import org.apache.openejb.assembler.classic.AppInfo; +import org.apache.openejb.assembler.classic.Assembler; +import org.apache.openejb.loader.SystemInstance; +import org.chorem.lima.business.AccountService; +import org.chorem.lima.business.ejb.JournalServiceImpl; +import org.chorem.lima.business.ejb.PeriodServiceImpl; + +/** + * Is class is a service factory based on embedded openejb container. + * + * More information: + * <ul> + * <li>http://openejb.apache.org/embedding-openejb.html</li> + * </ul> + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class LimaServiceFactory { + + /** Log. */ + private static Log log = LogFactory.getLog(LimaServiceFactory.class); + + protected static LimaServiceFactory instance; + + protected InitialContext ctx; + + /** + * Init openejb jndi context. + */ + public LimaServiceFactory() { + Properties properties = new Properties(); + + // embedded server + properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory"); + + try { + ctx = new InitialContext(properties); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't initialize initial context", eee); + } + } + } + + /** + * Return service factory singleton instance. + * + * Init it at first call. + * + * @return singleton instance + */ + public static LimaServiceFactory getInstance() { + if (instance == null) { + instance = new LimaServiceFactory(); + } + return instance; + } + + /** + * Get Account service. + * + * @return account service proxy + */ + public AccountService getAccountService() { + + // first way is + // FooHome ejbHome = (FooHome)new InitialContext().lookup("java:openejb/ejb/my/bean/Foo"); + + // second way is (spec compliant) + // Object obj = ctx.lookup("my/bean/Foo"); + // FooHome ejbHome = (FooHome) + // PortableRemoteObject.narrow(obj, FooHome.class); + + // TODO put lookup name in configuration + String lookupName = AccountService.class.getName().replace('.', '/'); + AccountService ejbHome = null; + try { + ejbHome = (AccountService)ctx.lookup("AccountServiceImplLocal"); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't lookup for service : " + lookupName, eee); + } + } + return ejbHome; + } + + /** + * Get Journal service. + * + * @return account service proxy + */ + public JournalServiceImpl getJournalService() { + + // first way is + // FooHome ejbHome = (FooHome)new InitialContext().lookup("java:openejb/ejb/my/bean/Foo"); + + // second way is (spec compliant) + // Object obj = ctx.lookup("my/bean/Foo"); + // FooHome ejbHome = (FooHome) + // PortableRemoteObject.narrow(obj, FooHome.class); + + // TODO put lookup name in configuration + String lookupName = JournalServiceImpl.class.getName().replace('.', '/'); + JournalServiceImpl ejbHome = null; + try { + ejbHome = (JournalServiceImpl)ctx.lookup("JournalServiceImplLocal"); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't lookup for service : " + lookupName, eee); + } + } + return ejbHome; + } + + /** + * Get Journal service. + * + * @return account service proxy + */ + public PeriodServiceImpl getPeriodService() { + + // first way is + // FooHome ejbHome = (FooHome)new InitialContext().lookup("java:openejb/ejb/my/bean/Foo"); + + // second way is (spec compliant) + // Object obj = ctx.lookup("my/bean/Foo"); + // FooHome ejbHome = (FooHome) + // PortableRemoteObject.narrow(obj, FooHome.class); + + // TODO put lookup name in configuration + String lookupName = PeriodServiceImpl.class.getName().replace('.', '/'); + PeriodServiceImpl ejbHome = null; + try { + ejbHome = (PeriodServiceImpl)ctx.lookup("PeriodServiceImplLocal"); + } catch (NamingException eee) { + if (log.isErrorEnabled()) { + log.error("Can't lookup for service : " + lookupName, eee); + } + } + return ejbHome; + } + + /** + * Destroy openejb jndi context. + * + * Code taken from openEJB faq : + * http://openejb.apache.org/faq.html + * + * @throws Exception when trying to destroy a non existent application + */ + public void destroy() throws Exception { + Assembler assembler = SystemInstance.get().getComponent(Assembler.class); + for (AppInfo appInfo : assembler.getDeployedApplications()) { + assembler.destroyApplication(appInfo.jarPath); + } + OpenEJB.destroy(); + } +} Property changes on: trunk/lima-swing/src/main/java/org/chorem/lima/service/LimaServiceFactory.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL