]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.jcr/src/org/argeo/maintenance/backup/LogicalRestore.java
Move time UUID nodeid back to the factory
[lgpl/argeo-commons.git] / org.argeo.cms.jcr / src / org / argeo / maintenance / backup / LogicalRestore.java
1 package org.argeo.maintenance.backup;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.file.DirectoryStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8
9 import javax.jcr.ImportUUIDBehavior;
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.Session;
13
14 import org.argeo.api.cms.CmsConstants;
15 import org.argeo.api.cms.CmsLog;
16 import org.argeo.cms.jcr.CmsJcrUtils;
17 import org.argeo.jcr.Jcr;
18 import org.argeo.jcr.JcrException;
19 import org.argeo.jcr.JcrUtils;
20 import org.osgi.framework.BundleContext;
21
22 /** Restores a backup in the format defined by {@link LogicalBackup}. */
23 public class LogicalRestore implements Runnable {
24 private final static CmsLog log = CmsLog.getLog(LogicalRestore.class);
25
26 private final Repository repository;
27 private final BundleContext bundleContext;
28 private final Path basePath;
29
30 public LogicalRestore(BundleContext bundleContext, Repository repository, Path basePath) {
31 this.repository = repository;
32 this.basePath = basePath;
33 this.bundleContext = bundleContext;
34 }
35
36 @Override
37 public void run() {
38 Path workspaces = basePath.resolve(LogicalBackup.WORKSPACES_BASE);
39 try {
40 // import jcr:system first
41 // Session defaultSession = NodeUtils.openDataAdminSession(repository, null);
42 // try (DirectoryStream<Path> xmls = Files.newDirectoryStream(
43 // workspaces.resolve(NodeConstants.SYS_WORKSPACE + LogicalBackup.JCR_VERSION_STORAGE_PATH),
44 // "*.xml")) {
45 // for (Path xml : xmls) {
46 // try (InputStream in = Files.newInputStream(xml)) {
47 // defaultSession.getWorkspace().importXML(LogicalBackup.JCR_VERSION_STORAGE_PATH, in,
48 // ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
49 // if (log.isDebugEnabled())
50 // log.debug("Restored " + xml + " to " + defaultSession.getWorkspace().getName() + ":");
51 // }
52 // }
53 // } finally {
54 // Jcr.logout(defaultSession);
55 // }
56
57 // non-system content
58 try (DirectoryStream<Path> workspaceDirs = Files.newDirectoryStream(workspaces)) {
59 for (Path workspacePath : workspaceDirs) {
60 String workspaceName = workspacePath.getFileName().toString();
61 Session session = JcrUtils.loginOrCreateWorkspace(repository, workspaceName);
62 try (DirectoryStream<Path> xmls = Files.newDirectoryStream(workspacePath, "*.xml")) {
63 xmls: for (Path xml : xmls) {
64 if (xml.getFileName().toString().startsWith("rep:"))
65 continue xmls;
66 try (InputStream in = Files.newInputStream(xml)) {
67 session.getWorkspace().importXML("/", in,
68 ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
69 if (log.isDebugEnabled())
70 log.debug("Restored " + xml + " to workspace " + workspaceName);
71 }
72 }
73 } finally {
74 Jcr.logout(session);
75 }
76 }
77 }
78 } catch (IOException e) {
79 throw new RuntimeException("Cannot restore backup from " + basePath, e);
80 } catch (RepositoryException e) {
81 throw new JcrException("Cannot restore backup from " + basePath, e);
82 }
83 }
84
85 }