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.ResourceHandler;
19
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.List;
29
30 /**
31 * Base implementation of the {@link griffon.core.resources.ResourceHandler} interface.
32 *
33 * @author Andres Almiray
34 * @since 2.0.0
35 */
36 public abstract class AbstractResourceHandler implements ResourceHandler {
37 @Nullable
38 public InputStream getResourceAsStream(@Nonnull String name) {
39 return classloader().getResourceAsStream(name);
40 }
41
42 @Nullable
43 public URL getResourceAsURL(@Nonnull String name) {
44 return classloader().getResource(name);
45 }
46
47 @Nullable
48 public List<URL> getResources(@Nonnull String name) {
49 Enumeration<URL> resources = null;
50 try {
51 resources = classloader().getResources(name);
52 } catch (IOException e) {
53 // ignore
54 }
55
56 return resources != null ? toList(resources) : Collections.<URL>emptyList();
57 }
58
59 private static <T> List<T> toList(Enumeration<T> self) {
60 List<T> answer = new ArrayList<>();
61 while (self.hasMoreElements()) {
62 answer.add(self.nextElement());
63 }
64 return answer;
65 }
66 }
|