01 /*
02 * SPDX-License-Identifier: Apache-2.0
03 *
04 * Copyright 2008-2017 the original author or authors.
05 *
06 * Licensed under the Apache License, Version 2.0 (the "License");
07 * you may not use this file except in compliance with the License.
08 * You may obtain a copy of the License at
09 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.codehaus.griffon.runtime.core.resources;
19
20 import griffon.core.resources.ResourceHandler;
21
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.List;
31
32 /**
33 * Base implementation of the {@link griffon.core.resources.ResourceHandler} interface.
34 *
35 * @author Andres Almiray
36 * @since 2.0.0
37 */
38 public abstract class AbstractResourceHandler implements ResourceHandler {
39 @Nullable
40 public InputStream getResourceAsStream(@Nonnull String name) {
41 return classloader().getResourceAsStream(name);
42 }
43
44 @Nullable
45 public URL getResourceAsURL(@Nonnull String name) {
46 return classloader().getResource(name);
47 }
48
49 @Nullable
50 public List<URL> getResources(@Nonnull String name) {
51 Enumeration<URL> resources = null;
52 try {
53 resources = classloader().getResources(name);
54 } catch (IOException e) {
55 // ignore
56 }
57
58 return resources != null ? toList(resources) : Collections.<URL>emptyList();
59 }
60
61 private static <T> List<T> toList(Enumeration<T> self) {
62 List<T> answer = new ArrayList<>();
63 while (self.hasMoreElements()) {
64 answer.add(self.nextElement());
65 }
66 return answer;
67 }
68 }
|