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