You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-dev@jakarta.apache.org by Sonny To <so...@gmail.com> on 2006/10/16 10:05:46 UTC

Fwd: BSFManager.setClassLoader doesn't seem to work

I asked this question on the bsf-user mailing list but it doesnt seem
to be very active. The bsf-user archieve at
http://mail-archives.eu.apache.org/mod_mbox/jakarta-bsf-usr/ is also
not available. I'm working on an opensource project
http://code.google.com/p/wulumuqi/ that integrates BSF. Any help
getting BSF to use URLClassLoaders will be much appreciated.

thanks,
Sonny

---------- Forwarded message ----------
From: Sonny To <so...@gmail.com>
Date: Oct 16, 2006 3:47 PM
Subject: BSFManager.setClassLoader doesn't seem to work
To: bsf-user@jakarta.apache.org


Hi,

I am trying to use URLClassLoader with BSFManager so that it loads
classes from my webserver.  The following code does not seem to work:

package net.omnivention.bsf;

import java.net.URL;
import java.net.URLClassLoader;

import org.apache.bsf.BSFManager;
import org.python.core.PySystemState;

public class WuluClassLoaderTest {

        public static void main(String[] args) throws Exception {
                BSFManager.registerScriptingEngine("ruby",
"org.jruby.javasupport.bsf.JRubyEngine", new String[] { "rb" });
                BSFManager manager = new BSFManager();
                manager.loadScriptingEngine("ruby");
                manager.loadScriptingEngine("jython");
                URLClassLoader clzLoader = new URLClassLoader(new URL[]{ new
URL("http://linuxjedi.omnivention.net/~son/") });

                Class<?> clz =
clzLoader.loadClass("cn.com.blackdragonmedia.Address");
                System.out.println("clz="+clz);
                manager.setClassLoader(clzLoader);
                System.out.println(manager.getClassLoader());

                PySystemState.add_package("cn.com.blackdragonmedia");
                manager.exec("jython", "abc", 0, 0, "from
cn.com.blackdragonmedia
import Address");
                manager.exec("ruby", "abc", 0, 0, "include_class
cn.com.blackdragonmedia.Address'");
        }
}
URLClassLoader is loading the class when using
clzLoader.loadClass("cn.com.blackdragonmedia.Address")
but not when I run the python script and ruby script to import the class

for python, I get
Exception in thread "main" org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<string>", line 1, in ?
ImportError: cannot import name Address

        at org.apache.bsf.engines.jython.JythonEngine.exec(JythonEngine.java:146)
        at org.apache.bsf.BSFManager$6.run(BSFManager.java:493)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.exec(BSFManager.java:491)
        at net.omnivention.bsf.WuluClassLoaderTest.main(WuluClassLoaderTest.java:24)

for ruby, I get

Exception in thread "main" org.apache.bsf.BSFException: Exception
        at org.jruby.javasupport.bsf.JRubyEngine.exec(JRubyEngine.java:110)
        at org.apache.bsf.BSFManager$6.run(BSFManager.java:493)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.exec(BSFManager.java:491)
        at net.omnivention.bsf.WuluClassLoaderTest.main(WuluClassLoaderTest.java:25)

Any idea how to BSF to use URLClassLoader?

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Sonny,
> it works as I expected.
Fine.
> thanks for your help!
You are welcome.

Regards,

---rony


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by Sonny To <so...@gmail.com>.
Hi Ronny,
it works as I expected.

thanks for your help!

regards,
Sonny

On 10/30/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
> Hi Sonny,
>
> in the meantime I have incorporated your patch and created a temporary
> build at <http://wi.wu-wien.ac.at/rgf/tmp/bsf.tmp/>. Please check it
> out, if it works as you expect it, I would check that version of
> "JythonEngine.java".
>
> Regards
>
> ---rony
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Sonny,

in the meantime I have incorporated your patch and created a temporary
build at <http://wi.wu-wien.ac.at/rgf/tmp/bsf.tmp/>. Please check it
out, if it works as you expect it, I would check that version of
"JythonEngine.java".

Regards

---rony




---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by Sonny To <so...@gmail.com>.
On 10/28/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
<http://wi.wu-wien.ac.at/rgf/rexx/bsf4rexx/docs/docs.apache.bsf/org/apache/bsf/BSFEngine.html>)
>       and what they are meant for:
>          1. "exec(...)": Java coder needs to execute a script, there are
>             no arguments to be passed to the script nor is there any
>             return value expected,
>          2. "eval(...)": Java coder wants to evaluate a script, does not
>             need to supply any arguments, but expects a return value,
>          3. "apply(...)": Java coder needs to execute a script, needs to
>             supply arguments to the script and *is* expecting a return
>             value,
>          4. "call(...)": Java coder wants to invoke a method in a
>             script, supplying arguments and expecting a return value.
>
> So you see, your patch would only alleviate the class reference issue in
> the Jython engine for case # 1, where it probably could also help in
> cases #2 and #3. (Maybe also for a  "compileScript(...)" method if that
> makes sense in the context of Jython.)

case #4 doesn't seem relevant for my patch... here's a patch for case
#1 to #3 to import packages

Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Sonny,
> I've never used the other methods and I'm not sure how they are used.
Hmm, maybe two little remarks:

