DefaultCompositeResourceBundleBuilder.java
001 /*
002  * Copyright 2008-2017 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.util;
017 
018 import griffon.core.resources.ResourceHandler;
019 import griffon.util.Instantiator;
020 import griffon.util.PropertiesReader;
021 import griffon.util.PropertiesResourceBundle;
022 import griffon.util.ResourceBundleReader;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 
026 import javax.annotation.Nonnull;
027 import javax.inject.Inject;
028 import java.io.IOException;
029 import java.net.URL;
030 import java.util.ArrayList;
031 import java.util.Collection;
032 import java.util.List;
033 import java.util.Properties;
034 import java.util.ResourceBundle;
035 
036 import static griffon.util.GriffonNameUtils.requireNonBlank;
037 
038 /**
039  @author Andres Almiray
040  @since 2.0.0
041  */
042 public class DefaultCompositeResourceBundleBuilder extends AbstractCompositeResourceBundleBuilder {
043     private static final Logger LOG = LoggerFactory.getLogger(DefaultCompositeResourceBundleBuilder.class);
044     protected static final String PROPERTIES_SUFFIX = ".properties";
045     protected static final String CLASS_SUFFIX = ".class";
046 
047     protected final Instantiator instantiator;
048     protected final PropertiesReader propertiesReader;
049     protected final ResourceBundleReader resourceBundleReader;
050 
051     @Inject
052     public DefaultCompositeResourceBundleBuilder(@Nonnull Instantiator instantiator,
053                                                  @Nonnull ResourceHandler resourceHandler,
054                                                  @Nonnull PropertiesReader propertiesReader,
055                                                  @Nonnull ResourceBundleReader resourceBundleReader) {
056         super(resourceHandler);
057         this.instantiator = instantiator;
058         this.propertiesReader = propertiesReader;
059         this.resourceBundleReader = resourceBundleReader;
060     }
061 
062     @Nonnull
063     protected Collection<ResourceBundle> loadBundlesFor(@Nonnull String fileName) {
064         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
065         List<ResourceBundle> bundles = new ArrayList<>();
066         bundles.addAll(loadBundleFromClass(fileName));
067         bundles.addAll(loadBundleFromProperties(fileName));
068         return bundles;
069     }
070 
071     @Nonnull
072     protected Collection<ResourceBundle> loadBundleFromProperties(@Nonnull String fileName) {
073         requireNonBlank(fileName, ERROR_FILENAME_BLANK);
074         List<ResourceBundle> bundles = new ArrayList<>();
075         List<URL> resources = getResources(fileName, PROPERTIES_SUFFIX);
076         if (resources != null) {
077             for (URL resource : resources) {
078                 if (null == resource) { continue}
079                 try {
080                     Properties properties = propertiesReader.load(resource.openStream());
081                     bundles.add(new PropertiesResourceBundle(properties));
082                 catch (IOException e) {
083                     // ignore
084                 }
085             }
086         }
087         return bundles;
088     }
089 
090     @Nonnull
091     protected Collection<ResourceBundle> loadBundleFromClass(@Nonnull String fileName) {
092         List<ResourceBundle> bundles = new ArrayList<>();
093         URL resource = getResourceAsURL(fileName, CLASS_SUFFIX);
094         if (null != resource) {
095             String url = resource.toString();
096             String className = fileName.replace('/''.');
097             try {
098                 Class klass = loadClass(className);
099                 if (ResourceBundle.class.isAssignableFrom(klass)) {
100                     bundles.add(resourceBundleReader.read(newInstance(klass)));
101                 }
102             catch (ClassNotFoundException e) {
103                 // ignore
104             catch (Exception e) {
105                 LOG.warn("An error occurred while loading resource bundle " + fileName + " from " + url, e);
106             }
107         }
108         return bundles;
109     }
110 
111     protected Class<?> loadClass(String classNamethrows ClassNotFoundException {
112         return getResourceHandler().classloader().loadClass(className);
113     }
114 
115     protected ResourceBundle newInstance(Class<?> klassthrows IllegalAccessException, InstantiationException {
116         return instantiator.instantiate((Class<? extends ResourceBundle>klass);
117     }
118 }