1 2 Previous Next 16 Replies Latest reply on May 11, 2011 7:06 PM by manta7

    Scripting

    manta7

      Hi guys

       

      I have a problem but I didn't find the answer on the internet: from a camel route I'm trying to access to a script file

       

      My camel-context is the following:

       

      <scripting:endpoint

              service="test:script"

              endpoint="scriptEndpoint"

              script="classpath:script/script.js" />

       

      And my camel route seems like:

       

      from("seda:javascript").

           to("language://javascript:scriptEndpoint");

       

      But Servicemix told me that the "scriptEndpoint" is not defined.. So I wondering how can I access to my scripting-endpoint to launch a Javascript ?

       

      Thanks in advance!

        • 1. Re: Scripting
          davsclaus

          Can you post the entire XML file. I wonder what namespace you use for the scripting tag?

          • 2. Re: Scripting
            davsclaus

            And what version of ServiceMix and JDK are you using?

            • 3. Re: Scripting
              manta7

              Thanks for helping me,

               

              I use Servicemix 4.3 (and the OSGI way), with Camel 2.6 components and the JDK 1.5 (is there a difference with Java 6?).

              Sorry I didn't have my XML on this computer, but I followed this  documentation with:

               

              <beans xmlns:scripting="http://servicemix.apache.org/scripting/1.0"

                     xmlns:test="http://servicemix.apache.org/test"

                     xmlns:util="http://www.springframework.org/schema/util">

               

              But I added also the schemaLocation:

              http://servicemix.apache.org/scripting/1.0 http://servicemix.apache.org/scripting/1.0/servicemix-scripting.xsd

              Indeed, without this line my component was not deployed

              • 4. Re: Scripting
                ffang

                Hi,

                 

                You misuse camel language component and scripting JBI component here.

                The endpoint you defined here

                <scripting:endpoint

                service="test:script"

                endpoint="scriptEndpoint"

                script="classpath:script/script.js" />

                is a JBI endpoint, it can't be used directly in camel language component. Per the

                 

                The correct URI for camel language endpoint is

                language://languageName[:script][?options]

                 

                where the script part is the script to execute, but it shouldn't be a endpoint name of  JBI scripting endpoint.

                 

                http://camel.apache.org/language.html

                 

                Freeman

                • 5. Re: Scripting
                  manta7

                  Ok but I don't know how to complete the script argument in the language:// command...

                   

                  "but it shouldn't be a endpoint name of JBI scripting endpoint"

                  What should it be so to access to my script.js  ?

                   

                  Edited by: manta7 on May 5, 2011 7:00 AM

                  • 6. Re: Scripting
                    davsclaus

                    The  is as Freeman said a JBI component.

                     

                    So you need to use the Camel JBI to interact with it

                    http://camel.apache.org/jbi

                     

                    You cannot use the Camel language component directly with the JBI scripting component.

                     

                    We should maybe improve the Camel language component so it can load a script from a resource (eg from a file on the classpath etc.). I will log an enhancement for that.

                    • 7. Re: Scripting
                      davsclaus
                      • 8. Re: Scripting
                        manta7

                        Ok thanks for the ticket and the answer I understand...

                         

                        So according to my jbi endpoint which is:

                         

                        <scripting:endpoint

                                service="test:script"

                                endpoint="scriptEndpoint"

                                script="classpath:script/script.js" />

                         

                        And to my test namespace which is: xmlns:test="http://company.perso.org"

                         

                        My camel route should be: to("jbi:endpoint:http://company.perso.org/script/scriptEndpoint")

                         

                        Isn't it ?

                        Because I get this exception :

                         

                        09:34:27,564 | ERROR | 1 - seda://mslJS | SedaConsumer                     | ?                                   ? | 944 - org.apache.camel.camel-core

                        - 2.6.0 | Error processing exchange. Exchange[Message: ]. Caused by: [org.apache.camel.RuntimeCamelException - java.lang.IllegalStateEx

                        ception: Invalid BundleContext.]

                        • 9. Re: Scripting
                          manta7

                          Weird.. I restart Servicemix and I don't have the Invalid BundleContext anymore, but I can't still access to my JBI endpoint

                           

                          <scripting:endpoint

                                  service="test:script"

                                  endpoint="scriptEndpoint"

                                  script="classpath:script/script.js" />

                           

                          <bean class="org.apache.servicemix.common.osgi.EndpointExporter" />  

                           

                          <osgi:camelContext  id="camelCTX" xmlns="http://camel.apache.org/schema/spring">

                          <package>org.my.mosaic.route</package>

                          </osgi:camelContext>

                          • 10. Re: Scripting
                            ffang

                            Hi,

                             

                            Scripting endpoint is a kind of consumer jbi endpoint, it's an initial side of a MEP, so it couldn't play the role as a target.

                             

                            Freeman

                            • 11. Re: Scripting
                              davsclaus

                              A solution with pure Camel would be to load the script file and provide it as a header before you send the message the the camel language component.

                               

                              See that last example

                              http://camel.apache.org/language

                               

                              You would then have to use for example java code to load the script file from the classpath.

                              • 12. Re: Scripting
                                manta7

                                Ok I succeed it !

                                Thanks for all your reply !

                                 

                                So for the next people's who will be lost as I was, the solution is indeed the last on this page :  http://camel.apache.org/language

                                 

                                You have to use a bean:

                                from("...").

                                to("bean:MyBean?method=xx");

                                 

                                Into the xx method, I wrote that:

                                 

                                CamelContext ctx = exchange.getContext();

                                ProducerTemplate producer = new DefaultProducerTemplate(ctx);

                                try {

                                producer.start();

                                producer.requestBodyAndHeader("language:javascript", exchange.getIn(), Exchange.LANGUAGE_SCRIPT, fileToString(myFile));

                                } catch (Exception e) {

                                //Exception

                                }

                                 

                                The fileToString method take a File as argument and return the content as a string

                                 

                                After on the js file you can access to the argument with context, exchange... (ScriptContext paragraph on  http://camel.apache.org/javascript.html)

                                 

                                Thx again !

                                • 13. Re: Scripting
                                  manta7

                                  Arf.. Unfortunately this solution leads to a problem..

                                   

                                  Ideed, the JSR 223 provides the ScriptEngine class ( http://download.oracle.com/javase/6/docs/api/javax/script/ScriptEngine.html) and the put method which allows to set a Java object into the script.

                                   

                                  With the source above, I can't pass Java objects... I'm lost :/

                                  • 14. Re: Scripting
                                    manta7

                                    Ok I solved the problem! (I know it's a monologue )

                                     

                                    The only thing I found was to change the camel-script component.

                                     

                                    Indeed, in my component I put a Map<String, Object> into the header of the message. And in the evaluateScript(Exchange) method of the camel-script component, I iterate on the Map and I use the put() method of the ScriptEngine.

                                     

                                    I know this is not the most used component, but I think it could be a nice little evolution.

                                    Don't you think ?

                                    1 2 Previous Next