GroovyAwareCompositeResourceBundleBuilder.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.util.ConfigReader;
20 import griffon.util.Instantiator;
21 import griffon.util.PropertiesReader;
22 import griffon.util.ResourceBundleReader;
23 import groovy.lang.Script;
24 import org.codehaus.griffon.runtime.util.DefaultCompositeResourceBundleBuilder;
25 
26 import javax.annotation.Nonnull;
27 import javax.inject.Inject;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.ResourceBundle;
33 
34 import static java.util.Objects.requireNonNull;
35 
36 /**
37  @author Andres Almiray
38  @since 2.0.0
39  */
40 public class GroovyAwareCompositeResourceBundleBuilder extends DefaultCompositeResourceBundleBuilder {
41     protected static final String GROOVY_SUFFIX = ".groovy";
42     private final ConfigReader configReader;
43 
44     @Inject
45     public GroovyAwareCompositeResourceBundleBuilder(@Nonnull Instantiator instantiator,
46                                                      @Nonnull ResourceHandler resourceHandler,
47                                                      @Nonnull PropertiesReader propertiesReader,
48                                                      @Nonnull ResourceBundleReader resourceBundleReader,
49                                                      @Nonnull ConfigReader configReader) {
50         super(instantiator, resourceHandler, propertiesReader, resourceBundleReader);
51         this.configReader = requireNonNull(configReader, "Argument 'reader' must not be null");
52     }
53 
54     @Nonnull
55     @Override
56     @SuppressWarnings("unchecked")
57     protected Collection<ResourceBundle> loadBundleFromClass(@Nonnull String fileName) {
58         List<ResourceBundle> bundles = new ArrayList<>();
59         URL resource = getResourceAsURL(fileName, GROOVY_SUFFIX);
60         if (null != resource) {
61             bundles.add(new GroovyScriptResourceBundle(configReader, resource));
62             return bundles;
63         }
64 
65         resource = getResourceAsURL(fileName, CLASS_SUFFIX);
66         if (null != resource) {
67             String className = fileName.replace('/''.');
68             try {
69                 Class<?> klass = loadClass(className);
70                 if (Script.class.isAssignableFrom(klass)) {
71                     bundles.add(new GroovyScriptResourceBundle(configReader, (Class<? extends Script>klass));
72                     return bundles;
73                 }
74             catch (ClassNotFoundException e) {
75                 // ignore
76             }
77         }
78 
79         return super.loadBundleFromClass(fileName);
80     }
81 }