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 public class ScheduleAnnotationParser extends AbstractEndpointAnnotationParser
37 {
38
39 @Override
40 public InboundEndpoint parseInboundEndpoint(Annotation annotation, Map metaInfo) throws MuleException
41 {
42 Schedule schedule = (Schedule) annotation;
43 ScheduleConfigBuilder builder = lookupConfig(schedule.config(), ScheduleConfigBuilder.class);
44 if (builder != null)
45 {
46 return builder.buildScheduler();
47 }
48 else
49 {
50 return super.parseInboundEndpoint(annotation, Collections.emptyMap());
51 }
52 }
53
54 protected AnnotatedEndpointData createEndpointData(Annotation annotation) throws MuleException
55 {
56
57 Schedule schedule = (Schedule) annotation;
58
59 String uri = "quartz://schedule" + UUID.getUUID();
60 AnnotatedEndpointData epData = new AnnotatedEndpointData(MessageExchangePattern.ONE_WAY, ChannelType.Inbound, annotation);
61
62 epData.setProperties(convertProperties(getProperties(schedule)));
63
64
65 String threads = (String) epData.getProperties().get("threads");
66 if (threads == null)
67 {
68 threads = "1";
69 epData.getProperties().put("threads", threads);
70 }
71 epData.setAddress(uri);
72 epData.setConnector(getConnector());
73
74 EventGeneratorJobConfig config = new EventGeneratorJobConfig();
75 config.setStateful(threads.equals("1"));
76 epData.getProperties().put(QuartzConnector.PROPERTY_JOB_CONFIG, config);
77 return epData;
78 }
79
80
81 protected String[] getProperties(Schedule schedule) throws MuleException
82 {
83 List<String> props = new ArrayList<String>(2);
84 if (StringUtils.isNotBlank(schedule.cron()))
85 {
86 props.add(QuartzConnector.PROPERTY_CRON_EXPRESSION + "=" + schedule.cron());
87 }
88 else if (schedule.interval() > -1)
89 {
90 props.add(QuartzConnector.PROPERTY_REPEAT_INTERVAL + "=" + schedule.interval());
91
92 if (schedule.startDelay() > -1)
93 {
94 props.add(QuartzConnector.PROPERTY_START_DELAY +"=" + schedule.startDelay());
95 }
96 }
97 else
98 {
99 throw new IllegalArgumentException("cron or repeatInterval must be set");
100 }
101 return CollectionUtils.toArrayOfComponentType(props, String.class);
102
103 }
104
105 protected String getIdentifier()
106 {
107 return Schedule.class.getAnnotation(Channel.class).identifer();
108 }
109
110 protected QuartzConnector getConnector() throws MuleException
111 {
112 QuartzConnector connector = new QuartzConnector(muleContext);
113 connector.setName("scheduler." + connector.hashCode());
114 muleContext.getRegistry().registerConnector(connector);
115 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 return !clazz.isInterface() && super.supports(annotation, clazz, member);
134 }
135 }