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