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.groovy.util;
19
20 import griffon.core.resources.ResourceHandler;
21 import griffon.inject.Evicts;
22 import griffon.util.ConfigReader;
23 import griffon.util.Instantiator;
24 import griffon.util.ResourceBundleReader;
25 import groovy.lang.Script;
26 import org.codehaus.griffon.runtime.util.ClassResourceBundleLoader;
27
28 import javax.annotation.Nonnull;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.ResourceBundle;
36
37 import static griffon.util.GriffonNameUtils.requireNonBlank;
38 import static java.util.Objects.requireNonNull;
39
40 /**
41 * @author Andres Almiray
42 * @since 2.11.0
43 */
44 @Evicts("class")
45 @Named("groovy")
46 public class GroovyScriptResourceBundleLoader extends ClassResourceBundleLoader {
47 protected static final String GROOVY_SUFFIX = ".groovy";
48 private final ConfigReader configReader;
49
50 @Inject
51 public GroovyScriptResourceBundleLoader(@Nonnull Instantiator instantiator,
52 @Nonnull ResourceHandler resourceHandler,
53 @Nonnull ResourceBundleReader resourceBundleReader,
54 @Nonnull ConfigReader configReader) {
55 super(instantiator, resourceHandler, resourceBundleReader);
56 this.configReader = requireNonNull(configReader, "Argument 'configReader' 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, GROOVY_SUFFIX);
65 if (null != resource) {
66 bundles.add(new GroovyScriptResourceBundle(configReader, resource));
67 return bundles;
68 }
69
70 resource = getResourceAsURL(name, CLASS_SUFFIX);
71 if (null != resource) {
72 String className = name.replace('/', '.');
73 try {
74 Class<?> klass = loadClass(className);
75 if (Script.class.isAssignableFrom(klass)) {
76 bundles.add(new GroovyScriptResourceBundle(configReader, (Class<? extends Script>) klass));
77 return bundles;
78 }
79 } catch (ClassNotFoundException e) {
80 // ignore
81 }
82 }
83
84 return super.load(name);
85 }
86 }
|