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