Coverage Report - org.mule.transport.quartz.jobs.EventGeneratorJob
 
Classes in this File Line Coverage Branch Coverage Complexity
EventGeneratorJob
0%
0/35
0%
0/14
15
 
 1  
 /*
 2  
  * $Id: EventGeneratorJob.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.jobs;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.config.MuleProperties;
 16  
 import org.mule.transport.AbstractConnector;
 17  
 import org.mule.transport.AbstractMessageReceiver;
 18  
 import org.mule.transport.NullPayload;
 19  
 import org.mule.transport.quartz.QuartzConnector;
 20  
 import org.mule.transport.quartz.QuartzMessageReceiver;
 21  
 import org.mule.transport.quartz.i18n.QuartzMessages;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.quartz.Job;
 26  
 import org.quartz.JobDataMap;
 27  
 import org.quartz.JobExecutionContext;
 28  
 import org.quartz.JobExecutionException;
 29  
 import org.quartz.SchedulerException;
 30  
 
 31  
 /**
 32  
  * Will generate a new event based o the scheduled time. The payload of the event is
 33  
  * currently a static object or instance of {@link org.mule.transport.NullPayload} if no payload
 34  
  * has been set.
 35  
  *
 36  
  * We may want to extend this but allowing the payload to be generated using a factory.
 37  
  */
 38  0
 public class EventGeneratorJob implements Job
 39  
 {
 40  
 
 41  
     /**
 42  
      * The logger used for this class
 43  
      */
 44  0
     protected transient Log logger = LogFactory.getLog(getClass());
 45  
 
 46  
     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
 47  
     {
 48  
         MuleContext muleContext;
 49  
         try
 50  
         {
 51  0
             muleContext = (MuleContext)jobExecutionContext.getScheduler().getContext().get(MuleProperties.MULE_CONTEXT_PROPERTY);
 52  
         }
 53  0
         catch (SchedulerException e)
 54  
         {
 55  0
             throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e);
 56  0
         }
 57  
 
 58  0
         JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap();
 59  
 
 60  0
         String receiverKey = (String)map.get(QuartzMessageReceiver.QUARTZ_RECEIVER_PROPERTY);
 61  0
         if (receiverKey == null)
 62  
         {
 63  0
             throw new JobExecutionException(QuartzMessages.receiverNotInJobDataMap().getMessage());
 64  
         }
 65  
 
 66  0
         String connectorName = (String)map.get(QuartzMessageReceiver.QUARTZ_CONNECTOR_PROPERTY);
 67  0
         if (connectorName == null)
 68  
         {
 69  0
             throw new JobExecutionException(QuartzMessages.connectorNotInJobDataMap().getMessage());
 70  
         }
 71  
 
 72  0
         AbstractConnector connector = (AbstractConnector) muleContext.getRegistry().lookupConnector(connectorName);
 73  0
         if (connector == null)
 74  
         {
 75  0
             throw new JobExecutionException(QuartzMessages.noConnectorFound(connectorName).getMessage());
 76  
         }
 77  
 
 78  0
         AbstractMessageReceiver receiver = (AbstractMessageReceiver)connector.lookupReceiver(receiverKey);
 79  0
         if (receiver == null)
 80  
         {
 81  0
             throw new JobExecutionException(
 82  
                 QuartzMessages.noReceiverInConnector(receiverKey, connectorName).getMessage());
 83  
         }
 84  
 
 85  0
         Object payload = jobExecutionContext.getJobDetail().getJobDataMap().get(
 86  
             QuartzConnector.PROPERTY_PAYLOAD);
 87  
 
 88  
         try
 89  
         {
 90  0
             if (payload == null)
 91  
             {
 92  0
                 String ref = jobExecutionContext.getJobDetail().getJobDataMap().getString(
 93  
                     QuartzConnector.PROPERTY_PAYLOAD);
 94  
 
 95  0
                 if (ref == null)
 96  
                 {
 97  0
                     payload = NullPayload.getInstance();
 98  
                 }
 99  
                 else
 100  
                 {
 101  0
                     payload = muleContext.getRegistry().lookupObject(ref);
 102  
                 }
 103  
 
 104  0
                 if (payload==null)
 105  
                 {
 106  0
                     logger.warn("There is no payload attached to this quartz job. Sending Null payload");
 107  0
                     payload = NullPayload.getInstance();
 108  
                 }
 109  
             }
 110  
             
 111  0
             MuleMessage msg = receiver.createMuleMessage(payload, receiver.getEndpoint().getEncoding());
 112  
             // If the job is stateful users can store state in this map and have it available for the next job trigger
 113  0
             msg.setInvocationProperty(
 114  
                     QuartzConnector.PROPERTY_JOB_DATA, jobExecutionContext.getJobDetail().getJobDataMap());
 115  0
             receiver.routeMessage(msg);
 116  
         }
 117  0
         catch (Exception e)
 118  
         {
 119  0
             muleContext.getExceptionListener().handleException(e);
 120  0
         }
 121  0
     }
 122  
 }