1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  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. */
  17. package org.apache.tools.ant.util;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.launch.Locator;
  21. /**
  22. * ClassLoader utility methods
  23. *
  24. */
  25. public class LoaderUtils {
  26. /**
  27. * Set the context classloader
  28. *
  29. * @param loader the ClassLoader to be used as the context class loader
  30. * on the current thread.
  31. */
  32. public static void setContextClassLoader(ClassLoader loader) {
  33. Thread currentThread = Thread.currentThread();
  34. currentThread.setContextClassLoader(loader);
  35. }
  36. /**
  37. * JDK1.1 compatible access to set the context class loader.
  38. *
  39. * @return the ClassLoader instance being used as the context
  40. * classloader on the current thread. Returns null on JDK 1.1
  41. */
  42. public static ClassLoader getContextClassLoader() {
  43. Thread currentThread = Thread.currentThread();
  44. return currentThread.getContextClassLoader();
  45. }
  46. /**
  47. * Indicates if the context class loader methods are available
  48. *
  49. * @return true if the get and set methods dealing with the context
  50. * classloader are available.
  51. */
  52. public static boolean isContextLoaderAvailable() {
  53. return true;
  54. }
  55. /**
  56. * Normalize a source location
  57. *
  58. * @param source the source location to be normalized.
  59. *
  60. * @return the normalized source location.
  61. */
  62. private static File normalizeSource(File source) {
  63. if (source != null) {
  64. FileUtils fileUtils = FileUtils.newFileUtils();
  65. try {
  66. source = fileUtils.normalize(source.getAbsolutePath());
  67. } catch (BuildException e) {
  68. // relative path
  69. }
  70. }
  71. return source;
  72. }
  73. /**
  74. * Find the directory or jar file the class has been loaded from.
  75. *
  76. * @param c the class whose location is required.
  77. * @return the file or jar with the class or null if we cannot
  78. * determine the location.
  79. *
  80. * @since Ant 1.6
  81. */
  82. public static File getClassSource(Class c) {
  83. return normalizeSource(Locator.getClassSource(c));
  84. }
  85. /**
  86. * Find the directory or a give resource has been loaded from.
  87. *
  88. * @param c the classloader to be consulted for the source
  89. * @param resource the resource whose location is required.
  90. *
  91. * @return the file with the resource source or null if
  92. * we cannot determine the location.
  93. *
  94. * @since Ant 1.6
  95. */
  96. public static File getResourceSource(ClassLoader c, String resource) {
  97. if (c == null) {
  98. c = LoaderUtils.class.getClassLoader();
  99. }
  100. return normalizeSource(Locator.getResourceSource(c, resource));
  101. }
  102. }