Improve logical backups.
[lgpl/argeo-commons.git] / org.argeo.maintenance / src / org / argeo / maintenance / backup / LogicalRestore.java
index b430af6e1190d82818a6f2a0f6c0e0b8bca40f69..145c5bb3dac374f0bf974bba99b2682dd5d0e744 100644 (file)
@@ -11,13 +11,17 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 import org.osgi.framework.BundleContext;
 
 /** Restores a backup in the format defined by {@link LogicalBackup}. */
 public class LogicalRestore implements Runnable {
+       private final static Log log = LogFactory.getLog(LogicalRestore.class);
+
        private final Repository repository;
        private final BundleContext bundleContext;
        private final Path basePath;
@@ -31,15 +35,48 @@ public class LogicalRestore implements Runnable {
        @Override
        public void run() {
                Path workspaces = basePath.resolve(LogicalBackup.WORKSPACES_BASE);
-               try (DirectoryStream<Path> xmls = Files.newDirectoryStream(workspaces, "*.xml")) {
-                       for (Path workspacePath : xmls) {
-                               String workspaceName = FilenameUtils.getBaseName(workspacePath.getFileName().toString());
-                               Session session = JcrUtils.loginOrCreateWorkspace(repository, workspaceName);
-                               try (InputStream in = Files.newInputStream(workspacePath)) {
-                                       session.getWorkspace().importXML("/", in,
-                                                       ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
-                               } finally {
-                                       JcrUtils.logoutQuietly(session);
+               try {
+                       // import jcr:system first
+                       try (DirectoryStream<Path> workspaceDirs = Files.newDirectoryStream(workspaces)) {
+                               dirs: for (Path workspacePath : workspaceDirs) {
+                                       String workspaceName = workspacePath.getFileName().toString();
+                                       try (DirectoryStream<Path> xmls = Files.newDirectoryStream(workspacePath, "*.xml")) {
+                                               for (Path xml : xmls) {
+                                                       if (xml.getFileName().toString().equals("jcr:system.xml")) {
+                                                               Session session = JcrUtils.loginOrCreateWorkspace(repository, workspaceName);
+                                                               try (InputStream in = Files.newInputStream(xml)) {
+                                                                       session.getWorkspace().importXML("/", in,
+                                                                                       ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
+                                                                       if (log.isDebugEnabled())
+                                                                               log.debug("Restored " + xml + " to workspace " + workspaceName);
+                                                                       break dirs;
+                                                               } finally {
+                                                                       Jcr.logout(session);
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       // non-system content
+                       try (DirectoryStream<Path> workspaceDirs = Files.newDirectoryStream(workspaces)) {
+                               for (Path workspacePath : workspaceDirs) {
+                                       String workspaceName = workspacePath.getFileName().toString();
+                                       Session session = JcrUtils.loginOrCreateWorkspace(repository, workspaceName);
+                                       try (DirectoryStream<Path> xmls = Files.newDirectoryStream(workspacePath, "*.xml")) {
+                                               xmls: for (Path xml : xmls) {
+                                                       if (xml.getFileName().toString().equals("jcr:system.xml"))
+                                                               continue xmls;
+                                                       try (InputStream in = Files.newInputStream(xml)) {
+                                                               session.getWorkspace().importXML("/", in,
+                                                                               ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
+                                                               if (log.isDebugEnabled())
+                                                                       log.debug("Restored " + xml + " to workspace " + workspaceName);
+                                                       }
+                                               }
+                                       } finally {
+                                               Jcr.logout(session);
+                                       }
                                }
                        }
                } catch (IOException e) {