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