KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > loading > MBeanClassLoader


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 package com.sun.enterprise.admin.mbeans.custom.loading;
25
26 import com.sun.enterprise.admin.mbeans.custom.CMBStrings;
27 import com.sun.enterprise.admin.servermgmt.RepositoryConfig;
28 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
29 import com.sun.enterprise.util.SystemPropertyConstants;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.io.BufferedInputStream JavaDoc;
38 import com.sun.enterprise.util.OS;
39
40 /** A custom class loader to load the MBeans defined by users. This class-loader delegates
41  * to system-class-loader, <code> sun.misc.Launcher$AppClassLoader@169e11 for Sun's JRE</code>.
42  * One instance of this class-loader is created at the startup and it loads the classes from
43  * a specific location that is passed to it. If the bits of the class that has been loaded
44  * by this instance of the class are now changed on the disk, a new instance of the class loader
45  * is first created and that instance of this class-loader loads the new bits giving birth to a new class.
46  * More about this in the design document.
47  * @since SJSAS 9.0
48  */

49 public final class MBeanClassLoader extends ClassLoader JavaDoc {
50     
51     private URL JavaDoc url;
52     private ClassLoader JavaDoc parent;
53     /** Creates a new instance of MBeanClassLoader */
54     /*
55     public MBeanClassLoader(final URL urlToLoadFrom, final ClassLoader parent) throws IllegalArgumentException {
56         super(parent);
57         if (urlToLoadFrom == null || parent == null)
58             throw new IllegalArgumentException("null arguments"); //TODO
59         initialize(urlToLoadFrom, parent);
60     }
61     public MBeanClassLoader(final File diskFolder, final ClassLoader parent) throws IllegalArgumentException {
62         super(parent);
63         if (diskFolder == null)
64             throw new IllegalArgumentException("null arguments"); //TODO
65         if (!diskFolder.isDirectory())
66             throw new IllegalArgumentException("invalid, can load only from a folder"); //TODO
67         if (!diskFolder.exists())
68             throw new IllegalArgumentException("invalid, does not exist: " + diskFolder.getPath()); //TODO
69         if (!diskFolder.canRead())
70             throw new IllegalArgumentException("invalid, can not read: " + diskFolder.getPath()); //TODO
71         try {
72             initialize(diskFolder.toURL());
73         } catch (final MalformedURLException m) {
74             throw new IllegalArgumentException(m);
75         }
76     }
77     */

78     public MBeanClassLoader() throws IllegalArgumentException JavaDoc {
79         super(MBeanClassLoader.class.getClassLoader());
80         init();
81     }
82     
83     public MBeanClassLoader(final ClassLoader JavaDoc delegatee) throws IllegalArgumentException JavaDoc {
84         super(delegatee);
85         init();
86     }
87     
88     private void init() throws IllegalArgumentException JavaDoc {
89         final File JavaDoc mbf = getDefaultMBeanDirectory();
90         try {
91             this.url = mbf.toURL();
92         } catch (final MalformedURLException JavaDoc m) {
93             throw new IllegalArgumentException JavaDoc(m);
94         }
95     }
96     
97     private File JavaDoc getDefaultMBeanDirectory() {
98         /* Ideally, PEFileLayout should be utilized here which should be the central place
99          * to get all these paths, locations. But that is too cryptic to find out. Hence
100          * I am relying on the properties that are passed to the VM at startup.
101          * These properties include the SystemPropertyConstants.INSTANCE_ROOT which always
102          * points to where the server instance or domain configuration is stored.
103          */

104         final File JavaDoc appsFolder = new File JavaDoc(System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), PEFileLayout.APPLICATIONS_DIR);
105         return ( new File JavaDoc(appsFolder, PEFileLayout.MBEAN_FOLDER_NAME) );
106     }
107     protected Class JavaDoc findClass(final String JavaDoc className) throws ClassNotFoundException JavaDoc {
108         final byte[] cd = getClassData(className);
109         if (cd == null)
110             throw new ClassNotFoundException JavaDoc(CMBStrings.get("findClassFailed", className));
111         return ( defineClass(className, cd, 0, cd.length) );
112     }
113     
114     private byte[] getClassData(final String JavaDoc cn) {
115         byte[] cd = null;
116         try {
117             if(isURLFile()) {
118                 cd = getClassDataFromFile(url.getFile(), cn);
119             } else {
120                 cd = getClassDataFromURL();
121             }
122         } catch(final Exception JavaDoc e) {
123             e.printStackTrace();
124         }
125         return ( cd );
126     }
127     private boolean isURLFile() {
128         return ( "file".equals(url.getProtocol()) );
129     }
130     
131     private byte[] getClassDataFromFile(final String JavaDoc base, final String JavaDoc cn) throws IOException JavaDoc, FileNotFoundException JavaDoc {
132         String JavaDoc path = base + '/' + cn.replace('.', '/') + ".class";
133         path = prunePath(path);
134         final FileInputStream JavaDoc fis = new FileInputStream JavaDoc(path);
135         final BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
136         final ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
137         byte[] cd = null;
138         try {
139             byte[] buf = new byte[1024]; // reading several bytes
140
int br = bis.read(buf, 0, buf.length);
141             while (br != -1) {
142                 bos.write(buf, 0, br);
143                 br = bis.read(buf, 0, buf.length);
144             }
145             cd = bos.toByteArray();
146         } catch(final FileNotFoundException JavaDoc fe) {
147             throw fe;
148         } catch (final IOException JavaDoc e) {
149             throw e;
150         } finally {
151             try {
152                 //This is the single most important item in loading the classes!
153
bis.close();
154                 bos.close();
155             } catch(Throwable JavaDoc t) {}
156             return ( cd );
157         }
158     }
159     private String JavaDoc prunePath(final String JavaDoc path) {
160         /* don't know why url.toFile has got a leading '/' on Windows */
161         if (OS.isWindows() && path.charAt(0) == '/') {
162             return ( path.substring(1) );
163         }
164         else
165             return ( path ) ;
166     }
167     private byte[] getClassDataFromURL() {
168         throw new UnsupportedOperationException JavaDoc(CMBStrings.get("InternalError", "getClassDataFromURL() -- not implemented yet"));
169     }
170     ///// Private Methods /////
171
}
172
Popular Tags