Skip to content

Commit

Permalink
Add exclude-archives-from-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
kabir committed Oct 26, 2023
1 parent e338acd commit fbc9f12
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 23 deletions.
26 changes: 13 additions & 13 deletions cli/src/main/java/org/wildfly/glow/cli/commands/ScanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@
*/
package org.wildfly.glow.cli.commands;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.jboss.galleon.util.IoUtils;
import org.wildfly.glow.Arguments;
import static org.wildfly.glow.Arguments.CLOUD_EXECUTION_CONTEXT;
import static org.wildfly.glow.Arguments.COMPACT_PROPERTY;
import org.wildfly.glow.FeaturePacks;
import org.wildfly.glow.GlowMessageWriter;
import org.wildfly.glow.GlowSession;
import org.wildfly.glow.HiddenPropertiesAccessor;
import org.wildfly.glow.OutputContent;
import org.wildfly.glow.OutputFormat;
import static org.wildfly.glow.OutputFormat.BOOTABLE_JAR;
import static org.wildfly.glow.OutputFormat.DOCKER_IMAGE;

import org.wildfly.glow.ScanArguments.Builder;
import org.wildfly.glow.ScanResults;
import org.wildfly.glow.error.IdentifiedError;
import org.wildfly.glow.maven.MavenResolver;

import picocli.CommandLine;
import picocli.CommandLine.Parameters;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.wildfly.glow.Arguments.CLOUD_EXECUTION_CONTEXT;
import static org.wildfly.glow.Arguments.COMPACT_PROPERTY;
import static org.wildfly.glow.OutputFormat.BOOTABLE_JAR;
import static org.wildfly.glow.OutputFormat.DOCKER_IMAGE;

