001 /*
002 * Copyright 2008-2014 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.*;
026
027 import static griffon.util.ConfigUtils.getConfigValue;
028 import static griffon.util.GriffonNameUtils.requireNonBlank;
029 import static java.util.Objects.requireNonNull;
030
031 /**
032 * @author Andres Almiray
033 * @since 2.0.0
034 */
035 public class GroovyScriptResourceBundle extends ResourceBundle {
036 private static final String ERROR_READER_NULL = "Argument 'reader' must not be null";
037 private final ConfigObject config;
038 private final List<String> keys = new ArrayList<>();
039 private final String source;
040
041 public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull URL location) {
042 this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(location, "Argument 'location' cannot ne null")), location.toString());
043 }
044
045 public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull Script script) {
046 this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(script, "Argument 'script' cannot ne null")), script.getClass().getName());
047 }
048
049 public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull String script) {
050 this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonBlank(script, "Argument 'script' cannot ne blank")), "<<INLINE SCRIPT>>");
051 }
052
053 public GroovyScriptResourceBundle(@Nonnull ConfigReader reader, @Nonnull Class<? extends Script> scriptClass) {
054 this(requireNonNull(reader, ERROR_READER_NULL).parse(requireNonNull(scriptClass, "Argument 'scriptClass' cannot ne null")), scriptClass.getName());
055 }
056
057 @SuppressWarnings("unchecked")
058 private GroovyScriptResourceBundle(@Nonnull ConfigObject config, @Nonnull String source) {
059 this.config = requireNonNull(config, "Argument 'config' must not be null");
060 this.source = source;
061 keys.addAll(this.config.flatten(new LinkedHashMap<>()).keySet());
062 }
063
064 @Nonnull
065 public String getSource() {
066 return source;
067 }
068
069 @Override
070 public String toString() {
071 return super.toString() + "[" + source + "]";
072 }
073
074 @Nullable
075 @Override
076 @SuppressWarnings("unchecked")
077 protected Object handleGetObject(@Nonnull String key) {
078 Object value = getConfigValue(config, requireNonBlank(key, "Argument 'key' must not be blank"), null);
079 if (null == value) return null;
080 if (value instanceof ConfigObject) {
081 return ((ConfigObject) value).isEmpty() ? null : value;
082 }
083 return value;
084 }
085
086 @Nonnull
087 @Override
088 public Enumeration<String> getKeys() {
089 final Iterator<String> keysIterator = keys.iterator();
090 return new Enumeration<String>() {
091 public boolean hasMoreElements() {
092 return keysIterator.hasNext();
093 }
094
095 public String nextElement() {
096 return keysIterator.next();
097 }
098 };
099 }
100 }
|