Move various CMS extensions to Argeo SLC.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jackrabbit / JackrabbitDataModelMigration.java
index 53f0e4457837cf85820d46d4964d3f47ef207ce7..838446d5cb355fe3c491b54d36f340dfcb0eb42d 100644 (file)
@@ -1,46 +1,29 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * 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.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.net.URL;
 
-import javax.jcr.Node;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 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.commons.cnd.ParseException;
 import org.apache.jackrabbit.core.config.RepositoryConfig;
-import org.argeo.jcr.ArgeoJcrException;
-import org.argeo.jcr.ArgeoNames;
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrCallback;
 import org.argeo.jcr.JcrUtils;
-import org.springframework.core.io.Resource;
 
 /** Migrate the data in a Jackrabbit repository. */
-public class JackrabbitDataModelMigration implements
-               Comparable<JackrabbitDataModelMigration> {
-       private final static Log log = LogFactory
-                       .getLog(JackrabbitDataModelMigration.class);
+@Deprecated
+public class JackrabbitDataModelMigration implements Comparable<JackrabbitDataModelMigration> {
+//     private final static Log log = LogFactory.getLog(JackrabbitDataModelMigration.class);
 
        private String dataModelNodePath;
        private String targetVersion;
-       private Resource migrationCnd;
+       private URL migrationCnd;
        private JcrCallback dataModification;
 
        /**
@@ -56,27 +39,26 @@ public class JackrabbitDataModelMigration implements
                try {
                        // check if already migrated
                        if (!session.itemExists(dataModelNodePath)) {
-                               log.warn("Node " + dataModelNodePath
-                                               + " does not exist: nothing to migrate.");
+//                             log.warn("Node " + dataModelNodePath + " does not exist: nothing to migrate.");
                                return false;
                        }
-                       Node dataModelNode = session.getNode(dataModelNodePath);
-                       if (dataModelNode.hasProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION)) {
-                               String currentVersion = dataModelNode.getProperty(
-                                               ArgeoNames.ARGEO_DATA_MODEL_VERSION).getString();
-                               if (compareVersions(currentVersion, targetVersion) >= 0) {
-                                       log.info("Data model at version " + currentVersion
-                                                       + ", no need to migrate.");
-                                       return false;
-                               }
-                       }
+//                     Node dataModelNode = session.getNode(dataModelNodePath);
+//                     if (dataModelNode.hasProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION)) {
+//                             String currentVersion = dataModelNode.getProperty(
+//                                             ArgeoNames.ARGEO_DATA_MODEL_VERSION).getString();
+//                             if (compareVersions(currentVersion, targetVersion) >= 0) {
+//                                     log.info("Data model at version " + currentVersion
+//                                                     + ", no need to migrate.");
+//                                     return false;
+//                             }
+//                     }
 
                        // apply transitional CND
                        if (migrationCnd != null) {
-                               reader = new InputStreamReader(migrationCnd.getInputStream());
+                               reader = new InputStreamReader(migrationCnd.openStream());
                                CndImporter.registerNodeTypes(reader, session, true);
                                session.save();
-                               log.info("Registered migration node types from " + migrationCnd);
+//                             log.info("Registered migration node types from " + migrationCnd);
                        }
 
                        // modify data
@@ -86,14 +68,17 @@ public class JackrabbitDataModelMigration implements
                        session.save();
 
                        long duration = System.currentTimeMillis() - begin;
-                       log.info("Migration of data model " + dataModelNodePath + " to "
-                                       + targetVersion + " performed in " + duration + "ms");
+//                     log.info("Migration of data model " + dataModelNodePath + " to " + targetVersion + " performed in "
+//                                     + duration + "ms");
                        return true;
-               } catch (Exception e) {
+               } catch (RepositoryException e) {
+                       JcrUtils.discardQuietly(session);
+                       throw new JcrException(
+                                       "Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.", e);
+               } catch (ParseException | IOException e) {
                        JcrUtils.discardQuietly(session);
-                       throw new ArgeoJcrException("Migration of data model "
-                                       + dataModelNodePath + " to " + targetVersion + " failed.",
-                                       e);
+                       throw new RuntimeException(
+                                       "Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.", e);
                } finally {
                        JcrUtils.logoutQuietly(session);
                        IOUtils.closeQuietly(reader);
@@ -110,11 +95,11 @@ public class JackrabbitDataModelMigration implements
                try {
                        String customeNodeTypesPath = "/nodetypes/custom_nodetypes.xml";
                        // FIXME causes weird error in Eclipse
-                       //repositoryConfig.getFileSystem().deleteFile(customeNodeTypesPath);
-                       if (log.isDebugEnabled())
-                               log.debug("Cleared " + customeNodeTypesPath);
-               } catch (Exception e) {
-                       throw new ArgeoJcrException("Cannot clear caches", e);
+//                      repositoryConfig.getFileSystem().deleteFile(customeNodeTypesPath);
+//                     if (log.isDebugEnabled())
+//                             log.debug("Cleared " + customeNodeTypesPath);
+               } catch (RuntimeException e) {
+                       throw e;
                }
 
                // File customNodeTypes = new File(home.getPath()
@@ -135,11 +120,9 @@ public class JackrabbitDataModelMigration implements
        public int compareTo(JackrabbitDataModelMigration dataModelMigration) {
                // TODO make ordering smarter
                if (dataModelNodePath.equals(dataModelMigration.dataModelNodePath))
-                       return compareVersions(targetVersion,
-                                       dataModelMigration.targetVersion);
+                       return compareVersions(targetVersion, dataModelMigration.targetVersion);
                else
-                       return dataModelNodePath
-                                       .compareTo(dataModelMigration.dataModelNodePath);
+                       return dataModelNodePath.compareTo(dataModelMigration.dataModelNodePath);
        }
 
        @Override
@@ -164,7 +147,7 @@ public class JackrabbitDataModelMigration implements
                this.targetVersion = targetVersion;
        }
 
-       public void setMigrationCnd(Resource migrationCnd) {
+       public void setMigrationCnd(URL migrationCnd) {
                this.migrationCnd = migrationCnd;
        }