KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LoaderUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.File JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.launch.Locator;
23
24 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
25

26 /**
27  * ClassLoader utility methods
28  *
29  */

30 public class LoaderUtils {
31
32     /** Utilities used for file operations */
33     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
34
35     /**
36      * Set the context classloader
37      *
38      * @param loader the ClassLoader to be used as the context class loader
39      * on the current thread.
40      */

41     public static void setContextClassLoader(ClassLoader JavaDoc loader) {
42         Thread JavaDoc currentThread = Thread.currentThread();
43         currentThread.setContextClassLoader(loader);
44     }
45
46
47     /**
48      * JDK1.1 compatible access to set the context class loader.
49      *
50      * @return the ClassLoader instance being used as the context
51      * classloader on the current thread. Returns null on JDK 1.1
52      */

53     public static ClassLoader JavaDoc getContextClassLoader() {
54         Thread JavaDoc currentThread = Thread.currentThread();
55         return currentThread.getContextClassLoader();
56     }
57
58     /**
59      * Indicates if the context class loader methods are available
60      *
61      * @return true if the get and set methods dealing with the context
62      * classloader are available.
63      */

64     public static boolean isContextLoaderAvailable() {
65         return true;
66     }
67
68     /**
69      * Normalize a source location
70      *
71      * @param source the source location to be normalized.
72      *
73      * @return the normalized source location.
74      */

75     private static File JavaDoc normalizeSource(File JavaDoc source) {
76         if (source != null) {
77             try {
78                 source = FILE_UTILS.normalize(source.getAbsolutePath());
79             } catch (BuildException e) {
80                 // relative path
81
}
82         }
83
84         return source;
85     }
86
87     /**
88      * Find the directory or jar file the class has been loaded from.
89      *
90      * @param c the class whose location is required.
91      * @return the file or jar with the class or null if we cannot
92      * determine the location.
93      *
94      * @since Ant 1.6
95      */

96     public static File JavaDoc getClassSource(Class JavaDoc c) {
97         return normalizeSource(Locator.getClassSource(c));
98     }
99
100     /**
101      * Find the directory or a give resource has been loaded from.
102      *
103      * @param c the classloader to be consulted for the source
104      * @param resource the resource whose location is required.
105      *
106      * @return the file with the resource source or null if
107      * we cannot determine the location.
108      *
109      * @since Ant 1.6
110      */

111     public static File JavaDoc getResourceSource(ClassLoader JavaDoc c, String JavaDoc resource) {
112         if (c == null) {
113             c = LoaderUtils.class.getClassLoader();
114         }
115         return normalizeSource(Locator.getResourceSource(c, resource));
116     }
117
118     /**
119      * Return the resource name of a class name.
120      * @param className the name of the class to convert.
121      * @return the corresponding resource name.
122      * @since Ant 1.7.0.
123      */

124     public static String JavaDoc classNameToResource(String JavaDoc className) {
125         return className.replace('.', '/') + ".class";
126     }
127
128     /**
129      * Check if a classloader has a classname resource.
130      * @param loader the classloader to look it.
131      * @param className the name of the class to look for.
132      * @return true if the classexists, false otherwise
133      * @since Ant 1.7.0.
134      */

135     public static boolean classExists(ClassLoader JavaDoc loader, String JavaDoc className) {
136         return loader.getResource(classNameToResource(className)) != null;
137     }
138 }
139
140
Popular Tags