View Javadoc

1   /*
2    * $Id: ScheduleAnnotationParser.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Creates a Quartz inbound endpoint for a service
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          //This will only get called if there is no config builder configured
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          //By default the scheduler should only use a single thread
64          //TODO configure threads
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          //Create event generator job
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      * Validates that this parser can parse the supplied annotation.  Only returns true if the clazz is not an interface
120      * and the annotation is an instance of {@link org.mule.api.annotations.Schedule}
121      *
122      * @param annotation the annotation being processed
123      * @param clazz      the class on which the annotation was found
124      * @param member     the member on which the annotation was found inside the class.  this is only set when the annotation
125      *                   was either set on a {@link java.lang.reflect.Method}, {@link java.lang.reflect.Field} or {@link java.lang.reflect.Constructor}
126      *                   class members, otherwise this value is null.
127      * @return true if this parser supports the current annotation and the clazz is not an interface
128      * @throws IllegalArgumentException if the class parameter is an interface
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 }