From 78aa4904d828118416f655ac5da988d84b4ce5b1 Mon Sep 17 00:00:00 2001 From: Matthias Pfisterer Date: Fri, 6 Mar 2020 23:40:30 +0100 Subject: [PATCH] - changed to actually using ScanLocation's list of files if present - documentation update --- .../execution/ExecutionParameters.java | 2 +- .../licensescout/execution/Executor.java | 27 +++++++++++----- .../exporter/AbstractVelocityExporter.java | 3 +- .../licensescout/finder/AbstractFinder.java | 20 ++++++++++-- .../licensescout/finder/JavaJarFinder.java | 30 +++++++---------- .../execution/SimpleExecutionTest.java | 32 ++++++++++++++++++- 6 files changed, 82 insertions(+), 32 deletions(-) diff --git a/licensescout-core/src/main/java/org/aposin/licensescout/execution/ExecutionParameters.java b/licensescout-core/src/main/java/org/aposin/licensescout/execution/ExecutionParameters.java index b1046863..c609f507 100644 --- a/licensescout-core/src/main/java/org/aposin/licensescout/execution/ExecutionParameters.java +++ b/licensescout-core/src/main/java/org/aposin/licensescout/execution/ExecutionParameters.java @@ -35,7 +35,7 @@ public class ExecutionParameters { private ArchiveType archiveType; /** - * Directory to scan for archives. + * Location to scan for archives. */ private ScanLocation scanLocation; diff --git a/licensescout-core/src/main/java/org/aposin/licensescout/execution/Executor.java b/licensescout-core/src/main/java/org/aposin/licensescout/execution/Executor.java index bcd59b38..89df3d0d 100644 --- a/licensescout-core/src/main/java/org/aposin/licensescout/execution/Executor.java +++ b/licensescout-core/src/main/java/org/aposin/licensescout/execution/Executor.java @@ -156,7 +156,7 @@ public void execute() throws LicenseScoutExecutionException, LicenseScoutFailOnE final AbstractFinder finder = FinderFactory.getInstance().createFinder(executionParameters, licenseStoreData, finderParameters); getLog().info("Starting scan on " - + getExecutionParameters().getScanLocation().getScanDirectory().getAbsolutePath() + "..."); + + getExecutionParameters().getScanLocation().toLogString() + "..."); OutputResult outputResult; try { @@ -441,15 +441,26 @@ private OutputResult createOutputResult(final FinderResult finderResult) { } private void checkParameters(final ILSLog log) throws LicenseScoutExecutionException { - final File scanDirectory = getExecutionParameters().getScanLocation().getScanDirectory(); - if (scanDirectory != null) { - if (!scanDirectory.exists()) { - throw new LicenseScoutExecutionException( - "This scan directory does not exist: " + scanDirectory.getAbsolutePath()); + final ScanLocation scanLocation = getExecutionParameters().getScanLocation(); + if (scanLocation != null) { + List scanFiles = scanLocation.getScanFiles(); + if (scanFiles != null && !scanFiles.isEmpty()) { + // OK + log.info("using scan files: " + scanFiles); + } else { + final File scanDirectory = scanLocation.getScanDirectory(); + if (scanDirectory != null) { + if (!scanDirectory.exists()) { + throw new LicenseScoutExecutionException( + "This scan directory does not exist: " + scanDirectory.getAbsolutePath()); + } + log.info("using scan directory: " + scanDirectory.getAbsolutePath()); + } else { + throw new LicenseScoutExecutionException("neither scanFiles nor scanDirectory configured"); + } } - log.info("using scan directory: " + scanDirectory.getAbsolutePath()); } else { - throw new LicenseScoutExecutionException("scanDirectory not configured"); + throw new LicenseScoutExecutionException("scan location not configured"); } } diff --git a/licensescout-core/src/main/java/org/aposin/licensescout/exporter/AbstractVelocityExporter.java b/licensescout-core/src/main/java/org/aposin/licensescout/exporter/AbstractVelocityExporter.java index 37cdd46b..4f90951c 100644 --- a/licensescout-core/src/main/java/org/aposin/licensescout/exporter/AbstractVelocityExporter.java +++ b/licensescout-core/src/main/java/org/aposin/licensescout/exporter/AbstractVelocityExporter.java @@ -88,7 +88,8 @@ private VelocityContext createVelocityContext(final OutputResult outputResult, final List archiveFiles = getSortedArchives(outputResult); context.put("archiveFiles", archiveFiles); - context.put("sourcePath", outputResult.getFinderResult().getScanDirectory().getAbsolutePath()); + final File scanDirectory = outputResult.getFinderResult().getScanDirectory(); + context.put("sourcePath", scanDirectory != null ? scanDirectory.getAbsolutePath() : ""); context.put("detectionStatusStatistics", outputResult.getDetectionStatusStatistics()); context.put("legalStatusStatistics", outputResult.getLegalStatusStatistics()); context.put("generalStatistics", outputResult.getGeneralStatistics()); diff --git a/licensescout-core/src/main/java/org/aposin/licensescout/finder/AbstractFinder.java b/licensescout-core/src/main/java/org/aposin/licensescout/finder/AbstractFinder.java index c01559a3..dedf4e2d 100644 --- a/licensescout-core/src/main/java/org/aposin/licensescout/finder/AbstractFinder.java +++ b/licensescout-core/src/main/java/org/aposin/licensescout/finder/AbstractFinder.java @@ -86,9 +86,9 @@ protected final ILSLog getLog() { } /** - * Sets the directory to scan. + * Sets the scan location. * - * @param scanLocation the directory to scan + * @param scanLocation the location to scan */ public final void setScanLocation(final ScanLocation scanLocation) { this.scanLocation = scanLocation; @@ -97,6 +97,9 @@ public final void setScanLocation(final ScanLocation scanLocation) { /** * Obtains the scan location. * + *

