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
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.api
.NodeConstants
;
17 import org
.argeo
.api
.NodeUtils
;
18 import org
.argeo
.jcr
.Jcr
;
19 import org
.argeo
.jcr
.JcrException
;
20 import org
.argeo
.jcr
.JcrUtils
;
21 import org
.osgi
.framework
.BundleContext
;
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);
27 private final Repository repository
;
28 private final BundleContext bundleContext
;
29 private final Path basePath
;
31 public LogicalRestore(BundleContext bundleContext
, Repository repository
, Path basePath
) {
32 this.repository
= repository
;
33 this.basePath
= basePath
;
34 this.bundleContext
= bundleContext
;
39 Path workspaces
= basePath
.resolve(LogicalBackup
.WORKSPACES_BASE
);
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),
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() + ":");
55 // Jcr.logout(defaultSession);
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:"))
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
);
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
);