Well, the problem seems to be that rhino is treating 'org' as a Java
package object rather than a JavaScript object.

One solution would be to create an "org" variable within a function,
then make sure all the code runs inside it.  That is, instead of
running the cometd code in the global scope, shoehorn it into a
function something like this:

(function(){
    var org = {};
    // all cometd code here
})();

I'm not sure if that will work, or how hard it would be.

Another option would be to pick a different top level variable for
cometd, something that's not reserved by rhino as being a package
variable.  This would require either hacking the cometd code, or
grabbing the file contents and performing a string replacement prior
to eval().

Another option would be to simply override the "org" package level
variable with "var org = {};" directly in the global scope.  If you
did that, you'd have to remember to do Packages.org.whatever later
when you meant to reach out into the Java space though.

Just some ideas.  Good luck!

-- Jim R. Wilson (jimbojw)

On Fri, May 14, 2010 at 4:02 PM, Balko, Soeren <soeren.ba...@sap.com> wrote:
> Hi,
>
> I've been playing around with Rhino to build some JUnitEE tests for some 
> JavaScript code that uses jQuery and CometD. I could successfully load the 
> env-js library (which emulates the browser stuff), jQuery and some other JS 
> libraries we require. In particular the Cometd functionality requires two 
> more JS files to be included:
> -       cometd.js which defines a (constructor) function "org.cometd.Cometd" 
> and
> -       jquery.cometd.js (the jQuery binding for cometd) which (among other 
> things) invokes that constructor: "new org.cometd.Cometd()".
>
> My JUnitEE test resides in a web application that also has the Java pieces of 
> Cometd in its class path. That includes a number of classes and interfaces 
> that belong to a package "org.cometd" (and child packages thereof).
>
> Here is what happens when I successively load the JS libraries (using the 
> Context.evaluateReader(...)): env-js, cometd.js and jQuery load fine. 
> However, when I try to load "jquery.cometd.js", Rhino complains about a name 
> clash (?), reporting:
>
> org.mozilla.javascript.EcmaError: TypeError: [JavaPackage org.cometd.Cometd] 
> is not a function, it is object. (jquery/jquery.cometd.js#26)
>        at 
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3654)
>        at 
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3632)
>        at 
> org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3660)
>        at 
> org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3679)
>        at 
> org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3734)
>        at 
> org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3722)
>        at 
> org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3363)
>        at script(jquery/jquery.cometd.js:26)
>        at script(jquery/jquery.cometd.js:4)
>        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2487)
>        at 
> org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
>        at 
> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
>        at 
> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
>        at 
> org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
>        at org.mozilla.javascript.Context.evaluateReader(Context.java:1135)
>        at com.sap.gravity.test.client.model.ModelTest.setUp(ModelTest.java:75)
>        ...
>
> Seemingly, Rhino is confused about "org.cometd" both being a Java package and 
> a JavaScript object. Here is what I tried to get rid of this issue:
>
> (1)     Setting a ClassShutter that rejects all classes belonging to 
> "org.cometd" and sub packages --> no change.
> (2)     Defining my custom ClassLoader and setting it as the application 
> class loader for the Rhino context. That custom ClassLoader (a) throws a 
> ClassNotFoundException in findClass(...) for all classes belonging to 
> org.cometd and sub packages, (b) does not report "org.cometd" (or any sub 
> package thereof) in "getPackages" and (c) returns null for getPackage(...) 
> when called with "org.cometd" (or any sub package thereof) --> no change 
> either.
>
> I am starting to run out of ideas apart from removing the org.cometd stuff 
> from my class path (which would be somewhat unfortunate as I would also like 
> to include some test cases for the Java part). However, I was wondering if 
> there are any other chances to work around this issue... Any hint would be 
> greatly appreciated.
>
> Thanks and cheers,
> Soeren
>
> Mandatory Disclosure Statements: 
> http://www.sap.com/company/legal/impressum.epx
>
> Please be considerate of the environment before printing this email. Thank 
> you!
>
>
>
>
>
> _______________________________________________
> dev-tech-js-engine-rhino mailing list
> dev-tech-js-engine-rhino@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
>
_______________________________________________
dev-tech-js-engine-rhino mailing list
dev-tech-js-engine-rhino@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to