KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URLClassLoader JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34
35 /**
36  * Abstract superclass of classes that manage the class path needed to run
37  * the Java Web Start-aware ACC.
38  *<p>
39  * Some details vary among releases of the Java runtime This abstract class
40  * and its concrete implementation subclasses isolate those dependencies.
41  *
42  * @author tjquinn
43  */

44 public abstract class ClassPathManager {
45     
46     /** instance of appropriate type of class path manager, depending on the Java runtime version */
47     private static ClassPathManager mgr = null;
48     
49     /**
50      *Returns the appropriate type of ClassPathManager.
51      *@return an instance of the correct implementation subclass class path manager
52      */

53     public static ClassPathManager getClassPathManager() {
54         if (mgr == null) {
55             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
56             /*
57              *Distinguish between 1.6 and earlier by seeing if the curent
58              *class loader - the JNLP class loader - is also a URLClassLoader
59              *or not.
60              */

61             if (loader instanceof URLClassLoader JavaDoc) {
62                 mgr = new ClassPathManager16(loader);
63             } else {
64                 mgr = new ClassPathManager15(loader);
65             }
66         }
67         return mgr;
68     }
69     
70     /** the JNLP class loader active during the instantiation of this mgr */
71     private ClassLoader JavaDoc jnlpClassLoader = null;
72
73     /**
74      *Returns a new instance of the manager.
75      *@param loader the class loader provided by Java Web Start
76      */

77     protected ClassPathManager(ClassLoader JavaDoc loader) {
78         jnlpClassLoader = loader;
79     }
80     
81     /**
82      *Locates the URI for the JAR containing the specified class
83      *@param className the name of the class to be located
84      *@return the URI for the JAR file containing the class of interest
85      */

86     public URI JavaDoc locateClass(String JavaDoc className) throws
87             IllegalAccessException JavaDoc,
88             InvocationTargetException JavaDoc,
89             MalformedURLException JavaDoc,
90             URISyntaxException JavaDoc {
91         String JavaDoc resourceName = classNameToResourceName(className);
92         URL JavaDoc classURL = locateResource(resourceName);
93         File JavaDoc f = findContainingJar(classURL);
94         return f.toURI();
95     }
96
97     /**
98      *Returns the appropriate parent class loader for the ACC.
99      *@return the correct class loader instance
100      */

101     public abstract ClassLoader JavaDoc getParentClassLoader();
102
103     /*
104      *Returns the jar that contains the specified resource.
105      *@param resourceURL URL to look for
106      *@return File object for the jar or directory containing the entry
107      */

108     public abstract File JavaDoc findContainingJar(URL JavaDoc resourceURL) throws
109             IllegalArgumentException JavaDoc,
110             URISyntaxException JavaDoc,
111             MalformedURLException JavaDoc,
112             IllegalAccessException JavaDoc,
113             InvocationTargetException JavaDoc;
114
115     /**
116      *Returns the Java Web Start-provided class loader recorded when the
117      *class path manager was created.
118      *@return the Java Web Start class loader
119      */

120     protected ClassLoader JavaDoc getJNLPClassLoader() {
121         return jnlpClassLoader;
122     }
123     
124     /**
125      *Converts a class name to a resource name.
126      *@param className the name of the class of interest in x.y.z format
127      *@return the resource name in x/y/z.class format
128      */

129     protected String JavaDoc classNameToResourceName(String JavaDoc className) {
130         return className.replace(".", "/") + ".class";
131     }
132
133     /**
134      *Finds a resource using the class's class loader.
135      *@param resourceName the class to find
136      *@return URL for the resource; null if not found
137      */

138     protected URL JavaDoc locateResource(String JavaDoc resourceName) {
139         URL JavaDoc resourceURL = getClass().getClassLoader().getResource(resourceName);
140         return resourceURL;
141     }
142      
143     /**
144      *Reports URLs for the locally-cached copies of the JARs downloaded by
145      *Java Web Start needed for the ACC's class path and policy settings.
146      *@return array of URLs, one entry for each downloaded JAR
147      */

148     public URL JavaDoc[] locateDownloadedJars() throws
149             ClassNotFoundException JavaDoc,
150             URISyntaxException JavaDoc,
151             NoSuchMethodException JavaDoc,
152             IllegalAccessException JavaDoc,
153             InvocationTargetException JavaDoc,
154             MalformedURLException JavaDoc {
155         /*
156          *For each downloaded unsigned app client jar, get a URL that locates
157          *it and add the URL to the list.
158          *
159          *This set of values should be automated on the server side and
160          *communicated via a property setting in the JNLP document so
161          *any changes in the list of downloaded files does not need to be made
162          *there and here.
163          */

164         String JavaDoc probeClassNames = System.getProperty("com.sun.aas.jar.probe.class.names",
165                 "com.sun.enterprise.appclient.jws.boot.JWSACCMain," /* appserv-jwsacc */ +
166                 "com.sun.enterprise.appclient.Main," /* appserv-rt */ +
167                 "com.sun.jdo.api.persistence.enhancer.ByteCodeEnhancer," /* appserv-cmp */ +
168                 "com.sun.enterprise.admin.servermgmt.DomainConfig," /* appserv-admin */ +
169                 "com.sun.enterprise.deployment.client.DeploymentClientUtils," /* appserv-deployment-client */ +
170                 "javax.ejb.EJB," /* javaee */ +
171                 "com.sun.appserv.management.ext.logging.LogAnalyzer," /* appserv-ext */ +
172                 "com.sun.mail.iap.Argument," /* mail */ +
173                 "com.sun.activation.viewers.ImageViewer," /* activation */ +
174                 "com.sun.codemodel.ClassType," /* appserv-ws */ +
175                 "org.apache.derby.client.ClientDataSourceFactory," /* derbyclient.jar */ +
176                 "persistence.antlr.ActionElement," /* toplink-essentials */ +
177                 "org.netbeans.modules.dbschema.ColumnElement," /* dbschema */ +
178                 "com.sun.jms.spi.xa.JMSXAConnection," /* imqjmsra */ +
179                 "com.sun.jndi.fscontext.FSContext" /* fscontext */
180                 );
181
182         String JavaDoc [] classNames = probeClassNames.split(",");
183
184         /*
185          *For each class name, find the jar that contains it by getting a URL
186          *to the class and then using that URL to find the jar.
187          */

188         URL JavaDoc [] urls = new URL JavaDoc[classNames.length];
189         int nextURL = 0;
190
191         for (String JavaDoc className : classNames) {
192             URI JavaDoc jarFileURI = locateClass(className);
193             URL JavaDoc url = jarFileURI.toURL();
194             urls[nextURL++] = url;
195         }
196         return urls;
197     }
198 }
199
Popular Tags