]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitDataModelMigration.java
Update license headers
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitDataModelMigration.java
index 37d852907adb741d49ea0ff44222ada2c070f75a..61e05e54966e5c17980052553ad8f18fceda666e 100644 (file)
@@ -1,6 +1,20 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.jackrabbit;
 
-import java.io.File;
 import java.io.InputStreamReader;
 import java.io.Reader;
 
@@ -11,6 +25,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.commons.cnd.CndImporter;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
 import org.argeo.ArgeoException;
 import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.JcrCallback;
@@ -35,17 +50,17 @@ public class JackrabbitDataModelMigration implements
         * @return true if a migration was performed and the repository needs to be
         *         restarted and its caches cleared.
         */
-       public Boolean migrate(Session adminSession) {
+       public Boolean migrate(Session session) {
                long begin = System.currentTimeMillis();
                Reader reader = null;
                try {
                        // check if already migrated
-                       if (!adminSession.itemExists(dataModelNodePath)) {
+                       if (!session.itemExists(dataModelNodePath)) {
                                log.warn("Node " + dataModelNodePath
                                                + " does not exist: nothing to migrate.");
                                return false;
                        }
-                       Node dataModelNode = adminSession.getNode(dataModelNodePath);
+                       Node dataModelNode = session.getNode(dataModelNodePath);
                        if (dataModelNode.hasProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION)) {
                                String currentVersion = dataModelNode.getProperty(
                                                ArgeoNames.ARGEO_DATA_MODEL_VERSION).getString();
@@ -57,30 +72,30 @@ public class JackrabbitDataModelMigration implements
                        }
 
                        // apply transitional CND
-                       reader = new InputStreamReader(migrationCnd.getInputStream());
-                       CndImporter.registerNodeTypes(reader, adminSession, true);
+                       if (migrationCnd != null) {
+                               reader = new InputStreamReader(migrationCnd.getInputStream());
+                               CndImporter.registerNodeTypes(reader, session, true);
+                               session.save();
+                               log.info("Registered migration node types from " + migrationCnd);
+                       }
 
                        // modify data
-                       dataModification.execute(adminSession);
-
-                       // set data model version
-                       dataModelNode.setProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION,
-                                       targetVersion);
+                       dataModification.execute(session);
 
                        // apply changes
-                       adminSession.save();
+                       session.save();
 
                        long duration = System.currentTimeMillis() - begin;
                        log.info("Migration of data model " + dataModelNodePath + " to "
                                        + targetVersion + " performed in " + duration + "ms");
                        return true;
                } catch (Exception e) {
-                       JcrUtils.discardQuietly(adminSession);
+                       JcrUtils.discardQuietly(session);
                        throw new ArgeoException("Migration of data model "
                                        + dataModelNodePath + " to " + targetVersion + " failed.",
                                        e);
                } finally {
-                       JcrUtils.logoutQuietly(adminSession);
+                       JcrUtils.logoutQuietly(session);
                        IOUtils.closeQuietly(reader);
                }
        }
@@ -91,16 +106,25 @@ public class JackrabbitDataModelMigration implements
        }
 
        /** To be called on a stopped repository. */
-       public static void clearRepositoryCaches(File home) {
-               File customNodeTypes = new File(home.getPath()
-                               + "/repository/nodetypes/custom_nodetypes.xml");
-               if (customNodeTypes.exists()) {
-                       customNodeTypes.delete();
+       public static void clearRepositoryCaches(RepositoryConfig repositoryConfig) {
+               try {
+                       String customeNodeTypesPath = "/nodetypes/custom_nodetypes.xml";
+                       repositoryConfig.getFileSystem().deleteFile(customeNodeTypesPath);
                        if (log.isDebugEnabled())
-                               log.debug("Cleared " + customNodeTypes);
-               } else {
-                       log.warn("File " + customNodeTypes + " not found.");
+                               log.debug("Cleared " + customeNodeTypesPath);
+               } catch (Exception e) {
+                       throw new ArgeoException("Cannot clear caches", e);
                }
+
+               // File customNodeTypes = new File(home.getPath()
+               // + "/repository/nodetypes/custom_nodetypes.xml");
+               // if (customNodeTypes.exists()) {
+               // customNodeTypes.delete();
+               // if (log.isDebugEnabled())
+               // log.debug("Cleared " + customNodeTypes);
+               // } else {
+               // log.warn("File " + customNodeTypes + " not found.");
+               // }
        }
 
        /*
@@ -147,4 +171,12 @@ public class JackrabbitDataModelMigration implements
                this.dataModification = dataModification;
        }
 
+       public String getDataModelNodePath() {
+               return dataModelNodePath;
+       }
+
+       public String getTargetVersion() {
+               return targetVersion;
+       }
+
 }