1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.transport.quartz.config; |
11 | |
|
12 | |
import org.mule.MessageExchangePattern; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.annotations.Schedule; |
15 | |
import org.mule.api.annotations.meta.Channel; |
16 | |
import org.mule.api.annotations.meta.ChannelType; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.config.endpoint.AbstractEndpointAnnotationParser; |
19 | |
import org.mule.config.endpoint.AnnotatedEndpointData; |
20 | |
import org.mule.transport.quartz.QuartzConnector; |
21 | |
import org.mule.transport.quartz.jobs.EventGeneratorJobConfig; |
22 | |
import org.mule.util.CollectionUtils; |
23 | |
import org.mule.util.StringUtils; |
24 | |
import org.mule.util.UUID; |
25 | |
|
26 | |
import java.lang.annotation.Annotation; |
27 | |
import java.lang.reflect.Member; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Collections; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class ScheduleAnnotationParser extends AbstractEndpointAnnotationParser |
37 | |
{ |
38 | |
|
39 | |
@Override |
40 | |
public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException |
41 | |
{ |
42 | 0 | Schedule schedule = (Schedule) annotation; |
43 | 0 | ScheduleConfigBuilder builder = lookupConfig(schedule.config(), ScheduleConfigBuilder.class); |
44 | 0 | if (builder != null) |
45 | |
{ |
46 | 0 | return builder.buildScheduler(); |
47 | |
} |
48 | |
else |
49 | |
{ |
50 | 0 | return super.parseInboundEndpoint(annotation, Collections.emptyMap()); |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
protected AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException |
55 | |
{ |
56 | |
|
57 | 0 | Schedule schedule = (Schedule) annotation; |
58 | |
|
59 | 0 | String uri = "quartz://schedule" + UUID.getUUID(); |
60 | 0 | AnnotatedEndpointData epData = new AnnotatedEndpointData(MessageExchangePattern.ONE_WAY, ChannelType.Inbound, annotation); |
61 | |
|
62 | 0 | epData.setProperties(convertProperties(getProperties(schedule))); |
63 | |
|
64 | |
|
65 | 0 | String threads = (String) epData.getProperties().get("threads"); |
66 | 0 | if (threads == null) |
67 | |
{ |
68 | 0 | threads = "1"; |
69 | 0 | epData.getProperties().put("threads", threads); |
70 | |
} |
71 | 0 | epData.setAddress(uri); |
72 | 0 | epData.setConnector(getConnector()); |
73 | |
|
74 | 0 | EventGeneratorJobConfig config = new EventGeneratorJobConfig(); |
75 | 0 | config.setStateful(threads.equals("1")); |
76 | 0 | epData.getProperties().put(QuartzConnector.PROPERTY_JOB_CONFIG, config); |
77 | 0 | return epData; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
protected String[] getProperties(Schedule schedule) throws MuleException |
82 | |
{ |
83 | 0 | List<String> props = new ArrayList<String>(2); |
84 | 0 | if (StringUtils.isNotBlank(schedule.cron())) |
85 | |
{ |
86 | 0 | props.add(QuartzConnector.PROPERTY_CRON_EXPRESSION + "=" + schedule.cron()); |
87 | |
} |
88 | 0 | else if (schedule.interval() > -1) |
89 | |
{ |
90 | 0 | props.add(QuartzConnector.PROPERTY_REPEAT_INTERVAL + "=" + schedule.interval()); |
91 | |
|
92 | 0 | if (schedule.startDelay() > -1) |
93 | |
{ |
94 | 0 | props.add(QuartzConnector.PROPERTY_START_DELAY +"=" + schedule.startDelay()); |
95 | |
} |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | throw new IllegalArgumentException("cron or repeatInterval must be set"); |
100 | |
} |
101 | 0 | return CollectionUtils.toArrayOfComponentType(props, String.class); |
102 | |
|
103 | |
} |
104 | |
|
105 | |
protected String getIdentifier() |
106 | |
{ |
107 | 0 | return Schedule.class.getAnnotation(Channel.class).identifer(); |
108 | |
} |
109 | |
|
110 | |
protected QuartzConnector getConnector() throws MuleException |
111 | |
{ |
112 | 0 | QuartzConnector connector = new QuartzConnector(muleContext); |
113 | 0 | connector.setName("scheduler." + connector.hashCode()); |
114 | 0 | muleContext.getRegistry().registerConnector(connector); |
115 | 0 | return connector; |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
@Override |
131 | |
public boolean supports(Annotation annotation, Class clazz, Member member) |
132 | |
{ |
133 | 0 | return !clazz.isInterface() && super.supports(annotation, clazz, member); |
134 | |
} |
135 | |
} |