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
.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
;
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);
26 private final Repository repository
;
27 private final BundleContext bundleContext
;
28 private final Path basePath
;
30 public LogicalRestore(BundleContext bundleContext
, Repository repository
, Path basePath
) {
31 this.repository
= repository
;
32 this.basePath
= basePath
;
33 this.bundleContext
= bundleContext
;
38 Path workspaces
= basePath
.resolve(LogicalBackup
.WORKSPACES_BASE
);
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),
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() + ":");
54 // Jcr.logout(defaultSession);
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:"))
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
);
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
);