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.resources;
17
18 import griffon.core.resources.NoSuchResourceException;
19 import griffon.util.CompositeResourceBundleBuilder;
20
21 import javax.annotation.Nonnull;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.ResourceBundle;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import static griffon.util.GriffonNameUtils.requireNonBlank;
28 import static java.util.Objects.requireNonNull;
29
30 /**
31 * @author Andres Almiray
32 * @since 2.0.0
33 */
34 public class DefaultResourceResolver extends AbstractResourceResolver {
35 private final String basename;
36 private final Map<Locale, ResourceBundle> bundles = new ConcurrentHashMap<>();
37 private final CompositeResourceBundleBuilder compositeResourceBundleBuilder;
38
39 public DefaultResourceResolver(@Nonnull CompositeResourceBundleBuilder builder, @Nonnull String basename) {
40 this.compositeResourceBundleBuilder = requireNonNull(builder, "Argument 'builder' must not be null");
41 this.basename = requireNonBlank(basename, "Argument 'basename' must not be blank");
42 }
43
44 @Nonnull
45 public String getBasename() {
46 return basename;
47 }
48
49 @Nonnull
50 protected Object doResolveResourceValue(@Nonnull String key, @Nonnull Locale locale) throws NoSuchResourceException {
51 requireNonBlank(key, ERROR_KEY_BLANK);
52 requireNonNull(locale, ERROR_LOCALE_NULL);
53 return getBundle(locale).getObject(key);
54 }
55
56 @Nonnull
57 protected ResourceBundle getBundle(@Nonnull Locale locale) {
58 requireNonNull(locale, ERROR_LOCALE_NULL);
59 ResourceBundle rb = bundles.get(locale);
60 if (null == rb) {
61 rb = compositeResourceBundleBuilder.create(basename, locale);
62 bundles.put(locale, rb);
63 }
64 return rb;
65 }
66 }
|