GroovyScriptResourceBundle.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.codehaus.griffon.runtime.groovy.util;
019 
020 import griffon.util.ConfigReader;
021 import groovy.lang.Script;
022 import groovy.util.ConfigObject;
023 
024 import javax.annotation.Nonnull;
025 import javax.annotation.Nullable;
026 import java.net.URL;
027 import java.util.Enumeration;
028 import java.util.Iterator;
029 import java.util.LinkedHashMap;
030 import java.util.LinkedHashSet;
031 import java.util.ResourceBundle;
032 import java.util.Set;
033 
034 import static griffon.util.ConfigUtils.getConfigValue;
035 import static griffon.util.GriffonNameUtils.requireNonBlank;
036 import static java.util.Objects.requireNonNull;
037 
038 /**
039  @author Andres Almiray
040  @since 2.0.0
041  */
042 public class GroovyScriptResourceBundle extends ResourceBundle {
043     private static final String ERROR_READER_NULL = "Argument 'reader' must not be null";
044     private final ConfigObject config;
045     private final Set<String> keys = new LinkedHashSet<>();
046     private final String source;
047 
048     public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull URL location) {
049         this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(location, "Argument 'location' must not be null")), location.toString());
050     }
051 
052     public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull Script script) {
053         this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(script, "Argument 'script' must not be null")), script.getClass().getName());
054     }
055 
056     public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull String script) {
057         this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonBlank(script, "Argument 'script' must not be blank"))"<<INLINE SCRIPT>>");
058     }
059 
060     public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull Class<? extends Script> scriptClass) {
061         this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(scriptClass, "Argument 'scriptClass' must not be null")), scriptClass.getName());
062     }
063 
064     @SuppressWarnings("unchecked")
065     private GroovyScriptResourceBundle(@Nonnull ConfigObject config, @Nonnull String source) {
066         this.config = requireNonNull(config, "Argument 'config' must not be null");
067         this.source = source;
068         keys.addAll(this.config.flatten(new LinkedHashMap<>()).keySet());
069     }
070 
071     @Nonnull
072     public String getSource() {
073         return source;
074     }
075 
076     @Override
077     public String toString() {
078         return super.toString() "[" + source + "]";
079     }
080 
081     @Nullable
082     @Override
083     @SuppressWarnings("unchecked")
084     protected Object handleGetObject(@Nonnull String key) {
085         Object value = getConfigValue(config, requireNonBlank(key, "Argument 'key' must not be blank")null);
086         if (null == valuereturn null;
087         if (value instanceof ConfigObject) {
088             return ((ConfigObjectvalue).isEmpty() null : value;
089         }
090         return value;
091     }
092 
093     @Nonnull
094     @Override
095     public Enumeration<String> getKeys() {
096         final Iterator<String> keysIterator = keys.iterator();
097         return new Enumeration<String>() {
098             public boolean hasMoreElements() {
099                 return keysIterator.hasNext();
100             }
101 
102             public String nextElement() {
103                 return keysIterator.next();
104             }
105         };
106     }
107 
108     @Override
109     public Set<String> keySet() {
110         return new LinkedHashSet<>(keys);
111     }
112 }