001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.util;
027:
028: import java.io.File;
029: import java.io.IOException;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.net.URLClassLoader;
033: import java.util.StringTokenizer;
034: import com.sun.mirror.apt.Filer;
035:
036: /**
037: *
038: * @author WS Development Team
039: */
040: public abstract class ProcessorEnvironmentBase implements
041: ProcessorEnvironment {
042:
043: protected URLClassLoader classLoader = null;
044: protected Filer filer = null;
045:
046: /**
047: * Get a URLClassLoader from using the classpath
048: */
049: public URLClassLoader getClassLoader() {
050: if (classLoader == null) {
051: classLoader = new URLClassLoader(
052: pathToURLs(getClassPath()), this .getClass()
053: .getClassLoader());
054: }
055: return classLoader;
056: }
057:
058: /**
059: * Utility method for converting a search path string to an array
060: * of directory and JAR file URLs.
061: *
062: * @param path the search path string
063: * @return the resulting array of directory and JAR file URLs
064: */
065: public static URL[] pathToURLs(String path) {
066: StringTokenizer st = new StringTokenizer(path,
067: File.pathSeparator);
068: URL[] urls = new URL[st.countTokens()];
069: int count = 0;
070: while (st.hasMoreTokens()) {
071: URL url = fileToURL(new File(st.nextToken()));
072: if (url != null) {
073: urls[count++] = url;
074: }
075: }
076: if (urls.length != count) {
077: URL[] tmp = new URL[count];
078: System.arraycopy(urls, 0, tmp, 0, count);
079: urls = tmp;
080: }
081: return urls;
082: }
083:
084: /**
085: * Returns the directory or JAR file URL corresponding to the specified
086: * local file name.
087: *
088: * @param file the File object
089: * @return the resulting directory or JAR file URL, or null if unknown
090: */
091: public static URL fileToURL(File file) {
092: String name;
093: try {
094: name = file.getCanonicalPath();
095: } catch (IOException e) {
096: name = file.getAbsolutePath();
097: }
098: name = name.replace(File.separatorChar, '/');
099: if (!name.startsWith("/")) {
100: name = "/" + name;
101: }
102:
103: // If the file does not exist, then assume that it's a directory
104: if (!file.isFile()) {
105: name = name + "/";
106: }
107: try {
108: return new URL("file", "", name);
109: } catch (MalformedURLException e) {
110: throw new IllegalArgumentException("file");
111: }
112: }
113:
114: public Filer getFiler() {
115: return filer;
116: }
117:
118: public void setFiler(Filer filer) {
119: this.filer = filer;
120: }
121: }
|