Skip to content

Commit

Permalink
chore(merge): release-10.2.0 into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bonita-ci committed Jun 28, 2024
2 parents a6a673a + 50e3acc commit 09417a3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.bonitasoft.console.common.server.preferences.properties;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
Expand All @@ -24,7 +23,6 @@
import org.apache.commons.io.FileUtils;
import org.bonitasoft.console.common.server.preferences.constants.WebBonitaConstantsUtils;
import org.bonitasoft.console.common.server.utils.PlatformManagementUtils;
import org.bonitasoft.engine.exception.BonitaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -41,19 +39,12 @@ public static ConfigurationFilesManager getInstance() {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationFilesManager.class.getName());

/*
* Map<realConfigurationFileName, File>
*/
private Map<String, File> tenantsConfigurationFiles = new HashMap<>();
private final Map<String, Properties> tenantConfigurations = new HashMap<>();

private final Map<String, File> tenantConfigurationFiles = new HashMap<>();

/*
* Map<propertiesFileName, Properties>
*/
private Map<String, Properties> platformConfigurations = new HashMap<>();

/*
* Map<configurationFileName, File>
*/
private final Map<String, File> platformConfigurationFiles = new HashMap<>();

public Properties getPlatformProperties(String propertiesFile) {
Expand All @@ -66,27 +57,25 @@ public Properties getPlatformProperties(String propertiesFile) {

Properties getAlsoCustomAndInternalPropertiesFromFilename(String propertiesFileName) {
Properties properties = new Properties();
try {
final Map<String, Properties> propertiesByFilename = getResources();
if (propertiesByFilename.containsKey(propertiesFileName)) {
properties.putAll(propertiesByFilename.get(propertiesFileName));
// if -internal properties also exists, merge key/value pairs:
final String internalSuffixedVersion = getSuffixedPropertyFilename(propertiesFileName, "-internal");
if (propertiesByFilename.containsKey(internalSuffixedVersion)) {
properties.putAll(propertiesByFilename.get(internalSuffixedVersion));
}
// if -custom properties also exists, merge key/value pairs (and overwrite previous values if same key name):
final String customSuffixedVersion = getSuffixedPropertyFilename(propertiesFileName, "-custom");
if (propertiesByFilename.containsKey(customSuffixedVersion)) {
properties.putAll(propertiesByFilename.get(customSuffixedVersion));
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("File " + propertiesFileName + " not found. Returning empty properties object.");
}
Properties tenantConfiguration = getTenantConfiguration(propertiesFileName);
if (tenantConfiguration != null) {
properties.putAll(tenantConfiguration);
// if -internal properties also exists, merge key/value pairs:
final String internalPropertyFilename = getSuffixedPropertyFilename(propertiesFileName, "-internal");
final Properties internalConfiguration = getTenantConfiguration(internalPropertyFilename);
if (internalConfiguration != null) {
properties.putAll(internalConfiguration);
}
// if -custom properties also exists, merge key/value pairs (and overwrite previous values if same key name):
final String customPropertyFilename = getSuffixedPropertyFilename(propertiesFileName, "-custom");
final Properties customConfiguration = getTenantConfiguration(customPropertyFilename);
if (customConfiguration != null) {
properties.putAll(customConfiguration);
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("File " + propertiesFileName + " not found. Returning empty properties object.");
}
} catch (IOException e) {
LOGGER.error("Cannot retrieve tenant configurations", e);
}
return properties;
}
Expand Down Expand Up @@ -124,75 +113,53 @@ public void setPlatformConfigurations(Map<String, byte[]> configurationFiles) th
}
}

public void setTenantConfigurationFiles(Map<String, byte[]> configurationFiles) throws IOException {
Map<String, File> tenantFiles = new HashMap<>();
public synchronized void setTenantConfigurationFiles(Map<String, byte[]> configurationFiles) throws IOException {
for (Map.Entry<String, byte[]> entry : configurationFiles.entrySet()) {
if (!entry.getKey().endsWith(".properties")) {
File file = new File(WebBonitaConstantsUtils.getTenantInstance().getTempFolder(), entry.getKey());
FileUtils.writeByteArrayToFile(file, entry.getValue());
tenantFiles.put(entry.getKey(), file);
}
}
tenantsConfigurationFiles = tenantFiles;
}

public void removeProperty(String propertiesFilename, String propertyName) throws IOException {
// Now internal behavior stores and removes from -internal file:
final String internalFilename = getSuffixedPropertyFilename(propertiesFilename, "-internal");
Map<String, Properties> resources = getResources();
Properties properties = resources.get(internalFilename);
if (properties != null) {
properties.remove(propertyName);
update(internalFilename, properties);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("File " + internalFilename + " not found. Cannot remove property '" + propertyName + "'.");
tenantConfigurationFiles.put(entry.getKey(), file);
}
tenantConfigurations.put(entry.getKey(),
ConfigurationFilesManager.getProperties(entry.getValue()));
}
}

private String getSuffixedPropertyFilename(String propertiesFilename, String suffix) {
return propertiesFilename.replaceAll("\\.properties$", suffix + ".properties");
}

private void update(String propertiesFilename, Properties properties) throws IOException {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
properties.store(byteArrayOutputStream, "");
getPlatformManagementUtils().updateConfigurationFile(propertiesFilename,
byteArrayOutputStream.toByteArray());
} catch (BonitaException e) {
throw new IOException(e);
}
}

PlatformManagementUtils getPlatformManagementUtils() {
return new PlatformManagementUtils();
}

Map<String, Properties> getResources() throws IOException {
return getPlatformManagementUtils().getTenantConfigurations();
}

public void setProperty(String propertiesFilename, String propertyName, String propertyValue) throws IOException {
Map<String, Properties> resources = getResources();
// Now internal behavior stores and removes from -internal file:
final String internalFilename = getSuffixedPropertyFilename(propertiesFilename, "-internal");
Properties properties = resources.get(internalFilename);
if (properties != null) {
properties.setProperty(propertyName, propertyValue);
update(internalFilename, properties);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("File " + internalFilename + " not found. Cannot remove property '" + propertyName + "'.");
public File getTenantConfigurationFile(String fileName) {
if (tenantConfigurationFiles.isEmpty()) {
try {
setTenantConfigurationFiles(getPlatformManagementUtils().readTenantConfigurationsFromEngine());
} catch (IOException e) {
LOGGER.error("Cannot retrieve tenant configuration files", e);
throw new RuntimeException(e);
}
}
return tenantConfigurationFiles.get(fileName);
}

public File getTenantConfigurationFile(String fileName) {
if (tenantsConfigurationFiles != null) {
return tenantsConfigurationFiles.get(fileName);
Properties getTenantConfiguration(String propertiesFilename) {
if (tenantConfigurations.isEmpty()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Tenant configuration file {} not yet in cache. Adding it.", propertiesFilename);
}
try {
setTenantConfigurationFiles(getPlatformManagementUtils().readTenantConfigurationsFromEngine());
} catch (IOException e) {
LOGGER.error("Cannot retrieve tenant configuration", e);
throw new RuntimeException(e);
}
} else if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Retrieving tenant configuration file {} from cache.", propertiesFilename);
}
return null;
return tenantConfigurations.get(propertiesFilename);
}

public File getPlatformConfigurationFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.bonitasoft.console.common.server.preferences.properties.ConfigurationFilesManager;
import org.bonitasoft.engine.api.ApiAccessType;
Expand Down Expand Up @@ -126,27 +124,20 @@ long getDefaultTenantId() {
}

/**
* Long => tenantId
* String => configuration file name
* Properties => content of the configuration file
* byte[] => content of the configuration file
*/
public Map<String, Properties> getTenantConfigurations() throws IOException {
public Map<String, byte[]> readTenantConfigurationsFromEngine() {
try {
final PlatformSession platformSession = platformLogin();
try {
Map<String, byte[]> clientTenantConfigurations = getPlatformAPI(platformSession)
.getClientTenantConfigurations();
Map<String, Properties> clientTenantConfigurationProperties = new HashMap<>();
for (Entry<String, byte[]> entry : clientTenantConfigurations.entrySet()) {
clientTenantConfigurationProperties.put(entry.getKey(),
ConfigurationFilesManager.getProperties(entry.getValue()));
}
return clientTenantConfigurationProperties;
return getPlatformAPI(platformSession).getClientTenantConfigurations();
} finally {
platformLogout(platformSession);
}
} catch (BonitaException e) {
throw new IOException(e);
} catch (BonitaException | IOException e) {
LOGGER.error("Cannot retrieve tenant configurations", e);
return Collections.emptyMap();
}
}

Expand Down
Loading

0 comments on commit 09417a3

Please sign in to comment.