This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
/
BackupOperationsImpl.java
135 lines (121 loc) · 4.9 KB
/
BackupOperationsImpl.java
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
package org.springframework.roo.addon.backup;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.process.manager.FileManager;
import org.springframework.roo.process.manager.MutableFile;
import org.springframework.roo.project.Path;
import org.springframework.roo.project.ProjectOperations;
import org.springframework.roo.support.logging.HandlerUtils;
import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.FileUtils;
import org.springframework.roo.support.util.IOUtils;
/**
* Operations for the 'backup' add-on.
*
* @author Stefan Schmidt
* @author Ben Alex
* @author Alan Stewart
* @since 1.0
*/
@Component
@Service
public class BackupOperationsImpl implements BackupOperations {
private static final Logger LOGGER = HandlerUtils
.getLogger(BackupOperationsImpl.class);
@Reference private FileManager fileManager;
@Reference private ProjectOperations projectOperations;
public String backup() {
Assert.isTrue(isBackupPossible(), "Project metadata unavailable");
// For Windows, make a date format that can legally form part of a
// filename (ROO-277)
final String pattern = File.separatorChar == '\\' ? "yyyy-MM-dd_HH.mm.ss"
: "yyyy-MM-dd_HH:mm:ss";
final DateFormat df = new SimpleDateFormat(pattern);
final long start = System.nanoTime();
ZipOutputStream zos = null;
try {
final File projectDirectory = new File(projectOperations
.getPathResolver().getFocusedIdentifier(Path.ROOT, "."));
final MutableFile file = fileManager.createFile(FileUtils
.getCanonicalPath(new File(projectDirectory,
projectOperations.getFocusedProjectName() + "_"
+ df.format(new Date()) + ".zip")));
zos = new ZipOutputStream(file.getOutputStream());
zip(projectDirectory, projectDirectory, zos);
}
catch (final FileNotFoundException e) {
LOGGER.fine("Could not determine project directory");
}
catch (final IOException e) {
LOGGER.fine("Could not create backup archive");
}
finally {
IOUtils.closeQuietly(zos);
}
final long milliseconds = (System.nanoTime() - start) / 1000000;
return "Backup completed in " + milliseconds + " ms";
}
public boolean isBackupPossible() {
return projectOperations.isFocusedProjectAvailable();
}
private void zip(final File directory, final File base,
final ZipOutputStream zos) throws IOException {
final File[] files = directory.listFiles(new FilenameFilter() {
public boolean accept(final File dir, final String name) {
// Don't use this directory if it's "target" under base
if (dir.equals(base) && name.equals("target")) {
return false;
}
// Skip existing backup files
if (dir.equals(base) && name.endsWith(".zip")) {
return false;
}
// Skip files that start with "."
return !name.startsWith(".");
}
});
final byte[] buffer = new byte[8192];
int read = 0;
for (final File file : files) {
if (file.isDirectory()) {
if (file.listFiles().length == 0) {
final ZipEntry dirEntry = new ZipEntry(file.getPath()
.substring(base.getPath().length() + 1)
+ File.separatorChar);
zos.putNextEntry(dirEntry);
}
zip(file, base, zos);
}
else {
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(
file));
final ZipEntry entry = new ZipEntry(file.getPath()
.substring(base.getPath().length() + 1));
zos.putNextEntry(entry);
while ((read = inputStream.read(buffer)) != -1) {
zos.write(buffer, 0, read);
}
}
finally {
IOUtils.closeQuietly(inputStream);
}
}
}
}
}