Skip to content

Commit

Permalink
feat: Load service dynamically based on configuration property
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Mar 26, 2024
1 parent 8ac4581 commit dc3e470
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.lpvs.entity.enums.LPVSPullRequestStatus;
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.service.scan.LPVSDetectService;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service;
package com.lpvs.service.scan;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.lpvs.service.LPVSGitHubConnectionService;
import com.lpvs.service.LPVSGitHubService;
import com.lpvs.service.LPVSLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
Expand All @@ -23,10 +25,8 @@

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.service.scanner.scanoss.LPVSScanossDetectService;
import com.lpvs.util.LPVSCommentUtil;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -36,16 +36,6 @@
@Slf4j
public class LPVSDetectService {

/**
* The type of license detection scanner.
*/
private String scannerType;

/**
* Service responsible for performing license detection using the ScanOSS scanner.
*/
private LPVSScanossDetectService scanossDetectService;

/**
* Service responsible for establishing and managing connections to the GitHub API.
*/
Expand All @@ -61,6 +51,11 @@ public class LPVSDetectService {
*/
private LPVSGitHubService gitHubService;

/**
* Service responsible for initialization of the scanner.
*/
private LPVSScanService scanService;

/**
* GitHub pull request used to trigger a single license scan (optional).
*/
Expand All @@ -82,29 +77,24 @@ public class LPVSDetectService {
* Constructs an instance of LPVSDetectService with the specified parameters.
*
* @param scannerType The type of license detection scanner.
* @param isInternal
* @param gitHubConnectionService Service for connecting to the GitHub API.
* @param scanossDetectService Service for license detection using ScanOSS.
* @param licenseService Service for license conflict analysis.
* @param gitHubService Service for GitHub connection and operation.
* @param scanServiceFactory
*/
@Autowired
public LPVSDetectService(
@Value("${scanner:scanoss}") String scannerType,
@Value("${internal:false}") boolean isInternal,
LPVSGitHubConnectionService gitHubConnectionService,
LPVSScanossDetectService scanossDetectService,
LPVSLicenseService licenseService,
LPVSGitHubService gitHubService) {
this.scannerType = scannerType;
LPVSGitHubService gitHubService,
LPVSScanServiceFactory scanServiceFactory) {
this.gitHubConnectionService = gitHubConnectionService;
this.scanossDetectService = scanossDetectService;
this.licenseService = licenseService;
this.gitHubService = gitHubService;
}

/**
* Initializes the LPVSDetectService bean and logs the selected license detection scanner.
*/
@PostConstruct
private void init() {
this.scanService = scanServiceFactory.createScanService(scannerType, isInternal);
log.info("License detection scanner: " + scannerType);
}

Expand Down Expand Up @@ -166,10 +156,7 @@ public void runOneScan() {
* @throws Exception if an error occurs during the scan.
*/
public List<LPVSFile> runScan(LPVSQueue webhookConfig, String path) throws Exception {
if (scannerType.equals("scanoss")) {
scanossDetectService.runScan(webhookConfig, path);
return scanossDetectService.checkLicenses(webhookConfig);
}
return new ArrayList<>();
scanService.runScan(webhookConfig, path);
return scanService.checkLicenses(webhookConfig);
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/lpvs/service/scan/LPVSScanService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scan;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;

import java.util.List;

/**
* Interface representing a service for scanning files and checking licenses.
*/
public interface LPVSScanService {

/**
* Runs a scan on the specified path using the provided webhook configuration.
*
* @param webhookConfig The webhook configuration to use for the scan.
* @param path The path to the file or directory to scan.
* @throws Exception if an error occurs during the scan process.
*/
void runScan(LPVSQueue webhookConfig, String path) throws Exception;

/**
* Checks licenses for files using the provided webhook configuration.
*
* @param webhookConfig The webhook configuration to use for checking licenses.
* @return A list of LPVSFile objects representing files with detected licenses.
*/
List<LPVSFile> checkLicenses(LPVSQueue webhookConfig);
}
50 changes: 50 additions & 0 deletions src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2024, Samsung Electronics Co., Ltd. All rights reserved.
*
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scan;

import java.lang.reflect.Constructor;

/**
* Factory class for creating instances of {@link LPVSScanService}.
*/
public class LPVSScanServiceFactory {

/**
* Creates a scan service based on the specified scanner type and configuration.
*
* @param scannerType The type of scanner to create.
* @param isInternal Flag indicating whether the scanner is internal or not.
* @return An instance of {@link LPVSScanService} corresponding to the specified scanner type.
* @throws IllegalArgumentException if the specified scanner type is not supported or if an error occurs during
* the creation process.
*/
public LPVSScanService createScanService(String scannerType, boolean isInternal) {
try {
Class<?> serviceClass = Class.forName(getServiceClassName(scannerType, isInternal));
Constructor<?> constructor = serviceClass.getDeclaredConstructor();
return (LPVSScanService) constructor.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Error creating scan service for type: " + scannerType, e);
}
}

/**
* Gets the fully qualified class name of the scan service based on the specified scanner type and configuration.
*
* @param scannerType The type of scanner.
* @param isInternal Flag indicating whether the scanner is internal or not.
* @return The fully qualified class name of the scan service.
* @throws IllegalArgumentException if the specified scanner type is null or empty string.
*/
private String getServiceClassName(String scannerType, boolean isInternal) {
if (scannerType != null && !scannerType.isEmpty()) {
return "com.lpvs." + (isInternal ? "internal." : "") + "service.scan.scanner.LPVS" + scannerType.substring(0, 1).toUpperCase() + scannerType.substring(1) + "DetectService";
} else {
throw new IllegalArgumentException("Scanner type cannot be null or empty.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/
package com.lpvs.service.scanner.scanoss;
package com.lpvs.service.scan.scanner;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
Expand All @@ -13,6 +13,7 @@
import com.lpvs.entity.LPVSQueue;
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.service.LPVSLicenseService;
import com.lpvs.service.scan.LPVSScanService;
import com.lpvs.util.LPVSWebhookUtil;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -29,12 +30,12 @@
import static com.lpvs.util.LPVSFileUtil.getScanResultsJsonFilePath;

/**
* Service class responsible for interacting with the Scanoss scanner to detect licenses in files.
* Service class responsible for interacting with the Scanoss scanner to scan licenses in files.
* It handles the initiation of the scan, processing scan results, and checking for license conflicts.
*/
@Service
@Slf4j
public class LPVSScanossDetectService {
public class LPVSScanossDetectService implements LPVSScanService {

/**
* The service for managing licenses, providing operations related to licenses.
Expand Down Expand Up @@ -301,16 +302,16 @@ private class ScanossJsonStructure {
private String matched;
private String oss_lines;
private ArrayList<String> purl;
private String release_date;
private ScanossServer server;
private String source_hash;
private String status;
private String url;
private String url_hash;
private String vendor;
private String version;
private String release_date;
private ScanossServer server;
private String source_hash;
private String status;
private String url;
private String url_hash;
private String vendor;
private String version;

private class ScanossLicense {
private class ScanossLicense {
private String checklist_url;
private String copyleft;
private ArrayList<String> incompatible_with;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

/**
* The {@code scanoss} package contains services and utilities related to the Scanoss scanner integration.
* Scanoss is a tool used for scanning and analyzing open source software components to detect licenses,
* Scanoss is a tool used for scanning and analyzing open source software components to scan licenses,
* vulnerabilities, and other relevant information. This package encapsulates functionality for running
* Scanoss scans, processing scan results, and handling license-related operations.
* The package also contains utility classes for handling file paths, reading scan results, and managing licenses.
*/
package com.lpvs.service.scanner.scanoss;
package com.lpvs.service.scan.scanner;
Loading

0 comments on commit dc3e470

Please sign in to comment.