r470 - trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services
Author: athimel Date: 2014-08-05 14:56:55 +0200 (Tue, 05 Aug 2014) New Revision: 470 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/470 Log: Introduce ServiceFactory Added: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceFactory.java Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceContext.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceInvocationHandler.java Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceContext.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceContext.java 2014-08-05 12:11:22 UTC (rev 469) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceContext.java 2014-08-05 12:56:55 UTC (rev 470) @@ -58,72 +58,122 @@ private static final Log log = LogFactory.getLog(FaxToMailServiceContext.class); + protected FaxToMailServiceContext delegateServiceContext; + protected FaxToMailConfiguration applicationConfig; protected FaxToMailTopiaApplicationContext topiaApplicationContext; protected FaxToMailTopiaPersistenceContext persistenceContext; - protected boolean calledFromProxy = false; + protected FaxToMailServiceFactory serviceFactory; public FaxToMailServiceContext(FaxToMailTopiaApplicationContext topiaApplicationContext) { this.topiaApplicationContext = topiaApplicationContext; + this.serviceFactory = new FaxToMailProxiedServiceFactory(); } + private FaxToMailServiceContext(FaxToMailServiceContext delegateServiceContext, FaxToMailServiceFactory serviceFactory) { + this.delegateServiceContext = delegateServiceContext; + this.serviceFactory = serviceFactory; + } + public FaxToMailConfiguration getApplicationConfig() { - return applicationConfig; + if (delegateServiceContext != null) { + return delegateServiceContext.getApplicationConfig(); + } else { + return applicationConfig; + } } public FaxToMailTopiaPersistenceContext getPersistenceContext() { - if (persistenceContext == null) { - persistenceContext = topiaApplicationContext.newPersistenceContext(); + if (delegateServiceContext != null) { + return delegateServiceContext.getPersistenceContext(); + } else { + if (persistenceContext == null) { + persistenceContext = topiaApplicationContext.newPersistenceContext(); + } + return persistenceContext; } - return persistenceContext; } - public FaxToMailTopiaApplicationContext getTopiaApplicationContext() { - return topiaApplicationContext; - } +// public FaxToMailTopiaApplicationContext getTopiaApplicationContext() { +// return topiaApplicationContext; +// } public void setApplicationConfig(FaxToMailConfiguration applicationConfig) { - this.applicationConfig = applicationConfig; + if (delegateServiceContext != null) { + delegateServiceContext.setApplicationConfig(applicationConfig); + } else { + this.applicationConfig = applicationConfig; + } } - public void setCalledFromProxy(boolean calledFromProxy) { - this.calledFromProxy = calledFromProxy; + public <E extends FaxToMailService> E newService(Class<E> serviceInterfaceClass) { + return serviceFactory.newService(serviceInterfaceClass); } - public <E extends FaxToMailService> E newService(Class<E> serviceInterfaceClass) { + public class FaxToMailDefaultServiceFactory implements FaxToMailServiceFactory { - E service; - try { - Class<E> serviceImplClass = (Class<E>) Class.forName(serviceInterfaceClass.getCanonicalName() + "Impl"); + protected FaxToMailServiceContext serviceContext; - // special case for mock services - if (serviceImplClass.equals(LdapServiceImpl.class) && applicationConfig.isLdapMock()) { - serviceImplClass = (Class<E>) LdapServiceMock.class; - } + public void setServiceContext(FaxToMailServiceContext serviceContext) { + this.serviceContext = serviceContext; + } - Constructor<E> constructor = serviceImplClass.getConstructor(); - service = constructor.newInstance(); + @Override + public <E extends FaxToMailService> E newService(Class<E> serviceInterfaceClass) { - if (!calledFromProxy) { - service = (E) Proxy.newProxyInstance(serviceInterfaceClass.getClassLoader(), new Class[]{serviceInterfaceClass}, - new FaxToMailServiceInvocationHandler(service, this)); + E service; + try { + Class<E> serviceImplClass = (Class<E>) Class.forName(serviceInterfaceClass.getCanonicalName() + "Impl"); + + // special case for mock services + if (serviceImplClass.equals(LdapServiceImpl.class) && serviceContext.getApplicationConfig().isLdapMock()) { + serviceImplClass = (Class<E>) LdapServiceMock.class; + } + + Constructor<E> constructor = serviceImplClass.getConstructor(); + service = constructor.newInstance(); + + } catch (NoSuchMethodException e) { + throw new ApplicationTechnicalException("all services must provide a non-argument constructor", e); + } catch (InvocationTargetException|InstantiationException|IllegalAccessException e) { + throw new ApplicationTechnicalException("unable to instantiate service", e); + } catch (ClassNotFoundException e) { + throw new ApplicationTechnicalException("unable to find the implementation of the service", e); } - } catch (NoSuchMethodException e) { - throw new ApplicationTechnicalException("all services must provide a non-argument constructor", e); - } catch (InvocationTargetException|InstantiationException|IllegalAccessException e) { - throw new ApplicationTechnicalException("unable to instantiate service", e); - } catch (ClassNotFoundException e) { - throw new ApplicationTechnicalException("unable to find the implementation of the service", e); + service.setServiceContext(serviceContext); + + return service; + } + } - service.setServiceContext(this); + public class FaxToMailProxiedServiceFactory implements FaxToMailServiceFactory { - return service; + protected FaxToMailDefaultServiceFactory defaultServiceFactory; + protected FaxToMailServiceContext subServiceContext; + + @Override + public <E extends FaxToMailService> E newService(Class<E> serviceInterfaceClass) { + + if (defaultServiceFactory == null) { + defaultServiceFactory = new FaxToMailDefaultServiceFactory(); + subServiceContext = new FaxToMailServiceContext(FaxToMailServiceContext.this, defaultServiceFactory); + defaultServiceFactory.setServiceContext(subServiceContext); + } + + E realService = defaultServiceFactory.newService(serviceInterfaceClass); + + E service = (E) Proxy.newProxyInstance(serviceInterfaceClass.getClassLoader(), new Class[]{serviceInterfaceClass}, + new FaxToMailServiceInvocationHandler(realService, FaxToMailServiceContext.this)); + + return service; + + } } public Date getNow() { @@ -132,35 +182,35 @@ } public DecoratorService getDecoratorService() { - return newService(DecoratorServiceImpl.class); + return newService(DecoratorService.class); } public ValidationService getValidationService() { - return newService(ValidationServiceImpl.class); + return newService(ValidationService.class); } public MailFolderService getMailFolderService() { - return newService(MailFolderServiceImpl.class); + return newService(MailFolderService.class); } public UserService getUserService() { - return newService(UserServiceImpl.class); + return newService(UserService.class); } public LdapService getLdapService() { - return newService(LdapServiceImpl.class); + return newService(LdapService.class); } public EmailService getEmailService() { - return newService(EmailServiceImpl.class); + return newService(EmailService.class); } public ReferentielService getReferentielService() { - return newService(ReferentielServiceImpl.class); + return newService(ReferentielService.class); } public ConfigurationService getConfigurationService() { - return newService(ConfigurationServiceImpl.class); + return newService(ConfigurationService.class); } @Override Added: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceFactory.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceFactory.java (rev 0) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceFactory.java 2014-08-05 12:56:55 UTC (rev 470) @@ -0,0 +1,10 @@ +package com.franciaflex.faxtomail.services; + +/** + * @author Arnaud Thimel (Code Lutin) + */ +public interface FaxToMailServiceFactory { + + public <E extends FaxToMailService> E newService(Class<E> serviceInterfaceClass); + +} Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceInvocationHandler.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceInvocationHandler.java 2014-08-05 12:11:22 UTC (rev 469) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceInvocationHandler.java 2014-08-05 12:56:55 UTC (rev 470) @@ -22,13 +22,11 @@ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { - serviceContext.setCalledFromProxy(true); Object result = method.invoke(wrappedService, args); return result; } finally { IOUtils.closeQuietly(serviceContext); - serviceContext.setCalledFromProxy(false); } } }
participants (1)
-
athimelï¼ users.forge.codelutin.com