01 /*
02 * Copyright 2008-2014 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.util;
17
18 import griffon.core.resources.ResourceHandler;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import javax.annotation.Nonnull;
23 import javax.inject.Inject;
24 import java.io.IOException;
25 import java.net.URL;
26 import java.util.*;
27
28 import static griffon.util.GriffonNameUtils.requireNonBlank;
29
30 /**
31 * @author Andres Almiray
32 * @since 2.0.0
33 */
34 public class DefaultCompositeResourceBundleBuilder extends AbstractCompositeResourceBundleBuilder {
35 private static final Logger LOG = LoggerFactory.getLogger(DefaultCompositeResourceBundleBuilder.class);
36 protected static final String PROPERTIES_SUFFIX = ".properties";
37 protected static final String CLASS_SUFFIX = ".class";
38
39 @Inject
40 public DefaultCompositeResourceBundleBuilder(@Nonnull ResourceHandler resourceHandler) {
41 super(resourceHandler);
42 }
43
44 @Nonnull
45 protected Collection<ResourceBundle> loadBundlesFor(@Nonnull String fileName) {
46 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
47 List<ResourceBundle> bundles = new ArrayList<>();
48 bundles.addAll(loadBundleFromClass(fileName));
49 bundles.addAll(loadBundleFromProperties(fileName));
50 return bundles;
51 }
52
53 @Nonnull
54 protected Collection<ResourceBundle> loadBundleFromProperties(@Nonnull String fileName) {
55 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
56 List<ResourceBundle> bundles = new ArrayList<>();
57 List<URL> resources = getResources(fileName, PROPERTIES_SUFFIX);
58 if (resources != null) {
59 for (URL resource : resources) {
60 if (null == resource) continue;
61 try {
62 bundles.add(new PropertyResourceBundle(resource.openStream()));
63 } catch (IOException e) {
64 // ignore
65 }
66 }
67 }
68 return bundles;
69 }
70
71 @Nonnull
72 protected Collection<ResourceBundle> loadBundleFromClass(@Nonnull String fileName) {
73 List<ResourceBundle> bundles = new ArrayList<>();
74 URL resource = getResourceAsURL(fileName, CLASS_SUFFIX);
75 if (null != resource) {
76 String url = resource.toString();
77 String className = fileName.replace('/', '.');
78 try {
79 Class<?> klass = loadClass(className);
80 if (ResourceBundle.class.isAssignableFrom(klass)) {
81 bundles.add(newInstance(klass));
82 }
83 } catch (ClassNotFoundException e) {
84 // ignore
85 } catch (IllegalAccessException | InstantiationException e) {
86 LOG.warn("An error occurred while loading resource bundle " + fileName + " from " + url, e);
87 }
88 }
89 return bundles;
90 }
91
92 protected Class<?> loadClass(String className) throws ClassNotFoundException {
93 return getResourceHandler().classloader().loadClass(className);
94 }
95
96 protected ResourceBundle newInstance(Class<?> klass) throws IllegalAccessException, InstantiationException {
97 return (ResourceBundle) klass.newInstance();
98 }
99 }
|