Author: bleny Date: 2013-03-13 14:53:50 +0100 (Wed, 13 Mar 2013) New Revision: 21 Url: http://forge.codelutin.com/projects/franciaflex-magalie/repository/revisions... Log: introduce session, application context and injection by interceptor Added: trunk/magalie-persistence/src/main/java/com/ trunk/magalie-persistence/src/main/java/com/franciaflex/ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieApplicationConfig.java trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieTechnicalException.java trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/DefaultMagalieServiceContext.java trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieServiceContext.java trunk/magalie-services/src/test/java/com/ trunk/magalie-services/src/test/java/com/franciaflex/ trunk/magalie-services/src/test/java/com/franciaflex/magalie/ trunk/magalie-services/src/test/java/com/franciaflex/magalie/services/ trunk/magalie-services/src/test/java/com/franciaflex/magalie/services/RequestedItemServiceTest.java trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationContext.java trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationListener.java trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieSession.java Removed: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/ServiceContext.java Modified: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieService.java trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/RequestedItemService.java trunk/magalie-web/pom.xml trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieInterceptor.java trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/WithdrawItemAction.java trunk/magalie-web/src/main/resources/log4j.properties trunk/magalie-web/src/main/webapp/WEB-INF/web.xml trunk/pom.xml Added: trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieApplicationConfig.java =================================================================== --- trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieApplicationConfig.java (rev 0) +++ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieApplicationConfig.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,4 @@ +package com.franciaflex.magalie; + +public class MagalieApplicationConfig { +} Added: trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieTechnicalException.java =================================================================== --- trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieTechnicalException.java (rev 0) +++ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/MagalieTechnicalException.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,19 @@ +package com.franciaflex.magalie; + +public class MagalieTechnicalException extends RuntimeException { + + public MagalieTechnicalException() { + } + + public MagalieTechnicalException(String message) { + super(message); + } + + public MagalieTechnicalException(String message, Throwable cause) { + super(message, cause); + } + + public MagalieTechnicalException(Throwable cause) { + super(cause); + } +} Added: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/DefaultMagalieServiceContext.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/DefaultMagalieServiceContext.java (rev 0) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/DefaultMagalieServiceContext.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,69 @@ +package com.franciaflex.magalie.services; + +import com.franciaflex.magalie.MagalieTechnicalException; +import com.franciaflex.magalie.persistence.entities.MagalieDAOHelper; +import com.franciaflex.magalie.persistence.entities.MagalieUserDAO; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaRuntimeException; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +public class DefaultMagalieServiceContext implements MagalieServiceContext { + + protected TopiaContext context; + + @Override + public <E extends MagalieService> E newService(Class<E> serviceClass) { + + E service; + + try { + + Constructor<E> constructor = serviceClass.getConstructor(); + + service = constructor.newInstance(); + + } catch (NoSuchMethodException e) { + + throw new MagalieTechnicalException("all services must provide a non-argument contructor", e); + + } catch (InvocationTargetException e) { + + throw new MagalieTechnicalException("unable to instantiate magalie service", e); + + } catch (InstantiationException e) { + + throw new MagalieTechnicalException("unable to instantiate magalie service", e); + + } catch (IllegalAccessException e) { + + throw new MagalieTechnicalException("unable to instantiate magalie service", e); + + } + + service.setServiceContext(this); + + return service; + + } + + @Override + public MagalieUserDAO getMagalieUserDao() { + + MagalieUserDAO magalieUserDAO; + + try { + + magalieUserDAO = MagalieDAOHelper.getMagalieUserDAO(context); + + } catch (TopiaException e) { + + throw new TopiaRuntimeException(e); + + } + + return magalieUserDAO; + } +} Modified: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieService.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieService.java 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieService.java 2013-03-13 13:53:50 UTC (rev 21) @@ -25,6 +25,6 @@ public interface MagalieService { - void setServiceContext(ServiceContext serviceContext); + void setServiceContext(MagalieServiceContext serviceContext); } Copied: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieServiceContext.java (from rev 18, trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/ServiceContext.java) =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieServiceContext.java (rev 0) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/MagalieServiceContext.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,37 @@ +package com.franciaflex.magalie.services; + +/* + * #%L + * MagaLiE :: Services + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.franciaflex.magalie.persistence.entities.MagalieUserDAO; + +/** + * @author bleny + */ +public interface MagalieServiceContext { + + <E extends MagalieService> E newService(Class<E> serviceClass); + + MagalieUserDAO getMagalieUserDao(); + +} Modified: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/RequestedItemService.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/RequestedItemService.java 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/RequestedItemService.java 2013-03-13 13:53:50 UTC (rev 21) @@ -29,9 +29,9 @@ public class RequestedItemService implements MagalieService { - protected ServiceContext serviceContext; + protected MagalieServiceContext serviceContext; - public void setServiceContext(ServiceContext serviceContext) { + public void setServiceContext(MagalieServiceContext serviceContext) { this.serviceContext = serviceContext; } Deleted: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/ServiceContext.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/ServiceContext.java 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/ServiceContext.java 2013-03-13 13:53:50 UTC (rev 21) @@ -1,31 +0,0 @@ -package com.franciaflex.magalie.services; - -/* - * #%L - * MagaLiE :: Services - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2013 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 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 Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -/** - * @author bleny - */ -public interface ServiceContext { - -} Added: trunk/magalie-services/src/test/java/com/franciaflex/magalie/services/RequestedItemServiceTest.java =================================================================== --- trunk/magalie-services/src/test/java/com/franciaflex/magalie/services/RequestedItemServiceTest.java (rev 0) +++ trunk/magalie-services/src/test/java/com/franciaflex/magalie/services/RequestedItemServiceTest.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,10 @@ +package com.franciaflex.magalie.services; + +import org.junit.Test; + +public class RequestedItemServiceTest { + + @Test + public void testGetWithdrawTask() throws Exception { + } +} Modified: trunk/magalie-web/pom.xml =================================================================== --- trunk/magalie-web/pom.xml 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-web/pom.xml 2013-03-13 13:53:50 UTC (rev 21) @@ -162,7 +162,7 @@ <path>/${defaultWebContextPath}</path> <systemProperties> <siteUrl>${defaultSiteUrl}</siteUrl> - <pollen.log.dir>${defaultLogDir}</pollen.log.dir> + <magalie.log.dir>${defaultLogDir}</magalie.log.dir> </systemProperties> <uriEncoding>UTF-8</uriEncoding> </configuration> Added: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationContext.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationContext.java (rev 0) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationContext.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,40 @@ +package com.franciaflex.magalie.web; + +import com.franciaflex.magalie.MagalieApplicationConfig; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.DateUtil; + +import java.util.Date; + +public class MagalieApplicationContext { + + private static final Log log = LogFactory.getLog(MagalieApplicationContext.class); + + protected static final String APPLICATION_CONTEXT_PARAMETER = "magalieApplicationContext"; + + protected MagalieApplicationConfig applicationConfig; + + protected Date applicationConfigLastRead; + + public MagalieApplicationConfig getMagalieApplicationConfig() { + + Date now = new Date(); + + if (applicationConfigLastRead == null || DateUtil.getDifferenceInDays(applicationConfigLastRead, now) > 1) { + + applicationConfigLastRead = now; + + if (log.isDebugEnabled()) { + log.debug("reloading config"); + } + + applicationConfig = new MagalieApplicationConfig(); + + } + + return applicationConfig; + + } + +} Added: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationListener.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationListener.java (rev 0) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieApplicationListener.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,31 @@ +package com.franciaflex.magalie.web; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +public class MagalieApplicationListener implements ServletContextListener { + + private static final Log log = LogFactory.getLog(MagalieApplicationListener.class); + + @Override + public void contextInitialized(ServletContextEvent sce) { + + MagalieApplicationContext applicationContext = new MagalieApplicationContext(); + + ServletContext servletContext = sce.getServletContext(); + + servletContext.setAttribute( + MagalieApplicationContext.APPLICATION_CONTEXT_PARAMETER, + applicationContext); + + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // nothing to do + } +} Modified: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieInterceptor.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieInterceptor.java 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieInterceptor.java 2013-03-13 13:53:50 UTC (rev 21) @@ -23,20 +23,22 @@ * #L% */ +import com.franciaflex.magalie.MagalieApplicationConfig; +import com.franciaflex.magalie.services.DefaultMagalieServiceContext; import com.franciaflex.magalie.services.MagalieService; +import com.franciaflex.magalie.services.MagalieServiceContext; import com.google.common.base.Preconditions; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.util.beans.BeanUtil; -import java.beans.BeanInfo; -import java.beans.Introspector; import java.beans.PropertyDescriptor; -import java.lang.reflect.Constructor; +import java.util.Set; /** - * This interceptor injects services in actions. + * This interceptor injects services, config and session in actions. */ public class MagalieInterceptor implements Interceptor { @@ -54,27 +56,44 @@ if (action instanceof MagalieActionSupport) { - BeanInfo beanInfo = Introspector.getBeanInfo(action.getClass()); + Set<PropertyDescriptor> descriptors = + BeanUtil.getDescriptors( + action.getClass(), + BeanUtil.IS_WRITE_DESCRIPTOR); - for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { + MagalieServiceContext serviceContext = newServiceContext(); + for (PropertyDescriptor propertyDescriptor : descriptors) { + Class<?> propertyType = propertyDescriptor.getPropertyType(); + Object toInject = null; + if (MagalieService.class.isAssignableFrom(propertyType)) { - Constructor<? extends MagalieService> constructor = (Constructor<? extends MagalieService>) propertyType.getConstructor(); + Class<? extends MagalieService> serviceClass = + (Class<? extends MagalieService>) propertyType; - Preconditions.checkNotNull(constructor); + toInject = serviceContext.newService(serviceClass); - MagalieService service = constructor.newInstance(); + } else if (MagalieSession.class.isAssignableFrom(propertyType)) { - service.setServiceContext(null); + toInject = getMagalieSession(invocation); - propertyDescriptor.getWriteMethod().invoke(action, service); + } else if (MagalieApplicationConfig.class.isAssignableFrom(propertyType)) { + toInject = getMagalieApplicationConfig(invocation); + + } + + if (toInject != null) { + if (log.isTraceEnabled()) { - log.trace("injecting service " + service + " in action " + action); + log.trace("injecting " + toInject + " in action " + action); } + + propertyDescriptor.getWriteMethod().invoke(action, toInject); + } } } @@ -82,6 +101,54 @@ return invocation.invoke(); } + protected MagalieApplicationConfig getMagalieApplicationConfig(ActionInvocation invocation) { + + MagalieApplicationConfig applicationConfig = getMagalieApplicationContext(invocation).getMagalieApplicationConfig(); + + return applicationConfig; + + } + + protected MagalieSession getMagalieSession(ActionInvocation invocation) { + + MagalieSession session = (MagalieSession) invocation.getInvocationContext().getSession().get(MagalieSession.SESSION_PARAMETER); + + if (session == null) { + + session = new MagalieSession(); + + invocation.getInvocationContext().getSession().put(MagalieSession.SESSION_PARAMETER, session); + + } + + return session; + + } + + protected MagalieApplicationContext getMagalieApplicationContext(ActionInvocation invocation) { + + MagalieApplicationContext applicationContext = + (MagalieApplicationContext) invocation + .getInvocationContext() + .getApplication() + .get(MagalieApplicationContext.APPLICATION_CONTEXT_PARAMETER); + + Preconditions.checkNotNull( + "application context must be initialized before calling an action", + applicationContext); + + return applicationContext; + + } + + protected MagalieServiceContext newServiceContext() { + + DefaultMagalieServiceContext serviceContext = new DefaultMagalieServiceContext(); + + return serviceContext; + + } + @Override public void destroy() { // nothing to do Added: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieSession.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieSession.java (rev 0) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/MagalieSession.java 2013-03-13 13:53:50 UTC (rev 21) @@ -0,0 +1,18 @@ +package com.franciaflex.magalie.web; + +import java.io.Serializable; + +/** + * The single object every user will have in its session. + * + * @author bleny + */ +public class MagalieSession implements Serializable { + + /** + * Key to store the {@link MagalieSession} instance in the session's map. + */ + public static final String SESSION_PARAMETER = "magalieSession"; + + +} Modified: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/WithdrawItemAction.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/WithdrawItemAction.java 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/WithdrawItemAction.java 2013-03-13 13:53:50 UTC (rev 21) @@ -23,6 +23,7 @@ * #L% */ +import com.franciaflex.magalie.MagalieApplicationConfig; import com.franciaflex.magalie.services.RequestedItemService; import com.franciaflex.magalie.services.WithdrawTask; import com.franciaflex.magalie.web.MagalieActionSupport; @@ -37,12 +38,18 @@ protected RequestedItemService service; + protected MagalieApplicationConfig config; + protected WithdrawTask withdrawTask; public void setService(RequestedItemService service) { this.service = service; } + public void setConfig(MagalieApplicationConfig config) { + this.config = config; + } + public void setModel(String model) { if (log.isErrorEnabled()) { @@ -64,6 +71,10 @@ withdrawTask = service.getWithdrawTask(); + if (log.isDebugEnabled()) { + log.debug("config = " + config); + } + return INPUT; } Modified: trunk/magalie-web/src/main/resources/log4j.properties =================================================================== --- trunk/magalie-web/src/main/resources/log4j.properties 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-web/src/main/resources/log4j.properties 2013-03-13 13:53:50 UTC (rev 21) @@ -30,7 +30,7 @@ # FileAppender : need to have pollen.log.dir in system properties at application startup log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=${magalie.log.dir}/pollen.log +log4j.appender.file.File=${magalie.log.dir}/magalie.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=4 log4j.appender.file.layout=org.apache.log4j.PatternLayout Modified: trunk/magalie-web/src/main/webapp/WEB-INF/web.xml =================================================================== --- trunk/magalie-web/src/main/webapp/WEB-INF/web.xml 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/magalie-web/src/main/webapp/WEB-INF/web.xml 2013-03-13 13:53:50 UTC (rev 21) @@ -62,12 +62,10 @@ <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> -<!-- <listener> <description>Init</description> - <listener-class>fr.cerqual.nflogement.extranet.ui.ApplicationListener</listener-class> + <listener-class>com.franciaflex.magalie.web.MagalieApplicationListener</listener-class> </listener> ---> </web-app> Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2013-03-12 17:23:23 UTC (rev 20) +++ trunk/pom.xml 2013-03-13 13:53:50 UTC (rev 21) @@ -91,7 +91,7 @@ <!-- versions --> <nuitonWebVersion>1.12-beta-2</nuitonWebVersion> - <nuitonUtilsVersion>2.6.10</nuitonUtilsVersion> + <nuitonUtilsVersion>2.6.11-SNAPSHOT</nuitonUtilsVersion> <h2Version>1.3.170</h2Version> <postgresqlVersion>9.1-901-1.jdbc4</postgresqlVersion> <struts2Version>2.3.8</struts2Version>