Coverage Report - org.mule.providers.quartz.QuartzMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
QuartzMessageReceiver
0%
0/56
0%
0/7
2.667
 
 1  
 /*
 2  
  * $Id: QuartzMessageReceiver.java 7976 2007-08-21 14:26:13Z 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.providers.quartz;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.providers.AbstractMessageReceiver;
 15  
 import org.mule.providers.quartz.i18n.QuartzMessages;
 16  
 import org.mule.providers.quartz.jobs.MuleReceiverJob;
 17  
 import org.mule.umo.UMOComponent;
 18  
 import org.mule.umo.UMOException;
 19  
 import org.mule.umo.endpoint.EndpointException;
 20  
 import org.mule.umo.endpoint.UMOEndpoint;
 21  
 import org.mule.umo.lifecycle.InitialisationException;
 22  
 import org.mule.umo.provider.UMOConnector;
 23  
 
 24  
 import java.util.Date;
 25  
 
 26  
 import org.quartz.CronTrigger;
 27  
 import org.quartz.JobDataMap;
 28  
 import org.quartz.JobDetail;
 29  
 import org.quartz.ObjectAlreadyExistsException;
 30  
 import org.quartz.Scheduler;
 31  
 import org.quartz.SimpleTrigger;
 32  
 import org.quartz.Trigger;
 33  
 
 34  
 /**
 35  
  * Listens for Quartz sheduled events using the Receiver Job and fires events to the
 36  
  * component associated with this receiver.
 37  
  */
 38  
 public class QuartzMessageReceiver extends AbstractMessageReceiver
 39  
 {
 40  
 
 41  
     public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
 42  
     public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector";
 43  
 
 44  
     private final QuartzConnector connector;
 45  
 
 46  
     public QuartzMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
 47  
         throws InitialisationException
 48  
     {
 49  0
         super(connector, component, endpoint);
 50  0
         this.connector = (QuartzConnector)connector;
 51  0
     }
 52  
 
 53  
     protected void doDispose()
 54  
     {
 55  
         // template method
 56  0
     }
 57  
 
 58  
     protected void doStart() throws UMOException
 59  
     {
 60  
         try
 61  
         {
 62  0
             Scheduler scheduler = connector.getQuartzScheduler();
 63  
 
 64  0
             JobDetail jobDetail = new JobDetail();
 65  0
             jobDetail.setName(endpoint.getEndpointURI().toString());
 66  0
             jobDetail.setJobClass(MuleReceiverJob.class);
 67  0
             JobDataMap jobDataMap = new JobDataMap();
 68  0
             jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey());
 69  0
             jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName());
 70  0
             jobDataMap.putAll(endpoint.getProperties());
 71  0
             jobDetail.setJobDataMap(jobDataMap);
 72  
 
 73  0
             Trigger trigger = null;
 74  0
             String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
 75  0
             String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
 76  0
             String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
 77  0
             String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
 78  0
             String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME);
 79  0
             String jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME);
 80  
 
 81  0
             if (groupName == null)
 82  
             {
 83  0
                 groupName = QuartzConnector.DEFAULT_GROUP_NAME;
 84  
             }
 85  0
             if (jobGroupName == null)
 86  
             {
 87  0
                 jobGroupName = groupName;
 88  
             }
 89  
 
 90  0
             jobDetail.setGroup(groupName);
 91  
 
 92  0
             if (cronExpression != null)
 93  
             {
 94  0
                 CronTrigger ctrigger = new CronTrigger();
 95  0
                 ctrigger.setCronExpression(cronExpression);
 96  0
                 trigger = ctrigger;
 97  
             }
 98  0
             else if (repeatInterval != null)
 99  
             {
 100  0
                 SimpleTrigger strigger = new SimpleTrigger();
 101  0
                 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
 102  0
                 if (repeatCount != null)
 103  
                 {
 104  0
                     strigger.setRepeatCount(Integer.parseInt(repeatCount));
 105  
                 }
 106  
                 else
 107  
                 {
 108  0
                     strigger.setRepeatCount(-1);
 109  
                 }
 110  0
                 trigger = strigger;
 111  
             }
 112  
             else
 113  
             {
 114  0
                 throw new IllegalArgumentException(
 115  
                     QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
 116  
             }
 117  0
             long start = System.currentTimeMillis();
 118  0
             if (startDelay != null)
 119  
             {
 120  0
                 start += Long.parseLong(startDelay);
 121  
             }
 122  0
             trigger.setStartTime(new Date(start));
 123  0
             trigger.setName(endpoint.getEndpointURI().toString());
 124  0
             trigger.setGroup(groupName);
 125  0
             trigger.setJobName(endpoint.getEndpointURI().toString());
 126  0
             trigger.setJobGroup(jobGroupName);
 127  
 
 128  
             // We need to handle cases when the job has already been
 129  
             // persisted
 130  
             try
 131  
             {
 132  0
                 scheduler.scheduleJob(jobDetail, trigger);
 133  
             }
 134  0
             catch (ObjectAlreadyExistsException oaee)
 135  
             {
 136  
                 // Do anything here?
 137  0
             }
 138  
 
 139  0
             scheduler.start();
 140  
         }
 141  0
         catch (Exception e)
 142  
         {
 143  0
             throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e);
 144  0
         }
 145  0
     }
 146  
 
 147  
     protected void doStop() throws UMOException
 148  
     {
 149  
         // nothing to do
 150  0
     }
 151  
 
 152  
     protected void doConnect() throws Exception
 153  
     {
 154  
         // nothing to do
 155  0
     }
 156  
 
 157  
     protected void doDisconnect() throws Exception
 158  
     {
 159  
         // nothing to do
 160  0
     }
 161  
 
 162  
 }