r855 - trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui
Author: tchemit Date: 2013-10-20 15:45:37 +0200 (Sun, 20 Oct 2013) New Revision: 855 Url: http://forge.codelutin.com/projects/echobase/repository/revisions/855 Log: improve init code Modified: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationContext.java trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java Modified: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationContext.java =================================================================== --- trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationContext.java 2013-10-19 13:06:51 UTC (rev 854) +++ trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationContext.java 2013-10-20 13:45:37 UTC (rev 855) @@ -27,6 +27,8 @@ import com.google.common.collect.Sets; import com.opensymphony.xwork2.ActionContext; import fr.ifremer.echobase.config.EchoBaseConfiguration; +import fr.ifremer.echobase.entities.DriverType; +import fr.ifremer.echobase.entities.EchoBaseUser; import fr.ifremer.echobase.entities.TopiaEchoBaseInternalPersistenceContext; import fr.ifremer.echobase.entities.TopiaEchoBasePersistenceContext; import fr.ifremer.echobase.entities.spatial.SpatialDataCache; @@ -35,6 +37,9 @@ import fr.ifremer.echobase.persistence.EchobaseTopiaContexts; import fr.ifremer.echobase.services.DefaultEchoBaseServiceContext; import fr.ifremer.echobase.services.EchoBaseServiceContext; +import fr.ifremer.echobase.services.service.UserService; +import fr.ifremer.echobase.services.service.embeddedapplication.EmbeddedApplicationService; +import fr.ifremer.echobase.services.service.workingDb.WorkingDbConfigurationService; import fr.ird.converter.FloatConverter; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; @@ -44,11 +49,15 @@ import org.nuiton.i18n.I18n; import org.nuiton.i18n.init.DefaultI18nInitializer; import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; import org.nuiton.util.converter.ConverterUtil; import javax.servlet.ServletContext; import java.beans.Introspector; +import java.io.File; +import java.io.IOException; import java.sql.SQLException; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -179,6 +188,24 @@ setConfiguration(configuration); setDbMeta(EchoBaseDbMeta.newDbMeta()); setInternalRootContext(internalRootContext); + + // create a service context + EchoBaseServiceContext serviceContext = + DefaultEchoBaseServiceContext.newContext( + Locale.getDefault(), + getConfiguration(), + getDbMeta(), + getSpatialDataCache()); + + // init database (and create minimal admin user if required) + initInternalDatabase(serviceContext); + + // copy drivers if required + try { + copyDriverFiles(serviceContext); + } catch (IOException e) { + throw new TopiaException("Could not install drivers", e); + } } public EchoBaseConfiguration getConfiguration() { @@ -276,4 +303,87 @@ this.configuration = configuration; } + + /** + * Init the internal database, says : + * <ul> + * <li>If no schema found or if asked to updateSchema using the + * {@code updateSchema} configuration option is on.</li> + * <li>If no user found is db, create a administrator user + * {@code admin/admin}</li> + * </ul> + */ + protected void initInternalDatabase(EchoBaseServiceContext serviceContext) throws TopiaException { + + + EchoBaseConfiguration configuration = this.getConfiguration(); + Preconditions.checkNotNull(configuration); + + EchoBaseDbMeta dbMeta = getDbMeta(); + Preconditions.checkNotNull(dbMeta); + + TopiaContext rootContext = this.getInternalRootContext(); + Preconditions.checkNotNull(rootContext); + + if (configuration.isUpdateSchema()) { + if (log.isInfoEnabled()) { + log.info("Will update schema..."); + } + rootContext.updateSchema(); + } + + TopiaContext tx = rootContext.beginTransaction(); + try { + serviceContext.setEchoBaseInternalPersistenceContext(new TopiaEchoBaseInternalPersistenceContext(tx)); + + UserService service = serviceContext.newService(UserService.class); + + 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 " + + "users."); + } + + service.createDefaultUsers(); + } + + if (configuration.isEmbedded()) { + + if (log.isInfoEnabled()) { + log.info("Will try t create default working db " + + "configuration for embedded db."); + } + // try to create a default embedded working db configuration + serviceContext.newService(WorkingDbConfigurationService.class). + createEmbeddedWorkingDbConfiguration(); + } + } finally { + serviceContext.setEchoBaseInternalPersistenceContext(null); + EchoBaseEntityHelper.closeConnection(tx); + } + } + + protected void copyDriverFiles(EchoBaseServiceContext serviceContext) throws IOException { + EmbeddedApplicationService service = + serviceContext.newService(EmbeddedApplicationService.class); + EchoBaseConfiguration configuration = serviceContext.getConfiguration(); + File libDirectory = configuration.getLibDirectory(); + for (DriverType driverType : DriverType.values()) { + String pilotFileName = driverType.getPilotFileName(configuration); + File pilotFile = new File(libDirectory, pilotFileName); + if (!pilotFile.exists()) { + + // copy it from class-path + if (log.isInfoEnabled()) { + log.info("Copy driver " + pilotFileName + + " to directory " + libDirectory); + } + service.copyEmbeddedBinaryFile(pilotFileName, libDirectory); + } + } + } } Modified: trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java =================================================================== --- trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java 2013-10-19 13:06:51 UTC (rev 854) +++ trunk/echobase-ui/src/main/java/fr/ifremer/echobase/ui/EchoBaseApplicationListener.java 2013-10-20 13:45:37 UTC (rev 855) @@ -23,32 +23,13 @@ */ package fr.ifremer.echobase.ui; -import com.google.common.base.Preconditions; -import fr.ifremer.echobase.config.EchoBaseConfiguration; -import fr.ifremer.echobase.entities.DriverType; -import fr.ifremer.echobase.entities.EchoBaseUser; -import fr.ifremer.echobase.entities.TopiaEchoBaseInternalPersistenceContext; -import fr.ifremer.echobase.persistence.EchoBaseDbMeta; -import fr.ifremer.echobase.persistence.EchoBaseEntityHelper; -import fr.ifremer.echobase.services.DefaultEchoBaseServiceContext; -import fr.ifremer.echobase.services.EchoBaseServiceContext; -import fr.ifremer.echobase.services.service.embeddedapplication.EmbeddedApplicationService; -import fr.ifremer.echobase.services.service.UserService; -import fr.ifremer.echobase.services.service.workingDb.WorkingDbConfigurationService; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaException; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; -import java.io.File; -import java.io.IOException; import java.util.Date; -import java.util.List; -import java.util.Locale; /** * To listen start or end of the application. @@ -99,25 +80,6 @@ sce.getServletContext(), applicationContext); applicationContext.init(); - - // create a service context - EchoBaseServiceContext serviceContext = - DefaultEchoBaseServiceContext.newContext( - Locale.getDefault(), - applicationContext.getConfiguration(), - applicationContext.getDbMeta(), - applicationContext.getSpatialDataCache()); - - // init database (and create minimal admin user if required) - - initInternalDatabase(applicationContext, serviceContext); - - // copy drivers if required - try { - copyDriverFiles(serviceContext); - } catch (IOException e) { - throw new TopiaException("Could not install drivers", e); - } } @Override @@ -136,113 +98,5 @@ EchoBaseApplicationContext.removeApplicationContext(servletContext); applicationContext.close(); - -// // release internal db -// TopiaContext rootContext = -// applicationContext.getInternalRootContext(); -// EchoBaseEntityHelper.releaseRootContext(rootContext); -// finally { - -// // release all user sessions -// Set<EchoBaseSession> sessions = applicationContext.getEchoBaseSessions(); -// if (CollectionUtils.isNotEmpty(sessions)) { -// for (EchoBaseSession session : sessions) { -// applicationContext.destroyEchoBaseSession(session); -// } -// } - -// // see http://wiki.apache.org/commons/Logging/FrequentlyAskedQuestions#A_memory_lea... -// ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); -// LogFactory.release(contextClassLoader); -// -// Introspector.flushCaches(); -// } } - - /** - * Init the internal database, says : - * <ul> - * <li>If no schema found or if asked to updateSchema using the - * {@code updateSchema} configuration option is on.</li> - * <li>If no user found is db, create a administrator user - * {@code admin/admin}</li> - * </ul> - * - * @param context application context where to store global internal db root context - */ - protected void initInternalDatabase(EchoBaseApplicationContext context, - EchoBaseServiceContext serviceContext) throws TopiaException { - - Preconditions.checkNotNull(context); - - EchoBaseConfiguration configuration = context.getConfiguration(); - Preconditions.checkNotNull(configuration); - - EchoBaseDbMeta dbMeta = context.getDbMeta(); - Preconditions.checkNotNull(dbMeta); - - TopiaContext rootContext = context.getInternalRootContext(); - Preconditions.checkNotNull(rootContext); - - if (configuration.isUpdateSchema()) { - if (log.isInfoEnabled()) { - log.info("Will update schema..."); - } - rootContext.updateSchema(); - } - - TopiaContext tx = rootContext.beginTransaction(); - try { - serviceContext.setEchoBaseInternalPersistenceContext(new TopiaEchoBaseInternalPersistenceContext(tx)); - - UserService service = serviceContext.newService(UserService.class); - - 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 " + - "users."); - } - - service.createDefaultUsers(); - } - - if (configuration.isEmbedded()) { - - if (log.isInfoEnabled()) { - log.info("Will try t create default working db " + - "configuration for embedded db."); - } - // try to create a default embedded working db configuration - serviceContext.newService(WorkingDbConfigurationService.class). - createEmbeddedWorkingDbConfiguration(); - } - } finally { - serviceContext.setEchoBaseInternalPersistenceContext(null); - EchoBaseEntityHelper.closeConnection(tx); - } - } - - protected void copyDriverFiles(EchoBaseServiceContext serviceContext) throws IOException { - EmbeddedApplicationService service = - serviceContext.newService(EmbeddedApplicationService.class); - EchoBaseConfiguration configuration = serviceContext.getConfiguration(); - File libDirectory = configuration.getLibDirectory(); - for (DriverType driverType : DriverType.values()) { - String pilotFileName = driverType.getPilotFileName(configuration); - File pilotFile = new File(libDirectory, pilotFileName); - if (!pilotFile.exists()) { - - // copy it from class-path - if (log.isInfoEnabled()) { - log.info("Copy driver " + pilotFileName + - " to directory " + libDirectory); - } - service.copyEmbeddedBinaryFile(pilotFileName, libDirectory); - } - } - } }
participants (1)
-
tchemit@users.forge.codelutin.com