+ throw new IllegalStateException("Deployment is already available");
+ }
+
+ // home
+ prepareDataModel(NodeConstants.NODE_REPOSITORY, deployedNodeRepository, publishAsLocalRepo);
+
+ // init from repository
+ Collection<ServiceReference<Repository>> initRepositorySr;
+ try {
+ initRepositorySr = bc.getServiceReferences(Repository.class,
+ "(" + NodeConstants.CN + "=" + NodeConstants.NODE_INIT + ")");
+ } catch (InvalidSyntaxException e1) {
+ throw new IllegalArgumentException(e1);
+ }
+ Iterator<ServiceReference<Repository>> it = initRepositorySr.iterator();
+ while (it.hasNext()) {
+ ServiceReference<Repository> sr = it.next();
+ Object labeledUri = sr.getProperties().get(LdapAttrs.labeledURI.name());
+ Repository initRepository = bc.getService(sr);
+ if (log.isDebugEnabled())
+ log.debug("Found init repository " + labeledUri + ", copying it...");
+ initFromRepository(deployedNodeRepository, initRepository);
+ log.info("Node repository initialised from " + labeledUri);
+ }
+ }
+
+ /** Init from a (typically remote) repository. */
+ private void initFromRepository(Repository deployedNodeRepository, Repository initRepository) {
+ Session initSession = null;
+ try {
+ initSession = initRepository.login();
+ workspaces: for (String workspaceName : initSession.getWorkspace().getAccessibleWorkspaceNames()) {
+ if ("security".equals(workspaceName))
+ continue workspaces;
+ if (log.isDebugEnabled())
+ log.debug("Copying workspace " + workspaceName + " from init repository...");
+ long begin = System.currentTimeMillis();
+ Session targetSession = null;
+ Session sourceSession = null;
+ try {
+ try {
+ targetSession = NodeUtils.openDataAdminSession(deployedNodeRepository, workspaceName);
+ } catch (IllegalArgumentException e) {// no such workspace
+ Session adminSession = NodeUtils.openDataAdminSession(deployedNodeRepository, null);
+ try {
+ adminSession.getWorkspace().createWorkspace(workspaceName);
+ } finally {
+ Jcr.logout(adminSession);
+ }
+ targetSession = NodeUtils.openDataAdminSession(deployedNodeRepository, workspaceName);
+ }
+ sourceSession = initRepository.login(workspaceName);
+// JcrUtils.copyWorkspaceXml(sourceSession, targetSession);
+ // TODO deal with referenceable nodes
+ JcrUtils.copy(sourceSession.getRootNode(), targetSession.getRootNode());
+ targetSession.save();
+ long duration = System.currentTimeMillis() - begin;
+ if (log.isDebugEnabled())
+ log.debug("Copied workspace " + workspaceName + " from init repository in " + (duration / 1000)
+ + " s");
+ } catch (Exception e) {
+ log.error("Cannot copy workspace " + workspaceName + " from init repository.", e);
+ } finally {
+ Jcr.logout(sourceSession);
+ Jcr.logout(targetSession);
+ }
+ }
+ } catch (RepositoryException e) {
+ throw new JcrException(e);
+ } finally {
+ Jcr.logout(initSession);