@CommandLine.Command(
name = Constants.SCAN_COMMAND,
sortOptions = true
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/wildfly/glow/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class Arguments implements GoOfflineArguments, ScanArguments {

Expand All @@ -30,7 +31,7 @@ public class Arguments implements GoOfflineArguments, ScanArguments {
private final Set<String> manualLayers;
private final boolean verbose;
private final boolean techPreview;
private final List<String> excludeArchivesFromScan;
private final List<Pattern> excludeArchivesFromScan;

protected Arguments(
String executionContext,
Expand All @@ -45,7 +46,7 @@ protected Arguments(
Set<String> layersForJndi,
boolean verbose,
boolean techPreview,
List<String> excludeArchivesFromScan) {
List<Pattern> excludeArchivesFromScan) {
this.executionProfiles = executionProfiles;
this.userEnabledAddOns = userEnabledAddOns;
this.binaries = binaries;
Expand Down Expand Up @@ -169,7 +170,7 @@ public boolean isTechPreview() {
}

@Override
public List<String> getExcludeArchivesFromScan() {
public List<Pattern> getExcludeArchivesFromScan() {
return excludeArchivesFromScan;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class BaseArgumentsBuilder {
protected Set<String> executionProfiles = Collections.emptySet();
Expand All @@ -36,13 +38,17 @@ public class BaseArgumentsBuilder {
protected boolean verbose;
protected boolean techPreview;

protected List<String> excludeJarsFromScan;
protected List<String> excludeJarsFromScan = Collections.emptyList();

protected BaseArgumentsBuilder() {

}

public Arguments build() {
List<Pattern> excludeJarsFromScan = this.excludeJarsFromScan.stream()
.map(v -> Pattern.compile(Utils.escapePattern(v)))
.collect(Collectors.toList());

return new Arguments(
executionContext,
executionProfiles,
Expand Down
17 changes: 13 additions & 4 deletions core/src/main/java/org/wildfly/glow/DeploymentScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ public class DeploymentScanner implements AutoCloseable {
private final Path binary;
private final Path tempDirectory;
private boolean verbose;
private final List<Pattern> excludeArchivesFromScan;
private ArchiveType archiveType;
private DeploymentScanner parent;
private final boolean isArchive;

public DeploymentScanner(Path binary, boolean verbose) throws IOException {
this(null, binary, verbose);
public DeploymentScanner(Path binary, boolean verbose, List<Pattern> excludeArchivesFromScan) throws IOException {
this(null, binary, verbose, excludeArchivesFromScan);
}

private DeploymentScanner(DeploymentScanner parent, Path binary, boolean verbose) throws IOException {
private DeploymentScanner(DeploymentScanner parent, Path binary, boolean verbose, List<Pattern> excludeArchivesFromScan) throws IOException {
this.parent = parent;
this.tempDirectory = parent == null ? Files.createTempDirectory("glow") : parent.tempDirectory;
this.verbose = verbose;
this.excludeArchivesFromScan = excludeArchivesFromScan;

if (!Files.exists(binary)) {
throw new IllegalArgumentException(binary.normalize().toAbsolutePath() + " is not an archive");
Expand Down Expand Up @@ -311,7 +313,14 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
}

private void scanWithNestedScanner(Path file, DeploymentScanContext ctx) throws IOException {
try (DeploymentScanner nestedScanner = new DeploymentScanner(DeploymentScanner.this, file, verbose)) {
// Check it is not an excluded archive
for (Pattern exclude : excludeArchivesFromScan) {
if (exclude.matcher(file.getFileName().toString()).matches()) {
return;
}
}

try (DeploymentScanner nestedScanner = new DeploymentScanner(DeploymentScanner.this, file, verbose, excludeArchivesFromScan)) {
try {
nestedScanner.scan(ctx);
} catch (RuntimeException | IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/wildfly/glow/GlowSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public ScanResults scan() throws Exception {
if (windup == null) {
for (Path d : arguments.getBinaries()) {
//System.out.println("SCAN " + d);
try (DeploymentScanner deploymentScanner = new DeploymentScanner(d, arguments.isVerbose())) {
try (DeploymentScanner deploymentScanner = new DeploymentScanner(d, arguments.isVerbose(), arguments.getExcludeArchivesFromScan())) {
deploymentScanner.scan(mapping, layers, all, errorSession);
}
}
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/org/wildfly/glow/ScanArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package org.wildfly.glow;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public interface ScanArguments {

Expand Down Expand Up @@ -66,7 +68,7 @@ public interface ScanArguments {

boolean isTechPreview();

List<String> getExcludeArchivesFromScan();
List<Pattern> getExcludeArchivesFromScan();

default Builder createScanArgumentsBuilder() {
return new Builder();
Expand Down Expand Up @@ -137,6 +139,10 @@ public Builder setTechPreview(boolean techPreview) {
return this;
}

public Builder setExcludeArchivesFromScan(String... archives) {
return setExcludeArchivesFromScan(Arrays.asList(archives));
}

public Builder setExcludeArchivesFromScan(List<String> archives) {
this.excludeJarsFromScan = archives;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@ private void testEar(Consumer<Path> pathConsumer) throws Exception {
"ee-core-profile-server,ejb-lite,embedded-activemq,h2-driver,microprofile-reactive-messaging-kafka,pojo,sar", layers);
}

@Test
public void testEarWithExclusions() throws Exception {
EnterpriseArchive archive = createEar();
Path archivePath = exportArchive(archive);

Arguments arguments = Arguments.scanBuilder().
setBinaries(Collections.singletonList(archivePath))
.setExcludeArchivesFromScan("*.jar", "web.war")
.build();
ScanResults scanResults = GlowSession.scan(MavenResolver.newMavenResolver(), arguments, GlowMessageWriter.DEFAULT);
String layers = scanResults.getCompactInformation();
Assert.assertEquals("[resource-adapters, sar]==>ee-core-profile-server,resource-adapters,sar", layers);
}


// EAR - end
///////////////////////

Expand Down

0 comments on commit fbc9f12

Please sign in to comment.