Coverage Report - org.mule.transport.quartz.QuartzMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
QuartzMessageDispatcher
84%
57/68
64%
14/22
3.167
 
 1  
 /*
 2  
  * $Id: QuartzMessageDispatcher.java 11678 2008-05-02 12:03:07Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
 
 11  
 package org.mule.transport.quartz;
 12  
 
 13  
 import org.mule.api.MuleEvent;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.OutboundEndpoint;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.transport.AbstractMessageDispatcher;
 18  
 import org.mule.transport.quartz.config.JobConfig;
 19  
 import org.mule.transport.quartz.i18n.QuartzMessages;
 20  
 import org.mule.transport.quartz.jobs.CustomJob;
 21  
 import org.mule.transport.quartz.jobs.CustomJobConfig;
 22  
 import org.mule.transport.quartz.jobs.CustomJobFromMessageConfig;
 23  
 
 24  
 import java.util.Date;
 25  
 import java.util.Iterator;
 26  
 
 27  
 import org.quartz.CronTrigger;
 28  
 import org.quartz.Job;
 29  
 import org.quartz.JobDataMap;
 30  
 import org.quartz.JobDetail;
 31  
 import org.quartz.Scheduler;
 32  
 import org.quartz.SimpleTrigger;
 33  
 import org.quartz.Trigger;
 34  
 
 35  
 /**
 36  
  * Can schedule a Job with the Quartz scheduler. The event must contain the Job to
 37  
  * invoke or have it set as a property. Time triggger properties can be set on the
 38  
  * event to control how and when the event is fired.
 39  
  */
 40  
 public class QuartzMessageDispatcher extends AbstractMessageDispatcher
 41  
 {
 42  
 
 43  
     public QuartzMessageDispatcher(OutboundEndpoint endpoint)
 44  
     {
 45  6
         super(endpoint);
 46  6
     }
 47  
 
 48  
     protected void doDispose()
 49  
     {
 50  
         // template method
 51  6
     }
 52  
 
 53  
     protected void doDispatch(MuleEvent event) throws Exception
 54  
     {
 55  6
         JobConfig jobConfig = (JobConfig)endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG);
 56  6
         if(jobConfig==null)
 57  
         {
 58  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
 59  
         }
 60  
 
 61  6
         JobDetail jobDetail = new JobDetail();
 62  
         // make the job name unique per endpoint (MULE-753)
 63  6
         jobDetail.setName(event.getEndpoint().getEndpointURI().getAddress() + "-" + event.getId());
 64  
 
 65  6
         JobDataMap jobDataMap = new JobDataMap();
 66  6
         MuleMessage msg = event.getMessage();
 67  6
         for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
 68  
         {
 69  42
             String propertyKey = (String)iterator.next();
 70  42
             jobDataMap.put(propertyKey, msg.getProperty(propertyKey));
 71  42
         }
 72  6
         jobDetail.setJobDataMap(jobDataMap);
 73  
 
 74  6
         Job job = null;
 75  
         // work out what we're actually calling
 76  6
         Object payload = event.transformMessage();
 77  
 
 78  6
         if(jobConfig instanceof CustomJobConfig)
 79  
         {
 80  0
             job = ((CustomJobConfig)jobConfig).getJob();
 81  
         }
 82  6
         else if(jobConfig instanceof CustomJobFromMessageConfig)
 83  
         {
 84  4
             job = ((CustomJobFromMessageConfig)jobConfig).getJob(msg);
 85  
             //rewrite the jobConfig to the real Jobconfig on the message
 86  4
             jobConfig = ((CustomJobFromMessageConfig)jobConfig).getJobConfig(msg);
 87  
         }
 88  
 
 89  6
         jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig);        
 90  6
         jobDetail.setJobClass(jobConfig.getJobClass());
 91  
         // If there has been a job created or found then we default to a customJob configuration
 92  6
         if (job!=null )
 93  
         {
 94  4
             jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job);
 95  4
             jobDetail.setJobClass(CustomJob.class);
 96  
         }
 97  
 
 98  
        
 99  
         // The payload will be ignored by the CustomJob - don't know why
 100  
         // we need it here
 101  
         //RM: The custom job may want the message and the Job type may not be delegating job
 102  6
         jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload);
 103  
 
 104  
         Trigger trigger;
 105  6
         String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
 106  6
         String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
 107  6
         String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
 108  6
         String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
 109  6
         String groupName = jobConfig.getGroupName();
 110  6
         String jobGroupName = jobConfig.getJobGroupName();
 111  
 
 112  6
         if (groupName == null)
 113  
         {
 114  0
             groupName = QuartzConnector.DEFAULT_GROUP_NAME;
 115  
         }
 116  6
         if (jobGroupName == null)
 117  
         {
 118  0
             jobGroupName = groupName;
 119  
         }
 120  
 
 121  6
         jobDetail.setGroup(groupName);
 122  
 
 123  6
         if (cronExpression != null)
 124  
         {
 125  0
             CronTrigger ctrigger = new CronTrigger();
 126  0
             ctrigger.setCronExpression(cronExpression);
 127  0
             trigger = ctrigger;
 128  0
         }
 129  6
         else if (repeatInterval != null)
 130  
         {
 131  6
             SimpleTrigger strigger = new SimpleTrigger();
 132  6
             strigger.setRepeatInterval(Long.parseLong(repeatInterval));
 133  6
             if (repeatCount != null)
 134  
             {
 135  0
                 strigger.setRepeatCount(Integer.parseInt(repeatCount));
 136  
             }
 137  
             else
 138  
             {
 139  6
                 strigger.setRepeatCount(-1);
 140  
             }
 141  6
             trigger = strigger;
 142  6
         }
 143  
         else
 144  
         {
 145  0
             throw new IllegalArgumentException(
 146  
                 QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
 147  
         }
 148  6
         long start = System.currentTimeMillis();
 149  6
         if (startDelay != null)
 150  
         {
 151  0
             start += Long.parseLong(startDelay);
 152  
         }
 153  6
         trigger.setStartTime(new Date(start));
 154  6
         trigger.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId());
 155  6
         trigger.setGroup(groupName);
 156  6
         trigger.setJobName(jobDetail.getName());
 157  6
         trigger.setJobGroup(jobGroupName);
 158  
 
 159  6
         Scheduler scheduler = ((QuartzConnector)this.getConnector()).getQuartzScheduler();
 160  6
         scheduler.scheduleJob(jobDetail, trigger);
 161  
        // scheduler.start();
 162  6
     }
 163  
 
 164  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 165  
     {
 166  6
         doDispatch(event);
 167  6
         return null;
 168  
     }
 169  
 
 170  
     protected void doConnect() throws Exception
 171  
     {
 172  
         // template method
 173  6
     }
 174  
 
 175  
     protected void doDisconnect() throws Exception
 176  
     {
 177  
         // template method
 178  6
     }
 179  
 
 180  
 }