Author: tchemit Date: 2011-11-07 11:55:42 +0100 (Mon, 07 Nov 2011) New Revision: 16 Url: http://forge.codelutin.com/repositories/revision/echobase/16 Log: let's play it by the nflogement way :) Added: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java Removed: trunk/echobase-services/src/main/java/fr/ifremer/echobase/EchoBaseObjectFactory.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInitializable.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInjectable.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSingleton.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/IOCService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ioc/ trunk/echobase-services/src/main/resources/META-INF/ trunk/echobase-services/src/main/resources/echobase-config Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceFactory.java trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/LoginAction.java trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/validators/LoginValidator.java trunk/echobase-ui/src/main/webapp/WEB-INF/web.xml Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/EchoBaseObjectFactory.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/EchoBaseObjectFactory.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/EchoBaseObjectFactory.java 2011-11-07 10:55:42 UTC (rev 16) @@ -1,216 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, 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% - */ -package fr.ifremer.echobase; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import fr.ifremer.echobase.services.EchoBaseServiceInitializable; -import fr.ifremer.echobase.services.EchoBaseServiceInjectable; -import fr.ifremer.echobase.services.EchoBaseServiceSingleton; -import fr.ifremer.echobase.services.IOCService; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.framework.TopiaTransactionAware; -import org.nuiton.util.ObjectUtil; - -import java.util.Map; - -/** - * To obtain services. - * - * @author chemit <chemit@codelutin.com> - * @since 0.1 - */ -public class EchoBaseObjectFactory { - - /** Logger. */ - private static final Log log = LogFactory.getLog(EchoBaseObjectFactory.class); - - /** - * Shared application configuration. - * <p/> - * <b>Note:</b> this configuration must be filled before any invocation - * to a service. - */ - protected static EchoBaseConfiguration configuration; - - /** - * Sets the shared application configuration. - * - * @param configuration the shared application configuration to set - */ - public static void setConfiguration(EchoBaseConfiguration configuration) { - EchoBaseObjectFactory.configuration = configuration; - } - - /** - * Get the shared application configuration. - * - * @return the share application configuration, or {@code null} if none - * was setted - */ - public static EchoBaseConfiguration getConfiguration() { - return configuration; - } - - /** - * Obtain a new EchoBase factory. - * <p/> - * <strong>Note:</strong> The shared configuration must have been setted - * before using this method via the method - * {@link #setConfiguration(EchoBaseConfiguration)} - * - * @return the new instanciated EchoBase factory. - */ - public static EchoBaseObjectFactory newInstance() { - // must have a configuration to start the factory - Preconditions.checkNotNull( - configuration, - "No EchoBase appliation configuration registred."); - - if (log.isInfoEnabled()) { - log.info("New EchoBaseObjectFactory with configuration " + configuration); - } - // instanciate factory - EchoBaseObjectFactory factory = new EchoBaseObjectFactory(); - return factory; - } - - /** - * Destroy the static states of this class (mainly should be called at the - * end of the application). - */ - public static void destroy() { - setConfiguration(null); - } - - /** - * To store shared services. - * <p/> - * A shared service is in fact a singleton so can keep it once for all - * while using {@link #newService(Class)}. - */ - protected final Map<Class<?>, ?> services; - - /** - * Gets an instance of the service of the given type. - * <p/> - * If the service is marked as share, then the same instance will be - * always delivred, otheriwse a new instance will be each time created. - * - * @param serviceClass the type of service to obtain - * @param <S> the type of service to obtain - * @return the instance of required service - */ - public <S> S newService(Class<S> serviceClass) { - S service = newService0(serviceClass, null); - return service; - } - - /** - * Gets an instance of the transactional service of the given type. - * <p/> - * The given transaction will be injected into the transactional service. - * <p/> - * <strong>Note:</strong> To avoid problems, a transactional service should - * always not to be shared service. - * - * @param serviceClass the type of the service to obtain - * @param tx the transaction to inject in the service - * @param <S> the type of the service to obtain - * @return the instance of the required service - */ - public <S extends TopiaTransactionAware> S newTransactionalService(Class<S> serviceClass, - TopiaContext tx) { - S service = newService0(serviceClass, tx); - return service; - } - - @Override - protected void finalize() throws Throwable { - if (services != null) { - services.clear(); - } - super.finalize(); - } - - /** Protected constructor to avoid external instanciations... */ - protected EchoBaseObjectFactory() { - services = Maps.newHashMap(); - } - - /** - * Gets a service given his type. - * <p/> - * It will first search in cache (if service is marked as shared). - * <p/> - * If not found, then creates the new service and can do some stuffs on - * the freshly instanciaed service. - * - * @param serviceClass the type of service to obtain - * @param tx the optinal transaction to set - * @param <S> the type of service to obtain - * @return the required service (from cache) or freshly instanciated - * @see EchoBaseServiceInitializable - */ - @SuppressWarnings({"unchecked"}) - private <S> S newService0(Class<S> serviceClass, TopiaContext tx) { - Object service = null; - if (EchoBaseServiceSingleton.class.isAssignableFrom(serviceClass)) { - - // try to obtain it from cache - service = services.get(serviceClass); - } - if (service == null) { - - service = ObjectUtil.newInstance(serviceClass); - if (tx != null && service instanceof TopiaTransactionAware) { - TopiaTransactionAware transactionAware = (TopiaTransactionAware) service; - transactionAware.setTransaction(tx); - } - if (service instanceof EchoBaseServiceInitializable) { - EchoBaseServiceInitializable initializable = (EchoBaseServiceInitializable) service; - initializable.init(this); - } - if (service instanceof EchoBaseServiceInjectable) { - try { - newService(IOCService.class).injectExcept(service); - } catch (Exception e) { - throw new IllegalStateException( - "Could not inject into service " + service, e); - } - } - if (service instanceof EchoBaseServiceSingleton) { - ((Map) services).put(serviceClass, service); - } - } - return (S) service; - } - - public TopiaContext beginTransaction() { - //TODO Make it happens - return null; - } -} Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseService.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseService.java 2011-11-07 10:55:42 UTC (rev 16) @@ -25,7 +25,11 @@ package fr.ifremer.echobase.services; /** + * Contract to place on each EchBase service to push the {@code serviceContext} + * inside the service. + * * @author chemit <chemit@codelutin.com> + * @see EchoBaseServiceContext * @since 0.1 */ public interface EchoBaseService { Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceFactory.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceFactory.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceFactory.java 2011-11-07 10:55:42 UTC (rev 16) @@ -26,11 +26,14 @@ import fr.ifremer.echobase.EchoBaseTechnicalException; import java.lang.reflect.InvocationTargetException; + /** + * Factory of services. + * * @author chemit <chemit@codelutin.com> * @since 0.1 - **/ - public class EchoBaseServiceFactory { + */ +public class EchoBaseServiceFactory { public <E extends EchoBaseService> E newService(Class<E> clazz, EchoBaseServiceContext serviceContext) { // instantiate service using empty constructor Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInitializable.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInitializable.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInitializable.java 2011-11-07 10:55:42 UTC (rev 16) @@ -1,46 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, 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% - */ -package fr.ifremer.echobase.services; - -/** - * @author chemit <chemit@codelutin.com> - * @since 0.1 - **/ -import fr.ifremer.echobase.EchoBaseObjectFactory; - -/** - * Contract for service which need an intialization after their instanciation. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0 - */ -public interface EchoBaseServiceInitializable { - - /** - * Init the service using the T3 service factory which instanciated the service. - * - * @param factory the factory used to instanciate the service - */ - void init(EchoBaseObjectFactory factory); -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInjectable.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInjectable.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceInjectable.java 2011-11-07 10:55:42 UTC (rev 16) @@ -1,34 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, 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% - */ -package fr.ifremer.echobase.services; - -/** - * Contract for service which need an injections. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.1 - */ -public interface EchoBaseServiceInjectable { - -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSingleton.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSingleton.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceSingleton.java 2011-11-07 10:55:42 UTC (rev 16) @@ -1,38 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, 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% - */ -package fr.ifremer.echobase.services; - - -import fr.ifremer.echobase.EchoBaseObjectFactory; - -/** - * A contract to mark a shared service. - * <p/> - * A shared service will be keep as a singleton in the {@link EchoBaseObjectFactory}. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.1 - */ -public interface EchoBaseServiceSingleton { -} Deleted: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/IOCService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/IOCService.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/IOCService.java 2011-11-07 10:55:42 UTC (rev 16) @@ -1,166 +0,0 @@ -/* - * #%L - * EchoBase :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2011 Ifremer, 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% - */ -package fr.ifremer.echobase.services; - -import com.google.common.collect.Lists; -import fr.ifremer.echobase.EchoBaseObjectFactory; -import fr.ifremer.echobase.services.ioc.Injector; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.ServiceLoader; - -/** - * Service to inject stuff using T3 IOC engine. - * - * @author tchemit <chemit@codelutin.com> - * @see Injector - * @since 0.1 - */ -public class IOCService implements EchoBaseServiceInitializable, EchoBaseServiceSingleton { - - /** Logger. */ - private static final Log log = LogFactory.getLog(IOCService.class); - - protected Collection<Injector<?, ?>> injectors; - - @Override - public void init(EchoBaseObjectFactory factory) { - for (Injector<?, ?> injector : getInjectors()) { - injector.init(factory); - } - } - - public void injectExcept(Object bean, - Class<?>... excludedInjectors) throws Exception { - - // obtain the list of available injectors - Collection<Injector<?, ?>> injectorsToUse; - - if (excludedInjectors.length == 0) { - - // use all injectors - injectorsToUse = getInjectors(); - } else { - injectorsToUse = Lists.newArrayList(getInjectors()); - List<Class<?>> annotations = Arrays.asList(excludedInjectors); - Iterator<Injector<?, ?>> itr = injectorsToUse.iterator(); - while (itr.hasNext()) { - Injector<?, ?> injector = itr.next(); - if (annotations.contains(injector.getAnnotationType())) { - itr.remove(); - } - } - } - - // get all fields for the given type - injectForType(bean, bean.getClass(), injectorsToUse); - } - - public void injectOnly(Object bean, - Class<?>... onlyInjectors) throws Exception { - - // obtain the list of available injectors - Collection<Injector<?, ?>> injectorsToUse = - Lists.newArrayList(getInjectors()); - List<Class<?>> annotations = Arrays.asList(onlyInjectors); - Iterator<Injector<?, ?>> itr = injectorsToUse.iterator(); - while (itr.hasNext()) { - Injector<?, ?> injector = itr.next(); - if (!annotations.contains(injector.getAnnotationType())) { - itr.remove(); - } - } - - // get all fields for the given type - injectForType(bean, bean.getClass(), injectorsToUse); - } - - protected void injectForType(Object bean, - Class<?> beanType, - Collection<Injector<?, ?>> injectors) throws Exception { - Field[] fields = beanType.getDeclaredFields(); - for (Field field : fields) { - - if (Modifier.isFinal(field.getModifiers())) { - - // nothing to affect to a final field - continue; - } - - if (Modifier.isStatic(field.getModifiers())) { - - // nothing to affect to a static field - continue; - } - - Injector injector = getInjector(field, injectors); - if (injector != null) { - if (log.isDebugEnabled()) { - log.debug("Will use injector " + injector + " for " + field); - } - injector.processField(field, bean); - } - } - Class<?> superclass = beanType.getSuperclass(); - if (superclass != null && superclass.isAssignableFrom(beanType)) { - injectForType(bean, superclass, injectors); - } - } - - protected Injector<?, ?> getInjector(Field field, - Collection<Injector<?, ?>> injectors) { - - Injector<?, ?> result = null; - for (Injector<?, ?> injector : injectors) { - Class<? extends Annotation> annotationType = - injector.getAnnotationType(); - if (field.isAnnotationPresent(annotationType)) { - result = injector; - break; - } - } - - return result; - } - - protected Collection<Injector<?, ?>> getInjectors() { - if (injectors == null) { - injectors = Lists.newArrayList(); - for (Injector<?, ?> injector : - ServiceLoader.load(Injector.class)) { - - injectors.add(injector); - } - } - return injectors; - } -} Deleted: trunk/echobase-services/src/main/resources/echobase-config =================================================================== --- trunk/echobase-services/src/main/resources/echobase-config 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/resources/echobase-config 2011-11-07 10:55:42 UTC (rev 16) @@ -1,2 +0,0 @@ -application.version=${project.version} -application.site.url=${project.url} Modified: trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties =================================================================== --- trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-services/src/main/resources/i18n/echobase-services_fr_FR.properties 2011-11-07 10:55:42 UTC (rev 16) @@ -1,9 +0,0 @@ -echobase.config.data.directory.description= -echobase.config.internal.db.directory.description= -echobase.config.level0.weightedSetWeight.description= -echobase.config.parameterProfiles.storage.directory.description= -echobase.config.rf1.maximumrate.description= -echobase.config.rf1.minimumrate.description= -echobase.config.stratum.weightRatio.description= -echobase.config.treatment.working.directory.description= -echobase.user.log.directory.description= Copied: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java (from rev 14, trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/ApplicationListener.java) =================================================================== --- trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java (rev 0) +++ trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java 2011-11-07 10:55:42 UTC (rev 16) @@ -0,0 +1,255 @@ +/* + * #%L + * EchoBase :: UI + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, 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% + */ +package fr.ifremer.echobase.ui; + +import com.google.common.base.Supplier; +import fr.ifremer.echobase.EchoBaseConfiguration; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.EchoBaseTopiaRootContextSupplierFactory; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.EchoBaseUserDTO; +import fr.ifremer.echobase.entities.EchoBaseUserDTOImpl; +import fr.ifremer.echobase.entities.EchoBaseUserImpl; +import fr.ifremer.echobase.services.EchoBaseServiceContext; +import fr.ifremer.echobase.services.EchoBaseServiceContextImpl; +import fr.ifremer.echobase.services.EchoBaseServiceFactory; +import fr.ifremer.echobase.services.UserService; +import fr.ifremer.echobase.ui.actions.EchoBaseActionSupport; +import fr.ird.converter.FloatConverter; +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.beanutils.Converter; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.i18n.I18n; +import org.nuiton.i18n.init.DefaultI18nInitializer; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaUtil; +import org.nuiton.util.converter.ConverterUtil; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Properties; + +/** + * To listen start or end of the application. + * <p/> + * On start we will load the configuration and check connection to internal + * database, creates schema and create an admin user in none found in database. + * <p/> + * On stop, just release the application configuration. + * + * @author chemit <chemit@codelutin.com> + * @since 0.1 + */ +public class EchoBaseApplicationListener implements ServletContextListener { + + /** Logger. */ + protected static final Log log = + LogFactory.getLog(EchoBaseApplicationListener.class); + + private Supplier<TopiaContext> rootContextSupplier; + + @Override + public void contextInitialized(ServletContextEvent sce) { + + if (log.isInfoEnabled()) { + log.info("Application starting at " + new Date() + "..."); + } + + // init I18n + DefaultI18nInitializer i18nInitializer = + new DefaultI18nInitializer("echobase-i18n"); + i18nInitializer.setMissingKeyReturnNull(true); + I18n.init(i18nInitializer, Locale.getDefault()); + + EchoBaseApplicationContext applicationContext = new EchoBaseApplicationContext(); + sce.getServletContext().setAttribute(EchoBaseActionSupport.APPLICATION_CONTEXT_PARAMETER, applicationContext); + + // initialize configuration + EchoBaseConfiguration configuration = new EchoBaseConfiguration(); + applicationContext.setConfiguration(configuration); + + if (log.isInfoEnabled()) { + log.info("Initializing RootContextSupplier..."); + } + EchoBaseTopiaRootContextSupplierFactory factory = + new EchoBaseTopiaRootContextSupplierFactory(); + rootContextSupplier = factory.newDatabaseFromConfig(configuration); + applicationContext.setRootContextSupplier(rootContextSupplier); + + // register our not locale dependant converter + Converter converter = ConverterUtil.getConverter(Float.class); + if (converter != null) { + ConvertUtils.deregister(Float.class); + } + ConvertUtils.register(new FloatConverter(), Float.class); + + // init database (and create minimal admin user if required) + try { + boolean schemaExist = isSchemaCreated(); + if (!schemaExist) { + + updateSchema(configuration); + } + + createAdminUser(configuration); + } catch (TopiaException e) { + throw new EchoBaseTechnicalException("Could not init db", e); + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + + if (log.isInfoEnabled()) { + log.info("Application is ending at " + new Date() + "..."); + } + if (rootContextSupplier != null) { + if (log.isInfoEnabled()) { + log.info("Shuting down RootContextSupplier..."); + } + TopiaContext rootContext = rootContextSupplier.get(); + if (!rootContext.isClosed()) { + try { + rootContext.closeContext(); + } catch (TopiaException te) { + if (log.isErrorEnabled()) { + log.error("Could not close rootContext", te); + } + } + } + } + } + + protected void updateSchema(EchoBaseConfiguration configuration) throws TopiaException { + if (log.isInfoEnabled()) { + log.info("Will create or update schema for db."); + } + // must create the schema + + Properties dbConf = configuration.getProperties(); + + dbConf.put("hibernate.hbm2ddl.auto", "update"); + + EchoBaseTopiaRootContextSupplierFactory factory = + new EchoBaseTopiaRootContextSupplierFactory(); + Supplier<TopiaContext> topiaContextSupplier = + factory.newDatabaseFromProperties(dbConf); + + // start a connexion to load schema + TopiaContext tx = null; + + try { + tx = topiaContextSupplier.get().beginTransaction(); + + } finally { + + // no more update of schema... + dbConf.put("hibernate.hbm2ddl.auto", "none"); + + closeTransaction(tx); + } + } + + /** + * Creates the adminsitrator ({@code admin/admin}) on the internal + * database. + * + * @param configuration EchoBase configuration + * @throws TopiaException if could not create the user. + */ + protected void createAdminUser(EchoBaseConfiguration configuration) throws TopiaException { + + EchoBaseServiceFactory serviceFactory = + new EchoBaseServiceFactory(); + TopiaContext transaction = rootContextSupplier.get().beginTransaction(); + + try { + EchoBaseServiceContext serviceContext = new EchoBaseServiceContextImpl( + transaction, + configuration, + serviceFactory + ); + + UserService service = serviceFactory.newService(UserService.class, serviceContext); + + List<EchoBaseUser> users = service.getUsers(); + + if (CollectionUtils.isEmpty(users)) { + + // no users in database create the admin user + if (log.isInfoEnabled()) { + log.info("No user in database, will create default " + + "admin user (password admin)."); + } + + EchoBaseUserDTO userDTO = new EchoBaseUserDTOImpl(); + userDTO.setEmail("admin"); + userDTO.setPassword("admin"); + userDTO.setAdmin(true); + service.createOrUpdate(userDTO); + } + } finally { + transaction.closeContext(); + } + + } + + protected boolean isSchemaCreated() throws TopiaException { + + TopiaContextImplementor tx = + (TopiaContextImplementor) + rootContextSupplier.get(); + try { + boolean schemaFound = TopiaUtil.isSchemaExist( + tx.getHibernateConfiguration(), + EchoBaseUserImpl.class.getName() + ); + + return schemaFound; + + } finally { + closeTransaction(tx); + } + } + + /** + * Try to close the given transaction. + * + * @param tx the transaction to close + * @throws TopiaException if could not close the transaction + */ + protected void closeTransaction(TopiaContext tx) throws TopiaException { + if (tx != null && !tx.isClosed()) { + tx.closeContext(); + } + } + +} Property changes on: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/LoginAction.java =================================================================== --- trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/LoginAction.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/actions/LoginAction.java 2011-11-07 10:55:42 UTC (rev 16) @@ -120,7 +120,6 @@ // remove user from session userSession.setEchoBaseUser(null); - userSession.setObjectFactory(null); // remove echoBaseSession from application session ActionContext.getContext().getSession().remove(SESSION_PARAMETER); Modified: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/validators/LoginValidator.java =================================================================== --- trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/validators/LoginValidator.java 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/validators/LoginValidator.java 2011-11-07 10:55:42 UTC (rev 16) @@ -25,7 +25,6 @@ import com.opensymphony.xwork2.validator.ValidationException; import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.EchoBaseUserDTO; import fr.ifremer.echobase.services.UserService; /** Modified: trunk/echobase-ui/src/main/webapp/WEB-INF/web.xml =================================================================== --- trunk/echobase-ui/src/main/webapp/WEB-INF/web.xml 2011-11-07 10:46:58 UTC (rev 15) +++ trunk/echobase-ui/src/main/webapp/WEB-INF/web.xml 2011-11-07 10:55:42 UTC (rev 16) @@ -75,7 +75,7 @@ <listener> <description>Init</description> - <listener-class>fr.ifremer.echobase.ui.ApplicationListener</listener-class> + <listener-class>fr.ifremer.echobase.ui.EchoBaseApplicationListener</listener-class> </listener> <welcome-file-list>