KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > Loader


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 net.sf.dozer.util.mapping.util;
17
18 import java.io.File JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import net.sf.dozer.util.mapping.MappingException;
23
24 /**
25  * @author tierney.matt
26  * @author garsombke.franz
27  */

28 public class Loader {
29   
30   public URL JavaDoc getResource(String JavaDoc resource) {
31     URL JavaDoc result = Thread.currentThread().getContextClassLoader().getResource(resource);
32     
33     // Could not find resource. Try with the classloader that loaded this class.
34
if (result == null) {
35       ClassLoader JavaDoc classLoader = Loader.class.getClassLoader();
36       if(classLoader != null) {
37         result = classLoader.getResource(resource);
38       }
39     }
40     
41     // Last ditch attempt searching classpath
42
if (result == null) {
43       result = ClassLoader.getSystemResource(resource);
44     }
45     
46     //Patch to load mapping file from outside of classpath.
47
if (resource.startsWith(MapperConstants.FILE_PREFIX)) {
48       try {
49         return new File JavaDoc(resource.substring(MapperConstants.FILE_PREFIX.length())).toURL();
50       } catch (MalformedURLException JavaDoc e) {
51         throw new MappingException(e);
52       }
53     }
54     
55     return result;
56   }
57   
58 }
Popular Tags