+ createJackrabbitRepository();
+
+ // migrate if needed
+ migrate();
+
+ // apply new CND files after migration
+ if (cndFiles != null && cndFiles.size() > 0)
+ importNodeTypeDefinitions(repository);
+ }
+
+ /** Actually creates a new repository. */
+ protected void createJackrabbitRepository() {
+ long begin = System.currentTimeMillis();
+ try {
+ // remote repository
+ if (uri != null && !uri.trim().equals("")) {
+ Map<String, String> params = new HashMap<String, String>();
+ params.put(
+ org.apache.jackrabbit.commons.JcrUtils.REPOSITORY_URI,
+ uri);
+ repository = new Jcr2davRepositoryFactory()
+ .getRepository(params);
+ if (repository == null)
+ throw new ArgeoException("Remote Davex repository " + uri
+ + " not found");
+ log.info("Initialized Jackrabbit repository " + repository
+ + " from URI " + uri);
+ // do not perform further initialization since we assume that
+ // the
+ // remote repository has been properly configured
+ return;
+ }
+
+ // local repository
+ if (inMemory && getHomeDirectory().exists()) {
+ FileUtils.deleteDirectory(getHomeDirectory());
+ log.warn("Deleted Jackrabbit home directory "
+ + getHomeDirectory());
+ }
+
+ Properties vars = getConfigurationProperties();
+ InputStream in = configuration.getInputStream();
+ try {
+ vars.put(
+ RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
+ getHomeDirectory().getCanonicalPath());
+ repositoryConfig = RepositoryConfig.create(new InputSource(in),
+ vars);
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot read configuration", e);
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+
+ if (inMemory)
+ repository = new TransientRepository(repositoryConfig);
+ else
+ repository = RepositoryImpl.create(repositoryConfig);
+
+ double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
+ log.info("Initialized Jackrabbit repository in " + duration
+ + " s, home: " + getHomeDirectory() + ", config: "
+ + configuration);
+ } catch (Exception e) {
+ throw new ArgeoException("Cannot create Jackrabbit repository "
+ + getHomeDirectory(), e);
+ }
+ }
+
+ /** Executes migrations, if needed. */
+ protected void migrate() {
+ // No migration to perform
+ if (dataModelMigrations.size() == 0)
+ return;
+
+ Boolean restartAndClearCaches = false;
+
+ // migrate data
+ Session session = null;
+ try {
+ session = login();
+ for (JackrabbitDataModelMigration dataModelMigration : new TreeSet<JackrabbitDataModelMigration>(
+ dataModelMigrations)) {
+ if (dataModelMigration.migrate(session)) {
+ restartAndClearCaches = true;
+ }
+ }
+ } catch (ArgeoException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new ArgeoException("Cannot migrate", e);
+ } finally {
+ JcrUtils.logoutQuietly(session);