Coverage Report - org.mule.transport.quartz.config.ScheduleAnnotationParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ScheduleAnnotationParser
0%
0/35
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: ScheduleAnnotationParser.java 19191 2010-08-25 21:05:23Z tcarlson $
 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  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  
         //This will only get called if there is no config builder configured
 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  
         //By default the scheduler should only use a single thread
 64  
         //TODO configure threads
 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  
         //Create event generator job
 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  
      * 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  0
         return !clazz.isInterface() && super.supports(annotation, clazz, member);
 134  
     }
 135  
 }