01 /*
02 * Copyright 2012 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;
17
18 import griffon.core.ResourceHandler;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.List;
26
27 import static org.codehaus.groovy.runtime.DefaultGroovyMethods.toList;
28
29 /**
30 * Base implementation of the {@link ResourceHandler} interface.
31 *
32 * @author Andres Almiray
33 * @since 0.9.5
34 */
35 public class ResourceLocator implements ResourceHandler {
36 public InputStream getResourceAsStream(String name) {
37 return classLoader().getResourceAsStream(name);
38 }
39
40 public URL getResourceAsURL(String name) {
41 return classLoader().getResource(name);
42 }
43
44 public List<URL> getResources(String name) {
45 Enumeration<URL> resources = null;
46 try {
47 resources = classLoader().getResources(name);
48 } catch (IOException e) {
49 // ignore
50 }
51
52 return resources != null ? toList(resources) : Collections.<URL>emptyList();
53 }
54
55 private ClassLoader classLoader() {
56 return this.getClass().getClassLoader();
57 }
58 }
|