1
1
package be .idoneus .felix .bundle .extractor ;
2
2
3
- import be .idoneus .felix .bundle .extractor .BundleExtractor ;
4
-
5
3
import org .apache .commons .io .FileUtils ;
6
4
import org .apache .commons .logging .Log ;
7
5
import org .apache .commons .logging .LogFactory ;
11
9
import org .jetbrains .java .decompiler .main .decompiler .ConsoleDecompiler ;
12
10
import org .jetbrains .java .decompiler .main .extern .IFernflowerLogger ;
13
11
import org .jetbrains .java .decompiler .main .extern .IFernflowerPreferences ;
12
+
14
13
import java .io .File ;
15
14
import java .io .FileOutputStream ;
16
15
import java .io .IOException ;
20
19
import java .nio .channels .Channels ;
21
20
import java .nio .channels .ReadableByteChannel ;
22
21
import java .nio .file .*;
23
- import java .util .Comparator ;
24
- import java .util .Enumeration ;
25
- import java .util .HashMap ;
26
- import java .util .Map ;
22
+ import java .util .*;
27
23
import java .util .jar .JarEntry ;
28
24
import java .util .jar .JarFile ;
29
25
import java .util .jar .Manifest ;
30
26
import java .util .stream .Stream ;
31
27
32
28
public class BundleExtractor {
33
29
34
- private Log log = LogFactory .getLog (BundleExtractor .class );
35
-
30
+ public static final String IS_EXCLUDED = " is excluded" ;
31
+ public static final String SOURCES = "sources" ;
32
+ public static final String SOURCES_JAR = "-sources.jar" ;
36
33
private final BundleExtractorConfig config ;
34
+ private final Log log = LogFactory .getLog (BundleExtractor .class );
37
35
38
36
public BundleExtractor (BundleExtractorConfig config ) {
39
37
this .config = config ;
@@ -51,19 +49,19 @@ public BundleExtractionResult extract(Path path) {
51
49
extractJarFile (result , path , bundlePath );
52
50
result .setProcessed (true );
53
51
} else {
54
- log .info ("Could not find bundle jar" + path . toString () );
52
+ log .info ("Could not find bundle jar" + path );
55
53
result .setProcessed (false );
56
54
}
57
55
} else {
58
- log .info ("Could not find version directory" + path . toString () );
56
+ log .info ("Could not find version directory" + path );
59
57
result .setProcessed (false );
60
58
}
61
59
} catch (Exception e ) {
62
60
log .error ("Could not extract bundle" , e );
63
61
result .setProcessed (false );
64
62
}
65
63
long elapsedTime = System .nanoTime () - startTime ;
66
- log .debug ("Extraction of " + path . toString () + " took " + elapsedTime / 1_000_000_000 + " seconds" );
64
+ log .debug ("Extraction of " + path + " took " + elapsedTime / 1_000_000_000 + " seconds" );
67
65
return result ;
68
66
}
69
67
@@ -83,7 +81,7 @@ private void extractJarFile(BundleExtractionResult result, Path path, Path bundl
83
81
}
84
82
}
85
83
if (!extracted ) {
86
- if (!config .isExludeNonMavenArtifacts ()) {
84
+ if (!config .isExcludeNonMavenArtifacts ()) {
87
85
log .info (
88
86
"Could not get a pom.xml for bundle " + path .toString () + " , defaulting back to manifest" );
89
87
extractFromManifest (result , path , bundlePath , jarFile );
@@ -121,11 +119,11 @@ private void extractFromManifest(BundleExtractionResult result, Path path, Path
121
119
result .setVersion (version );
122
120
123
121
if (isExcludedGroupId (groupId )) {
124
- log .info ("Not extracting manifest because the group id " + groupId + " is excluded" );
122
+ log .info ("Not extracting manifest because the group id " + groupId + IS_EXCLUDED );
125
123
result .setExcluded (true );
126
124
return ;
127
125
} else if (isExcludedArtifactId (artifactId )) {
128
- log .info ("Not extracting manifest because the artifact id " + artifactId + " is excluded" );
126
+ log .info ("Not extracting manifest because the artifact id " + artifactId + IS_EXCLUDED );
129
127
result .setExcluded (true );
130
128
return ;
131
129
}
@@ -139,7 +137,7 @@ private void extractFromManifest(BundleExtractionResult result, Path path, Path
139
137
Files .copy (bundlePath , buildDirectory .resolve (artifactId + "-" + version + ".jar" ));
140
138
createSourceJar (buildDirectory , artifactId , version );
141
139
result .setDecompiled (true );
142
- moveToOutputFolder (buildDirectory , groupId , artifactId , version );
140
+ moveToOutputFolder (buildDirectory , artifactId , version );
143
141
FileUtils .deleteDirectory (buildDirectory .toFile ());
144
142
log .info ("Extracted and renamed with manifest.mf for " + groupId + ":" + artifactId );
145
143
}
@@ -160,6 +158,13 @@ private boolean isExcludedGroupId(String groupId) {
160
158
}
161
159
}
162
160
161
+ private boolean isIncludedGroupId (String groupId ) {
162
+ if (config .getIncludeGroupIds () == null || config .getIncludeGroupIds ().equals ("" )) {
163
+ return true ;
164
+ }
165
+ return groupId .matches (config .getIncludeGroupIds ());
166
+ }
167
+
163
168
private void deleteDirectory (Path path ) throws IOException {
164
169
try (Stream <Path > paths = Files .walk (path , FileVisitOption .FOLLOW_LINKS )) {
165
170
paths .sorted (Comparator .reverseOrder ()).map (Path ::toFile ).forEach (File ::delete );
@@ -173,17 +178,14 @@ private String getManifestAttribute(JarFile jarFile, String manifestAttributeNam
173
178
if (entry .getName ().equals ("META-INF/MANIFEST.MF" )) {
174
179
Manifest manifest = new Manifest (jarFile .getInputStream (entry ));
175
180
String embeddedDependencies = manifest .getMainAttributes ().getValue (manifestAttributeName );
176
- if (embeddedDependencies != null ) {
177
- return embeddedDependencies ;
178
- }
179
- return "" ;
181
+ return Objects .requireNonNullElse (embeddedDependencies , "" );
180
182
}
181
183
}
182
184
return "" ;
183
185
}
184
186
185
187
private boolean extractPom (BundleExtractionResult result , Path path , Path bundlePath , JarFile jarFile ,
186
- JarEntry entry , String embeddedDependencies ) throws IOException , XmlPullParserException {
188
+ JarEntry entry , String embeddedDependencies ) throws IOException , XmlPullParserException {
187
189
MavenXpp3Reader reader = new MavenXpp3Reader ();
188
190
Model model = reader .read (jarFile .getInputStream (entry ));
189
191
String groupId = model .getGroupId ();
@@ -203,12 +205,18 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
203
205
result .setGroupId (groupId );
204
206
result .setVersion (version );
205
207
208
+ if (!isIncludedGroupId (groupId )) {
209
+ log .info ("Not extracting pom because the group id " + groupId + "is not included" );
210
+ result .setExcluded (true );
211
+ return true ;
212
+ }
213
+
206
214
if (isExcludedGroupId (groupId )) {
207
- log .info ("Not extracting pom because the group id " + groupId + " is excluded" );
215
+ log .info ("Not extracting pom because the group id " + groupId + IS_EXCLUDED );
208
216
result .setExcluded (true );
209
217
return true ;
210
218
} else if (isExcludedArtifactId (artifactId )) {
211
- log .info ("Not extracting pom because the artifact id " + artifactId + " is excluded" );
219
+ log .info ("Not extracting pom because the artifact id " + artifactId + IS_EXCLUDED );
212
220
result .setExcluded (true );
213
221
return true ;
214
222
}
@@ -230,7 +238,7 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
230
238
} else {
231
239
result .setHasSources (true );
232
240
}
233
- moveToOutputFolder (buildDirectory , groupId , artifactId , version );
241
+ moveToOutputFolder (buildDirectory , artifactId , version );
234
242
FileUtils .deleteDirectory (buildDirectory .toFile ());
235
243
log .info ("Extracted and renamed for " + groupId + ":" + artifactId );
236
244
@@ -243,24 +251,26 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
243
251
}
244
252
245
253
private void deleteDirectoryStream (Path path ) throws IOException {
246
- Files .walk (path ).sorted (Comparator .reverseOrder ()).map (Path ::toFile ).forEach (File ::delete );
254
+ try (Stream <Path > input = Files .walk (path )) {
255
+ input .sorted (Comparator .reverseOrder ()).map (Path ::toFile ).forEach (File ::delete );
256
+ }
247
257
}
248
258
249
- private void moveToOutputFolder (Path buildDirectory , String groupId , String artifactId , String version )
259
+ private void moveToOutputFolder (Path buildDirectory , String artifactId , String version )
250
260
throws IOException {
251
261
Path outputPath = Paths .get (config .getBundleOutputDir ());
252
262
Path artifact = buildDirectory .resolve (artifactId + "-" + version + ".jar" );
253
- Path sources = buildDirectory .resolve (artifactId + "-" + version + "-sources.jar" );
263
+ Path sources = buildDirectory .resolve (artifactId + "-" + version + SOURCES_JAR );
254
264
Files .move (artifact , outputPath .resolve ("artifacts" ).resolve (artifactId + "-" + version + ".jar" ),
255
265
StandardCopyOption .REPLACE_EXISTING );
256
- Files .move (sources , outputPath .resolve ("sources" ).resolve (artifactId + "-" + version + "-sources.jar" ),
266
+ Files .move (sources , outputPath .resolve (SOURCES ).resolve (artifactId + "-" + version + SOURCES_JAR ),
257
267
StandardCopyOption .REPLACE_EXISTING );
258
268
}
259
269
260
- public boolean downloadSources (Path directory , String groupId , String artifectId , String version ) {
270
+ public boolean downloadSources (Path directory , String groupId , String artifactId , String version ) {
261
271
try {
262
- String sourcesJarName = artifectId + "-" + version + "-sources.jar" ;
263
- URL website = new URL ("https://repo1.maven.org/maven2/" + groupId .replaceAll ("\\ ." , "/" ) + "/" + artifectId
272
+ String sourcesJarName = artifactId + "-" + version + SOURCES_JAR ;
273
+ URL website = new URL ("https://repo1.maven.org/maven2/" + groupId .replace ("\\ ." , "/" ) + "/" + artifactId
264
274
+ "/" + version + "/" + sourcesJarName );
265
275
HttpURLConnection huc = (HttpURLConnection ) website .openConnection ();
266
276
huc .setRequestMethod ("HEAD" );
@@ -284,7 +294,7 @@ public boolean downloadSources(Path directory, String groupId, String artifectId
284
294
}
285
295
286
296
private Path getVersionDirectory (Path path ) throws IOException {
287
- final Path [] result = { null };
297
+ final Path [] result = {null };
288
298
try (Stream <Path > paths = Files .walk (path )) {
289
299
paths .forEach (filePath -> {
290
300
if (filePath .toString ().contains ("version" ) && result [0 ] == null ) {
@@ -299,12 +309,12 @@ private void createSourceJar(Path directory, String artifactId, String version)
299
309
long startTime = System .nanoTime ();
300
310
Map <String , Object > options = new HashMap <>();
301
311
options .put (IFernflowerPreferences .LOG_LEVEL , IFernflowerLogger .Severity .ERROR .toString ());
302
- ConsoleDecompiler decompiler = new ConsoleDecompiler (directory .resolve ("sources" ).toFile (), options );
312
+ ConsoleDecompiler decompiler = new ConsoleDecompiler (directory .resolve (SOURCES ).toFile (), options );
303
313
decompiler .addSpace (directory .resolve (artifactId + "-" + version + ".jar" ).toFile (), true );
304
314
decompiler .decompileContext ();
305
- Files .move (directory .resolve ("sources" ).resolve (artifactId + "-" + version + ".jar" ),
306
- directory .resolve (artifactId + "-" + version + "-sources.jar" ));
307
- deleteDirectory (directory .resolve ("sources" ));
315
+ Files .move (directory .resolve (SOURCES ).resolve (artifactId + "-" + version + ".jar" ),
316
+ directory .resolve (artifactId + "-" + version + SOURCES_JAR ));
317
+ deleteDirectory (directory .resolve (SOURCES ));
308
318
long elapsedTime = System .nanoTime () - startTime ;
309
319
log .debug ("Decompilation of " + artifactId + " took " + elapsedTime / 1_000_000_000 + " seconds" );
310
320
}
0 commit comments