KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > classfile > BCELClassFileLoader


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
24 /*
25  * BCELClassFileLoader.java
26  *
27  * Created on September 7, 2004, 5:41 PM
28  */

29
30 package com.sun.enterprise.tools.verifier.apiscan.classfile;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.net.URLClassLoader JavaDoc;
38 import java.net.URLConnection JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * *This is a factory for {@link BCELClassFile}. This is not a public class, as
46  * I expect users to use {@link ClassFileLoaderFactory} interface. This class
47  * internally uses the the standard Java ClassLoader to load the resource and
48  * construct BCELClassFile object out of it.
49  *
50  * @author Sanjeeb.Sahoo@Sun.COM
51  */

52 class BCELClassFileLoader implements ClassFileLoader {
53
54     private ClassLoader JavaDoc cl;
55     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
56     private static Logger JavaDoc logger = Logger.getLogger("apiscan.classfile", resourceBundleName); // NOI18N
57
private final static String JavaDoc myClassName = "BCELClassFileLoader"; // NOI18N
58

59     /**
60      * Creates a new instance of BCELClassFileLoader.
61      *
62      * @param cp that will be used to create a new java.net.URLClassLoader. In
63      * subsequent load operations, this classloader will be used.
64      */

65     public BCELClassFileLoader(String JavaDoc cp) {
66         ArrayList JavaDoc<URL JavaDoc> urls = new ArrayList JavaDoc<URL JavaDoc>();
67         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cp, File.pathSeparator);
68              st.hasMoreTokens();) {
69             String JavaDoc entry = st.nextToken();
70             try {
71                 // toURI().toURL() takes care of all the escape characters
72
// in the absolutePath. The toURI() method encodes all escape
73
// characters. Later URLClassLoader decodes and regenerates
74
// correct escape characters.
75
urls.add(new File JavaDoc(entry).toURI().toURL());
76             } catch (MalformedURLException JavaDoc e) {
77                 logger.logp(Level.WARNING, myClassName, "init<>", getClass().getName() + ".exception1", new Object JavaDoc[]{entry});
78                 logger.log(Level.WARNING, "", e);
79             }
80         }
81         //We do not want system class loader or even extension class loadera s our parent.
82
//We want only boot class loader as our parent. Boot class loader is represented as null.
83
cl = new URLClassLoader JavaDoc((URL JavaDoc[]) urls.toArray(new URL JavaDoc[0]), null);
84     }
85
86     /**
87      * Creates a new instance of BCELClassFileLoader.
88      *
89      * @param cl is the classloader that will be used in subsequent load
90      * operations.
91      */

92     public BCELClassFileLoader(ClassLoader JavaDoc cl) {
93         this.cl = cl;
94     }
95
96     //See corresponding method of ClassFileLoader
97
public ClassFile load(String JavaDoc externalClassName) throws IOException JavaDoc {
98         logger.entering("BCELClassFileLoader", "load", externalClassName); // NOI18N
99
//URLClassLoader library expects me to pass in internal form.
100
String JavaDoc internalClassName = externalClassName.replace('.', '/');
101         URL JavaDoc resource = cl.getResource(internalClassName + ".class");
102         //URLClassLoader returns null if resource is not found.
103
if(resource == null)
104             throw new IOException JavaDoc("Not able to load " + internalClassName + ".class");
105         URLConnection JavaDoc urlcon = resource.openConnection();
106         urlcon.setUseCaches(false); // bug 6328564
107
InputStream JavaDoc is = urlcon.getInputStream();
108         try {
109             ClassFile cf = new BCELClassFile(is, internalClassName + ".class"); // NOI18N
110
matchClassSignature(cf, externalClassName);
111             return cf;
112         } finally {
113             is.close();
114         }
115     }
116
117     //This method is neede to be protected against users who are passing us
118
//internal class names instead of external class names or
119
//when the file actually represents some other class, but it isnot
120
//available in in proper package hierarchy.
121
private void matchClassSignature(ClassFile cf, String JavaDoc externalClassName)
122             throws IOException JavaDoc {
123         String JavaDoc nameOfLoadedClass = cf.getName();
124         if (!nameOfLoadedClass.equals(externalClassName)) {
125             throw new IOException JavaDoc(externalClassName + ".class represents " +
126                     cf.getName() +
127                     ". Perhaps your package name is incorrect or you passed the " +
128                     "name using internal form instead of using external form.");
129         }
130     }
131 }
132
Popular Tags