Skip to content

Commit

Permalink
Stabilized ResourceBundle-Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Dec 12, 2023
1 parent c50b84f commit 77dc01c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import java.util.ResourceBundle;
import java.util.Set;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.Application;

import de.cuioss.portal.common.cdi.PortalBeanManager;
import de.cuioss.tools.logging.CuiLogger;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
Expand Down Expand Up @@ -52,6 +55,8 @@ public class PortalResourceBundleBean extends ResourceBundle implements Serializ

private static final long serialVersionUID = 3953649686127640297L;

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

/** Lookup name for el-expression within views: "msgs" */
public static final String BUNDLE_NAME = "msgs";

Expand All @@ -74,4 +79,14 @@ public Set<String> keySet() {
return resourceBundleWrapper.keySet();
}

/**
* Factory Method creating a new instance with loading the contained
* {@link ResourceBundleWrapper}, {@link SessionScoped} from the CDR-COntext
*/
public static PortalResourceBundleBean resolveFromCDIContext() {
LOGGER.debug("Resolving PortalResourceBundleBean from CDI-Context");
var resourceBundleWrapper = PortalBeanManager.resolveRequiredBean(ResourceBundleWrapper.class);
return new PortalResourceBundleBean(resourceBundleWrapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ default Optional<ResourceBundle> getBundle(Locale locale) {
return Optional.empty();
}
try {
Optional<ResourceBundle> loadedBundle = Optional.of(ResourceBundle.getBundle(bundlePath.get(), locale));
Optional<ResourceBundle> loadedBundle = Optional.of(
ResourceBundle.getBundle(bundlePath.get(), locale, Thread.currentThread().getContextClassLoader()));
LOGGER.debug("Successfully loaded %s '%s' for '%s'", getClass().getName(), bundlePath.get(), locale);
return loadedBundle;
} catch (MissingResourceException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ void shouldReturnKeys() {
assertTrue(keys.size() > 60, "Found: " + keys.size());
}

@Test
void shouldLoadFromFactoryMethod() {
var underTest = PortalResourceBundleBean.resolveFromCDIContext();
assertEquals("Internal server error", underTest.getString("page.error.title"));
assertEquals(PortalResourceBundleWrapperImplTest.PORTAL_TITLE, underTest.getString("portal.title"));
}

@Override
public PortalResourceBundleBean getUnderTest() {
return new PortalResourceBundleBean(wrapper);
Expand Down

0 comments on commit 77dc01c

Please sign in to comment.