01 /*
02 * Copyright 2008-2017 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 griffon.util.Instantiator;
20 import griffon.util.ResourceBundleReader;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import javax.annotation.Nonnull;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.ResourceBundle;
32
33 import static griffon.util.GriffonNameUtils.requireNonBlank;
34 import static java.util.Objects.requireNonNull;
35
36 /**
37 * @author Andres Almiray
38 * @since 2.11.0
39 */
40 @Named("class")
41 public class ClassResourceBundleLoader extends AbstractResourceBundleLoader {
42 private static final Logger LOG = LoggerFactory.getLogger(ClassResourceBundleLoader.class);
43 protected static final String CLASS_SUFFIX = ".class";
44
45 protected final Instantiator instantiator;
46 protected final ResourceBundleReader resourceBundleReader;
47
48 @Inject
49 public ClassResourceBundleLoader(@Nonnull Instantiator instantiator,
50 @Nonnull ResourceHandler resourceHandler,
51 @Nonnull ResourceBundleReader resourceBundleReader) {
52 super(resourceHandler);
53 this.instantiator = requireNonNull(instantiator, "Argument 'instantiator' must not be null");
54 this.resourceBundleReader = requireNonNull(resourceBundleReader, "Argument 'resourceBundleReader' must not be null");
55 }
56
57 @Nonnull
58 @Override
59 public Collection<ResourceBundle> load(@Nonnull String name) {
60 requireNonBlank(name, ERROR_FILENAME_BLANK);
61 List<ResourceBundle> bundles = new ArrayList<>();
62 URL resource = getResourceAsURL(name, CLASS_SUFFIX);
63 if (null != resource) {
64 String url = resource.toString();
65 String className = name.replace('/', '.');
66 try {
67 Class klass = loadClass(className);
68 if (ResourceBundle.class.isAssignableFrom(klass)) {
69 bundles.add(resourceBundleReader.read(newInstance(klass)));
70 }
71 } catch (ClassNotFoundException e) {
72 // ignore
73 } catch (Exception e) {
74 LOG.warn("An error occurred while loading resource bundle " + name + " from " + url, e);
75 }
76 }
77 return bundles;
78 }
79
80 @Nonnull
81 protected Class<?> loadClass(String className) throws ClassNotFoundException {
82 return getResourceHandler().classloader().loadClass(className);
83 }
84
85 @Nonnull
86 protected ResourceBundle newInstance(Class<?> klass) throws IllegalAccessException, InstantiationException {
87 return instantiator.instantiate((Class<? extends ResourceBundle>) klass);
88 }
89 }
|