Back to index

lightning-sunbird  0.9+nobinonly
ProxyClassLoaderFactory.java
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the MRJ Carbon OJI Plugin.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Patrick C. Beard <beard@netscape.com>
00023  *   Xiaobin Lu <Xiaobin.Lu@eng.Sun.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 package netscape.oji;
00040 
00041 import java.net.URLClassLoader;
00042 import java.net.URL;
00043 import java.net.MalformedURLException;
00044 
00045 import java.io.*;
00046 import java.util.zip.*;
00047 import java.util.WeakHashMap;
00048 
00049 import java.security.CodeSource;
00050 
00051 public abstract class ProxyClassLoaderFactory {
00052     static void debug(String message) {
00053         System.out.println("<<< " + message + " >>>");
00054     }
00055 
00059     private static byte[] getMRJPluginClassFile(String path) {
00060         try {
00061               String homeDir = System.getProperty("netscape.oji.plugin.home");
00062               ZipFile jarFile = new ZipFile(new File(homeDir, "MRJPlugin.jar"));
00063               ZipEntry classEntry = jarFile.getEntry(path);
00064               int size = (int) classEntry.getSize();
00065               if (size > 0) {
00066                      byte[] data = new byte[size];
00067                      DataInputStream input = new DataInputStream(jarFile.getInputStream(classEntry));
00068                      input.readFully(data);
00069                      input.close();
00070                      jarFile.close();
00071                      return data;
00072               }
00073         } catch (IOException ioe) {
00074         }
00075               return null;
00076     }
00077     
00083     private static class ProxyClassLoader extends URLClassLoader {
00084         private static byte[] data = getMRJPluginClassFile("netscape/oji/LiveConnectProxy.class");
00085         ProxyClassLoader(URL[] documentURLs) {
00086             super(documentURLs);
00087             if (data != null) {
00088                 Class proxyClass = defineClass("netscape.oji.LiveConnectProxy",
00089                                                data, 0, data.length,
00090                                                new CodeSource(documentURLs[0], null));
00091                 debug("ProxyClassLoader: defined LiveConnectProxy class.");
00092                 debug("Here're the permisssions you've got:");
00093                 debug(proxyClass.getProtectionDomain().getPermissions().toString());
00094             } else {
00095                 debug("ProxyClassLoader: failed to define LiveConnectProxy class.");
00096             }
00097         }
00098     }
00099     
00100     // XXX  Should this be a weak table of some sort?
00101     private static WeakHashMap mClassLoaders = new WeakHashMap();
00102 
00103     public static ClassLoader createClassLoader(final String documentURL) throws MalformedURLException {
00104         ClassLoader loader = (ClassLoader) mClassLoaders.get(documentURL);
00105         if (loader == null) {
00106             try {
00107                 URL[] documentURLs = new URL[] { new URL(documentURL) };
00108                 loader = new ProxyClassLoader(documentURLs);
00109                 mClassLoaders.put(documentURL, loader);
00110               } catch (MalformedURLException e) {
00111             }
00112         }
00113         return loader;
00114     }
00115     
00116     public static void destroyClassLoader(final String documentURL) {
00117         mClassLoaders.remove(documentURL);
00118     }
00119 }