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.resources.ResourceHandler;
19 import griffon.util.ResourceBundleLoader;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import javax.inject.Inject;
24 import java.net.URL;
25 import java.util.List;
26
27 import static griffon.util.GriffonNameUtils.requireNonBlank;
28 import static java.util.Objects.requireNonNull;
29
30 /**
31 * @author Andres Almiray
32 * @since 2.11.0
33 */
34 public abstract class AbstractResourceBundleLoader implements ResourceBundleLoader {
35 protected static final String ERROR_FILENAME_BLANK = "Argument 'fileName' must not be blank";
36 protected static final String ERROR_SUFFIX_BLANK = "Argument 'suffix' must not be blank";
37 protected static final String ERROR_RESOURCE_HANDLER_NULL = "Argument 'resourceHandler' must not be null";
38
39 protected final ResourceHandler resourceHandler;
40
41 @Inject
42 public AbstractResourceBundleLoader(@Nonnull ResourceHandler resourceHandler) {
43 this.resourceHandler = requireNonNull(resourceHandler, ERROR_RESOURCE_HANDLER_NULL);
44 }
45
46 @Nonnull
47 protected ResourceHandler getResourceHandler() {
48 return resourceHandler;
49 }
50
51 @Nullable
52 protected URL getResourceAsURL(@Nonnull String fileName, @Nonnull String suffix) {
53 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
54 requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
55 return resourceHandler.getResourceAsURL(fileName + suffix);
56 }
57
58 @Nullable
59 protected List<URL> getResources(@Nonnull String fileName, @Nonnull String suffix) {
60 requireNonBlank(fileName, ERROR_FILENAME_BLANK);
61 requireNonBlank(suffix, ERROR_SUFFIX_BLANK);
62 return resourceHandler.getResources(fileName + suffix);
63 }
64 }
|