]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java
Improve RAP deployment in Tomcat
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitContainer.java
index 942313f8a5b68d3b0dba896629ddf3c248469dab..294fdd5a3ec6d01f667bb1e360f73a7d868e6ca0 100644 (file)
@@ -18,6 +18,7 @@ package org.argeo.jackrabbit;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -55,6 +56,7 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.ResourceLoaderAware;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.SystemPropertyUtils;
 import org.xml.sax.InputSource;
 
 /**
@@ -87,7 +89,31 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
        private Executor systemExecutor;
        private Credentials adminCredentials;
 
+       // transition from legacy spring approach
+       private Boolean alreadyInitialized = false;
+       private Boolean alreadyDisposed = false;
+
+       /** @deprecated explicitly declare {@link #init()} as init-method instead. */
        public void afterPropertiesSet() throws Exception {
+               if (!alreadyInitialized) {
+                       log.warn("## If not already done,"
+                                       + " declare init-method=\"init\".");
+                       initImpl();
+               }
+       }
+
+       public void init() throws Exception {
+               initImpl();
+               alreadyInitialized = true;
+       }
+
+       protected void initImpl() throws Exception {
+               if (repository != null) {
+                       // we are just wrapping another repository
+                       importNodeTypeDefinitions(repository);
+                       return;
+               }
+
                // remote repository
                if (uri != null && !uri.trim().equals("")) {
                        Map<String, String> params = new HashMap<String, String>();
@@ -111,16 +137,9 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                }
 
                RepositoryConfig config;
+               Properties vars = getConfigurationProperties();
                InputStream in = configuration.getInputStream();
-               InputStream propsIn = null;
                try {
-                       Properties vars = new Properties();
-                       if (variables != null) {
-                               propsIn = variables.getInputStream();
-                               vars.load(propsIn);
-                       }
-                       // override with system properties
-                       vars.putAll(System.getProperties());
                        vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
                                        homeDirectory.getCanonicalPath());
                        config = RepositoryConfig.create(new InputSource(in), vars);
@@ -128,7 +147,6 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                        throw new RuntimeException("Cannot read configuration", e);
                } finally {
                        IOUtils.closeQuietly(in);
-                       IOUtils.closeQuietly(propsIn);
                }
 
                if (inMemory)
@@ -143,21 +161,91 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                                + homeDirectory + " with config " + configuration);
        }
 
+       /**
+        * @deprecated explicitly declare {@link #dispose()} as destroy-method
+        *             instead.
+        */
+       public void destroy() throws Exception {
+               if (!alreadyDisposed) {
+                       log.warn("## If not already done,"
+                                       + " declare destroy-method=\"dispose\".");
+                       disposeImpl();
+               }
+       }
+
+       public void dispose() throws Exception {
+               disposeImpl();
+               alreadyDisposed = true;
+       }
+
+       protected void disposeImpl() throws Exception {
+               if (repository != null) {
+                       if (repository instanceof JackrabbitRepository)
+                               ((JackrabbitRepository) repository).shutdown();
+                       else if (repository instanceof RepositoryImpl)
+                               ((RepositoryImpl) repository).shutdown();
+                       else if (repository instanceof TransientRepository)
+                               ((TransientRepository) repository).shutdown();
+               }
+
+               if (inMemory)
+                       if (homeDirectory.exists()) {
+                               FileUtils.deleteDirectory(homeDirectory);
+                               if (log.isDebugEnabled())
+                                       log.debug("Deleted Jackrabbit home directory "
+                                                       + homeDirectory);
+                       }
+
+               if (uri != null && !uri.trim().equals(""))
+                       log.info("Destroyed Jackrabbit repository with uri " + uri);
+               else
+                       log.info("Destroyed Jackrabbit repository " + repository + " in "
+                                       + homeDirectory + " with config " + configuration);
+       }
+
+       protected Properties getConfigurationProperties() {
+               InputStream propsIn = null;
+               Properties vars;
+               try {
+                       vars = new Properties();
+                       if (variables != null) {
+                               propsIn = variables.getInputStream();
+                               vars.load(propsIn);
+                       }
+                       // resolve system properties
+                       for (Object key : vars.keySet()) {
+                               // TODO: implement a smarter mechanism to resolve nested ${}
+                               String newValue = SystemPropertyUtils.resolvePlaceholders(vars
+                                               .getProperty(key.toString()));
+                               vars.put(key, newValue);
+                       }
+                       // override with system properties
+                       vars.putAll(System.getProperties());
+               } catch (IOException e) {
+                       throw new ArgeoException("Cannot read configuration properties", e);
+               } finally {
+                       IOUtils.closeQuietly(propsIn);
+               }
+               return vars;
+       }
+
        /**
         * Import declared node type definitions, trying to update them if they have
         * changed. In case of failures an error will be logged but no exception
         * will be thrown.
         */
        protected void importNodeTypeDefinitions(final Repository repository) {
-               final Credentials credentialsToUse;
-               if (systemExecutor == null) {
-                       if (adminCredentials == null)
-                               throw new ArgeoException(
-                                               "No system executor or admin credentials found");
-                       credentialsToUse = adminCredentials;
-               } else {
-                       credentialsToUse = null;
-               }
+               final Credentials credentialsToUse = null;
+               // if (systemExecutor == null) {
+               // if (adminCredentials == null) {
+               // log.error("No system executor or admin credentials found,"
+               // + " cannot import node types");
+               // return;
+               // }
+               // credentialsToUse = adminCredentials;
+               // } else {
+               // credentialsToUse = null;
+               // }
 
                Runnable action = new Runnable() {
                        public void run() {
@@ -192,31 +280,6 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                else
                        action.run();
        }
-       
-       public void destroy() throws Exception {
-               if (repository != null) {
-                       if (repository instanceof JackrabbitRepository)
-                               ((JackrabbitRepository) repository).shutdown();
-                       else if (repository instanceof RepositoryImpl)
-                               ((RepositoryImpl) repository).shutdown();
-                       else if (repository instanceof TransientRepository)
-                               ((TransientRepository) repository).shutdown();
-               }
-
-               if (inMemory)
-                       if (homeDirectory.exists()) {
-                               FileUtils.deleteDirectory(homeDirectory);
-                               if (log.isDebugEnabled())
-                                       log.debug("Deleted Jackrabbit home directory "
-                                                       + homeDirectory);
-                       }
-
-               if (uri != null && !uri.trim().equals(""))
-                       log.info("Destroyed Jackrabbit repository with uri " + uri);
-               else
-                       log.info("Destroyed Jackrabbit repository " + repository + " in "
-                                       + homeDirectory + " with config " + configuration);
-       }
 
        // JCR REPOSITORY (delegated)
        public String getDescriptor(String key) {
@@ -275,7 +338,6 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                try {
                        NamespaceHelper namespaceHelper = new NamespaceHelper(session);
                        namespaceHelper.registerNamespaces(namespaces);
-
                } catch (Exception e) {
                        throw new ArgeoException("Cannot process new session", e);
                }
@@ -352,4 +414,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                this.adminCredentials = adminCredentials;
        }
 
+       public void setRepository(Repository repository) {
+               this.repository = repository;
+       }
+
 }