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