-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Load scan services dynamically based on configuration properties (
#470) * feat: Load service dynamically based on configuration property Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com> * test: Add unit tests for new classes Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com> * fix: Add missing comments Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com> --------- Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
- Loading branch information
Showing
11 changed files
with
271 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/lpvs/service/scan/LPVSScanServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* 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.repository.LPVSLicenseRepository; | ||
import com.lpvs.service.LPVSLicenseService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Constructor; | ||
|
||
/** | ||
* Factory class for creating instances of {@link LPVSScanService}. | ||
*/ | ||
@Service | ||
public class LPVSScanServiceFactory { | ||
|
||
/** | ||
* Flag indicating whether the application is in debug mode. | ||
*/ | ||
@Value("${debug:false}") | ||
Boolean debug; | ||
|
||
/** | ||
* The service for managing licenses, providing operations related to licenses. | ||
*/ | ||
@Autowired LPVSLicenseService licenseService; | ||
|
||
/** | ||
* The repository for LPVSLicense entities, allowing database interactions for licenses. | ||
*/ | ||
@Autowired LPVSLicenseRepository lpvsLicenseRepository; | ||
|
||
/** | ||
* 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( | ||
Boolean.class, LPVSLicenseService.class, LPVSLicenseRepository.class); | ||
return (LPVSScanService) | ||
constructor.newInstance(debug, licenseService, lpvsLicenseRepository); | ||
} 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. | ||
*/ | ||
protected 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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Provides services related to scanning operations. | ||
* <p> | ||
* This package contains classes and interfaces that define services for performing scanning operations, | ||
* such as checking licenses, and managing scanning configurations. | ||
* </p> | ||
*/ | ||
package com.lpvs.service.scan; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.