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 griffon.util;
019
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022
023 import javax.annotation.Nonnull;
024 import javax.annotation.Nullable;
025 import java.util.ArrayList;
026 import java.util.Collection;
027 import java.util.Enumeration;
028 import java.util.Iterator;
029 import java.util.List;
030 import java.util.ResourceBundle;
031
032 import static griffon.util.ExpandableResourceBundle.wrapResourceBundle;
033 import static griffon.util.GriffonClassUtils.requireState;
034 import static griffon.util.GriffonNameUtils.requireNonBlank;
035 import static java.util.Objects.requireNonNull;
036
037 /**
038 * @author Andres Almiray
039 * @since 2.0.0
040 */
041 public class CompositeResourceBundle extends ResourceBundle {
042 private static final Logger LOG = LoggerFactory.getLogger(CompositeResourceBundle.class);
043 private final ResourceBundle[] bundles;
044 private final List<String> keys = new ArrayList<>();
045
046 public CompositeResourceBundle(@Nonnull Collection<ResourceBundle> bundles) {
047 this(toResourceBundleArray(bundles));
048 }
049
050 public CompositeResourceBundle(@Nonnull ResourceBundle[] bundles) {
051 requireNonNull(bundles, "Argument 'bundles' must not be null");
052 requireState(bundles.length > 0, "Argument 'bundles' must not be empty");
053 this.bundles = new ResourceBundle[bundles.length];
054 for (int i = 0; i < bundles.length; i++) {
055 this.bundles[i] = wrapResourceBundle(bundles[i]);
056 }
057
058 for (ResourceBundle bundle : this.bundles) {
059 Enumeration<String> ks = bundle.getKeys();
060 while (ks.hasMoreElements()) {
061 String key = ks.nextElement();
062 if (!keys.contains(key)) {
063 keys.add(key);
064 }
065 }
066 }
067 }
068
069 @Nullable
070 protected Object handleGetObject(@Nonnull String key) {
071 requireNonBlank(key, "Arguments 'key' must not be blank");
072
073 LOG.trace("Searching key={}", key);
074 for (ResourceBundle bundle : bundles) {
075 try {
076 Object value = bundle.getObject(key);
077 LOG.trace("Bundle {}; key={}; value='{}'", bundle, key, value);
078 if (value != null) {
079 return value;
080 }
081 } catch (Exception e) {
082 // ignore
083 }
084 }
085 return null;
086 }
087
088 @Nonnull
089 @Override
090 public Enumeration<String> getKeys() {
091 return new IteratorAsEnumeration<>(keys.iterator());
092 }
093
094 private static class IteratorAsEnumeration<E> implements Enumeration<E> {
095 private final Iterator<E> iterator;
096
097 public IteratorAsEnumeration(Iterator<E> iterator) {
098 this.iterator = iterator;
099 }
100
101 public boolean hasMoreElements() {
102 return iterator.hasNext();
103 }
104
105 public E nextElement() {
106 return iterator.next();
107 }
108 }
109
110 @Nonnull
111 private static ResourceBundle[] toResourceBundleArray(@Nonnull Collection<ResourceBundle> bundles) {
112 requireNonNull(bundles, "Argument 'bundles' must not be null");
113 if (bundles.isEmpty()) {
114 return new ResourceBundle[0];
115 }
116 return bundles.toArray(new ResourceBundle[bundles.size()]);
117 }
118 }
|