DefaultResourceInjector.java
01 /*
02  * Copyright 2008-2016 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 javax.annotation.Nullable;
23 import javax.inject.Inject;
24 import java.util.Locale;
25 
26 import static java.util.Objects.requireNonNull;
27 
28 /**
29  @author Andres Almiray
30  @since 2.0.0
31  */
32 public class DefaultResourceInjector extends AbstractResourceInjector {
33     private ResourceResolver resourceResolver;
34 
35     @Inject
36     public DefaultResourceInjector(@Nonnull ResourceResolver resourceResolver) {
37         this.resourceResolver = requireNonNull(resourceResolver, "Argument 'resourceResolver' must not be null");
38     }
39 
40     @Nullable
41     protected Object resolveResource(@Nonnull String key, @Nonnull String[] args) {
42         try {
43             return resourceResolver.resolveResource(key, args, Locale.getDefault());
44         catch (NoSuchResourceException nsre) {
45             return null;
46         }
47     }
48 
49     @Nullable
50     protected Object resolveResource(@Nonnull String key, @Nonnull String[] args, @Nonnull String defaultValue) {
51         return resourceResolver.resolveResource(key, args, Locale.getDefault(), defaultValue);
52     }
53 }