Skip to content

Commit

Permalink
Merge branch 'release-0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierocks committed Feb 10, 2021
2 parents 7af0aad + b8cad65 commit 1508934
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Atlas is a *plain-and-simple* binary transformer for Java artifacts, providing a
to manipulate Jars as you see fit.

```java
final Atlas atlas = new Atlas();
atlas.install((ctx) -> new JarEntryRemappingTransformer(
try (final Atlas atlas = new Atlas()) {
atlas.install((ctx) -> new JarEntryRemappingTransformer(
new LorenzRemapper(mappings)
));
atlas.run(Paths.get("input.jar"), Paths.get("output.jar"));
));
atlas.run(Paths.get("input.jar"), Paths.get("output.jar"));
}
```

## License
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ targetCompatibility = javaVersion

group = 'org.cadixdev'
archivesBaseName = project.name.toLowerCase()
version = '0.2.1'
version = '0.2.2'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ inceptionYear = 2019

# Build Settings
javaVersion = 1.8
bombeVersion = 0.3.1
bombeVersion = 0.3.5
98 changes: 95 additions & 3 deletions src/main/java/org/cadixdev/atlas/jar/JarFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ else if (name.endsWith(".class")) {
/**
* Transforms the JAR file, with the given {@link JarEntryTransformer}s, writing
* to the given output JAR path.
* <p>
* This will use {@link Executors#newWorkStealingPool()} as the executor service,
* use {@link #transform(Path, ExecutorService, JarEntryTransformer...)} if you
* wish to control this.
*
* @param export The JAR path to write to
* @param transformers The transformers to use
Expand Down Expand Up @@ -186,9 +190,6 @@ public void transform(final Path export, final ExecutorService executorService,
// Transform the entry
for (final JarEntryTransformer transformer : transformers) {
entry = entry.accept(transformer);

// If a transformer wants to remove an entry, it should return null.
// TODO: document this in Bombe
if (entry == null) return;
}

Expand All @@ -208,6 +209,97 @@ public void transform(final Path export, final ExecutorService executorService,
}, executorService)).toArray(CompletableFuture[]::new));

future.get();

// Add additions from transformers
for (final JarEntryTransformer transformer : transformers) {
for (final AbstractJarEntry addition : transformer.additions()) {
// Write to jar
final Path outEntry = fs.getPath("/", addition.getName());

// Ensure parent directory exists
Files.createDirectories(outEntry.getParent());

// Write the result to the new jar
Files.write(outEntry, addition.getContents());
Files.setLastModifiedTime(outEntry, FileTime.fromMillis(addition.getTime()));
}
}
}
catch (final InterruptedException ex) {
throw new RuntimeException(ex);
}
catch (final ExecutionException ex) {
try {
throw ex.getCause();
}
catch (final IOException ioe) {
throw ioe;
}
catch (final Throwable cause) {
throw new RuntimeException(cause);
}
}
}

/**
* Processes the JAR file, running the given {@link JarEntryTransformer jar entry transformers}
* for each path within the jar.
* <p>
* The eventual result of transformation is ignored, and not written to file.
* {@link #transform(Path, ExecutorService, JarEntryTransformer...)} should be used if such
* behaviour is desired.
* <p>
* This will use {@link Executors#newWorkStealingPool()} as the executor service, use
* {@link #process(ExecutorService, JarEntryTransformer...)} if you wish to control this.
*
* @param transformers The transformers to use
* @throws IOException Should an issue with reading occur
* @since 0.2.2
*/
public void process(final JarEntryTransformer... transformers) throws IOException {
final ExecutorService executorService = Executors.newWorkStealingPool();
try {
this.process(executorService, transformers);
}
finally {
executorService.shutdown();
}
}

/**
* Processes the JAR file, running the given {@link JarEntryTransformer jar entry transformers}
* for each path within the jar.
* <p>
* The eventual result of transformation is ignored, and not written to file.
* {@link #transform(Path, ExecutorService, JarEntryTransformer...)} should be used if such
* behaviour is desired.
*
* @param executorService The executor service to use
* @param transformers The transformers to use
* @throws IOException Should an issue with reading occur
* @since 0.2.2
*/
public void process(final ExecutorService executorService, final JarEntryTransformer... transformers)
throws IOException {
final CompletableFuture<Void> future = CompletableFuture.allOf(this.walk().map(path -> CompletableFuture.runAsync(() -> {
try {
// Get the entry
AbstractJarEntry entry = this.get(path);
if (entry == null) return;

// Transform the entry
for (final JarEntryTransformer transformer : transformers) {
entry = entry.accept(transformer);
if (entry == null) return;
}
}
catch (final IOException ex) {
throw new CompletionException(ex);
}
}, executorService)).toArray(CompletableFuture[]::new));

try {
future.get();
}
catch (final InterruptedException ex) {
throw new RuntimeException(ex);
Expand Down

0 comments on commit 1508934

Please sign in to comment.