Is intended to be used for logging. For processing, {@link #getScanDirectory()} + * and {@link #getScanFiles()} should be used.

+ * * @return the scan location */ protected final ScanLocation getScanLocation() { @@ -114,6 +117,17 @@ protected final File getScanDirectory() { return getScanLocation().getScanDirectory(); } + /** + * Obtains the files to scan. + * + * @return the scanDirectory + * + * @see #getScanDirectory() + */ + protected final List getScanFiles() { + return scanLocation.getScanFiles(); + } + /** * Adds an archive to the list of found archives. * @param foundArchive an archive @@ -133,7 +147,7 @@ public FinderResult findLicenses() throws Exception { if (debug) { printArchiveList(archiveFiles); } - getLog().info("Finished scanning for licenses in " + getScanDirectory().getAbsolutePath()); + getLog().info("Finished scanning for licenses in " + getScanLocation().toLogString()); return new FinderResult(getScanLocation(), archiveFiles); } diff --git a/licensescout-core/src/main/java/org/aposin/licensescout/finder/JavaJarFinder.java b/licensescout-core/src/main/java/org/aposin/licensescout/finder/JavaJarFinder.java index 2f452056..be2e6750 100644 --- a/licensescout-core/src/main/java/org/aposin/licensescout/finder/JavaJarFinder.java +++ b/licensescout-core/src/main/java/org/aposin/licensescout/finder/JavaJarFinder.java @@ -23,6 +23,7 @@ import java.util.List; import org.aposin.licensescout.archive.Archive; +import org.aposin.licensescout.execution.ScanLocation; import org.aposin.licensescout.license.IArtifactServerUtil; import org.aposin.licensescout.license.LicenseStoreData; import org.aposin.licensescout.util.ArchiveMetaInformation; @@ -36,20 +37,8 @@ * a starting point.

* */ -// TODO: rename to JavaDirectoryFinder public class JavaJarFinder extends AbstractJavaFinder { - protected enum ScanMode { - /** - * In a directory in the file system that is not an archive. - */ - DIRECTORY(), - /** - * In a directory in the file system that is an archive (i.e. in an unpacked archive). - */ - UNPACKED_ARCHIVE(); - } - private final List specialArchiveNames = new ArrayList<>(); private final FinderHandler fileSystemFinderHandler; @@ -77,9 +66,17 @@ private void initSpecialArchiveNames() { */ @Override protected void findLicensesImpl() throws Exception { - final File root = getScanDirectory(); - final String filePath = ""; - parseFile(root, filePath); + final File rootDirectory = getScanDirectory(); + if (rootDirectory != null) { + final String filePath = ""; + parseFile(rootDirectory, filePath); + } else { + final List rootFiles = getScanFiles(); + for (final File rootFile : rootFiles) { + final String filePath = ""; + parseFile(rootFile, filePath); + } + } } /** @@ -200,9 +197,6 @@ private Archive createAndAddArchive(final String fileName, final ArchiveMetaInfo * @return true, if is archive */ private static boolean isArchiveDirectory(final File dir) { - if (!dir.isDirectory()) { - return false; - } final File metaInfEntry = findEntry(dir, "META-INF"); if (metaInfEntry == null || !metaInfEntry.isDirectory()) { return false; diff --git a/licensescout-core/src/test/java/org/aposin/licensescout/execution/SimpleExecutionTest.java b/licensescout-core/src/test/java/org/aposin/licensescout/execution/SimpleExecutionTest.java index 8ab14889..35cd9a7b 100644 --- a/licensescout-core/src/test/java/org/aposin/licensescout/execution/SimpleExecutionTest.java +++ b/licensescout-core/src/test/java/org/aposin/licensescout/execution/SimpleExecutionTest.java @@ -56,7 +56,7 @@ public void testExecutionJavaNoOutputs() throws Exception { * @throws Exception */ @Test - public void testExecutionJavaWithOutputs() throws Exception { + public void testExecutionJavaWithOutputsScanDirectory() throws Exception { final File scanDirectory = new File("src/test/resources/scans/empty"); final ArrayList outputs = createTripleOutput(); final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanDirectory, @@ -64,6 +64,21 @@ public void testExecutionJavaWithOutputs() throws Exception { assertExecution(executionParameters); } + /** + * Test case for the method {@link Executor#execute()}. + * + * @throws Exception + */ + @Test + public void testExecutionJavaWithOutputsScanFiles() throws Exception { + final File scanDirectory = new File("src/test/resources/scans/empty"); + final ArrayList outputs = createTripleOutput(); + final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanDirectory, + outputs); + executionParameters.setScanLocation(new ScanLocation(Arrays.asList(scanDirectory))); + assertExecution(executionParameters); + } + /** * Test case for the method {@link Executor#execute()}. * @@ -243,6 +258,21 @@ public void testExecutionJavaNotExistingScanDirectory() throws Exception { assertExecution(executionParameters); } + /** + * Test case for the method {@link Executor#execute()}. + * + * @throws Exception + */ + @Test(expected = LicenseScoutExecutionException.class) + public void testExecutionJavaNoScanLocation() throws Exception { + // TODO: clean up + final File scanDirectory = new File("not_existing"); + final ExecutionParameters executionParameters = createExecutionParametersNoOutputs(ArchiveType.JAVA, + scanDirectory); + executionParameters.setScanLocation(null); + assertExecution(executionParameters); + } + /** * Test case for the method {@link Executor#execute()}. *