KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > jws > boot > ClassPathManager16


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.appclient.jws.boot;
24
25 import java.io.File JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.net.URISyntaxException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34
35 /**
36  * Class Path manager for Java Web Start-aware ACC running under Java runtime 1.6.
37  *
38  * @author tjquinn
39  */

40 public class ClassPathManager16 extends ClassPathManager {
41     
42     /** Class object for the JNLPClassLoader class */
43     private Class JavaDoc jnlpClassLoaderClass;
44
45     /** Method object for the getJarFile method on JNLPClassLoader - only in 1.6 and later */
46     private Method JavaDoc getJarFileMethod;
47
48     /**
49      *Returns a new instance of the class path manager for use under Java 1.6
50      *@param loader the Java Web Start-provided class loader
51      */

52     protected ClassPathManager16(ClassLoader JavaDoc loader) {
53         super(loader);
54         try {
55             prepareIntrospectionInfo();
56         } catch (Throwable JavaDoc thr) {
57             throw new RuntimeException JavaDoc(thr);
58         }
59     }
60
61     /**
62      *Prepares the reflection-related private variables for later use in
63      *locating classes in JARs.
64      *@throws ClassNotFoundException if the JNLPClassLoader class cannot be found
65      *@throws NoSuchMethodException if the getJarFile method cannot be found
66      */

67     private void prepareIntrospectionInfo() throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
68         jnlpClassLoaderClass = getJNLPClassLoader().loadClass("com.sun.jnlp.JNLPClassLoader");
69         getJarFileMethod = jnlpClassLoaderClass.getDeclaredMethod("getJarFile", URL JavaDoc.class);
70         getJarFileMethod.setAccessible(true);
71     }
72
73     public ClassLoader JavaDoc getParentClassLoader() {
74         return getJNLPClassLoader().getParent();
75     }
76
77     public File JavaDoc findContainingJar(URL JavaDoc resourceURL) throws IllegalArgumentException JavaDoc, URISyntaxException JavaDoc, MalformedURLException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
78         /*
79          *The URL will be similar to http://host:port/...path-in-server-namespace!resource-spec
80          *Extract the part preceding the ! and ask the Java Web Start loader to
81          *find the locally-cached JAR file corresponding to that part of the URL.
82          */

83         URI JavaDoc resourceURI = resourceURL.toURI();
84         String JavaDoc ssp = resourceURI.getSchemeSpecificPart();
85         String JavaDoc jarOnlySSP = ssp.substring(0, ssp.indexOf('!'));
86
87         URL JavaDoc jarOnlyURL = new URL JavaDoc(jarOnlySSP).toURI().toURL();
88
89         /*
90          *Use introspection to invoke the method. This avoids complications
91          *in building the app server under Java 1.5 in which the JNLPClassLoader
92          *does not provide the getJarFile method.
93          */

94         JarFile JavaDoc jarFile = (JarFile JavaDoc) getJarFileMethod.invoke(getJNLPClassLoader(), jarOnlyURL);
95         File JavaDoc f = new File JavaDoc(jarFile.getName());
96         return f;
97     }
98 }
99
Popular Tags