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