    * if a shortcoming in the Jython engine has been uncovered and
      should be corrected, it would be desirable to correct the
      shortcoming wherever it may appear in the Jython engine elsewhere,
          o reasoning: you never know how Java programmers will use the
            Jython engine, so code that works with "exec(...)" may not
            work with "apply(...)" which would baffle BSF users,
    * here are the different kinds of script engine invocations in BSF
      (cf. e.g.
      <http://wi.wu-wien.ac.at/rgf/rexx/bsf4rexx/docs/docs.apache.bsf/org/apache/bsf/BSFEngine.html>)
      and what they are meant for:
         1. "exec(...)": Java coder needs to execute a script, there are
            no arguments to be passed to the script nor is there any
            return value expected,
         2. "eval(...)": Java coder wants to evaluate a script, does not
            need to supply any arguments, but expects a return value,
         3. "apply(...)": Java coder needs to execute a script, needs to
            supply arguments to the script and *is* expecting a return
            value,
         4. "call(...)": Java coder wants to invoke a method in a
            script, supplying arguments and expecting a return value.

So you see, your patch would only alleviate the class reference issue in
the Jython engine for case # 1, where it probably could also help in
cases #2 and #3. (Maybe also for a  "compileScript(...)" method if that
makes sense in the context of Jython.)

What do you think?

Regards,

---rony

P.S.: There comes a default implementation for the BSFEngine interface,
which easies the creation of specific BSF engines considerably, cf.
<http://wi.wu-wien.ac.at/rgf/rexx/bsf4rexx/docs/docs.apache.bsf/org/apache/bsf/util/BSFEngineImpl.html>.



Re: A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by Sonny To <so...@gmail.com>.
On 10/28/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
> Hi Sonny,
>
> just looked at your changes for patching the Jython engine. Before doing
> that a question for you: is it intentional that your changes only apply
> to the "exec(...)" method and not the others (e.g. "apply(...)")? If so,
> what would be the reasoning (unless, of course, I oversee the obvious)?

Hi Rony,
I've never used the other methods and I'm not sure how they are used.
The purpose of my patch is to allow code like the following to work:

from com.foo import Bar
b = Bar()

without calling PySystemState.add_package("com.foo")
the above code would fail with something like package not found. Its
ugly to call  PySystemState.add_package(aPackage) manually to import a
package into jython.

Here is another patch that does the same thing but extract the
functionality in a method to make it clearer what the intent of the
code is.

> [Please realize that I have no working knowledge about Jython's inner
> workings, so maybe you can shed some light to this?]

I'm not an expert in the inner workings of the jython engine either so
if you think importPackage method belongs in the other methods too, go
ahead and add it. It can't hurt.

>
> Regards,
>
> ---rony
>
> ------------------- here's the present unified diff of your patch in my
> local copy ------------------
> --- JythonEngine.java.orig    2006-08-14 14:48:02.547874000 +0200
> +++ JythonEngine.java    2006-10-28 13:32:22.128012800 +0200
> @@ -16,8 +16,11 @@
>
>  package org.apache.bsf.engines.jython;
>
> +import java.beans.PropertyChangeEvent;
>  import java.io.ByteArrayInputStream;
>  import java.util.Vector;
> +import java.util.regex.Matcher;
> +import java.util.regex.Pattern;
>
>  import org.apache.bsf.BSFDeclaredBean;
>  import org.apache.bsf.BSFException;
> @@ -28,6 +31,7 @@
>  import org.python.core.PyException;
>  import org.python.core.PyJavaInstance;
>  import org.python.core.PyObject;
> +import org.python.core.PySystemState;
>  import org.python.util.InteractiveInterpreter;
>
>  /**
> @@ -41,7 +45,8 @@
>
>  public class JythonEngine extends BSFEngineImpl {
>    BSFPythonInterpreter interp;
> -
> +  private final static Pattern fromRegExp = Pattern.compile("from
> ([.^\\S]*)");
> +
>    /**
>     * call the named method of the given object.
>     */
> @@ -141,7 +146,13 @@
>    public void exec (String source, int lineNo, int columnNo,
>              Object script) throws BSFException {
>      try {
> -      interp.exec (byteify(script.toString ()));
> +      String scriptString = script.toString();
> +      Matcher matcher = fromRegExp.matcher(scriptString);
> +      while (matcher.find()) {
> +          String packageName = matcher.group(1);
> +          PySystemState.add_package(packageName);
> +      }
> +      interp.exec (byteify(scriptString));
>      } catch (PyException e) {
>        throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
>                    "exception from Jython:\n" + e, e);
> @@ -241,4 +252,15 @@
>            }
>        }
>    }
> +
> +
> +  public void propertyChange(PropertyChangeEvent e) {
> +      super.propertyChange(e);
> +      String name = e.getPropertyName();
> +      Object value = e.getNewValue();
> +      if (name.equals("classLoader")) {
> +        Py.getSystemState().setClassLoader((ClassLoader) value);
> +      }
> +
> +  }
>  }
> ------------------- here's the present unified diff of your patch in my
> local copy (The end.) ------------------
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-dev-help@jakarta.apache.org
>
>

