Skip to content

Commit

Permalink
adapt Resource-Bundle structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Oct 20, 2023
1 parent d297806 commit 82e9360
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
*
*/
package de.cuioss.portal.configuration;

import de.cuioss.portal.common.bundle.ResourceBundleWrapper;
import lombok.experimental.UtilityClass;

/**
* Defines Defaults for the portal-configuration system
*/
@UtilityClass
public class PortalConfigurationDefaults {

/**
* Defines the path for the optional bundle, that can be used for overwriting /
* extending the portal-built in messages. The content will be included to the
* Portal unified RessourcBundle, see {@link ResourceBundleWrapper}
*/
public static final String CUSTOM_BUNDLE_PATH = "i18n.custom-messages";

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@ToString
public class PropertiesConfigurationProvider implements ConfigurationSource {

private static final CuiLogger log = new CuiLogger(PropertiesConfigurationProvider.class);
private static final CuiLogger LOGGER = new CuiLogger(PropertiesConfigurationProvider.class);

private static final String PORTAL_503 = "Portal-503: Unable to load from properties file described by '{}', due to: '{}'";

Expand All @@ -62,7 +62,7 @@ public class PropertiesConfigurationProvider implements ConfigurationSource {
public PropertiesConfigurationProvider(final FileLoader fileLoader) {
requireNonNull(fileLoader, "fileLoader");
this.fileLoader = fileLoader;
log.info("Loading properties from: {}", fileLoader.getURL());
LOGGER.debug("Loading properties from: {}", fileLoader.getURL());
}

/**
Expand Down Expand Up @@ -94,16 +94,16 @@ public Map<String, String> getConfigurationMap() {
* errors it will return an empty {@link Properties}
*/
public Optional<Properties> getAsProperties() {
if ((null == fileLoader) || !fileLoader.isReadable()) {
log.error(PORTAL_503, fileLoader, MSG_READ_ERROR);
if (null == fileLoader || !fileLoader.isReadable()) {
LOGGER.error(PORTAL_503, fileLoader, MSG_READ_ERROR);
return Optional.empty();
}
final var properties = new Properties();
try (final var inputStream = new BufferedInputStream(fileLoader.inputStream())) {
properties.load(inputStream);
return Optional.of(properties);
} catch (final IOException e) {
log.error(PORTAL_503, fileLoader, e.getMessage(), e);
LOGGER.error(PORTAL_503, fileLoader, e.getMessage(), e);
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class PropertiesConfigSource extends AbstractPortalConfigSource implements FileConfigurationSource {

private static final CuiLogger log = new CuiLogger(PropertiesConfigSource.class);
private static final CuiLogger LOGGER = new CuiLogger(PropertiesConfigSource.class);

@Getter
protected String path;
Expand Down Expand Up @@ -96,7 +96,7 @@ public PropertiesConfigSource(final FileLoader fileLoader, final boolean optiona
properties = CollectionLiterals.immutableMap();
readable = false;
}
log.trace("Loaded data for {}: {}", getPath(), getProperties());
LOGGER.debug("Loaded data for {}: {}", getPath(), getProperties());
}

private void validateFileType() {
Expand Down
4 changes: 2 additions & 2 deletions modules/configuration/portal-configuration-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>Provides the implementation-classes for the
portal-configuration extensions</description>
<properties>
<awaitility.version>4.2.0</awaitility.version>
<version.waitility>4.2.0</version.waitility>
<maven.jar.plugin.automatic.module.name>
de.cuioss.portal.configuration.impl</maven.jar.plugin.automatic.module.name>
</properties>
Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<version>${version.waitility}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package de.cuioss.portal.configuration.impl.bundles;

import static de.cuioss.tools.collect.CollectionLiterals.immutableList;
import static de.cuioss.portal.configuration.PortalConfigurationDefaults.CUSTOM_BUNDLE_PATH;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
Expand All @@ -31,7 +30,6 @@
import de.cuioss.portal.common.priority.PortalPriorities;
import de.cuioss.tools.logging.CuiLogger;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
Expand All @@ -49,24 +47,28 @@ public class PortalVendorResourceBundleLocator implements ResourceBundleLocator

private static final CuiLogger log = new CuiLogger(PortalVendorResourceBundleLocator.class);

private static final String VENDOR_MESSAGES = "de.cuioss.portal.i18n.vendor-messages";

private static final long serialVersionUID = -8478481710191113463L;

@Getter
private List<String> configuredResourceBundles = immutableList(VENDOR_MESSAGES);
private String bundle;

/**
* Initializes the bean by loading the {@link ResourceBundle}
*/
@PostConstruct
public void initBean() {
try {
ResourceBundle.getBundle(VENDOR_MESSAGES, Locale.getDefault());
ResourceBundle.getBundle(CUSTOM_BUNDLE_PATH, Locale.getDefault());
bundle = CUSTOM_BUNDLE_PATH;
log.info("Custom messages found at '{}', ignoring.", CUSTOM_BUNDLE_PATH);
} catch (MissingResourceException e) {
log.info("vendor messages not found at '{}', ignored.", VENDOR_MESSAGES);
configuredResourceBundles = Collections.emptyList();
log.info("Custom messages not found at '{}', ignoring.", CUSTOM_BUNDLE_PATH);
bundle = null;
}
}

@Override
public Optional<String> getBundlePath() {
return Optional.ofNullable(bundle);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
@UtilityClass
public class LoaderUtils {

private static final CuiLogger LOG = new CuiLogger(LoaderUtils.class);
private static final CuiLogger LOGGER = new CuiLogger(LoaderUtils.class);

private static final String FILETYPE_NOT_PROVIDED_MSG = "Portal-151: Unsupported configuration file type: {}. Supported is: {}. If you want to support yaml files do not forget to add 'portal-configuration-yaml' to the deployment";

Expand All @@ -55,7 +55,7 @@ public class LoaderUtils {
*/
public static Map<String, String> loadConfigurationFromSource(final FileConfigurationSource source) {
if (null == source || isEmpty(source.getPath())) {
LOG.debug("Nothing to load found");
LOGGER.debug("Nothing to load found");
return Collections.emptyMap();
}
for (var resolver : loadResolver()) {
Expand All @@ -65,7 +65,7 @@ public static Map<String, String> loadConfigurationFromSource(final FileConfigur
}
}

LOG.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
LOGGER.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
return Collections.emptyMap();
}

Expand All @@ -78,7 +78,7 @@ public static Map<String, String> loadConfigurationFromSource(final FileConfigur
*/
public static Map<String, String> loadConfigurationFromSource(final FileLoader source) {
if (null == source || isEmpty(source.getFileName().getOriginalName())) {
LOG.debug("Nothing to load found");
LOGGER.debug("Nothing to load found");
return Collections.emptyMap();
}
for (var resolver : loadResolver()) {
Expand All @@ -88,7 +88,7 @@ public static Map<String, String> loadConfigurationFromSource(final FileLoader s
}
}

LOG.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
LOGGER.warn(FILETYPE_NOT_PROVIDED_MSG, source, retrieveSupportedSuffixes());
return Collections.emptyMap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package de.cuioss.portal.common.bundle;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

import javax.annotation.Priority;

Expand All @@ -33,8 +33,10 @@
public interface ResourceBundleLocator extends Serializable {

/**
* @return paths of the resource bundles
* @return paths of the resource bundles if it can be loaded. <em>Caution: </em>
* {@link ResourceBundleRegistry} assumes that only loadable paths are
* to be returned. Therefore each implementation must take care.
*/
List<String> getConfiguredResourceBundles();
Optional<String> getBundlePath();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -57,9 +55,6 @@ public class ResourceBundleRegistry implements Serializable {
/** "Portal-506: Duplicate ResourceBundlePath found for '{}'" */
public static final String ERR_DUPLICATE_RESOURCE_PATH = "Portal-506: Duplicate ResourceBundlePath found for '{}'";

/** "Portal-507: No ResourceBundle found with path " */
public static final String ERR_NO_RESOURCE_FOUND = "Portal-507: No ResourceBundle found with path ";

@Inject
Instance<ResourceBundleLocator> locatorList;

Expand All @@ -74,27 +69,25 @@ public class ResourceBundleRegistry implements Serializable {
*/
@PostConstruct
void initBean() {

var defaultLocale = Locale.getDefault();
final List<String> finalPaths = new ArrayList<>();
// Sort according to ResourceBundleDescripor#order
final List<ResourceBundleLocator> sortedLocators = PortalPriorities.sortByPriority(mutableList(locatorList));
for (final ResourceBundleLocator descriptor : sortedLocators) {
for (final String path : descriptor.getConfiguredResourceBundles()) {
var path = descriptor.getBundlePath();
if (path.isPresent()) {
// Check whether the path defines an existing ResourceBundle
try {
ResourceBundle.getBundle(path, defaultLocale);
// Check whether path is unique
if (finalPaths.contains(path)) {
log.error(ERR_DUPLICATE_RESOURCE_PATH, path);
}
finalPaths.add(path);
} catch (MissingResourceException e) {
log.error(ERR_NO_RESOURCE_FOUND + path, e);
// Check whether path is unique
if (finalPaths.contains(path.get())) {
log.error(ERR_DUPLICATE_RESOURCE_PATH, path);
}
finalPaths.add(path.get());
} else {
log.debug("No valid path given, ignoring");
}
}
log.debug("Determined ResourceBundle-Oath: '%s'", finalPaths);
resolvedPaths = CollectionLiterals.immutableList(finalPaths);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* <h2>Usage</h2>
* <p>
* The central element is
* {@link de.cuioss.portal.common.bundle.ResourceBundleWrapper} I and unifies
* all configured {@link java.util.ResourceBundle}s for the portal. In order to
* use it within a bean use:
* {@link de.cuioss.portal.common.bundle.ResourceBundleWrapper} It unifies all
* configured {@link java.util.ResourceBundle}s for the portal. In order to use
* it within a bean use:
* </p>
*
* <pre>
Expand All @@ -16,8 +16,8 @@
* private ResourceBundleWrapper resourceBundleWrapper;
* </pre>
*
* It is exposed as well as named bean "msgs" and can therefore used within
* xhtml as standard {@link java.util.ResourceBundle}:
* It is exposed as well as the named ResourceBundle "msgs" and can therefore
* used within xhtml as standard {@link java.util.ResourceBundle}:
*
* <pre>
* {@code #(msgs['page.dashboard.title'])}
Expand All @@ -26,10 +26,15 @@
* <h2>Configuration</h2>
* <p>
* Extending the {@link java.util.ResourceBundle}s is quite easy on a module
* level. You need to provide instance of
* level. You need to provide an instance of
* {@link de.cuioss.portal.common.bundle.ResourceBundleLocator} The actual
* configuration will be done with
* {@link de.cuioss.portal.common.bundle.ResourceBundleRegistry}
* </p>
* <p>
* On application-level you can use the extension point by adding a
* Resource-Bundle at 'i18n.custom-messages'. It will be loaded with highest
* priority and can therefore be used for overwriting portal-defaults and
* extending it.
*/
package de.cuioss.portal.common.bundle;
Loading

0 comments on commit 82e9360

Please sign in to comment.