You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResourceUtils.java 3.5KB

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