Skip to content

Commit

Permalink
Moving PortalVendorResourceBundleLocator from portal-core
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Aug 2, 2024
1 parent 0904bc9 commit d0af491
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.cuioss.portal.ui.runtime.application.bundle;

import de.cuioss.portal.common.bundle.ResourceBundleLocator;
import de.cuioss.portal.common.priority.PortalPriorities;
import de.cuioss.tools.logging.CuiLogger;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import lombok.EqualsAndHashCode;

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

import static de.cuioss.portal.configuration.PortalConfigurationDefaults.CUSTOM_BUNDLE_PATH;

/**
* Defines the vendor-specific bundle to be defined within a portal application,
* that is "de.cuioss.portal.i18n.vendor-messages" with the Priority
* {@link PortalPriorities#PORTAL_ASSEMBLY_LEVEL + 10}
*
* @author Matthias Walliczek
*/
@Priority(PortalPriorities.PORTAL_ASSEMBLY_LEVEL + 10)
@ApplicationScoped
@EqualsAndHashCode
public class PortalVendorResourceBundleLocator implements ResourceBundleLocator {

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

@Serial
private static final long serialVersionUID = -8478481710191113463L;

private String bundle;

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

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

@Override
public String toString() {
return "%s: Path='%s'".formatted(getClass().getName(), CUSTOM_BUNDLE_PATH);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.cuioss.portal.ui.runtime.application.bundle;

import de.cuioss.test.juli.junit5.EnableTestLogger;
import de.cuioss.test.valueobjects.junit5.contracts.ShouldHandleObjectContracts;
import jakarta.inject.Inject;
import lombok.Getter;
import org.jboss.weld.junit5.auto.EnableAutoWeld;

import java.util.Locale;

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

@EnableAutoWeld
@EnableTestLogger
class PortalVendorResourceBundleLocatorTest implements ShouldHandleObjectContracts<PortalVendorResourceBundleLocator> {

@Inject
@Getter
private PortalVendorResourceBundleLocator underTest;

void shouldHandleMissingBundle() {
assertFalse(underTest.getBundle(Locale.getDefault()).isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@
*/
package de.cuioss.portal.ui.runtime.support;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import org.jboss.weld.junit5.auto.AddBeanClasses;

import de.cuioss.portal.common.bundle.PortalResourceBundleBean;
import de.cuioss.portal.common.bundle.ResourceBundleRegistry;
import de.cuioss.portal.common.bundle.ResourceBundleWrapper;
import de.cuioss.portal.configuration.impl.bundles.PortalVendorResourceBundleLocator;
import de.cuioss.portal.ui.runtime.application.bundle.PortalDefaultResourceBundleLocator;
import de.cuioss.portal.ui.runtime.application.bundle.PortalVendorResourceBundleLocator;
import de.cuioss.portal.ui.test.mocks.PortalLocaleProducerMock;
import org.jboss.weld.junit5.auto.AddBeanClasses;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Using this annotations at type-level of a junit 5 test defines the basic
* Using this annotation at type-level of a junit 5 test defines the basic
* infrastructure for creating unit-tests containing the beans necessary for
* actual resolving of resource-bundle content.
*/
@Documented
@Retention(RUNTIME)
@Target(TYPE)
@AddBeanClasses({ PortalDefaultResourceBundleLocator.class, PortalResourceBundleBean.class,
@AddBeanClasses({PortalDefaultResourceBundleLocator.class, PortalResourceBundleBean.class,
PortalVendorResourceBundleLocator.class, ResourceBundleRegistry.class, ResourceBundleWrapper.class,
PortalLocaleProducerMock.class })
PortalLocaleProducerMock.class})
public @interface EnableResourceBundleSupport {
}

0 comments on commit d0af491

Please sign in to comment.