A question ad your suggested Jython-engine patch (Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Sonny,

just looked at your changes for patching the Jython engine. Before doing
that a question for you: is it intentional that your changes only apply
to the "exec(...)" method and not the others (e.g. "apply(...)")? If so,
what would be the reasoning (unless, of course, I oversee the obvious)?

[Please realize that I have no working knowledge about Jython's inner
workings, so maybe you can shed some light to this?]

Regards,

---rony

------------------- here's the present unified diff of your patch in my
local copy ------------------
--- JythonEngine.java.orig    2006-08-14 14:48:02.547874000 +0200
+++ JythonEngine.java    2006-10-28 13:32:22.128012800 +0200
@@ -16,8 +16,11 @@
 
 package org.apache.bsf.engines.jython;
 
+import java.beans.PropertyChangeEvent;
 import java.io.ByteArrayInputStream;
 import java.util.Vector;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.bsf.BSFDeclaredBean;
 import org.apache.bsf.BSFException;
@@ -28,6 +31,7 @@
 import org.python.core.PyException;
 import org.python.core.PyJavaInstance;
 import org.python.core.PyObject;
+import org.python.core.PySystemState;
 import org.python.util.InteractiveInterpreter;
 
 /**
@@ -41,7 +45,8 @@
 
 public class JythonEngine extends BSFEngineImpl {
   BSFPythonInterpreter interp;
- 
+  private final static Pattern fromRegExp = Pattern.compile("from
([.^\\S]*)");
+
   /**
    * call the named method of the given object.
    */
@@ -141,7 +146,13 @@
   public void exec (String source, int lineNo, int columnNo,
             Object script) throws BSFException {
     try {
-      interp.exec (byteify(script.toString ()));
+      String scriptString = script.toString();
+      Matcher matcher = fromRegExp.matcher(scriptString);
+      while (matcher.find()) {
+          String packageName = matcher.group(1);
+          PySystemState.add_package(packageName);
+      }
+      interp.exec (byteify(scriptString));
     } catch (PyException e) {
       throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
                   "exception from Jython:\n" + e, e);
@@ -241,4 +252,15 @@
           }
       }
   }
+
+
+  public void propertyChange(PropertyChangeEvent e) {
+      super.propertyChange(e);
+      String name = e.getPropertyName();
+      Object value = e.getNewValue();
+      if (name.equals("classLoader")) {
+        Py.getSystemState().setClassLoader((ClassLoader) value);
+      }
+
+  }
 }
------------------- here's the present unified diff of your patch in my
local copy (The end.) ------------------







---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by Sonny To <so...@gmail.com>.
thanks for quick response. here is the new diff without my own
formatting. I use eclipse and have it auto format to my own style.


