View Javadoc

1   /*
2    * $Id: GroovyRefreshableBeanBuilder.java 11195 2008-03-06 04:13:01Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.scripting.component;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.lifecycle.Callable;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.util.StringUtils;
18  
19  import groovy.lang.GroovyObject;
20  import groovy.lang.MetaMethod;
21  
22  public class GroovyRefreshableBeanBuilder implements Callable
23  {
24      private volatile Object refreshableBean;
25      private String methodName;
26      private static final String ON_CALL = "onCall";
27      private static final Class[] UMOEVENTCONTEXT = new Class[]{MuleEventContext.class};
28      
29      public GroovyRefreshableBeanBuilder()
30      {
31          super();
32      }
33  
34      public Object onCall(MuleEventContext eventContext) throws Exception
35      {
36          if (refreshableBean instanceof GroovyObject)
37          {
38              GroovyObject script = (GroovyObject)refreshableBean;
39              MetaMethod onCall = script.getMetaClass().pickMethod("onCall", UMOEVENTCONTEXT);
40  
41              if (onCall != null)
42              {
43                  return script.invokeMethod(ON_CALL, eventContext);
44              }
45              else
46              {
47                  if (StringUtils.isEmpty(methodName))
48                  {
49                      throw new DefaultMuleException(CoreMessages.propertiesNotSet("methodName"));
50                  }
51                  
52                  return script.invokeMethod(methodName, eventContext.transformMessage());
53              }
54              
55          }
56          
57          throw new Exception(new DefaultMuleException("script engine not supported"));
58      }
59  
60      public Object getRefreshableBean()
61      {
62          return refreshableBean;
63      }
64  
65      public void setRefreshableBean(Object refreshableBean)
66      {
67          this.refreshableBean = refreshableBean;
68      }
69      
70      public String getMethodName()
71      {
72          return methodName;
73      }
74  
75      public void setMethodName(String methodName)
76      {
77          this.methodName = methodName;
78      }
79  }
80  
81