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