/
UnzipArchiveToOutputFolder.java
179 lines (154 loc) · 6.81 KB
/
UnzipArchiveToOutputFolder.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
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
package org.jboss.windup.rules.apps.java.scan.operation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.operation.ruleelement.AbstractIterationOperation;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.dao.FileModelService;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.WindupConfigurationModel;
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.graph.service.GraphService;
import org.jboss.windup.util.ZipUtil;
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnzipArchiveToOutputFolder extends AbstractIterationOperation<ArchiveModel>
{
private static final String ARCHIVES = "archives";
private static final Logger LOG = LoggerFactory.getLogger(UnzipArchiveToOutputFolder.class);
public UnzipArchiveToOutputFolder(String variableName)
{
super(variableName);
}
public UnzipArchiveToOutputFolder()
{
super();
}
public static UnzipArchiveToOutputFolder unzip(String variableName)
{
return new UnzipArchiveToOutputFolder(variableName);
}
public static UnzipArchiveToOutputFolder unzip()
{
return new UnzipArchiveToOutputFolder();
}
@Override
public void perform(GraphRewrite event, EvaluationContext context, ArchiveModel payload)
{
LOG.info("Unzipping archive: " + payload.toPrettyString());
File zipFile = payload.asFile();
if (!zipFile.isFile())
{
final String msg = "Input path doesn't point to a file: " + zipFile.getAbsolutePath();
throw new WindupException(msg);
}
// create a temp folder for all archive contents
WindupConfigurationModel cfg = GraphService.getConfigurationModel(event.getGraphContext());
String windupOutputFolder = cfg.getOutputPath().getFilePath();
Path windupTempUnzippedArchiveFolder = Paths.get(windupOutputFolder, ARCHIVES);
if (!Files.isDirectory(windupTempUnzippedArchiveFolder))
{
try
{
Files.createDirectories(windupTempUnzippedArchiveFolder);
}
catch (IOException e)
{
throw new WindupException("Failed to create temporary folder: " + windupTempUnzippedArchiveFolder
+ " due to: " + e.getMessage(), e);
}
}
unzipToTempDirectory(event.getGraphContext(), windupTempUnzippedArchiveFolder, zipFile, payload);
}
private Path getAppArchiveFolder(Path tempFolder, String appArchiveName)
{
Path appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName);
int fileIdx = 1;
// if it is already created, try another folder name
while (Files.exists(appArchiveFolder))
{
appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName + "." + fileIdx);
fileIdx++;
}
return appArchiveFolder;
}
private void unzipToTempDirectory(final GraphContext context, final Path tempFolder,
final File inputZipFile,
final ArchiveModel archiveModel)
{
final FileModelService fileService = new FileModelService(context);
// Setup a temp folder for the archive
String appArchiveName = archiveModel.getArchiveName();
final Path appArchiveFolder = getAppArchiveFolder(tempFolder, appArchiveName);
try
{
Files.createDirectories(appArchiveFolder);
}
catch (IOException e)
{
throw new WindupException("Could not create a temporary directory for application \""
+ appArchiveName + "\" at \"" + appArchiveFolder.toString() + "\" due to: " + e.getMessage(), e);
}
// unzip to the temp folder
LOG.info("Unzipping " + inputZipFile.getPath() + " to " + appArchiveFolder.toString());
try
{
ZipUtil.unzipToFolder(inputZipFile, appArchiveFolder.toFile());
}
catch (Throwable e)
{
throw new WindupException("Unzipping archive: " + inputZipFile.getAbsolutePath() + " failed due to: "
+ e.getMessage(), e);
}
FileModel newFileModel = fileService.createByFilePath(appArchiveFolder.toString());
// mark the path to the archive
archiveModel.setUnzippedDirectory(newFileModel);
newFileModel.setParentArchive(archiveModel);
// add all unzipped files, and make sure their parent archive is set
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, newFileModel);
}
/**
* Recurses the given folder and adds references to these files to the graph as FileModels.
*
* We don't set the parent file model in the case of the inital children, as the direct parent is really the archive
* itself. For example for file "root.zip/pom.xml" - the parent for pom.xml is root.zip, not the directory temporary
* directory that happens to hold it.
*/
private void recurseAndAddFiles(GraphContext context, Path tempFolder, FileModelService fileService,
ArchiveModel archiveModel,
FileModel parentFileModel)
{
File fileReference = parentFileModel.asFile();
if (fileReference.isDirectory())
{
File[] subFiles = fileReference.listFiles();
if (subFiles != null)
{
for (File subFile : subFiles)
{
FileModel subFileModel = fileService.createByFilePath(parentFileModel, subFile.getAbsolutePath());
subFileModel.setParentArchive(archiveModel);
if (subFile.isFile() && ZipUtil.endsWithZipExtension(subFileModel.getFilePath()))
{
File newZipFile = subFileModel.asFile();
ArchiveModel newArchiveModel = GraphService.addTypeToModel(context, subFileModel,
ArchiveModel.class);
newArchiveModel.setParentArchive(archiveModel);
newArchiveModel.setArchiveName(newZipFile.getName());
archiveModel.addChildArchive(newArchiveModel);
unzipToTempDirectory(context, tempFolder, newZipFile, newArchiveModel);
}
if (subFile.isDirectory())
{
recurseAndAddFiles(context, tempFolder, fileService, archiveModel, subFileModel);
}
}
}
}
}
}