GroovyScriptResourceBundleLoader.java
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.groovy.util;
17 
18 import griffon.core.resources.ResourceHandler;
19 import griffon.inject.Evicts;
20 import griffon.util.ConfigReader;
21 import griffon.util.Instantiator;
22 import griffon.util.ResourceBundleReader;
23 import groovy.lang.Script;
24 import org.codehaus.griffon.runtime.util.ClassResourceBundleLoader;
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 @Evicts("class")
43 @Named("groovy")
44 public class GroovyScriptResourceBundleLoader extends ClassResourceBundleLoader {
45     protected static final String GROOVY_SUFFIX = ".groovy";
46     private final ConfigReader configReader;
47 
48     @Inject
49     public GroovyScriptResourceBundleLoader(@Nonnull Instantiator instantiator,
50                                             @Nonnull ResourceHandler resourceHandler,
51                                             @Nonnull ResourceBundleReader resourceBundleReader,
52                                             @Nonnull ConfigReader configReader) {
53         super(instantiator, resourceHandler, resourceBundleReader);
54         this.configReader = requireNonNull(configReader, "Argument 'configReader' 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, GROOVY_SUFFIX);
63         if (null != resource) {
64             bundles.add(new GroovyScriptResourceBundle(configReader, resource));
65             return bundles;
66         }
67 
68         resource = getResourceAsURL(name, CLASS_SUFFIX);
69         if (null != resource) {
70             String className = name.replace('/''.');
71             try {
72                 Class<?> klass = loadClass(className);
73                 if (Script.class.isAssignableFrom(klass)) {
74                     bundles.add(new GroovyScriptResourceBundle(configReader, (Class<? extends Script>klass));
75                     return bundles;
76                 }
77             catch (ClassNotFoundException e) {
78                 // ignore
79             }
80         }
81 
82         return super.load(name);
83     }
84 }