01 /*
02 * Copyright 2008-2017 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.util;
17
18 import griffon.core.injection.Injector;
19 import griffon.util.ResourceBundleLoader;
20
21 import javax.annotation.Nonnull;
22 import javax.inject.Inject;
23 import javax.inject.Provider;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.ResourceBundle;
29 import java.util.concurrent.ConcurrentHashMap;
30
31 import static griffon.util.AnnotationUtils.sortByDependencies;
32 import static java.util.Objects.requireNonNull;
33
34 /**
35 * @author Andres Almiray
36 * @since 2.0.0
37 */
38 public class DefaultCompositeResourceBundleBuilder extends AbstractCompositeResourceBundleBuilder {
39 protected static final String ERROR_INJECTOR_NULL = "Argument 'injector' must not be null";
40
41 private final Provider<Injector> injector;
42 private final Map<String, ResourceBundleLoader> loaders = new ConcurrentHashMap<>();
43
44 @Inject
45 public DefaultCompositeResourceBundleBuilder(@Nonnull Provider<Injector> injector) {
46 this.injector = requireNonNull(injector, ERROR_INJECTOR_NULL);
47 }
48
49 protected void initialize() {
50 if (loaders.isEmpty()) {
51 Collection<ResourceBundleLoader> instances = injector.get().getInstances(ResourceBundleLoader.class);
52 loaders.putAll(sortByDependencies(instances, "", "resource bundle loader"));
53 }
54 }
55
56 @Nonnull
57 protected Collection<ResourceBundle> loadBundlesFor(@Nonnull String basename) {
58 List<ResourceBundle> bundles = new ArrayList<>();
59 for (Map.Entry<String, ResourceBundleLoader> e : loaders.entrySet()) {
60 Collection<ResourceBundle> loaded = e.getValue().load(basename);
61 if (!loaded.isEmpty()) {
62 bundles.addAll(loaded);
63 }
64 }
65
66 return bundles;
67 }
68 }
|