FileStoreBackupServiceImpl.java
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.crx.CRXRepository
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.ReferencePolicyOption
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.oak.plugins.backup.FileStoreRestore
* org.apache.jackrabbit.oak.spi.state.NodeStore
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.core.backup;
import com.day.crx.CRXRepository;
import com.day.crx.core.backup.FileStoreBackupService;
import com.day.crx.core.backup.crx.OakBackup;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.ReferencePolicyOption;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.oak.plugins.backup.FileStoreRestore;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={FileStoreBackupService.class})
public class FileStoreBackupServiceImpl
implements FileStoreBackupService {
private static final Logger log = LoggerFactory.getLogger(FileStoreBackupServiceImpl.class);
@Reference(policy=ReferencePolicy.STATIC, policyOption=ReferencePolicyOption.GREEDY)
private NodeStore store;
@Reference(policy=ReferencePolicy.STATIC, policyOption=ReferencePolicyOption.GREEDY)
private CRXRepository repository;
private int delay = 10;
private OakBackup currentBackup;
@Override
public Boolean getBackupInProgress() {
return this.isBackupInProgress();
}
synchronized boolean isBackupInProgress() {
return this.currentBackup != null && !this.currentBackup.isFinished();
}
@Override
public Integer getBackupProgress() {
if (this.isBackupInProgress()) {
return this.currentBackup.getProgress();
}
return 0;
}
@Override
public String getCurrentBackupTarget() {
if (this.isBackupInProgress()) {
return this.currentBackup.getZipFile().getAbsolutePath();
}
return null;
}
@Override
public Boolean getBackupWasSuccessful() {
return this.currentBackup == null || this.currentBackup.getException() == null;
}
@Override
public String getBackupResult() {
return this.currentBackup == null ? "No backup excecuted so far." : this.currentBackup.getStatusMessage();
}
@Override
public void startBackup(String target) throws RepositoryException {
this.startBackup(null, target);
}
@Override
public synchronized void startBackup(String install, String target) throws RepositoryException {
if (this.isBackupInProgress()) {
return;
}
File installDir = null;
if (install == null || install.length() == 0) {
installDir = this.repository.getHomeDir().getParentFile().getParentFile();
} else {
try {
installDir = new File(install).getCanonicalFile();
}
catch (IOException e) {
throw new RepositoryException("Unexpected repository directory: " + install, (Throwable)e);
}
}
this.currentBackup = this.newBackup(installDir, target);
}
private OakBackup newBackup(File installDir, String target) {
OakBackup b = new OakBackup(installDir, FileStoreBackupServiceImpl.targetToFile(target, installDir), this.store, this.repository);
b.setDelay(this.delay);
b.start();
return b;
}
private static File targetToFile(String target, File installDir) {
File targetFile;
String targetFileName = target;
if (targetFileName == null || targetFileName.length() == 0) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmm");
targetFileName = "backup-" + df.format(new Date()) + ".zip";
}
if (!(targetFile = new File(targetFileName)).isAbsolute()) {
File targetDir = installDir.getParentFile();
targetFile = new File(targetDir, targetFileName);
}
return targetFile;
}
@Override
public void cancelBackup() {
if (this.isBackupInProgress()) {
try {
this.currentBackup.cancel();
}
catch (Exception e) {
log.warn("Problem cancelling the backup process", (Throwable)e);
}
}
}
@Override
public Integer getBackupDelay() {
return this.delay;
}
@Override
public void setBackupDelay(Integer delay) {
this.delay = delay;
}
@Override
public void nodeStoreRestore(String sourceDirectory) throws RepositoryException {
try {
long t = System.currentTimeMillis();
FileStoreRestore.restore((File)new File(sourceDirectory), (NodeStore)this.store);
log.info("Restore from {} finished in {} ms.", (Object)sourceDirectory, (Object)(System.currentTimeMillis() - t));
}
catch (Exception e) {
throw new RepositoryException(e.getMessage(), (Throwable)e);
}
}
@Override
public String checkpoint(long lifetime) {
return this.store.checkpoint(lifetime);
}
protected void bindStore(NodeStore nodeStore) {
this.store = nodeStore;
}
protected void unbindStore(NodeStore nodeStore) {
if (this.store == nodeStore) {
this.store = null;
}
}
protected void bindRepository(CRXRepository cRXRepository) {
this.repository = cRXRepository;
}
protected void unbindRepository(CRXRepository cRXRepository) {
if (this.repository == cRXRepository) {
this.repository = null;
}
}
}