Skip to content

Commit dc2333f

Browse files
authored
Merge pull request #3 from bdhoine/develop
Add include group id regex and fix sonar warnings
2 parents 6c6b243 + 2a74ed9 commit dc2333f

File tree

5 files changed

+86
-70
lines changed

5 files changed

+86
-70
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Sorts and decompiles your Felix / OSGi bundles in to a searchable directory
1515

1616
* -orfn/--outputResultFileName: Output result json file name (default result.json)
1717
* -t/--threadCount: Thread count of the bundle extractor (default 8)
18+
* -ig/--includedGroupIds: Regex pattern to include specific group ids (default empty)
1819
* -eg/--excludedGroupIds: Regex pattern to exclude specific group ids (default empty)
1920
* -ea/--excludedArtifactIds: Regex pattern to exclude specific artifact ids (default empty)
2021
* -enma/--excludeNonMavenArtifacts: Add argument to disable extraction of non maven artifacts, ex. artifacts that only contain a manifast. (default false)
2122

2223
## Included grep-in bash script
2324

24-
A bash script to allow quick searching in the sources folder. Usage: "bash grep-in-jars.sh ClassName"
25+
A bash script to allow quick searching in the sources folder. Usage: "bash grep-in-jars.sh ClassName"

src/main/java/be/idoneus/felix/bundle/extractor/BundleExtractor.java

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package be.idoneus.felix.bundle.extractor;
22

3-
import be.idoneus.felix.bundle.extractor.BundleExtractor;
4-
53
import org.apache.commons.io.FileUtils;
64
import org.apache.commons.logging.Log;
75
import org.apache.commons.logging.LogFactory;
@@ -11,6 +9,7 @@
119
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
1210
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
1311
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
12+
1413
import java.io.File;
1514
import java.io.FileOutputStream;
1615
import java.io.IOException;
@@ -20,20 +19,19 @@
2019
import java.nio.channels.Channels;
2120
import java.nio.channels.ReadableByteChannel;
2221
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.*;
2723
import java.util.jar.JarEntry;
2824
import java.util.jar.JarFile;
2925
import java.util.jar.Manifest;
3026
import java.util.stream.Stream;
3127

3228
public class BundleExtractor {
3329

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";
3633
private final BundleExtractorConfig config;
34+
private final Log log = LogFactory.getLog(BundleExtractor.class);
3735

3836
public BundleExtractor(BundleExtractorConfig config) {
3937
this.config = config;
@@ -51,19 +49,19 @@ public BundleExtractionResult extract(Path path) {
5149
extractJarFile(result, path, bundlePath);
5250
result.setProcessed(true);
5351
} else {
54-
log.info("Could not find bundle jar" + path.toString());
52+
log.info("Could not find bundle jar" + path);
5553
result.setProcessed(false);
5654
}
5755
} else {
58-
log.info("Could not find version directory" + path.toString());
56+
log.info("Could not find version directory" + path);
5957
result.setProcessed(false);
6058
}
6159
} catch (Exception e) {
6260
log.error("Could not extract bundle", e);
6361
result.setProcessed(false);
6462
}
6563
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");
6765
return result;
6866
}
6967

