01 /*
02 * Copyright 2008-2014 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.core;
17
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import javax.inject.Inject;
21 import java.util.*;
22
23 import static griffon.util.ConfigUtils.getConfigValue;
24 import static griffon.util.GriffonNameUtils.requireNonBlank;
25 import static java.util.Collections.unmodifiableMap;
26 import static java.util.Objects.requireNonNull;
27
28 /**
29 * @author Andres Almiray
30 * @since 2.0.0
31 */
32 public class ResourceBundleConfiguration extends AbstractConfiguration {
33 protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
34 private final ResourceBundle resourceBundle;
35 private final Map<String, Object> flatMap = new LinkedHashMap<>();
36
37 @Inject
38 public ResourceBundleConfiguration(@Nonnull ResourceBundle resourceBundle) {
39 this.resourceBundle = requireNonNull(resourceBundle, "Argument 'resourceBundle' must not be null");
40 Enumeration<String> keys = resourceBundle.getKeys();
41 while (keys.hasMoreElements()) {
42 String key = keys.nextElement();
43 flatMap.put(key, getConfigValue(resourceBundle, key));
44 }
45 }
46
47 public boolean containsKey(@Nonnull String key) {
48 return resourceBundle.containsKey(requireNonBlank(key, ERROR_KEY_BLANK));
49 }
50
51 @Nonnull
52 @Override
53 public Map<String, Object> asFlatMap() {
54 return unmodifiableMap(flatMap);
55 }
56
57 @Nonnull
58 @Override
59 public ResourceBundle asResourceBundle() {
60 return resourceBundle;
61 }
62
63 @Nullable
64 @Override
65 public Object get(@Nonnull String key) {
66 try {
67 return getConfigValue(resourceBundle, key);
68 } catch (MissingResourceException mre) {
69 return null;
70 }
71 }
72 }
|