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
.io
.FilenameUtils
;
15 import org
.argeo
.jcr
.JcrException
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.osgi
.framework
.BundleContext
;
19 /** Restores a backup in the format defined by {@link LogicalBackup}. */
20 public class LogicalRestore
implements Runnable
{
21 private final Repository repository
;
22 private final BundleContext bundleContext
;
23 private final Path basePath
;
25 public LogicalRestore(BundleContext bundleContext
, Repository repository
, Path basePath
) {
26 this.repository
= repository
;
27 this.basePath
= basePath
;
28 this.bundleContext
= bundleContext
;
33 Path workspaces
= basePath
.resolve(LogicalBackup
.WORKSPACES_BASE
);
34 try (DirectoryStream
<Path
> xmls
= Files
.newDirectoryStream(workspaces
, "*.xml")) {
35 for (Path workspacePath
: xmls
) {
36 String workspaceName
= FilenameUtils
.getBaseName(workspacePath
.getFileName().toString());
37 Session session
= JcrUtils
.loginOrCreateWorkspace(repository
, workspaceName
);
38 try (InputStream in
= Files
.newInputStream(workspacePath
)) {
39 session
.getWorkspace().importXML("/", in
,
40 ImportUUIDBehavior
.IMPORT_UUID_COLLISION_REPLACE_EXISTING
);
42 JcrUtils
.logoutQuietly(session
);
45 } catch (IOException e
) {
46 throw new RuntimeException("Cannot restore backup from " + basePath
, e
);
47 } catch (RepositoryException e
) {
48 throw new JcrException("Cannot restore backup from " + basePath
, e
);