1 package org.apache.maven.archiva.common.utils; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.commons.lang.StringUtils; 23 24 import java.io.File; 25 import java.io.IOException; 26 import java.net.URL; 27 28 /** 29 * <code>ResourceUtils</code> 30 */ 31 public class ResourceUtils 32 { 33 /** 34 * Lookup resource at the given path relative to the root of the classpath and if it exists return a file object 35 * that can be used to access it. 36 * <p> 37 * At test time the contents of both the src/resources and test/resources dirs are available at the root of the 38 * classpath. 39 * <p> 40 * To retrieve the file src/test/resources/sometest/test.properties use getResource("/sometest/test.properties"). 41 * 42 * @param resourcePath the path to the resource relative to the root of the classpath 43 * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found 44 */ 45 public static File getResource( String resourcePath ) 46 throws IOException 47 { 48 return getResource( resourcePath, null ); 49 } 50 51 /** 52 * Lookup resource at the given path relative to the root of the classpath and if it exists return a file object 53 * that can be used to access it. 54 * <p> 55 * At test time the contents of both the src/resources and test/resources dirs are available at the root of the 56 * classpath. 57 * <p> 58 * To retrieve the file src/test/resources/sometest/test.properties use getResource("/sometest/test.properties"). 59 * 60 * @param resourcePath the path to the resource relative to the root of the classpath 61 * @param classloader the classloader who's classpath should be searched for the resource 62 * @return File a file object pointing to the resource on the classpath or null if the resource cannot be found 63 */ 64 public static File getResource( String resourcePath, ClassLoader classloader ) 65 throws IOException 66 { 67 File testResource = null; 68 69 if ( StringUtils.isNotBlank( resourcePath ) ) 70 { 71 // make sure the retrieval is relative to the root of the classpath 72 resourcePath = resourcePath.startsWith( "/" ) ? resourcePath : "/" + resourcePath; 73 74 URL resourceUrl = getResourceUrl( resourcePath, classloader ); 75 if ( resourceUrl == null ) 76 { 77 throw new IOException( "Could not find test resource at path '" + resourcePath + "'" ); 78 } 79 testResource = new File( resourceUrl.getFile() ); 80 } 81 82 return testResource; 83 } 84 85 private static URL getResourceUrl( String resourcePath, ClassLoader classloader ) 86 { 87 return classloader != null ? classloader.getResource( resourcePath ) 88 : ResourceUtils.class.getResource( resourcePath ); 89 } 90 }