You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Des Magner <de...@icandriveatractor.com> on 2011/01/23 01:25:49 UTC

Adding my Java code to flowscript

Hi

I am trying to add some custom code to be called from my flowscript but 
with no success. I followed the instructions in the user documentation 
to the letter (http://cocoon.apache.org/2.1/userdocs/flow/java.html). I 
have added my source directory entry to the component-instance in the 
format "file:/path/to/my/source" but it doesn't seem to want to pick up 
the code. Say I have the following code in my flowscript:

var resource = new Packages.Test();

with the file Test.java placed in my source dir as specified above, I 
consistently get the following error:

org.mozilla.javascript.EvaluatorException: "file:///....js", line ..: 
Not a Java class: [JavaPackage Test]

I can get around it by placing the compiled class in the 
cocoon/WEB-INF/classes directory but this is not a very elegant solution.

Any help would be greatly appreciated.
Thanks
Des

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


Re: Adding my Java code to flowscript

Posted by Andre Juffer <aj...@sun3.oulu.fi>.
On 01/25/2011 06:57 PM, Des Magner wrote:
> Hi André
>
> That is more or less what I was trying to achieve, ie. write Java code that
> can be used in my flowscript but for this Java code to be completely
> independent of cocoon. From reading the documentation I was given
> to understand that this code could be compiled on the fly which would
> of course speed up development time (See the documentation link I referenced
> in the original post). I have never had any problem using standard Java
> classes, such as ArrayList, etc. in flowscript, the problem is accessing
> classes that I have written. So as far as I can see there are three options
> outlined below, the third one being the only one I have been able to
> successfully achieve.
>
> Option 1: Have your code compiled on the fly, specifiying in cocoon.xconf a
> classpath as to where it should look for source code. I cannot get this to
> work and this was why I posted the message in the first place. In more
> detail this is what I do:
>
> Add the following to cocoon.xconf:
>
> <flow-interpreters default="javascript" logger="flow">
>    <component-instance
> class="org.apache.cocoon.components.flow.javascript.fom.
> FOM_JavaScriptInterpreter"
> name="javascript">
>
> <load-on-startup>resource://org/apache/cocoon/components/flow/
> javascript/fom/fom_system.js</load-on-startup>
>      <reload-scripts>true</reload-scripts>
>      <check-time>4000</check-time>
>      <classpath>file:/path/to/my/java/src</classpath>
>    </component-instance>
> </flow-interpreters>
>
> Javascript in my flowscript:
>
> var resource = new Packages.mypackage.xyz.MyClass();
>
> Throws the following exception:
>
> org.mozilla.javascript.EcmaError: TypeError:
> [JavaPackage mypackage.xyx.MyClass] is not a function,
> it is org.mozilla.javascript.NativeJavaPackage.
>
>
> Option 2: Complile my code to the my application's WEB-INF/classes
> directory (or alternatively place it in a jar file in the application's
> WEB-INF/lib directory. This I also tried but to no avail.
>
> Option 3: Place the compiled classes in the cocoon's own classes folder.
> When I do this I can indeed access my Java classes from flowscript.
> But this for me is the least desireable solution as it requires me to
> mix my own class files with cocoons class files which I don't consider
> elegant nor correct. And also, cocoon would need to be restarted
> everytime I make a modication to my Java classes.

I have only experience with option 3, I cannot say so much about the 
other options. If you use cocoon 2.2, the dependency goes in the 
pom.xml. With earlier versions, one can write an ant script to ensure 
that the jar file (say test.jar) is deposited in WEB-INF/lib. This all 
certainly works very well and, to me (not to you apparently), is the 
simplest and easiest way.

It is correct that you would need to restart cocoon after a modification 
to your classes. On the other hand, if you have designed your classes 
well, and tested well, you can bring this down to a minimum. For a new 
project, I never start with cocoon, but work on a proper design and 
implementation of the domain and rules, and, if needed, basic 
infrastructure (database, etc) as well. When start to use cocoon, I am 
rather certain that every already works well (classes in the jar, that is).

BTW which version of cocoon are you using?

I also wonder about your design. You say you are required to mix your 
classes with cocoon classes. Why is that necessary? I may have 
misunderstood you.

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


-- 
Andre H. Juffer              | Phone: +358-8-553 1161
Biocenter Oulu and           | Fax: +358-8-553-1141
Department of Biochemistry   | Email: andre.juffer@oulu.fi
University of Oulu, Finland  | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat                   | WWW: www.strubiocat.oulu.fi
NordProt                     | WWW: www.nordprot.org
Triacle Biocomputing         | WWW: www.triacle-bc.com

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


Re: Adding my Java code to flowscript

Posted by Des Magner <de...@icandriveatractor.com>.
Hi André

That is more or less what I was trying to achieve, ie. write Java code that
can be used in my flowscript but for this Java code to be completely 
independent of cocoon. From reading the documentation I was given 
to understand that this code could be compiled on the fly which would 
of course speed up development time (See the documentation link I referenced 
in the original post). I have never had any problem using standard Java 
classes, such as ArrayList, etc. in flowscript, the problem is accessing 
classes that I have written. So as far as I can see there are three options 
outlined below, the third one being the only one I have been able to
successfully achieve.

Option 1: Have your code compiled on the fly, specifiying in cocoon.xconf a
classpath as to where it should look for source code. I cannot get this to 
work and this was why I posted the message in the first place. In more 
detail this is what I do:

Add the following to cocoon.xconf:

<flow-interpreters default="javascript" logger="flow">
  <component-instance
class="org.apache.cocoon.components.flow.javascript.fom.
FOM_JavaScriptInterpreter"
name="javascript">
   
<load-on-startup>resource://org/apache/cocoon/components/flow/
javascript/fom/fom_system.js</load-on-startup>
    <reload-scripts>true</reload-scripts>
    <check-time>4000</check-time>
    <classpath>file:/path/to/my/java/src</classpath>
  </component-instance>
</flow-interpreters>

Javascript in my flowscript:

var resource = new Packages.mypackage.xyz.MyClass();

Throws the following exception:

org.mozilla.javascript.EcmaError: TypeError: 
[JavaPackage mypackage.xyx.MyClass] is not a function, 
it is org.mozilla.javascript.NativeJavaPackage. 


Option 2: Complile my code to the my application's WEB-INF/classes 
directory (or alternatively place it in a jar file in the application's
WEB-INF/lib directory. This I also tried but to no avail.

Option 3: Place the compiled classes in the cocoon's own classes folder. 
When I do this I can indeed access my Java classes from flowscript. 
But this for me is the least desireable solution as it requires me to 
mix my own class files with cocoons class files which I don't consider 
elegant nor correct. And also, cocoon would need to be restarted 
everytime I make a modication to my Java classes.

Regards
Des


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


Re: Adding my Java code to flowscript

Posted by Andre Juffer <an...@oulu.fi>.
As far as I am concerned, the best solution is keep your Java 
development separated from Cocoon. If you Java has no knowledge about or 
dependency on cocoon, it is much simpler and in my mind also much better 
to place your Java classes in a separated Jar file and make your 
cocoon-based application dependent on your Java classes. If you use 
maven then this is very straightforward to accomplish in cocoon 2.2. The 
additional advantage is that your can very easy test your Java code 
without ever running cocoon (e.g. with Netbeans).

To allow my cocoon-based application (or any other application for that 
matter) to work with my Java code, I typically define a few suitable 
Facades that are instantiated in Flow using a simple Factory method or 
with Spring (I prefer the latter approach):

factory:

var facade = Packages.test.SomeFacade.newInstance();  // static method.

or, as Robby pointed out,

importClass(Packages.test.SomeFacade);
var facade = SomeFacade.newInstance();

Spring:

var facade = cocoon.getComponent("test.SomeFacade");   // 
test.SomeFacade is the bean ID that was defined for the facade
                                                                                                    // in a Spring configuration file.

Best regards,
André



On 25/01/11 08:17, Robby Pelssers wrote:
> importClass(Packages.java.util.ArrayList);
>
> function search() {
>      var list = new ArrayList();
>      ...
> }
>
> Kind regards,
> Robby Pelssers
>
>
> -----Oorspronkelijk bericht-----
> Van: Des Magner [mailto:des@icandriveatractor.com]
> Verzonden: zo 23-1-2011 1:25
> Aan: users@cocoon.apache.org
> Onderwerp: Adding my Java code to flowscript
>
> Hi
>
> I am trying to add some custom code to be called from my flowscript but
> with no success. I followed the instructions in the user documentation
> to the letter (http://cocoon.apache.org/2.1/userdocs/flow/java.html). I
> have added my source directory entry to the component-instance in the
> format "file:/path/to/my/source" but it doesn't seem to want to pick up
> the code. Say I have the following code in my flowscript:
>
> var resource = new Packages.Test();
>
> with the file Test.java placed in my source dir as specified above, I
> consistently get the following error:
>
> org.mozilla.javascript.EvaluatorException: "file:///....js", line ..:
> Not a Java class: [JavaPackage Test]
>
> I can get around it by placing the compiled class in the
> cocoon/WEB-INF/classes directory but this is not a very elegant solution.
>
> Any help would be greatly appreciated.
> Thanks
> Des
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>    
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org


-- 
Andre H. Juffer              | Phone: +358-8-553 1161
Biocenter Oulu and           | Fax: +358-8-553-1141
Department of Biochemistry   | Email: andre.juffer@oulu.fi
University of Oulu, Finland  | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat                   | WWW: www.strubiocat.oulu.fi
NordProt                     | WWW: www.nordprot.org
Triacle Biocomputing         | WWW: www.triacle-bc.com


RE: Adding my Java code to flowscript

Posted by Robby Pelssers <ro...@ciber.com>.
importClass(Packages.java.util.ArrayList);

function search() {
    var list = new ArrayList();
    ...
}

Kind regards,
Robby Pelssers


-----Oorspronkelijk bericht-----
Van: Des Magner [mailto:des@icandriveatractor.com]
Verzonden: zo 23-1-2011 1:25
Aan: users@cocoon.apache.org
Onderwerp: Adding my Java code to flowscript
 
Hi

I am trying to add some custom code to be called from my flowscript but 
with no success. I followed the instructions in the user documentation 
to the letter (http://cocoon.apache.org/2.1/userdocs/flow/java.html). I 
have added my source directory entry to the component-instance in the 
format "file:/path/to/my/source" but it doesn't seem to want to pick up 
the code. Say I have the following code in my flowscript:

var resource = new Packages.Test();

with the file Test.java placed in my source dir as specified above, I 
consistently get the following error:

org.mozilla.javascript.EvaluatorException: "file:///....js", line ..: 
Not a Java class: [JavaPackage Test]

I can get around it by placing the compiled class in the 
cocoon/WEB-INF/classes directory but this is not a very elegant solution.

Any help would be greatly appreciated.
Thanks
Des

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



Re: Adding my Java code to flowscript

Posted by Thomas Markus <t....@proventis.net>.
hm, a simple

var a = new Packages.java.lang.Integer(0);

works here. please check your classpath for a package named Test. maybe 
you have a folder Test and a file Test.class

regards
Thomas


Am 24.01.2011 20:17, schrieb Des Magner:
> Hi Thomas
>
> That is not a solution. Of course I had tried it initially with a class that was
> part of a package. I only described it this way for simplicity's sake. And there
> is no requirement that a Java class belong to a package.
>
> Regards
> Des
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>


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


Re: Adding my Java code to flowscript

Posted by Des Magner <de...@icandriveatractor.com>.
Hi Thomas

That is not a solution. Of course I had tried it initially with a class that was
part of a package. I only described it this way for simplicity's sake. And there
is no requirement that a Java class belong to a package. 

Regards
Des 



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


Re: Adding my Java code to flowscript

Posted by Thomas Markus <t....@proventis.net>.
Hi,

dont use the default package. Place your class in a package (fi 
test.Test) and it should work.

regards
Thomas


Am 23.01.2011 01:25, schrieb Des Magner:
> Hi
>
> I am trying to add some custom code to be called from my flowscript 
> but with no success. I followed the instructions in the user 
> documentation to the letter 
> (http://cocoon.apache.org/2.1/userdocs/flow/java.html). I have added 
> my source directory entry to the component-instance in the format 
> "file:/path/to/my/source" but it doesn't seem to want to pick up the 
> code. Say I have the following code in my flowscript:
>
> var resource = new Packages.Test();
>
> with the file Test.java placed in my source dir as specified above, I 
> consistently get the following error:
>
> org.mozilla.javascript.EvaluatorException: "file:///....js", line ..: 
> Not a Java class: [JavaPackage Test]
>
> I can get around it by placing the compiled class in the 
> cocoon/WEB-INF/classes directory but this is not a very elegant solution.
>
> Any help would be greatly appreciated.
> Thanks
> Des
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>


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


RE: Adding my Java code to flowscript

Posted by "Nathaniel, Alfred" <Al...@six-group.com>.
Hi Des,

Thanks for sharing this information.
Would you mind entering it into http://issues.apache.org/jira/browse/COCOON to keep track of it?

Cheers, Alfred.

-----Original Message-----
From: Des Magner [mailto:des@icandriveatractor.com] 
Sent: Sonntag, 11. September 2011 13:16
To: users@cocoon.apache.org
Subject: Re: Adding my Java code to flowscript

Hi

For those of you in need of an answer, I recently revisited this problem and
found out what the issue was. I debugged the CompilingClassLoader class
behaviour and eventually found that it was not working due to a conflict in the
libraries shipped with cocoon (version 2.1.11, the stable release). The problem
occurs when it tries to compile your java code using an instance of the eclipse
JavaCompilerImpl. There was an exception being thrown from within the call to
compile but this exception is never displayed in the logging nor on the console.
The eclipse JavaCompilerImpl instantiates a NamedEnvironmentAnswer object from
the jdt. However, it is expecting only one parameter in the constructor,
IBinaryType. But the constructor defined in the jdt jar that was shipped
contains an extra parameter of type AccessRestriction. Because of this there was
a NoSuchMethodException thrown. So I downloaded an older version of the jdt jar,
replaced it and the exception went away. So in summary you need to replace the
jdtcore-3.1.0.jar with jdt-3.0.2 jar in the cocoon installation.

Regards
Des

The content of this e-mail is intended only for the confidential use of the person addressed. 
If you are not the intended recipient, please notify the sender and delete this e-mail immediately.
Thank you.

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


Re: Adding my Java code to flowscript

Posted by Des Magner <de...@icandriveatractor.com>.
Hi

For those of you in need of an answer, I recently revisited this problem and
found out what the issue was. I debugged the CompilingClassLoader class
behaviour and eventually found that it was not working due to a conflict in the
libraries shipped with cocoon (version 2.1.11, the stable release). The problem
occurs when it tries to compile your java code using an instance of the eclipse
JavaCompilerImpl. There was an exception being thrown from within the call to
compile but this exception is never displayed in the logging nor on the console.
The eclipse JavaCompilerImpl instantiates a NamedEnvironmentAnswer object from
the jdt. However, it is expecting only one parameter in the constructor,
IBinaryType. But the constructor defined in the jdt jar that was shipped
contains an extra parameter of type AccessRestriction. Because of this there was
a NoSuchMethodException thrown. So I downloaded an older version of the jdt jar,
replaced it and the exception went away. So in summary you need to replace the
jdtcore-3.1.0.jar with jdt-3.0.2 jar in the cocoon installation.

Regards
Des


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


Re: Adding my Java code to flowscript

Posted by Thorsten Scherler <sc...@gmail.com>.
On Sun, 2011-01-23 at 01:25 +0100, Des Magner wrote:
> Hi
> 
> I am trying to add some custom code to be called from my flowscript but 
> with no success. I followed the instructions in the user documentation 
> to the letter (http://cocoon.apache.org/2.1/userdocs/flow/java.html). I 
> have added my source directory entry to the component-instance in the 
> format "file:/path/to/my/source" but it doesn't seem to want to pick up 
> the code. Say I have the following code in my flowscript:
> 
> var resource = new Packages.Test();
> 
> with the file Test.java placed in my source dir as specified above, I 
> consistently get the following error:
> 
> org.mozilla.javascript.EvaluatorException: "file:///....js", line ..: 
> Not a Java class: [JavaPackage Test]
> 
> I can get around it by placing the compiled class in the 
> cocoon/WEB-INF/classes directory but this is not a very elegant solution.
> 
> Any help would be greatly appreciated.
> Thanks
> Des

Did you see
http://www.mail-archive.com/users@cocoon.apache.org/msg08996.html

"the CompilingClassLoader on 12/26. Since I'm using 2.1.3, which was
released mid-November, this functionality isn't included in 2.1.3.

...

Did you restart Cocoon after modifying adding <classpath>? I just tested
this and got the same error as you before doing so, but then it worked
as expected after a restart. 

If that doesn't work, set a breakpoint on
org.apache.cocoon.components.flow.javascript.fom.CompilingClassLoader.loadClass(), and step through and try to see why it isn't finding your class. In particular see if its sourcePath field contains your classpath entry.
..."

When we use java objects in flow (or jx) we normally create it in the
same module/block or add the dep to the pom.xml. Then a mvn install and
everything gets build and is available in the flow. 

However as I understand your post you are mainly looking into the
CompilingClassLoader
http://www.mail-archive.com/dev@cocoon.apache.org/msg10144.html

salu2
-- 
Thorsten Scherler <thorsten.at.apache.org>
codeBusters S.L. - web based systems
<consulting, training and solutions>
http://www.codebusters.es/


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