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