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.core.resources.ResourceResolver;
20
21 import javax.annotation.Nonnull;
22 import java.util.Collection;
23 import java.util.Locale;
24
25 import static griffon.util.GriffonClassUtils.requireState;
26 import static griffon.util.GriffonNameUtils.requireNonBlank;
27 import static java.util.Objects.requireNonNull;
28
29 /**
30 * @author Andres Almiray
31 * @since 2.0.0
32 */
33 public class CompositeResourceResolver extends AbstractResourceResolver {
34 private final ResourceResolver[] resourceResolvers;
35
36 public CompositeResourceResolver(@Nonnull Collection<ResourceResolver> resourceResolvers) {
37 this(toResourceResolverArray(resourceResolvers));
38 }
39
40 public CompositeResourceResolver(@Nonnull ResourceResolver[] resourceResolvers) {
41 this.resourceResolvers = requireNonNull(resourceResolvers, "Argument 'resourceResolvers' must not be null");
42 }
43
44 private static ResourceResolver[] toResourceResolverArray(@Nonnull Collection<ResourceResolver> resourceResolvers) {
45 requireNonNull(resourceResolvers, "Argument 'resourceResolvers' must not be null");
46 requireState(!resourceResolvers.isEmpty(), "Argument 'resourceResolvers' must not be empty");
47 return resourceResolvers.toArray(new ResourceResolver[resourceResolvers.size()]);
48 }
49
50 @Nonnull
51 @Override
52 protected Object doResolveResourceValue(@Nonnull String key, @Nonnull Locale locale) throws NoSuchResourceException {
53 requireNonBlank(key, ERROR_KEY_BLANK);
54 requireNonNull(locale, ERROR_LOCALE_NULL);
55 for (ResourceResolver resourceResolver : resourceResolvers) {
56 try {
57 return resourceResolver.resolveResourceValue(key, locale);
58 } catch (NoSuchResourceException nsre) {
59 // ignore
60 }
61 }
62 throw new NoSuchResourceException(key, locale);
63 }
64 }
|