1 package org
.argeo
.maintenance
.backup
;
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
;
9 import javax
.jcr
.ImportUUIDBehavior
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
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
;
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);
23 private final Repository repository
;
24 // private final BundleContext bundleContext;
25 private final Path basePath
;
27 public LogicalRestore(Repository repository
, Path basePath
) {
28 this.repository
= repository
;
29 this.basePath
= basePath
;
30 // this.bundleContext = bundleContext;
35 Path workspaces
= basePath
.resolve(LogicalBackup
.WORKSPACES_BASE
);
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),
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() + ":");
51 // Jcr.logout(defaultSession);
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:"))
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
);
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
);