Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC for moving some configurations from properties to CouchDB #2815

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
* Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.datahandler.db;

import com.ibm.cloud.cloudant.v1.Cloudant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.SW360ConfigKeys;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.eclipse.sw360.datahandler.thrift.ConfigContainer;
import org.eclipse.sw360.datahandler.thrift.ConfigFor;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.users.User;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Collections;

public class SW360ConfigsDatabaseHandler {
private final Logger log = LogManager.getLogger(this.getClass());
private final ConfigContainerRepository repository;
private static final Map<String, String> configsMapInMem = new HashMap<>();
private static boolean updating = false;

public SW360ConfigsDatabaseHandler(Cloudant httpClient, String dbName) {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(httpClient, dbName);
repository = new ConfigContainerRepository(db);
try {
loadToConfigsMapInMem(repository.getByConfigFor(ConfigFor.SW360_CONFIGURATION));
} catch (IllegalStateException exception) {
log.error(exception.getMessage());
loadToConfigsMapInMem(null);
}
}

private void loadToConfigsMapInMem(ConfigContainer configContainer) {
configsMapInMem
.put(SW360ConfigKeys.SPDX_DOCUMENT_ENABLED, getOrDefault(configContainer, SW360ConfigKeys.SPDX_DOCUMENT_ENABLED, "false"));
}

private String getOrDefault(ConfigContainer configContainer, String configKey, String defaultValue) {
if (configContainer == null)
return defaultValue;
return configContainer.getConfigKeyToValues().getOrDefault(configKey, new HashSet<>()).stream().findFirst().orElse(defaultValue);
}

private boolean isBooleanValue(String value) {
return (value != null) && (value.equals("true") || value.equals("false"));
}

private boolean isConfigValid(String configKey, String configValue) {
switch (configKey) {
case SW360ConfigKeys.SPDX_DOCUMENT_ENABLED:
return isBooleanValue(configValue);
default:
return false;
}
}

private void updateExistingConfigs(Map<String, Set<String>> existingConfigs, Map<String, String> updatedConfigs) throws SW360Exception {
for (Map.Entry<String, String> config : updatedConfigs.entrySet()) {
if (!isConfigValid(config.getKey(), config.getValue())) {
updating = false;
throw new SW360Exception("Invalid config: [" + config.getKey() + " : " + config.getValue() + "]");
}
existingConfigs.put(config.getKey(), Collections.singleton(config.getValue()));
}
}

public RequestStatus updateSW360Configs(Map<String, String> updatedConfigs, User user) throws SW360Exception {
if (!PermissionUtils.isAdmin(user))
return RequestStatus.ACCESS_DENIED;

if (updating) {
return RequestStatus.IN_USE;
}

updating = true;

if (updatedConfigs == null || updatedConfigs.isEmpty()) {
updating = false;
return RequestStatus.SUCCESS;
}

ConfigContainer configContainer;
try {
configContainer = repository.getByConfigFor(ConfigFor.SW360_CONFIGURATION);
} catch (IllegalStateException exception) {
log.error(exception.getMessage());
configContainer = new ConfigContainer(ConfigFor.SW360_CONFIGURATION, new HashMap<>());
}
updateExistingConfigs(configContainer.getConfigKeyToValues(), updatedConfigs);

if (configContainer.getId() != null) {
repository.update(configContainer);
} else {
repository.add(configContainer);
}

loadToConfigsMapInMem(configContainer);
updating = false;
return RequestStatus.SUCCESS;
}


public Map<String, String> getSW360Configs() {
return configsMapInMem;
}

public String getConfigByKey(String key) {
return configsMapInMem.get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

import static org.eclipse.sw360.datahandler.common.CommonUtils.isNotNullEmptyOrWhitespace;
import static org.eclipse.sw360.datahandler.common.CommonUtils.isNullEmptyOrWhitespace;
import static org.eclipse.sw360.datahandler.common.SW360ConfigKeys.SPDX_DOCUMENT_ENABLED;

public class SpdxBOMImporter {
private static final Logger log = LogManager.getLogger(SpdxBOMImporter.class);
Expand Down Expand Up @@ -205,6 +206,7 @@ private RequestSummary importSpdxBOM(InputStream inputStream, AttachmentContent
} catch (InvalidSPDXAnalysisException | NullPointerException e) {
log.error("Can not open file to SpdxDocument " +e);
}

return requestSummary;
}

Expand Down Expand Up @@ -751,7 +753,7 @@ private Optional<SpdxBOMImporterSink.Response> importAsRelease(SpdxElement relat
spdxPackages.add(spdxPackageCheck);
}
importAsReleaseFromSpdxDocument(spdxPackages,attachmentContent,spdxDocument);
if (SW360Constants.SPDX_DOCUMENT_ENABLED) {
if (Boolean.parseBoolean(SW360Utils.getConfigByKey(SPDX_DOCUMENT_ENABLED))) {
try {
importSpdxDocument(response.getId(), spdxDocument, spdxPackage);
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -799,7 +801,7 @@ private void importAsReleaseFromSpdxDocument(List<SpdxPackage> packages, Attach
release.setAttachments(Collections.singleton(attachment));
}
final SpdxBOMImporterSink.Response response = sink.addRelease(release);
if (SW360Constants.SPDX_DOCUMENT_ENABLED) {
if (Boolean.parseBoolean(SW360Utils.getConfigByKey(SPDX_DOCUMENT_ENABLED))) {
try {
importSpdxDocument(response.getId(), spdxDocument, spdxElement);
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -938,7 +940,6 @@ private Map<String, ProjectReleaseRelationship> makeReleaseIdToProjectRelationsh
}));
}


private Optional<SpdxBOMImporterSink.Response> importAsProject(SpdxElement spdxElement, AttachmentContent attachmentContent, User user) throws SW360Exception, InvalidSPDXAnalysisException {
if (spdxElement instanceof SpdxPackage) {
final SpdxPackage spdxPackage = (SpdxPackage) spdxElement;
Expand Down
37 changes: 37 additions & 0 deletions backend/configurations/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
~ Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
~
~ This program and the accompanying materials are made
~ available under the terms of the Eclipse Public License 2.0
~ which is available at https://www.eclipse.org/legal/epl-2.0/
~
~ SPDX-License-Identifier: EPL-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.sw360</groupId>
<artifactId>backend</artifactId>
<version>19.0.0</version>
</parent>

<artifactId>backend-configurations</artifactId>
<packaging>war</packaging>
<name>backend-configurations</name>
<build><finalName>configurations</finalName></build>
<properties>
<artifact.deploy.dir>${backend.deploy.dir}</artifact.deploy.dir>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.sw360</groupId>
<artifactId>backend-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
* Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.configurations;

import org.eclipse.sw360.datahandler.common.DatabaseSettings;
import org.eclipse.sw360.datahandler.db.SW360ConfigsDatabaseHandler;
import org.eclipse.sw360.datahandler.thrift.ConfigContainer;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.configurations.SW360ConfigsService;
import org.eclipse.sw360.datahandler.thrift.users.User;
import java.util.Map;

public class SW360ConfigsHandler implements SW360ConfigsService.Iface {
private final SW360ConfigsDatabaseHandler sw360ConfigsDatabaseHandler;

public SW360ConfigsHandler() {
sw360ConfigsDatabaseHandler = new SW360ConfigsDatabaseHandler(DatabaseSettings.getConfiguredClient(), DatabaseSettings.COUCH_DB_CONFIG);
}

@Override
public RequestStatus createSW360Configs(ConfigContainer newConfig) {
return RequestStatus.SUCCESS;
}

@Override
public RequestStatus updateSW360Configs(Map<String, String> updatedConfigs, User user) throws SW360Exception {
return sw360ConfigsDatabaseHandler.updateSW360Configs(updatedConfigs, user);
}

@Override
public Map<String, String> getSW360Configs() {
return sw360ConfigsDatabaseHandler.getSW360Configs();
}

@Override
public String getConfigByKey(String key) {
return sw360ConfigsDatabaseHandler.getConfigByKey(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
* Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.configurations;

import org.apache.thrift.protocol.TCompactProtocol;
import org.eclipse.sw360.datahandler.thrift.configurations.SW360ConfigsService;
import org.eclipse.sw360.projects.Sw360ThriftServlet;

public class SW360ConfigsServlet extends Sw360ThriftServlet {
public SW360ConfigsServlet() {
super(new SW360ConfigsService.Processor<>(new SW360ConfigsHandler()), new TCompactProtocol.Factory());
}
}
35 changes: 35 additions & 0 deletions backend/configurations/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
~ Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
~
~ This program and the accompanying materials are made
~ available under the terms of the Eclipse Public License 2.0
~ which is available at https://www.eclipse.org/legal/epl-2.0/
~
~ SPDX-License-Identifier: EPL-2.0
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>SW360 Configurations Service</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<listener>
<listener-class>org.eclipse.sw360.SW360ServiceContextListener</listener-class>
</listener>

<servlet>
<servlet-name>SW360ConfigsService</servlet-name>
<servlet-class>org.eclipse.sw360.configurations.SW360ConfigsServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SW360ConfigsService</servlet-name>
<url-pattern>/thrift</url-pattern>
</servlet-mapping>

</web-app>
22 changes: 22 additions & 0 deletions backend/configurations/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
~ Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
~ Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
~
~ This program and the accompanying materials are made
~ available under the terms of the Eclipse Public License 2.0
~ which is available at https://www.eclipse.org/legal/epl-2.0/
~
~ SPDX-License-Identifier: EPL-2.0
~ Description: Welcome file for SW360 Config Health Service
-->

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome to the SW360 Configurations Service</title>
</head>

<body>
<h2>Welcome to the SW360 Configurations Service!</h2>
</body>
</html>
1 change: 1 addition & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<module>attachments</module>
<module>components</module>
<module>cvesearch</module>
<module>configurations</module>
<module>fossology</module>
<module>health</module>
<module>licenseinfo</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright TOSHIBA CORPORATION, 2024. Part of the SW360 Portal Project.
* Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2024. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.datahandler.common;

public class SW360ConfigKeys {
public static final String SPDX_DOCUMENT_ENABLED = "spdx.document.enabled";
public static final String ENABLE_FLEXIBLE_PROJECT_RELEASE_RELATIONSHIP = "enable.flexible.project.release.relationship";
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public class SW360Constants {
public static final String TOTAL_FILE_COUNT = "totalFileCount";
public static final String SVM_COMPONENT_ID;
public static final String SVM_MONITORINGLIST_ID;
public static final Boolean SPDX_DOCUMENT_ENABLED;
public static final String MAINLINE_COMPONENT_ID;
public static final String SVM_COMPONENT_ID_KEY;
public static final String SVM_SHORT_STATUS;
Expand Down Expand Up @@ -231,7 +230,6 @@ private SW360Constants() {
SVM_SHORT_STATUS_KEY = props.getProperty("svm.short.status.key", "");
SVM_SCHEDULER_EMAIL = props.getProperty("svm.scheduler.email", "");
SVM_MONITORINGLIST_ID = props.getProperty("svm.monitoringlist.id", "");
SPDX_DOCUMENT_ENABLED = Boolean.parseBoolean(props.getProperty("spdx.document.enabled", "false"));
DATA_HANDLER_POM_FILE_PATH = props.getProperty("datahandler.pom.file.path", "/META-INF/maven/org.eclipse.sw360/datahandler/pom.xml");
PACKAGE_PORTLET_WRITE_ACCESS_USER_ROLE = UserGroup.valueOf(props.getProperty("package.portlet.write.access.usergroup", UserGroup.USER.name()));
IS_PACKAGE_PORTLET_ENABLED = Boolean.parseBoolean(props.getProperty("package.portlet.enabled", "true"));
Expand Down
Loading
Loading