@@ -83,7 +81,7 @@ private void extractJarFile(BundleExtractionResult result, Path path, Path bundl
8381
}
8482
}
8583
if (!extracted) {
86-
if (!config.isExludeNonMavenArtifacts()) {
84+
if (!config.isExcludeNonMavenArtifacts()) {
8785
log.info(
8886
"Could not get a pom.xml for bundle " + path.toString() + " , defaulting back to manifest");
8987
extractFromManifest(result, path, bundlePath, jarFile);
@@ -121,11 +119,11 @@ private void extractFromManifest(BundleExtractionResult result, Path path, Path
121119
result.setVersion(version);
122120

123121
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);
125123
result.setExcluded(true);
126124
return;
127125
} 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);
129127
result.setExcluded(true);
130128
return;
131129
}
@@ -139,7 +137,7 @@ private void extractFromManifest(BundleExtractionResult result, Path path, Path
139137
Files.copy(bundlePath, buildDirectory.resolve(artifactId + "-" + version + ".jar"));
140138
createSourceJar(buildDirectory, artifactId, version);
141139
result.setDecompiled(true);
142-
moveToOutputFolder(buildDirectory, groupId, artifactId, version);
140+
moveToOutputFolder(buildDirectory, artifactId, version);
143141
FileUtils.deleteDirectory(buildDirectory.toFile());
144142
log.info("Extracted and renamed with manifest.mf for " + groupId + ":" + artifactId);
145143
}
@@ -160,6 +158,13 @@ private boolean isExcludedGroupId(String groupId) {
160158
}
161159
}
162160

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+
163168
private void deleteDirectory(Path path) throws IOException {
164169
try (Stream<Path> paths = Files.walk(path, FileVisitOption.FOLLOW_LINKS)) {
165170
paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
@@ -173,17 +178,14 @@ private String getManifestAttribute(JarFile jarFile, String manifestAttributeNam
173178
if (entry.getName().equals("META-INF/MANIFEST.MF")) {
174179
Manifest manifest = new Manifest(jarFile.getInputStream(entry));
175180
String embeddedDependencies = manifest.getMainAttributes().getValue(manifestAttributeName);
176-
if (embeddedDependencies != null) {
177-
return embeddedDependencies;
178-
}
179-
return "";
181+
return Objects.requireNonNullElse(embeddedDependencies, "");
180182
}
181183
}
182184
return "";
183185
}
184186

185187
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 {
187189
MavenXpp3Reader reader = new MavenXpp3Reader();
188190
Model model = reader.read(jarFile.getInputStream(entry));
189191
String groupId = model.getGroupId();
@@ -203,12 +205,18 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
203205
result.setGroupId(groupId);
204206
result.setVersion(version);
205207

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+
206214
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);
208216
result.setExcluded(true);
209217
return true;
210218
} 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);
212220
result.setExcluded(true);
213221
return true;
214222
}
@@ -230,7 +238,7 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
230238
} else {
231239
result.setHasSources(true);
232240
}
233-
moveToOutputFolder(buildDirectory, groupId, artifactId, version);
241+
moveToOutputFolder(buildDirectory, artifactId, version);
234242
FileUtils.deleteDirectory(buildDirectory.toFile());
235243
log.info("Extracted and renamed for " + groupId + ":" + artifactId);
236244

@@ -243,24 +251,26 @@ private boolean extractPom(BundleExtractionResult result, Path path, Path bundle
243251
}
244252

245253
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+
}
247257
}
248258

249-
private void moveToOutputFolder(Path buildDirectory, String groupId, String artifactId, String version)
259+
private void moveToOutputFolder(Path buildDirectory, String artifactId, String version)
250260
throws IOException {
251261
Path outputPath = Paths.get(config.getBundleOutputDir());
252262
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);
254264
Files.move(artifact, outputPath.resolve("artifacts").resolve(artifactId + "-" + version + ".jar"),
255265
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),
257267
StandardCopyOption.REPLACE_EXISTING);
258268
}
259269

