You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by David Legg <da...@searchevent.co.uk> on 2005/10/19 04:01:06 UTC

Flowscript run-time error- passing parameters from script to Java

I'm trying to do something in flowscript that must be pretty common.  I 
simply want to pass a HashMap of arguments from JavaScript to a Java method 
which will then process the Map to perform some useful business function. 
Can anyone spot what I'm doing wrong?  I get a Javascript exception (listed 
at the end of this email).

I suspect I'm not casting the HashMap properly but my JavaScript is weak in 
this area.  I think I'm doing something similar to the 'Calling Java' 
documentation (http://cocoon.apache.org/2.1/userdocs/flow/java.html) but 
maybe I missed something.

++++++++
My Flowscript looks something like this: -

  importClass(Packages.com.MySingleton);

  function main() {
    var theParams = new java.util.HashMap();
    theParams.put("param1", "value1");
    var usefulObject = MySingleton.doSomethingUseful(theParams);   <== 
Exception occurs here.
    cocoon.sendPage("apage.jx", {"useful" : usefulObject} );
  }

++++++++
My Java method is defined as follows: -

  public synchronized UsefulObject doSomethingUseful(Map params) {
    String[] vals = (String[])params.get("param1");
    if (vals == null || vals.length < 1)
      ...

++++++++
When I access the page I get the following error: -

java.lang.String
org.apache.avalon.framework.CascadingRuntimeException: 
"file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught JavaScript 
exception: at main (file:/D:/projects/.../flow/myflowscript.js, Line 6): 
java.lang.ClassCastException: java.lang.String

cause: java.lang.ClassCastException: java.lang.String

full exception chain stacktrace[hide]

org.apache.avalon.framework.CascadingRuntimeException: 
"file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught JavaScript 
exception:
at main (file:/D:/projects/.../flow/myflowscript.js, Line 6):
java.lang.ClassCastException: java.lang.String
	at 
org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter.callFunction(FOM_JavaScriptInterpreter.java:764)
	at 
org.apache.cocoon.components.treeprocessor.sitemap.CallFunctionNode.invoke(CallFunctionNode.java:135)
	at 
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invokeNodes(AbstractParentProcessingNode.java:46)
	at 
org.apache.cocoon.components.treeprocessor.sitemap.PreparableMatchNode.invoke(PreparableMatchNode.java:130)
	at 
org.apache.cocoon.components.treeprocessor.AbstractParentProcessingNode.invokeNodes(AbstractParentProcessingNode.java:68)
	at 
org.apache.cocoon.components.treeprocessor.sitemap.PipelineNode.invoke(PipelineNode.java:138)etc...
++++++++
Best Regards,
David Legg



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Flowscript run-time error- passing parameters from script to Java - SOLVED

Posted by David Legg <da...@searchevent.co.uk>.
Jason Johnston wrote:
> So here the value for the key param1 is the String "value1"...
> ...and here you're expecting the value for the key param1 to be an *array* 
> of Strings.  That explains the ClassCastException.
>
> So either you must expect a single String as the value, or set it as a 
> String array in the flowscript.  Creating Java arrays in JS is a bit odd, 
> you have to use reflection: http://www.mozilla.org/rhino/faq.html

Excellent response!  Thanks a lot Jason.

It turned out I really did need a HashMap of string arrays (the same key can 
have several values in my app) so that reference to the mozilla site you 
gave was spot on.

Thanks again.

Regards,
David Legg



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Flowscript run-time error- passing parameters from script to Java

Posted by Jason Johnston <co...@lojjic.net>.
David Legg wrote:
> I'm trying to do something in flowscript that must be pretty common.  I 
> simply want to pass a HashMap of arguments from JavaScript to a Java 
> method which will then process the Map to perform some useful business 
> function. Can anyone spot what I'm doing wrong?  I get a Javascript 
> exception (listed at the end of this email).
> 
> I suspect I'm not casting the HashMap properly but my JavaScript is weak 
> in this area.  I think I'm doing something similar to the 'Calling Java' 
> documentation (http://cocoon.apache.org/2.1/userdocs/flow/java.html) but 
> maybe I missed something.
> 
> ++++++++
> My Flowscript looks something like this: -
> 
>  importClass(Packages.com.MySingleton);
> 
>  function main() {
>    var theParams = new java.util.HashMap();
>    theParams.put("param1", "value1");

So here the value for the key param1 is the String "value1"...

>    var usefulObject = MySingleton.doSomethingUseful(theParams);   <== 
> Exception occurs here.
>    cocoon.sendPage("apage.jx", {"useful" : usefulObject} );
>  }
> 
> ++++++++
> My Java method is defined as follows: -
> 
>  public synchronized UsefulObject doSomethingUseful(Map params) {
>    String[] vals = (String[])params.get("param1");

...and here you're expecting the value for the key param1 to be an 
*array* of Strings.  That explains the ClassCastException.

So either you must expect a single String as the value, or set it as a 
String array in the flowscript.  Creating Java arrays in JS is a bit 
odd, you have to use reflection: http://www.mozilla.org/rhino/faq.html

Hope that helps.

>    if (vals == null || vals.length < 1)
>      ...
> 
> ++++++++
> When I access the page I get the following error: -
> 
> java.lang.String
> org.apache.avalon.framework.CascadingRuntimeException: 
> "file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught 
> JavaScript exception: at main 
> (file:/D:/projects/.../flow/myflowscript.js, Line 6): 
> java.lang.ClassCastException: java.lang.String
> 
> cause: java.lang.ClassCastException: java.lang.String
> 
> full exception chain stacktrace[hide]
> 
> org.apache.avalon.framework.CascadingRuntimeException: 
> "file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught 
> JavaScript exception:
> at main (file:/D:/projects/.../flow/myflowscript.js, Line 6):
> java.lang.ClassCastException: java.lang.String
>     at 
> org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter.callFunction(FOM_JavaScriptInterpreter.java:764) 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org