On 10/16/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
> Quoting Sonny To <so...@gmail.com>:
>
> > Yes I did track the problem. Attached is  the diff for JythonEngine.java
> > summar of change:
> >
> > 1) I've added propertyChange method to set the ClassLoader for jython
> > 2) modify execute method to call
> > PySystemState.add_package(packageName) when there's a from packageName
> > import className in python script
> >
> > When can this get incoporated? I dont want to maintain my own private
> > subversion repository to keep track of the these minor bug fixes which
> > is what im currently doing.
>
> Great!
>
> Please supply a unified diff which ignores the change in whitespaces (you seem to have reformatted
> using spaces) such that the "true" (only those lines that got changed/added/deleted ignoring white
> space formatting) diff will be generated, something like:
>
>     diff -wu newFile originalFile
>
> Having your unified diff hanging out here a few days will allow interested parties to see the
> intended changes and comment, if they see a need to do so.
>
> Regards,
>
> ---rony
>
> P.S.: Personally, I will be back home by Friday (but would be interested to see the diffs).
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-dev-help@jakarta.apache.org
>
>

Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Quoting Sonny To <so...@gmail.com>:

> Yes I did track the problem. Attached is  the diff for JythonEngine.java
> summar of change:
> 
> 1) I've added propertyChange method to set the ClassLoader for jython
> 2) modify execute method to call
> PySystemState.add_package(packageName) when there's a from packageName
> import className in python script
> 
> When can this get incoporated? I dont want to maintain my own private
> subversion repository to keep track of the these minor bug fixes which
> is what im currently doing.

Great!

Please supply a unified diff which ignores the change in whitespaces (you seem to have reformatted
using spaces) such that the "true" (only those lines that got changed/added/deleted ignoring white
space formatting) diff will be generated, something like: 

    diff -wu newFile originalFile

Having your unified diff hanging out here a few days will allow interested parties to see the
intended changes and comment, if they see a need to do so. 

Regards,

---rony

P.S.: Personally, I will be back home by Friday (but would be interested to see the diffs).


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by Sonny To <so...@gmail.com>.
Yes I did track the problem. Attached is  the diff for JythonEngine.java
summar of change:

1) I've added propertyChange method to set the ClassLoader for jython
2) modify execute method to call
PySystemState.add_package(packageName) when there's a from packageName
import className in python script

When can this get incoporated? I dont want to maintain my own private
subversion repository to keep track of the these minor bug fixes which
is what im currently doing.

On 10/16/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
> Hi Sonny,
>
> the developers are dispersed at the moment (I think no one is at home, but travelling from/to
> conferences).
>
> Quoting Sonny To <so...@gmail.com>:
>
> > I asked this question on the bsf-user mailing list but it doesnt seem
> > to be very active. The bsf-user archieve at
> > http://mail-archives.eu.apache.org/mod_mbox/jakarta-bsf-usr/ is also
> > not available. I'm working on an opensource project
> > http://code.google.com/p/wulumuqi/ that integrates BSF. Any help
> > getting BSF to use URLClassLoaders will be much appreciated.
>
> It seems from the stack traces that the problem is in the BSF engines for the respective languages.
> So you would be best served by tracking down the engine-creators and inform them about the problem.
> Alternatively, if you can trace it down and fix it (if it is a true error, nothing that is linked
> to security issues, or not being able to retrieve the class from the net, and the like), it would
> be great, if you could make a unified diff available (which we could apply at least to the Jython
> engine, as its source-code is in the BSF distro itself).
>
> Good luck!
>
> ---rony
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-dev-help@jakarta.apache.org
>
>

Re: Fwd: BSFManager.setClassLoader doesn't seem to work

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Sonny,

the developers are dispersed at the moment (I think no one is at home, but travelling from/to
conferences).

Quoting Sonny To <so...@gmail.com>:

> I asked this question on the bsf-user mailing list but it doesnt seem
> to be very active. The bsf-user archieve at
> http://mail-archives.eu.apache.org/mod_mbox/jakarta-bsf-usr/ is also
> not available. I'm working on an opensource project
> http://code.google.com/p/wulumuqi/ that integrates BSF. Any help
> getting BSF to use URLClassLoaders will be much appreciated.

It seems from the stack traces that the problem is in the BSF engines for the respective languages.
So you would be best served by tracking down the engine-creators and inform them about the problem.
Alternatively, if you can trace it down and fix it (if it is a true error, nothing that is linked
to security issues, or not being able to retrieve the class from the net, and the like), it would
be great, if you could make a unified diff available (which we could apply at least to the Jython
engine, as its source-code is in the BSF distro itself).

Good luck!

---rony


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org