1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.quartz.jobs; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.context.MuleContextAware; |
12 | |
import org.mule.config.i18n.CoreMessages; |
13 | |
import org.mule.transport.quartz.config.AbstractJobConfig; |
14 | |
import org.mule.transport.quartz.config.JobConfig; |
15 | |
import org.mule.util.ClassUtils; |
16 | |
|
17 | |
import java.lang.reflect.InvocationTargetException; |
18 | |
|
19 | |
import org.quartz.Job; |
20 | |
import org.quartz.StatefulJob; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class CustomJobFromMessageConfig extends AbstractJobConfig |
26 | |
{ |
27 | |
private String expression; |
28 | |
private String evaluator; |
29 | |
private String customEvaluator; |
30 | |
|
31 | |
public Job getJob(MuleMessage message) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException |
32 | |
{ |
33 | 0 | setupEvaluator(); |
34 | |
|
35 | 0 | Object result = getMuleContext().getExpressionManager().evaluate(expression, evaluator, message, true); |
36 | |
Class<? extends Job> clazz; |
37 | 0 | if (result instanceof Job) |
38 | |
{ |
39 | 0 | return (Job) result; |
40 | |
} |
41 | 0 | else if (result instanceof JobConfig) |
42 | |
{ |
43 | 0 | clazz = ((JobConfig)result).getJobClass(); |
44 | |
} |
45 | |
else |
46 | |
{ |
47 | 0 | throw new IllegalStateException(CoreMessages.propertyIsNotSupportedType(evaluator + ":" + expression, |
48 | |
new Class[]{Job.class, JobConfig.class}, result.getClass()).getMessage()); |
49 | |
} |
50 | |
|
51 | 0 | return ClassUtils.instanciateClass(clazz); |
52 | |
} |
53 | |
|
54 | |
public JobConfig getJobConfig(MuleMessage message) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException |
55 | |
{ |
56 | 0 | setupEvaluator(); |
57 | |
|
58 | 0 | final MuleContext muleContext = getMuleContext(); |
59 | 0 | Object result = muleContext.getExpressionManager().evaluate(expression, evaluator, message, true); |
60 | 0 | if (result instanceof Job) |
61 | |
{ |
62 | 0 | CustomJobConfig customJob = new CustomJobConfig(); |
63 | 0 | customJob.setJob((Job) result); |
64 | 0 | customJob.setMuleContext(muleContext); |
65 | 0 | return customJob; |
66 | |
} |
67 | 0 | else if (result instanceof JobConfig) |
68 | |
{ |
69 | 0 | if (result instanceof MuleContextAware) |
70 | |
{ |
71 | 0 | ((MuleContextAware) result).setMuleContext(muleContext); |
72 | |
} |
73 | 0 | return (JobConfig) result; |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 0 | throw new IllegalStateException(CoreMessages.propertyIsNotSupportedType(evaluator + ":" + expression, |
78 | |
new Class[]{Job.class, JobConfig.class, Class.class, String.class}, result.getClass()).getMessage()); |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
protected void setupEvaluator() |
83 | |
{ |
84 | 0 | if (evaluator.equals("custom")) |
85 | |
{ |
86 | 0 | evaluator = customEvaluator; |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
public String getCustomEvaluator() |
91 | |
{ |
92 | 0 | return customEvaluator; |
93 | |
} |
94 | |
|
95 | |
public void setCustomEvaluator(String customEvaluator) |
96 | |
{ |
97 | 0 | this.customEvaluator = customEvaluator; |
98 | 0 | } |
99 | |
|
100 | |
public String getEvaluator() |
101 | |
{ |
102 | 0 | return evaluator; |
103 | |
} |
104 | |
|
105 | |
public void setEvaluator(String evaluator) |
106 | |
{ |
107 | 0 | this.evaluator = evaluator; |
108 | 0 | } |
109 | |
|
110 | |
public String getExpression() |
111 | |
{ |
112 | 0 | return expression; |
113 | |
} |
114 | |
|
115 | |
public void setExpression(String expression) |
116 | |
{ |
117 | 0 | this.expression = expression; |
118 | 0 | } |
119 | |
|
120 | |
@Override |
121 | |
protected Class<? extends StatefulJob> getStatefulJobClass() |
122 | |
{ |
123 | 0 | return StatefulCustomJob.class; |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
protected Class<? extends Job> getStatelessJobClass() |
128 | |
{ |
129 | 0 | return CustomJob.class; |
130 | |
} |
131 | |
} |