5 Replies Latest reply on Nov 1, 2011 7:50 AM by labo32_delaboe

    How to read script from a file ?

    labo32_delaboe

      Hello,

       

      I have a route where I want to read a groovy script from a file.

       

      from("quartz://timerName?cron=0/5 * * * * ?")
                  .bean(groovy("file://test.txt"))
                  .to("log:ExampleRouter");  
      

       

      fails with

       

      org.springframework.beans.factory.BeanCreationException: Error creating bean wit
      h name 'camel-1': Invocation of init method failed; nested exception is org.code
      haus.groovy.control.MultipleCompilationErrorsException: startup failed:<|script1
      3198099994491557003854.groovy: 1: unexpected token:  @ line 1, column 16.<|   fi
      le://test.txt<|                  ^<|<|1 error<|
      

       

      How can I read a script from a file ?

       

      Thanks

      labo

        • 1. Re: How to read script from a file ?
          davsclaus

          Hi

           

          This is currently not supported directly, something which we should possible add. I think this has been asked before in the Camel user list.

           

          What you can do is to use the language component and provide the script as a header which is the file.

          http://camel.apache.org/language

           

          First set a header with the file name

          .setHeader("CamelLanguageScript", new File("test.txt"))

           

          And then call the language component

          .to("language:groovy")

          .to("log:xxx")

          • 2. Re: How to read script from a file ?
            labo32_delaboe

            Hi,

             

            thanks for the response but

             

            .setHeader("CamelLanguageScript", new File("test.txt"))
            

             

            is not possible with my camel 2.8.2 route.

             

            Code like

             

            .....
            InputStream in = getContext().getApplicationContextClassLoader().getResourceAsStream("/test.txt");
                      from("quartz://TimerGroup/matchUpdateTimer?cron=0/5 * * * * ?")               .setHeader("CamelLanguageScript",constant(convertStreamToString(in)))
                           .to("language:groovy")
                           .to("log:################### QUARTZ-log");
            ......
            ...
            
            
             public String convertStreamToString(InputStream is) {
            
                    try {
                           if (is != null) {
                               Writer writer = new StringWriter();
            
                               char[] buffer = new char[1024];
                               try {
                                   Reader reader = new BufferedReader(
                                           new InputStreamReader(is, "UTF-8"));
                                   int n;
                                   while ((n = reader.read(buffer)) != -1) {
                                       writer.write(buffer, 0, n);
                                   }
                               } finally {
                                   is.close();
                               }
                               return writer.toString();
                           } else {        
                               return "";
                           }
                      } catch (UnsupportedEncodingException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                           return "";
                      } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                           return "";
                      }
            

             

             

            works fine but I think this is not the most elegant way.....

             

            Can I use the file component after the quartz trigger and load the file which is located in my war file ?

             

            Greetz

            labo

            • 3. Re: How to read script from a file ?
              davsclaus

              Ah the file is in the WAR file?

               

              Then you need to load it from the classpath. Which is a bit more tricky. The language component in Camel doesn't support this, but we could add support for that.

               

              I will create a ticket.

               

              There is a API on CamelContext to load resources from the classpath, eg ClassResolver on CamelContext.

              • 4. Re: How to read script from a file ?
                davsclaus
                • 5. Re: How to read script from a file ?
                  labo32_delaboe

                  That's the way I do it at the moment.

                  Thanks a lot

                  labo