001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.transformer;
032:
033: import java.io.*;
034: import java.net.*;
035: import java.util.*;
036: import java.util.jar.*;
037:
038: /**
039: * @author Taras Puchko
040: */
041: class JarClassLoader extends URLClassLoader {
042:
043: private File file;
044:
045: public JarClassLoader(File file, ClassLoader parent)
046: throws IOException {
047: super (getClassPath(file), parent);
048: this .file = file;
049: }
050:
051: public String getMainClass() throws IOException {
052: return getAttribute(file, Attributes.Name.MAIN_CLASS);
053: }
054:
055: private static URL[] getClassPath(File file) throws IOException {
056: Set<URL> urls = new LinkedHashSet<URL>();
057: addToPath(urls, file);
058: return urls.toArray(new URL[urls.size()]);
059: }
060:
061: private static void addToPath(Set<URL> classPath, File file)
062: throws IOException {
063: file = file.getCanonicalFile();
064: if (!file.exists())
065: return;
066: URL url = file.toURI().toURL();
067: if (classPath.contains(url))
068: return;
069: classPath.add(url);
070: String attribute = getAttribute(file,
071: Attributes.Name.CLASS_PATH);
072: if (attribute != null) {
073: StringTokenizer tokenizer = new StringTokenizer(attribute);
074: while (tokenizer.hasMoreTokens()) {
075: addToPath(classPath, new File(file.getParent(),
076: tokenizer.nextToken()));
077: }
078: }
079: }
080:
081: private static String getAttribute(File file, Attributes.Name name) {
082: Attributes attributes = getAttributes(file);
083: return attributes == null ? null : attributes.getValue(name);
084: }
085:
086: private static Attributes getAttributes(File file) {
087: if (!file.isFile())
088: return null;
089: try {
090: JarFile jarFile = new JarFile(file);
091: try {
092: Manifest manifest = jarFile.getManifest();
093: return manifest == null ? null : manifest
094: .getMainAttributes();
095: } finally {
096: jarFile.close();
097: }
098: } catch (IOException e) {
099: return null;
100: }
101: }
102: }
|