Back to index

im-sdk  12.3.91
ODClassLoader.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.io.*;
00047 import java.net.URL;
00048 import java.util.HashMap;
00049 import java.util.HashSet;
00050 import java.util.Iterator;
00051 import java.util.jar.*;
00052 import java.security.*;
00053 import javax.swing.ImageIcon;
00054 
00055 public class ODClassLoader extends SecureClassLoader {
00056 
00057 
00058     private HashMap byteTable = new HashMap();
00059 
00060     ODClassLoader() {
00061        super(ClassLoader.getSystemClassLoader());
00062     }
00063 
00064     void addJar(JarInputStream jis) {
00065        JarEntry je;
00066        boolean defc;
00067        
00068        for (;;) {
00069            try {
00070               je = jis.getNextJarEntry();
00071            } catch(Exception e) {
00072               continue;
00073            }
00074            
00075            if (je == null) {
00076               break;
00077            }
00078 
00079            String name = je.getName();
00080            //
00081            // should use ByteArrayOutputStream and getByteArray()!!!
00082            //
00083            byte[] buf = new byte[1024];
00084            int bufLen = 2048;
00085            byte[] buffer = new byte[bufLen];
00086            int total = 0, len;
00087            try {
00088               for (;(len = jis.read(buf, 0, buf.length)) != -1;) {
00089                   if (bufLen < total + len) {
00090                      byte[] tmp = new byte[bufLen * 2];
00091                      System.arraycopy(buffer, 0, tmp, 0, total);
00092                      buffer = tmp;
00093                      bufLen *= 2;
00094                   }
00095                   System.arraycopy(buf, 0, buffer, total, len);
00096                   total += len;
00097               }
00098            } catch(Exception e) {
00099               if (Manager.DEBUG) {
00100                   e.printStackTrace();
00101               }
00102               continue;
00103            }
00104            //
00105            // store byte array for findClass() of ODClassLoader
00106            //
00107            byte[] realBuf = new byte[total];
00108            System.arraycopy(buffer, 0, realBuf, 0, total);
00109            byteTable.put(name, realBuf);
00110        }
00111     }
00112 
00116     public Class findClass(String name) {
00117        
00118        byte[] ba = (byte[])byteTable.get(name.replace('.', '/') + ".class");
00119 
00120        if (ba == null) {
00121            debug(" ODClassLoader: Unknown class " + name);
00122            return null;
00123        }
00124        Class cls = null;
00125        try {
00126            // CodeSource object will be added later.
00127            //
00128            cls = defineClass(name, ba, 0, ba.length, (CodeSource)null);
00129        } catch(Throwable e) {
00130            debug(" E = " + e);
00131            return null;
00132        }
00133 
00134        return cls;
00135     }
00136 
00137     public ImageIcon getImageIcon(String name) {
00138 
00139        byte[] ba = (byte[])byteTable.get(name);
00140        if (ba == null) {
00141            debug(" ODClassLoader: " + name + " not found.");
00142            return null;
00143        }
00144 
00145        return new ImageIcon(ba);
00146     }
00147 
00148     public String getString(String name) {
00149 
00150        byte[] ba = (byte[])byteTable.get(name);
00151        if (ba == null) {
00152            debug(" ODClassLoader: " + name + " not found.");
00153            return null;
00154        }
00155        String ret = null;
00156        try {
00157            ret = new String(ba, "UTF-8");
00158        } catch(Exception e) {
00159            if (Manager.DEBUG) {
00160               e.printStackTrace();
00161            }
00162        }
00163 
00164        return ret;
00165     }
00166 
00167     
00168 
00169     public URL getURL(String name) {
00170 
00171        byte[] ba = (byte[])byteTable.get(name);
00172 
00173        if (ba == null) {
00174            debug(" ODClassLoader: " + name + " not found.");
00175        }
00176        URL url = null;
00177        String anchor = null;
00178        try {
00179            File f = File.createTempFile("iiim", "iiim");
00180            String tempName = f.getAbsolutePath();
00181            f.delete();
00182            f = new File(tempName);
00183            f.mkdir();
00184 
00185            int anchorID = name.indexOf('#');
00186            if (anchorID > 0) {
00187               anchor = name.substring(anchorID);
00188               name = name.substring(0, anchorID);
00189            }
00190 
00191            int fileid = name.lastIndexOf("/");
00192            String dirname = name.substring(0, fileid);
00193            Object[] writeList = getWriteList(dirname);
00194 
00195            for (int i = 0; i < writeList.length; i++) { 
00196               String filename = ((String)writeList[i]).substring
00197                   (fileid, ((String)writeList[i]).length());
00198               if (filename.equals("") || filename.equals("/")) {
00199                   continue;
00200               }
00201               File f2 = new File(f, filename);
00202               byte[] ba2 = (byte[])byteTable.get((String)writeList[i]);
00203               if (ba2 == null) {
00204                   continue;
00205               }
00206               FileOutputStream fos = new FileOutputStream(f2);
00207               BufferedOutputStream bos = new BufferedOutputStream(fos);
00208               bos.write(ba2);
00209               bos.close();
00210               if (name.equals((String)writeList[i])) {
00211                   url = f2.toURL();
00212                   if (anchor != null) {
00213                      String s = url.toExternalForm();
00214                      url = new URL(s + anchor);
00215                   }
00216               }
00217            }
00218        } catch(Exception e) {
00219            debug(" getURL faied");
00220        }
00221 
00222        return url;
00223     }
00224 
00225     private Object[] getWriteList(String dirname) {
00226        HashSet strSet = new HashSet();
00227        Iterator iterator = byteTable.keySet().iterator();
00228        for (; iterator.hasNext();) {
00229            String key = (String)iterator.next();
00230            if (key.startsWith(dirname)) {
00231               strSet.add(key);
00232            }
00233        }
00234 
00235        return strSet.toArray();
00236     }
00237        
00238     private void debug(String str) {
00239        if (Manager.DEBUG) {
00240            System.err.println(str);
00241        }
00242     }
00243 }