home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / multimedia / ppsee / PPSeeSetup.exe / lib / bsh-commands-2.0b4.jar / bsh / commands / load.bsh < prev    next >
Text File  |  2005-05-23  |  2KB  |  89 lines

  1. /**
  2.     Load a serialized Java object from filename.  Returns the object.
  3. */
  4.  
  5. bsh.help.load = "usage: load(filename)";
  6.  
  7. setAccessibility(true);
  8.  
  9. import bsh.BshClassManager;
  10. import java.io.*;
  11. import java.lang.reflect.Proxy;
  12.  
  13. import bsh.Capabilities;
  14.  
  15. if ( Capabilities.classExists("bsh.ClassGeneratorImpl") )
  16. {
  17. public class BshObjectInputStream extends ObjectInputStream
  18. {
  19.     BshClassManager bcm;
  20.  
  21.     public BshObjectInputStream( BshClassManager bcm, InputStream in)
  22.         throws IOException, StreamCorruptedException
  23.     {
  24.         super(in);
  25.         this.bcm = bcm;
  26.     }
  27.  
  28.     protected Class resolveClass( ObjectStreamClass clas )
  29.         throws IOException, ClassNotFoundException
  30.     {
  31. //        ClassLoader loader = Thread.currentThread().getContextClassLoader();
  32. //        return Class.forName( clas.getName(), false, loader );
  33.         Class c = null;
  34.         try {
  35.             c = super.resolveClass( clas );
  36.         } catch ( ClassNotFoundException e ) { }
  37.         if ( c != null )
  38.             return c;
  39.         c = bcm.classForName( clas.getName() );
  40.         if ( c != null )
  41.             return c;
  42.         throw new ClassNotFoundException( "bcm not found: "+clas.getName() );
  43.     }
  44.  
  45. /*
  46.     protected Class resolveProxyClass( java.lang.String[] interfaces )
  47.         throws IOException, ClassNotFoundException
  48.     {
  49.         return super.resolveProxyClass( interfaces );
  50.  
  51. //        ClassLoader loader = Thread.currentThread().getContextClassLoader();
  52. //
  53. //        Class[] classes = new Class[interfaces.length];
  54. //
  55. //        for (int i = 0; i < interfaces.length; i++)
  56. //            classes[i] = Class.forName(interfaces[i], false, loader);
  57. //
  58. //        try {
  59. //            return Proxy.getProxyClass(loader, classes);
  60. //        } catch (IllegalArgumentException e) {
  61. //            throw new ClassNotFoundException("Proxy class not found", e);
  62. //        }
  63.     }
  64. */
  65. }
  66. }
  67.  
  68. Object load( String filename )
  69. {
  70.     this.file = pathToFile( filename );
  71.  
  72.     Object obj;
  73.     FileInputStream in = new FileInputStream( file );
  74.     javap( BshObjectInputStream );
  75.     ObjectInputStream oin;
  76.     if ( BshObjectInputStream != void )
  77.         oin = new BshObjectInputStream( this.namespace.getClassManager(), in );
  78.     else
  79.         oin = new ObjectInputStream( in );
  80.     obj = oin.readObject();
  81.     oin.close();
  82.  
  83.     // bind bsh objects into the caller's namespace
  84.     if ( obj instanceof bsh.This )
  85.         bind( obj, this.caller.namespace );
  86.  
  87.     return obj;
  88. }
  89.