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