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