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