View Javadoc

1   /*
2    * $Id: CustomJobFromMessageConfig.java 11733 2008-05-13 08:08:45Z dirk.olmes $
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  package org.mule.transport.quartz.jobs;
11  
12  import org.mule.api.MuleMessage;
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.transport.quartz.config.AbstractJobConfig;
15  import org.mule.transport.quartz.config.JobConfig;
16  import org.mule.util.ClassUtils;
17  import org.mule.util.expression.ExpressionEvaluatorManager;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import org.quartz.Job;
22  
23  /**
24   * This configuration simply holds a reference to a user defined job to execute.
25   */
26  public class CustomJobFromMessageConfig extends AbstractJobConfig
27  {
28      private String expression;
29      private String evaluator;
30      private String customEvaluator;
31  
32      public Job getJob(MuleMessage message) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException
33      {
34          if (evaluator.equals("custom"))
35          {
36              evaluator = customEvaluator;
37          }
38  
39          Object result = ExpressionEvaluatorManager.evaluate(expression, evaluator, message, true);
40          Class clazz;
41          if (result instanceof Job)
42          {
43              return (Job) result;
44          }
45          else if (result instanceof JobConfig)
46          {
47              clazz = ((JobConfig)result).getJobClass();
48          }
49          else
50          {
51              throw new IllegalStateException(CoreMessages.propertyIsNotSupportedType(evaluator + ":" + expression,
52                      new Class[]{Job.class, JobConfig.class}, result.getClass()).getMessage());
53          }
54  
55          return (Job) ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS);
56      }
57  
58      public JobConfig getJobConfig(MuleMessage message) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException
59      {
60          if (evaluator.equals("custom"))
61          {
62              evaluator = customEvaluator;
63          }
64  
65          Object result = ExpressionEvaluatorManager.evaluate(expression, evaluator, message, true);
66          if (result instanceof Job)
67          {
68              CustomJobConfig customJob = new CustomJobConfig();
69              customJob.setJob((Job) result);
70              return customJob;
71          }
72          else if (result instanceof JobConfig)
73          {
74              return (JobConfig)result;
75          }
76          else
77          {
78              throw new IllegalStateException(CoreMessages.propertyIsNotSupportedType(evaluator + ":" + expression,
79                      new Class[]{Job.class, JobConfig.class, Class.class, String.class}, result.getClass()).getMessage());
80          }
81      }
82  
83      public String getCustomEvaluator()
84      {
85          return customEvaluator;
86      }
87  
88      public void setCustomEvaluator(String customEvaluator)
89      {
90          this.customEvaluator = customEvaluator;
91      }
92  
93      public String getEvaluator()
94      {
95          return evaluator;
96      }
97  
98      public void setEvaluator(String evaluator)
99      {
100         this.evaluator = evaluator;
101     }
102 
103     public String getExpression()
104     {
105         return expression;
106     }
107 
108     public void setExpression(String expression)
109     {
110         this.expression = expression;
111     }
112 
113     public Class getJobClass()
114     {
115         return CustomJob.class;
116     }
117 
118 }