4 Replies Latest reply on Feb 12, 2010 5:17 AM by tfennelly

    How to reference objects stored in Smooks ExecutionContext?

    edx

      Hi

      I think I have a serious problem to solve.

      The huge data files (structured data records, lets assume all records have the same layout, e.g., CSV) are to be processed in a way they are  splitted and converted to small XML chunks. I have applied smooks engine with freemarker for this task and it works fine except that the processing pipeline do not provide traceability of final XML fragments back to datafile from which they are originated.

      I have created FileFetchExecute class that is implementation of ScheduledEventMessageComposer interface.

      This class prepares URI to the file and stores it in Header->Call->RelatesTo field of the created ESB message.

      Then the custom Action class invokes smooks engine. The reasons of using custom Action class are two:

      1. MyAction prepares FileReader for the source datafile and that is impossible in out-of-the-box SmooksAction (rosetta complains about serialization).

      2. MyActions stores the URI of the source file into Smooks ExecutionContext under some arbitrary name in order to be available by smooks resources defined and configured in smooks-config.xml.

       

      And here is the final question.

      How to reference objects from ExecutionContext from JavaBeans instantiated by Smooks and/or from Freemarker template processing?

       

      Thanks,

      Edward

        • 1. Re: How to reference objects stored in Smooks ExecutionContext?
          tfennelly

          Hi Edward.

           

          The data map that the Freemarker templates etc see is stored in a Map off the ExecutionContext.  Setting bean instances in it is done via the BeanRepository.addBean(String, Object) method.

           

          So, the code in your FileFetchExecute class might look something like:

           

          ExecutionContext execContext = smooks.createExecutionContext();
          BeanRepository beanRepo = BeanRepository.getInstance(execContext);
          
          beanRepo.addBean("inputFile", theFile);
          
          smooks.filterSource(new StreamSource(new FileReader(theFile)));
          

           

          Then in the FreeMarker template, you reference this bean like any other e.g.

           

          <splitMessage srcPath="${inputFile.absolutePath}">
              .....
          </splitMessage>
          

           

          The API around th bean context has been cleaned up a little in Smooks v1.3, where we introduced the BeanContext class as a first class construct in Smooks Core and made it accessible directly on the ExecutionContext i.e. the above code would be something like execContext.getBeanContext().addBean(...), which is a little more obvious.

          • 2. Re: How to reference objects stored in Smooks ExecutionContext?
            edx

            Thanks Tom,

            your answer is very helpful.

            Edward

            • 3. Re: How to reference objects stored in Smooks ExecutionContext?
              edx

              Tom,

              I spent one day trying to find out why it is not working for me.

              The clue is in the proper selection of the overloaded method smooks.filterSource(...).

              We need here filterSource() that submits already prepared ExecutionContext,

              otherwise filterSource() (like in your advice) creates new one where our beans are not referenced.

               

              Edward

              • 4. Re: How to reference objects stored in Smooks ExecutionContext?
                tfennelly

                Oh of course... I forgot to do that in the sameple code... sorry.  So the sample code should have been (diff is call to filterSource method):

                 

                ExecutionContext execContext = smooks.createExecutionContext();
                BeanRepository beanRepo = BeanRepository.getInstance(execContext);
                 
                beanRepo.addBean("inputFile", theFile);
                 
                smooks.filterSource(execContext, new StreamSource(new FileReader(theFile)));