001 /*
002 * Copyright 2008-2015 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 org.slf4j.Logger;
020 import org.slf4j.LoggerFactory;
021
022 import javax.annotation.Nonnull;
023 import javax.inject.Inject;
024 import java.io.IOException;
025 import java.net.URL;
026 import java.util.ArrayList;
027 import java.util.Collection;
028 import java.util.List;
029 import java.util.PropertyResourceBundle;
030 import java.util.ResourceBundle;
031
032 import static griffon.util.GriffonNameUtils.requireNonBlank;
033
034 /**
035 * @author Andres Almiray
036 * @since 2.0.0
037 */
038 public class DefaultCompositeResourceBundleBuilder extends AbstractCompositeResourceBundleBuilder {
039 private static final Logger LOG = LoggerFactory.getLogger(DefaultCompositeResourceBundleBuilder.class);
040 protected static final String PROPERTIES_SUFFIX = ".properties";
041 protected static final String CLASS_SUFFIX = ".class";
042
043 @Inject
044 public DefaultCompositeResourceBundleBuilder(@Nonnull ResourceHandler resourceHandler) {
045 super(resourceHandler);
046 }
047
048 @Nonnull
049 protected Collection<ResourceBundle> loadBundlesFor(@Nonnull String fileName) {
050 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
051 List<ResourceBundle> bundles = new ArrayList<>();
052 bundles.addAll(loadBundleFromClass(fileName));
053 bundles.addAll(loadBundleFromProperties(fileName));
054 return bundles;
055 }
056
057 @Nonnull
058 protected Collection<ResourceBundle> loadBundleFromProperties(@Nonnull String fileName) {
059 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
060 List<ResourceBundle> bundles = new ArrayList<>();
061 List<URL> resources = getResources(fileName, PROPERTIES_SUFFIX);
062 if (resources != null) {
063 for (URL resource : resources) {
064 if (null == resource) continue;
065 try {
066 bundles.add(new PropertyResourceBundle(resource.openStream()));
067 } catch (IOException e) {
068 // ignore
069 }
070 }
071 }
072 return bundles;
073 }
074
075 @Nonnull
076 protected Collection<ResourceBundle> loadBundleFromClass(@Nonnull String fileName) {
077 List<ResourceBundle> bundles = new ArrayList<>();
078 URL resource = getResourceAsURL(fileName, CLASS_SUFFIX);
079 if (null != resource) {
080 String url = resource.toString();
081 String className = fileName.replace('/', '.');
082 try {
083 Class<?> klass = loadClass(className);
084 if (ResourceBundle.class.isAssignableFrom(klass)) {
085 bundles.add(newInstance(klass));
086 }
087 } catch (ClassNotFoundException e) {
088 // ignore
089 } catch (Exception e) {
090 LOG.warn("An error occurred while loading resource bundle " + fileName + " from " + url, e);
091 }
092 }
093 return bundles;
094 }
095
096 protected Class<?> loadClass(String className) throws ClassNotFoundException {
097 return getResourceHandler().classloader().loadClass(className);
098 }
099
100 protected ResourceBundle newInstance(Class<?> klass) throws IllegalAccessException, InstantiationException {
101 return (ResourceBundle) klass.newInstance();
102 }
103 }
|