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