]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jackrabbit/JackrabbitDataModelMigration.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jackrabbit / JackrabbitDataModelMigration.java
1 package org.argeo.jackrabbit;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6 import java.net.URL;
7
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10
11 import org.apache.commons.io.IOUtils;
12 import org.apache.jackrabbit.commons.cnd.CndImporter;
13 import org.apache.jackrabbit.commons.cnd.ParseException;
14 import org.apache.jackrabbit.core.config.RepositoryConfig;
15 import org.argeo.jcr.JcrException;
16 import org.argeo.jcr.JcrCallback;
17 import org.argeo.jcr.JcrUtils;
18
19 /** Migrate the data in a Jackrabbit repository. */
20 @Deprecated
21 public class JackrabbitDataModelMigration implements Comparable<JackrabbitDataModelMigration> {
22 // private final static Log log = LogFactory.getLog(JackrabbitDataModelMigration.class);
23
24 private String dataModelNodePath;
25 private String targetVersion;
26 private URL migrationCnd;
27 private JcrCallback dataModification;
28
29 /**
30 * Expects an already started repository with the old data model to migrate.
31 * Expects to be run with admin rights (Repository.login() will be used).
32 *
33 * @return true if a migration was performed and the repository needs to be
34 * restarted and its caches cleared.
35 */
36 public Boolean migrate(Session session) {
37 long begin = System.currentTimeMillis();
38 Reader reader = null;
39 try {
40 // check if already migrated
41 if (!session.itemExists(dataModelNodePath)) {
42 // log.warn("Node " + dataModelNodePath + " does not exist: nothing to migrate.");
43 return false;
44 }
45 // Node dataModelNode = session.getNode(dataModelNodePath);
46 // if (dataModelNode.hasProperty(ArgeoNames.ARGEO_DATA_MODEL_VERSION)) {
47 // String currentVersion = dataModelNode.getProperty(
48 // ArgeoNames.ARGEO_DATA_MODEL_VERSION).getString();
49 // if (compareVersions(currentVersion, targetVersion) >= 0) {
50 // log.info("Data model at version " + currentVersion
51 // + ", no need to migrate.");
52 // return false;
53 // }
54 // }
55
56 // apply transitional CND
57 if (migrationCnd != null) {
58 reader = new InputStreamReader(migrationCnd.openStream());
59 CndImporter.registerNodeTypes(reader, session, true);
60 session.save();
61 // log.info("Registered migration node types from " + migrationCnd);
62 }
63
64 // modify data
65 dataModification.execute(session);
66
67 // apply changes
68 session.save();
69
70 long duration = System.currentTimeMillis() - begin;
71 // log.info("Migration of data model " + dataModelNodePath + " to " + targetVersion + " performed in "
72 // + duration + "ms");
73 return true;
74 } catch (RepositoryException e) {
75 JcrUtils.discardQuietly(session);
76 throw new JcrException(
77 "Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.", e);
78 } catch (ParseException | IOException e) {
79 JcrUtils.discardQuietly(session);
80 throw new RuntimeException(
81 "Migration of data model " + dataModelNodePath + " to " + targetVersion + " failed.", e);
82 } finally {
83 JcrUtils.logoutQuietly(session);
84 IOUtils.closeQuietly(reader);
85 }
86 }
87
88 protected static int compareVersions(String version1, String version2) {
89 // TODO do a proper version analysis and comparison
90 return version1.compareTo(version2);
91 }
92
93 /** To be called on a stopped repository. */
94 public static void clearRepositoryCaches(RepositoryConfig repositoryConfig) {
95 try {
96 String customeNodeTypesPath = "/nodetypes/custom_nodetypes.xml";
97 // FIXME causes weird error in Eclipse
98 // repositoryConfig.getFileSystem().deleteFile(customeNodeTypesPath);
99 // if (log.isDebugEnabled())
100 // log.debug("Cleared " + customeNodeTypesPath);
101 } catch (RuntimeException e) {
102 throw e;
103 }
104
105 // File customNodeTypes = new File(home.getPath()
106 // + "/repository/nodetypes/custom_nodetypes.xml");
107 // if (customNodeTypes.exists()) {
108 // customNodeTypes.delete();
109 // if (log.isDebugEnabled())
110 // log.debug("Cleared " + customNodeTypes);
111 // } else {
112 // log.warn("File " + customNodeTypes + " not found.");
113 // }
114 }
115
116 /*
117 * FOR USE IN (SORTED) SETS
118 */
119
120 public int compareTo(JackrabbitDataModelMigration dataModelMigration) {
121 // TODO make ordering smarter
122 if (dataModelNodePath.equals(dataModelMigration.dataModelNodePath))
123 return compareVersions(targetVersion, dataModelMigration.targetVersion);
124 else
125 return dataModelNodePath.compareTo(dataModelMigration.dataModelNodePath);
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (!(obj instanceof JackrabbitDataModelMigration))
131 return false;
132 JackrabbitDataModelMigration dataModelMigration = (JackrabbitDataModelMigration) obj;
133 return dataModelNodePath.equals(dataModelMigration.dataModelNodePath)
134 && targetVersion.equals(dataModelMigration.targetVersion);
135 }
136
137 @Override
138 public int hashCode() {
139 return targetVersion.hashCode();
140 }
141
142 public void setDataModelNodePath(String dataModelNodePath) {
143 this.dataModelNodePath = dataModelNodePath;
144 }
145
146 public void setTargetVersion(String targetVersion) {
147 this.targetVersion = targetVersion;
148 }
149
150 public void setMigrationCnd(URL migrationCnd) {
151 this.migrationCnd = migrationCnd;
152 }
153
154 public void setDataModification(JcrCallback dataModification) {
155 this.dataModification = dataModification;
156 }
157
158 public String getDataModelNodePath() {
159 return dataModelNodePath;
160 }
161
162 public String getTargetVersion() {
163 return targetVersion;
164 }
165
166 }