| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ResourceLoader |
|
| 4.0;4 |
| 1 | /** | |
| 2 | * Copyright 2005-2013 Dozer Project | |
| 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 org.dozer.util; | |
| 17 | ||
| 18 | import org.apache.commons.lang3.StringUtils; | |
| 19 | ||
| 20 | import java.net.MalformedURLException; | |
| 21 | import java.net.URL; | |
| 22 | ||
| 23 | /** | |
| 24 | * Internal class that loads resources from the classpath. Also supports loading | |
| 25 | * resources outside of the classpath if it is prepended with "file:". Only | |
| 26 | * intended for internal use. | |
| 27 | * | |
| 28 | * | |
| 29 | * @author tierney.matt | |
| 30 | * @author garsombke.franz | |
| 31 | */ | |
| 32 | public class ResourceLoader { | |
| 33 | ||
| 34 | private final ClassLoader classLoader; | |
| 35 | ||
| 36 | 11 | public ResourceLoader(ClassLoader classLoader) { |
| 37 | 11 | this.classLoader = classLoader; |
| 38 | 11 | } |
| 39 | ||
| 40 | public URL getResource(String resource) { | |
| 41 | 1225 | resource = resource.trim(); |
| 42 | ||
| 43 | 1225 | URL result = Thread.currentThread().getContextClassLoader().getResource(resource); |
| 44 | ||
| 45 | // Could not find resource. Try with the classloader that loaded this class. | |
| 46 | 1225 | if (result == null) { |
| 47 | 7 | ClassLoader classLoader = ResourceLoader.class.getClassLoader(); |
| 48 | 7 | if (classLoader != null) { |
| 49 | 7 | result = classLoader.getResource(resource); |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | // Last ditch attempt searching classpath | |
| 54 | 1225 | if (result == null) { |
| 55 | 7 | result = ClassLoader.getSystemResource(resource); |
| 56 | } | |
| 57 | ||
| 58 | // one more time | |
| 59 | 1225 | if (result == null && StringUtils.contains(resource, ":")) { |
| 60 | try { | |
| 61 | 3 | result = new URL(resource); |
| 62 | 1 | } catch (MalformedURLException e) { |
| 63 | 1 | MappingUtils.throwMappingException(e); |
| 64 | 2 | } |
| 65 | } | |
| 66 | ||
| 67 | 1224 | return result; |
| 68 | } | |
| 69 | ||
| 70 | } |