Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
- changed to actually using ScanLocation's list of files if present
Browse files Browse the repository at this point in the history
- documentation update
  • Loading branch information
pfistererm committed Mar 27, 2020
1 parent b716233 commit 78aa490
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ExecutionParameters {
private ArchiveType archiveType;

/**
* Directory to scan for archives.
* Location to scan for archives.
*/
private ScanLocation scanLocation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<File> 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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private VelocityContext createVelocityContext(final OutputResult outputResult,
final List<Archive> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -97,6 +97,9 @@ public final void setScanLocation(final ScanLocation scanLocation) {
/**
* Obtains the scan location.
*
* <p>Is intended to be used for logging. For processing, {@link #getScanDirectory()}
* and {@link #getScanFiles()} should be used.</p>
*
* @return the scan location
*/
protected final ScanLocation getScanLocation() {
Expand All @@ -114,6 +117,17 @@ protected final File getScanDirectory() {
return getScanLocation().getScanDirectory();
}

/**
* Obtains the files to scan.
*
* @return the scanDirectory
*
* @see #getScanDirectory()
*/
protected final List<File> getScanFiles() {
return scanLocation.getScanFiles();
}

/**
* Adds an archive to the list of found archives.
* @param foundArchive an archive
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,20 +37,8 @@
* a starting point.</p>
*
*/
// 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<String> specialArchiveNames = new ArrayList<>();
private final FinderHandler<File, FileSystemEntryContainer, File> fileSystemFinderHandler;

Expand Down Expand Up @@ -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<File> rootFiles = getScanFiles();
for (final File rootFile : rootFiles) {
final String filePath = "";
parseFile(rootFile, filePath);
}
}
}

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,29 @@ 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<ExecutionOutput> outputs = createTripleOutput();
final ExecutionParameters executionParameters = createExecutionParameters(ArchiveType.JAVA, scanDirectory,
outputs);
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<ExecutionOutput> 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()}.
*
Expand Down Expand Up @@ -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()}.
*
Expand Down

0 comments on commit 78aa490

Please sign in to comment.