260-
public boolean downloadSources(Path directory, String groupId, String artifectId, String version) {
270+
public boolean downloadSources(Path directory, String groupId, String artifactId, String version) {
261271
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
264274
+ "/" + version + "/" + sourcesJarName);
265275
HttpURLConnection huc = (HttpURLConnection) website.openConnection();
266276
huc.setRequestMethod("HEAD");
@@ -284,7 +294,7 @@ public boolean downloadSources(Path directory, String groupId, String artifectId
284294
}
285295

286296
private Path getVersionDirectory(Path path) throws IOException {
287-
final Path[] result = { null };
297+
final Path[] result = {null};
288298
try (Stream<Path> paths = Files.walk(path)) {
289299
paths.forEach(filePath -> {
290300
if (filePath.toString().contains("version") && result[0] == null) {
@@ -299,12 +309,12 @@ private void createSourceJar(Path directory, String artifactId, String version)
299309
long startTime = System.nanoTime();
300310
Map<String, Object> options = new HashMap<>();
301311
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);
303313
decompiler.addSpace(directory.resolve(artifactId + "-" + version + ".jar").toFile(), true);
304314
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));
308318
long elapsedTime = System.nanoTime() - startTime;
309319
log.debug("Decompilation of " + artifactId + " took " + elapsedTime / 1_000_000_000 + " seconds");
310320
}

src/main/java/be/idoneus/felix/bundle/extractor/BundleExtractorConfig.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ public class BundleExtractorConfig {
66
private String bundleOutputDir;
77
private String outputResultFileName;
88
private int threadCount;
9+
private String includeGroupIds;
910
private String excludeGroupIds;
1011
private String excludeArtifactIds;
11-
private boolean exludeNonMavenArtifacts = false;
12+
private boolean excludeNonMavenArtifacts = false;
1213

1314
public String getBundleInputDir() {
1415
return bundleInputDir;
1516
}
1617

17-
public boolean isExludeNonMavenArtifacts() {
18-
return exludeNonMavenArtifacts;
18+
public void setBundleInputDir(String bundleInputDir) {
19+
this.bundleInputDir = bundleInputDir;
20+
}
21+
22+
public boolean isExcludeNonMavenArtifacts() {
23+
return excludeNonMavenArtifacts;
1924
}
2025

21-
public void setExludeNonMavenArtifacts(boolean exludeNonMavenArtifacts) {
22-
this.exludeNonMavenArtifacts = exludeNonMavenArtifacts;
26+
public void setExcludeNonMavenArtifacts(boolean excludeNonMavenArtifacts) {
27+
this.excludeNonMavenArtifacts = excludeNonMavenArtifacts;
2328
}
2429

2530
public String getExcludeArtifactIds() {
@@ -54,16 +59,19 @@ public void setBundleOutputDir(String bundleOutputDir) {
5459
this.bundleOutputDir = bundleOutputDir;
5560
}
5661

57-
public void setBundleInputDir(String bundleInputDir) {
58-
this.bundleInputDir = bundleInputDir;
62+
public String getOutputResultFileName() {
63+
return outputResultFileName;
5964
}
6065

6166
public void setOutputResultFileName(String outputResultFileName) {
62-
this.outputResultFileName =outputResultFileName;
67+
this.outputResultFileName = outputResultFileName;
6368
}
6469

65-
public String getOutputResultFileName() {
66-
return outputResultFileName;
70+
public String getIncludeGroupIds() {
71+
return includeGroupIds;
6772
}
6873

69-
}
74+
public void setIncludeGroupIds(String includeGroupIds) {
75+
this.includeGroupIds = includeGroupIds;
76+
}
77+
}

src/main/java/be/idoneus/felix/bundle/extractor/BundleExtractorManager.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
package be.idoneus.felix.bundle.extractor;
22

3+
import com.google.gson.Gson;
34
import org.apache.commons.logging.Log;
45
import org.apache.commons.logging.LogFactory;
56

67
import java.io.IOException;
7-
import java.nio.file.*;
8+
import java.nio.file.DirectoryStream;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
812
import java.util.ArrayList;
913
import java.util.List;
10-
import java.util.concurrent.CompletableFuture;
11-
import java.util.concurrent.ExecutionException;
12-
import java.util.concurrent.ExecutorService;
13-
import java.util.concurrent.Executors;
14-
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.*;
1515
import java.util.stream.Collectors;
1616

17-
import com.google.gson.Gson;
18-
1917
/**
2018
* Extracts the bundles from the felix launchpad directory given by bundles.dir
2119
*/
2220
public class BundleExtractorManager {
2321

24-
private Log log = LogFactory.getLog(BundleExtractorManager.class);
25-
2622
private final BundleExtractor extractor;
2723
private final BundleExtractorConfig config;
24+
private final Log log = LogFactory.getLog(BundleExtractorManager.class);
2825

2926
public BundleExtractorManager(BundleExtractorConfig config) {
3027
this.config = config;
@@ -36,6 +33,7 @@ void run() {
3633
extractBundles();
3734
}
3835

36+
@SuppressWarnings("java:S2142")
3937
private void extractBundles() {
4038
ExecutorService executorService = Executors.newFixedThreadPool(config.getThreadCount());
4139

@@ -81,10 +79,10 @@ private void createOutputResult(List<BundleExtractionResult> bundleExtractionRes
8179
FelixBundleExtractorResult result = new FelixBundleExtractorResult();
8280
result.setBundleExtractionResults(
8381
bundleExtractionResults.stream().filter(r -> r.getArtifactId() != null).collect(Collectors.toList()));
84-
result.setDecompiledCount(bundleExtractionResults.stream().filter(r -> r.isDecompiled()).count());
85-
result.setDownloadCount(bundleExtractionResults.stream().filter(r -> r.hasSources()).count());
82+
result.setDecompiledCount(bundleExtractionResults.stream().filter(BundleExtractionResult::isDecompiled).count());
83+
result.setDownloadCount(bundleExtractionResults.stream().filter(BundleExtractionResult::hasSources).count());
8684
result.setUnprocessedCount(bundleExtractionResults.stream().filter(r -> !r.getProcessed()).count());
87-
result.setExcludedCount(bundleExtractionResults.stream().filter(r -> r.isExcluded()).count());
85+
result.setExcludedCount(bundleExtractionResults.stream().filter(BundleExtractionResult::isExcluded).count());
8886

8987
String jsonResult = new Gson().toJson(result);
9088

0 commit comments

Comments
 (0)