1 package org
.argeo
.jackrabbit
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStreamReader
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Session
;
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
;
19 /** Migrate the data in a Jackrabbit repository. */
21 public class JackrabbitDataModelMigration
implements Comparable
<JackrabbitDataModelMigration
> {
22 // private final static Log log = LogFactory.getLog(JackrabbitDataModelMigration.class);
24 private String dataModelNodePath
;
25 private String targetVersion
;
26 private URL migrationCnd
;
27 private JcrCallback dataModification
;
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).
33 * @return true if a migration was performed and the repository needs to be
34 * restarted and its caches cleared.
36 public Boolean
migrate(Session session
) {
37 long begin
= System
.currentTimeMillis();
40 // check if already migrated
41 if (!session
.itemExists(dataModelNodePath
)) {
42 // log.warn("Node " + dataModelNodePath + " does not exist: nothing to migrate.");
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.");
56 // apply transitional CND
57 if (migrationCnd
!= null) {
58 reader
= new InputStreamReader(migrationCnd
.openStream());
59 CndImporter
.registerNodeTypes(reader
, session
, true);
61 // log.info("Registered migration node types from " + migrationCnd);
65 dataModification
.execute(session
);
70 long duration
= System
.currentTimeMillis() - begin
;
71 // log.info("Migration of data model " + dataModelNodePath + " to " + targetVersion + " performed in "
72 // + duration + "ms");
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
);
83 JcrUtils
.logoutQuietly(session
);
84 IOUtils
.closeQuietly(reader
);
88 protected static int compareVersions(String version1
, String version2
) {
89 // TODO do a proper version analysis and comparison
90 return version1
.compareTo(version2
);
93 /** To be called on a stopped repository. */
94 public static void clearRepositoryCaches(RepositoryConfig repositoryConfig
) {
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
) {
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);
112 // log.warn("File " + customNodeTypes + " not found.");
117 * FOR USE IN (SORTED) SETS
120 public int compareTo(JackrabbitDataModelMigration dataModelMigration
) {
121 // TODO make ordering smarter
122 if (dataModelNodePath
.equals(dataModelMigration
.dataModelNodePath
))
123 return compareVersions(targetVersion
, dataModelMigration
.targetVersion
);
125 return dataModelNodePath
.compareTo(dataModelMigration
.dataModelNodePath
);
129 public boolean equals(Object obj
) {
130 if (!(obj
instanceof JackrabbitDataModelMigration
))
132 JackrabbitDataModelMigration dataModelMigration
= (JackrabbitDataModelMigration
) obj
;
133 return dataModelNodePath
.equals(dataModelMigration
.dataModelNodePath
)
134 && targetVersion
.equals(dataModelMigration
.targetVersion
);
138 public int hashCode() {
139 return targetVersion
.hashCode();
142 public void setDataModelNodePath(String dataModelNodePath
) {
143 this.dataModelNodePath
= dataModelNodePath
;
146 public void setTargetVersion(String targetVersion
) {
147 this.targetVersion
= targetVersion
;
150 public void setMigrationCnd(URL migrationCnd
) {
151 this.migrationCnd
= migrationCnd
;
154 public void setDataModification(JcrCallback dataModification
) {
155 this.dataModification
= dataModification
;
158 public String
getDataModelNodePath() {
159 return dataModelNodePath
;
162 public String
getTargetVersion() {
163 return targetVersion
;