cjolif      2003/03/14 03:02:02

  Modified:    sources/org/apache/batik/script/rhino RhinoClassLoader.java
                        RhinoInterpreter.java WindowWrapper.java
               xdocs    whoAreWe.xml
  Removed:     sources/org/apache/batik/script/rhino
                        BatikSecuritySupport.java
                        EventTargetWrapHandler.java
  Log:
  Changes in the Rhino Interpreter implementation to fit the new Rhino15R4
  version (change in the security model, add the ability to not wrap java
  primitives objects + some cosmetics changes).
  
  Revision  Changes    Path
  1.6       +21 -14    
xml-batik/sources/org/apache/batik/script/rhino/RhinoClassLoader.java
  
  Index: RhinoClassLoader.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/RhinoClassLoader.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RhinoClassLoader.java     12 Nov 2002 09:51:35 -0000      1.5
  +++ RhinoClassLoader.java     14 Mar 2003 11:02:02 -0000      1.6
  @@ -23,6 +23,8 @@
   import java.security.ProtectionDomain;
   import java.security.SecureClassLoader;
   
  +import org.mozilla.javascript.GeneratedClassLoader;
  +
   /**
    * This class loader implementation will work whether or not the
    * documentURL is null.
  @@ -30,7 +32,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a>
    * @version $Id$
    */
  -public class RhinoClassLoader extends URLClassLoader {
  +public class RhinoClassLoader extends URLClassLoader implements 
GeneratedClassLoader {
       /**
        * URL for the document referencing the script.
        */
  @@ -42,35 +44,34 @@
       protected CodeSource codeSource;
   
       /**
  -     * The AccessControlContext which can be associated with 
  +     * The AccessControlContext which can be associated with
        * code loaded by this class loader if it was running
        * stand-alone (i.e., not invoked by code with lesser
        * priviledges).
        */
       protected AccessControlContext rhinoAccessControlContext;
  -    
  +
       /**
        * Constructor.
  -     * @param documentURL the URL from which to load classes and resources 
  -     * @param parent the parent class loader for delegation 
  +     * @param documentURL the URL from which to load classes and resources
  +     * @param parent the parent class loader for delegation
        */
       public RhinoClassLoader(URL documentURL, ClassLoader parent){
           super(documentURL != null ? new URL[]{documentURL} : new URL[]{},
                 parent);
  -        // super(new URL[]{});
           this.documentURL = documentURL;
           if (documentURL != null){
               codeSource = new CodeSource(documentURL, null);
           }
  -        
  +
           //
           // Create the Rhino ProtectionDomain
           // and AccessControlContext
           //
  -        ProtectionDomain rhinoProtectionDomain 
  +        ProtectionDomain rhinoProtectionDomain
               = new ProtectionDomain(codeSource,
                                      getPermissions(codeSource));
  -        
  +
           rhinoAccessControlContext
               = new AccessControlContext(new ProtectionDomain[]{
                   rhinoProtectionDomain});
  @@ -79,13 +80,19 @@
       /**
        * Define and load a Java class
        */
  -    public Class defineClass(String name, 
  -                             byte[] data){
  -        // System.out.println("========================== Trying to load : " + 
name);
  +    public Class defineClass(String name,
  +                             byte[] data) {
           return super.defineClass(name, data, 0, data.length, codeSource);
       }
   
       /**
  +     * Links the Java class.
  +     */
  +    public void linkClass(Class clazz) {
  +        super.resolveClass(clazz);
  +    }
  +
  +    /**
        * Returns the AccessControlContext which should be associated with
        * RhinoCode.
        */
  @@ -94,7 +101,7 @@
       }
   
       /**
  -     * Returns the permissions for the given CodeSource object. 
  +     * Returns the permissions for the given CodeSource object.
        * Compared to URLClassLoader, this adds a FilePermission so
        * that files under the same root directory as the document
        * can be read.
  
  
  
  1.23      +54 -78    
xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java
  
  Index: RhinoInterpreter.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- RhinoInterpreter.java     12 Nov 2002 09:51:35 -0000      1.22
  +++ RhinoInterpreter.java     14 Mar 2003 11:02:02 -0000      1.23
  @@ -35,12 +35,12 @@
   import org.mozilla.javascript.ImporterTopLevel;
   import org.mozilla.javascript.JavaScriptException;
   import org.mozilla.javascript.NativeJavaPackage;
  -import org.mozilla.javascript.SecuritySupport;
  +import org.mozilla.javascript.SecurityController;
   import org.mozilla.javascript.Script;
   import org.mozilla.javascript.Scriptable;
   import org.mozilla.javascript.ScriptableObject;
   import org.mozilla.javascript.WrappedException;
  -import org.mozilla.javascript.WrapHandler;
  +import org.mozilla.javascript.WrapFactory;
   
   /**
    * A simple implementation of <code>Interpreter</code> interface to use
  @@ -84,23 +84,10 @@
        */
       public static final String BIND_NAME_WINDOW = "window";
   
  -    /**
  -     * Instantiate the "window" object
  -     */
  -    public static final String ECMA_WINDOW_INSTANTIATION = "window = new 
Window(window)";
  -
       private ScriptableObject globalObject = null;
       private LinkedList compiledScripts = new LinkedList();
  -    private WrapHandler wrapHandler =
  -        new EventTargetWrapHandler(this);
  -
  -    /**
  -     * The SecuritySupport implementation for Batik,
  -     * which ensures scripts have access to the
  -     * server they were downloaded from
  -     */
  -    private SecuritySupport securitySupport
  -        = new BatikSecuritySupport();
  +    private WrapFactory wrapFactory =
  +        new BatikWrapFactory(this);
   
       /**
        * The Rhino 'security domain'. We use the RhinoClassLoader
  @@ -110,6 +97,14 @@
       protected RhinoClassLoader rhinoClassLoader;
   
       /**
  +     * The SecurityController implementation for Batik,
  +     * which ensures scripts have access to the
  +     * server they were downloaded from
  +     */
  +    private SecurityController securityController
  +        = new BatikSecurityController();
  +
  +    /**
        * Default Context for scripts. This is used only for efficiency
        * reason.
        */
  @@ -132,27 +127,30 @@
       public RhinoInterpreter(URL documentURL) {
           rhinoClassLoader = new RhinoClassLoader(documentURL,
                                                   getClass().getClassLoader());
  -
           Context.setCachingEnabled(false); // reset the cache
           Context.setCachingEnabled(true);  // enable caching again
           // entering a context
  -        defaultContext = enterContext();
  -        Context ctx = defaultContext;
  -
  +        Context ctx = enterContext();
           try {
  -
  -            // init std object with an importer
  -            // building the importer automatically initialize the
  -            // context with it since Rhino1.5R3
  -            ImporterTopLevel importer = new ImporterTopLevel(ctx);
  -            globalObject = importer;
  +            try {
  +                Scriptable scriptable = ctx.initStandardObjects(null, false);
  +                ScriptableObject.defineClass(scriptable, WindowWrapper.class);
  +            } catch (Exception e) {
  +                // cannot happen
  +            }
  +            // we now have the window object as the global object from the
  +            // launch of the interpreter.
  +            // 1. it works around a Rhino bug introduced in 15R4 (but fixed
  +            // by a later patch.
  +            // 2. it sounds cleaner.
  +            WindowWrapper wWrapper = new WindowWrapper(ctx);
  +            globalObject = wWrapper;
               // import Java lang package & DOM Level 2 & SVG DOM packages
               NativeJavaPackage[] p= new NativeJavaPackage[TO_BE_IMPORTED.length];
               for (int i = 0; i < TO_BE_IMPORTED.length; i++) {
                   p[i] = new NativeJavaPackage(TO_BE_IMPORTED[i]);
               }
  -            importer.importPackage(ctx, globalObject, p, null);
  -            ctx.setWrapHandler(wrapHandler);
  +            wWrapper.importPackage(ctx, globalObject, p, null);
           } finally {
               Context.exit();
           }
  @@ -173,10 +171,11 @@
       public Context enterContext(){
           Context ctx = Context.enter();
           if (ctx != defaultContext){
  -            // Set the SecuritySupport the Context should
  +            // Set the SecurityController the Context should
               // use.
               if (!contexts.contains(ctx)) {
  -                ctx.setSecuritySupport(securitySupport);
  +                ctx.setWrapFactory(wrapFactory);
  +                ctx.setSecurityController(securityController);
                   contexts.add(ctx);
   
                   // Hopefully, we are not switching threads too
  @@ -210,7 +209,7 @@
       /**
        * This method evaluates a piece of ECMAScript.
        * @param scriptreader a <code>java.io.Reader</code> on the piece of script
  -     * @param description description which can be later used (e.g., for error 
  +     * @param description description which can be later used (e.g., for error
        *        messages).
        * @return if no exception is thrown during the call, should return the
        * value of the last expression evaluated in the script.
  @@ -219,14 +218,13 @@
           throws InterpreterException, IOException {
   
           Object rv = null;
  -        Context ctx = enterContext();
  +        final Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
  -            rv = ctx.evaluateReader(globalObject,
  -                                    scriptreader,
  -                                    description,
  -                                    1, rhinoClassLoader);
  +          rv = ctx.evaluateReader(globalObject,
  +                                  scriptreader,
  +                                  description,
  +                                  1, rhinoClassLoader);
           } catch (JavaScriptException e) {
               // exception from JavaScript (possibly wrapping a Java Ex)
               if (e.getValue() instanceof Exception) {
  @@ -236,10 +234,14 @@
                   throw new InterpreterException(e, e.getMessage(), -1, -1);
           } catch (WrappedException we) {
               // main Rhino RuntimeException
  -            throw
  -                new InterpreterException((Exception)we.getWrappedException(),
  -                                         we.getWrappedException().getMessage(),
  -                                         -1, -1);
  +            Throwable w = we.getWrappedException();
  +            if (w instanceof Exception)
  +                throw
  +                    new InterpreterException((Exception)we.getWrappedException(),
  +                                             we.getWrappedException().getMessage(),
  +                                             -1, -1);
  +            else
  +                throw new 
InterpreterException(we.getWrappedException().getMessage(), -1, -1);
           } catch (RuntimeException re) {
               // other RuntimeExceptions
               throw new InterpreterException(re, re.getMessage(), -1, -1);
  @@ -263,7 +265,6 @@
   
           final Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           Script script = null;
           Entry et = null;
           Iterator it = compiledScripts.iterator();
  @@ -281,7 +282,7 @@
           }
   
           if (script == null) {
  -            // this script has not been compiled yet or has been fogotten
  +            // this script has not been compiled yet or has been forgotten
               // since the compilation:
               // compile it and store it for future use.
   
  @@ -292,7 +293,7 @@
                                                        new StringReader(scriptstr),
                                                        SOURCE_NAME_SVG,
                                                        1, rhinoClassLoader);
  -                        } catch(IOException io) {
  +                        } catch (IOException io) {
                               // Should never happen: we are using a string
                               throw new Error();
                           }
  @@ -351,38 +352,17 @@
       public void bindObject(String name, Object object) {
           Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
  -            Scriptable jsObject =  Context.toObject(object, globalObject);
  -            globalObject.put(name, globalObject, jsObject);
  +            if (name.equals(BIND_NAME_WINDOW) && object instanceof Window) {
  +                ((WindowWrapper)globalObject).window = (Window)object;
  +            } else {
  +                Scriptable jsObject =  Context.toObject(object, globalObject);
  +                globalObject.put(name, globalObject, jsObject);
  +            }
  +
           } finally {
               Context.exit();
           }
  -
  -        if (name.equals(BIND_NAME_WINDOW) && object instanceof Window) {
  -            try {
  -                // Defines the 'Window' class.
  -                ScriptableObject.defineClass(globalObject,
  -                                             WindowWrapper.class);
  -
  -                // Wrap the given window object.
  -                evaluate(new StringReader(ECMA_WINDOW_INSTANTIATION));
  -
  -                // The window becomes the global object.
  -                globalObject =
  -                    (ScriptableObject)globalObject.get(BIND_NAME_WINDOW, 
globalObject);
  -
  -                // we need to init it as a global object...
  -                ctx = enterContext();
  -                try {
  -                    ctx.initStandardObjects(globalObject);
  -                } finally {
  -                    Context.exit();
  -                }
  -            } catch (Exception e) {
  -                // Cannot happen.
  -            }
  -        }
       }
   
       /**
  @@ -393,7 +373,6 @@
           throws JavaScriptException {
           Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
               arg = Context.toObject(arg, globalObject);
               Object[] args = {arg};
  @@ -412,7 +391,6 @@
           throws JavaScriptException {
           Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
               ScriptableObject.callMethod(obj, methodName, ab.buildArguments());
           } finally {
  @@ -428,7 +406,6 @@
           throws JavaScriptException {
           Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
               handler.call(ctx, globalObject, globalObject, args);
           } finally {
  @@ -444,7 +421,6 @@
           throws JavaScriptException {
           Context ctx = enterContext();
   
  -        ctx.setWrapHandler(wrapHandler);
           try {
               handler.call(ctx, globalObject, globalObject, ab.buildArguments());
           } finally {
  
  
  
  1.12      +52 -73    
xml-batik/sources/org/apache/batik/script/rhino/WindowWrapper.java
  
  Index: WindowWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/WindowWrapper.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- WindowWrapper.java        6 Nov 2002 14:43:27 -0000       1.11
  +++ WindowWrapper.java        14 Mar 2003 11:02:02 -0000      1.12
  @@ -19,8 +19,10 @@
   import org.mozilla.javascript.Function;
   import org.mozilla.javascript.FunctionObject;
   import org.mozilla.javascript.JavaScriptException;
  +import org.mozilla.javascript.ImporterTopLevel;
   import org.mozilla.javascript.NativeJavaObject;
   import org.mozilla.javascript.NativeObject;
  +import org.mozilla.javascript.PropertyException;
   import org.mozilla.javascript.Scriptable;
   import org.mozilla.javascript.ScriptableObject;
   import org.mozilla.javascript.Undefined;
  @@ -35,11 +37,13 @@
   
   /**
    * This class wraps a Window object to expose it to the interpreter.
  + * This will be the Global Object of our interpreter.
    *
  + * @author <a href="mailto:[EMAIL PROTECTED]">Christophe Jolif</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
    * @version $Id$
    */
  -public class WindowWrapper extends ScriptableObject {
  +public class WindowWrapper extends ImporterTopLevel {
   
       private final static Object[] EMPTY_ARGUMENTS = new Object[0];
   
  @@ -56,17 +60,16 @@
       /**
        * Creates a new WindowWrapper.
        */
  -    public WindowWrapper() {
  -    }
  -
  -    /**
  -     * The ecmascript constructor for the Window class.
  -     */
  -    public static Object jsConstructor(Context cx, Object[] args,
  -                                       Function ctorObj, boolean inNewExpr) {
  -        WindowWrapper result = new WindowWrapper();
  -        result.window = (Window)((Wrapper)args[0]).unwrap();
  -        return result;
  +    public WindowWrapper(Context context) {
  +        super(context);
  +        String[] names = { "setInterval", "setTimeout", "clearInterval", 
"clearTimeout",
  +                           "parseXML", "getURL", "alert", "confirm", "prompt" };
  +        try {
  +            this.defineFunctionProperties(names, WindowWrapper.class,
  +                                          ScriptableObject.DONTENUM);
  +        } catch (PropertyException e) {
  +            throw new Error();  // should never happen
  +        }
       }
   
       public String getClassName() {
  @@ -78,36 +81,12 @@
       }
   
       /**
  -     * Wraps the 'eval' methods of the Window interface.
  -     */
  -    public static void jsFunction_eval(Context cx,
  -                                       Scriptable thisObj,
  -                                       Object[] args,
  -                                       Function funObj)
  -        throws JavaScriptException {
  -        int len = args.length;
  -        WindowWrapper ww = (WindowWrapper)thisObj;
  -        Window window = ww.window;
  -        if (len >= 1) {
  -            String code =
  -                (String)NativeJavaObject.coerceType(String.class, args[0]);
  -            RhinoInterpreter interp =
  -                (RhinoInterpreter)window.getInterpreter();
  -            try {
  -                interp.evaluate(code);
  -            } catch (InterpreterException e) {
  -                throw new JavaScriptException(e);
  -            }
  -        }
  -    }
  -
  -    /**
        * Wraps the 'setInterval' methods of the Window interface.
        */
  -    public static Object jsFunction_setInterval(Context cx,
  -                                                Scriptable thisObj,
  -                                                Object[] args,
  -                                                Function funObj)
  +    public static Object setInterval(Context cx,
  +                                     Scriptable thisObj,
  +                                     Object[] args,
  +                                     Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -133,10 +112,10 @@
       /**
        * Wraps the 'setTimeout' methods of the Window interface.
        */
  -    public static Object jsFunction_setTimeout(Context cx,
  -                                               Scriptable thisObj,
  -                                               Object[] args,
  -                                               Function funObj)
  +    public static Object setTimeout(Context cx,
  +                                    Scriptable thisObj,
  +                                    Object[] args,
  +                                    Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -162,10 +141,10 @@
       /**
        * Wraps the 'clearInterval' method of the Window interface.
        */
  -    public static void jsFunction_clearInterval(Context cx,
  -                                                Scriptable thisObj,
  -                                                Object[] args,
  -                                                Function funObj)
  +    public static void clearInterval(Context cx,
  +                                     Scriptable thisObj,
  +                                     Object[] args,
  +                                     Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -179,10 +158,10 @@
       /**
        * Wraps the 'clearTimeout' method of the Window interface.
        */
  -    public static void jsFunction_clearTimeout(Context cx,
  -                                               Scriptable thisObj,
  -                                               Object[] args,
  -                                               Function funObj)
  +    public static void clearTimeout(Context cx,
  +                                    Scriptable thisObj,
  +                                    Object[] args,
  +                                    Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -196,10 +175,10 @@
       /**
        * Wraps the 'parseXML' method of the Window interface.
        */
  -    public static Object jsFunction_parseXML(Context cx,
  -                                             Scriptable thisObj,
  -                                             final Object[] args,
  -                                             Function funObj)
  +    public static Object parseXML(Context cx,
  +                                  Scriptable thisObj,
  +                                  final Object[] args,
  +                                  Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -223,10 +202,10 @@
       /**
        * Wraps the 'getURL' method of the Window interface.
        */
  -    public static void jsFunction_getURL(Context cx,
  -                                         Scriptable thisObj,
  -                                         final Object[] args,
  -                                         Function funObj)
  +    public static void getURL(Context cx,
  +                              Scriptable thisObj,
  +                              final Object[] args,
  +                              Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -270,10 +249,10 @@
       /**
        * Wraps the 'alert' method of the Window interface.
        */
  -    public static void jsFunction_alert(Context cx,
  -                                        Scriptable thisObj,
  -                                        Object[] args,
  -                                        Function funObj)
  +    public static void alert(Context cx,
  +                             Scriptable thisObj,
  +                             Object[] args,
  +                             Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -288,10 +267,10 @@
       /**
        * Wraps the 'confirm' method of the Window interface.
        */
  -    public static boolean jsFunction_confirm(Context cx,
  -                                             Scriptable thisObj,
  -                                             Object[] args,
  -                                             Function funObj)
  +    public static boolean confirm(Context cx,
  +                                  Scriptable thisObj,
  +                                  Object[] args,
  +                                  Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  @@ -307,10 +286,10 @@
       /**
        * Wraps the 'prompt' method of the Window interface.
        */
  -    public static String jsFunction_prompt(Context cx,
  -                                           Scriptable thisObj,
  -                                           Object[] args,
  -                                           Function funObj)
  +    public static String prompt(Context cx,
  +                                Scriptable thisObj,
  +                                Object[] args,
  +                                Function funObj)
           throws JavaScriptException {
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
  
  
  
  1.39      +2 -2      xml-batik/xdocs/whoAreWe.xml
  
  Index: whoAreWe.xml
  ===================================================================
  RCS file: /home/cvs/xml-batik/xdocs/whoAreWe.xml,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- whoAreWe.xml      17 Jun 2002 16:28:01 -0000      1.38
  +++ whoAreWe.xml      14 Mar 2003 11:02:02 -0000      1.39
  @@ -79,7 +79,7 @@
             <dt>Christophe Jolif (CJ)</dt><dd><br />
             <link href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</link><br />
             <em>
  -          Christophe Jolif is a Staff Software Engineer mainly working on the ILOG
  +          Christophe Jolif is a Software Architect mainly working on the ILOG
             JViews Component Suite, the ILOG Java visualization framework. He has
             been working for ILOG since 1997, and is representing the company at the
             Scalable Vector Graphics (SVG) W3C Working Group since 1999. He has 
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to