Skip to content

Commit

Permalink
Moving logic for loading resourcebUndle to Interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Nov 2, 2023
1 parent 6fbfca9 commit e85af75
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
package de.cuioss.portal.common.bundle;

import java.io.Serializable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;

import javax.annotation.Priority;

import de.cuioss.tools.logging.CuiLogger;

/**
* Used for configuring ResourceBundles. Implementations should provide a
* {@link Priority}. Because of the overwriting mechanics a higher
Expand All @@ -32,11 +37,33 @@
*/
public interface ResourceBundleLocator extends Serializable {

CuiLogger LOGGER = new CuiLogger(ResourceBundleLocator.class);

/**
* @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.
*/
Optional<String> getBundlePath();

/**
* @return an {@link Optional} {@link ResourceBundle} derived by the path of
* {@link #getBundlePath()}
*/
default Optional<ResourceBundle> getBundle(Locale locale) {
var bundlePath = getBundlePath();
if (bundlePath.isEmpty()) {
LOGGER.debug("No ResourceBundle to be loaded is present");
return Optional.empty();
}
try {
Optional<ResourceBundle> loadedBundle = Optional.of(ResourceBundle.getBundle(bundlePath.get(), locale));
LOGGER.debug("Successfully loaded ResourceBundle '%s'", bundlePath.get());
return loadedBundle;
} catch (MissingResourceException e) {
LOGGER.warn("Unable to load ResourceBundle '%s'".formatted(bundlePath.get()), e);
return Optional.empty();
}
}

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

import javax.annotation.PostConstruct;
Expand All @@ -29,7 +30,7 @@
import javax.inject.Inject;

import de.cuioss.portal.common.priority.PortalPriorities;
import de.cuioss.tools.collect.CollectionLiterals;
import de.cuioss.tools.collect.CollectionBuilder;
import de.cuioss.tools.logging.CuiLogger;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -62,31 +63,34 @@ public class ResourceBundleRegistry implements Serializable {
* The computed / resolved names in the correct order
*/
@Getter
private List<String> resolvedPaths;
private List<ResourceBundleLocator> resolvedPaths;

/**
* Initializes the bean. See class documentation for expected result.
*/
@PostConstruct
@SuppressWarnings("java:S3655") // owolff: false positive isPresent is checked
void initBean() {
final List<String> finalPaths = new ArrayList<>();
final var finalPaths = new CollectionBuilder<ResourceBundleLocator>();
// Sort according to ResourceBundleDescripor#order
final List<ResourceBundleLocator> sortedLocators = PortalPriorities.sortByPriority(mutableList(locatorList));
final var foundPaths = new ArrayList<String>();
for (final ResourceBundleLocator descriptor : sortedLocators) {
var path = descriptor.getBundlePath();
if (path.isPresent()) {
// Check whether the path defines an existing ResourceBundle
var resolvedBundle = descriptor.getBundle(Locale.getDefault());
if (resolvedBundle.isPresent() && descriptor.getBundlePath().isPresent()) {
// Check whether path is unique
if (finalPaths.contains(path.get())) {
log.error(ERR_DUPLICATE_RESOURCE_PATH, path);
if (foundPaths.contains(descriptor.getBundlePath().get())) {
log.warn(ERR_DUPLICATE_RESOURCE_PATH, descriptor);
} else {
log.debug("Adding Bundle '%s'", descriptor);
finalPaths.add(descriptor);
foundPaths.add(descriptor.getBundlePath().get());
}
finalPaths.add(path.get());
} else {
log.debug("No valid path given, ignoring");
log.warn("Ignoring Bundle '%s'", descriptor);
}
}
log.debug("Determined ResourceBundle-Oath: '%s'", finalPaths);
resolvedPaths = CollectionLiterals.immutableList(finalPaths);
resolvedPaths = finalPaths.toImmutableList();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,9 @@ void actOnLocaleChangeEven(@Observes @LocaleChangeEvent final Locale newLocale)
private List<ResourceBundle> getResolvedBundles() {
if (null == resolvedBundles) {
var builder = new CollectionBuilder<ResourceBundle>();
for (final String path : resourceBundleRegistry.getResolvedPaths()) {
try {
builder.add(ResourceBundle.getBundle(path, localeProvider.get()));
LOGGER.debug("Successfully loaded Resource-Bundle '%s'");
} catch (MissingResourceException e) {
LOGGER.warn("Unable to load Resource-Bundle '%s'".formatted(path), e);
}

for (final ResourceBundleLocator path : resourceBundleRegistry.getResolvedPaths()) {
path.getBundle(localeProvider.get()).ifPresent(builder::add);
}
resolvedBundles = builder.toImmutableList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.cuioss.portal.common.bundle;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Locale;

import org.junit.jupiter.api.Test;

import de.cuioss.portal.common.bundle.support.HighPrioBundles;
import de.cuioss.portal.common.bundle.support.InvalidBundelPath;
import de.cuioss.portal.common.bundle.support.MissingBundle;

class ResourceBundleLocatorTest {

@Test
void shouldHandleHappyCase() {
assertTrue(new HighPrioBundles().getBundle(Locale.GERMANY).isPresent());
}

@Test
void shouldHandleMissingPath() {
assertFalse(new MissingBundle().getBundle(Locale.GERMANY).isPresent());
}

@Test
void shouldHandleInvalidPath() {
assertFalse(new InvalidBundelPath().getBundle(Locale.GERMANY).isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void shouldInitPortalResourceBundles() {
assertNotNull(underTest);
assertNotNull(underTest.getResolvedPaths());
assertEquals(2, underTest.getResolvedPaths().size());
assertEquals(HighPrioBundles.HIGH_1, underTest.getResolvedPaths().get(0));
assertEquals(MediumPrioBundles.MEDIUM_1, underTest.getResolvedPaths().get(1));
assertEquals(HighPrioBundles.HIGH_1, underTest.getResolvedPaths().get(0).getBundlePath().get());
assertEquals(MediumPrioBundles.MEDIUM_1, underTest.getResolvedPaths().get(1).getBundlePath().get());
}

// TODO : verify log error msgs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.cuioss.portal.common.bundle.support;

import java.util.Optional;

import de.cuioss.portal.common.bundle.ResourceBundleLocator;

public class InvalidBundelPath implements ResourceBundleLocator {

private static final long serialVersionUID = 5363360633544306322L;

@Override
public Optional<String> getBundlePath() {
return Optional.of("de.not.there");
}

}

0 comments on commit e85af75

Please sign in to comment.