Coverage Report - org.mule.transport.quartz.jobs.EndpointPollingJob
 
Classes in this File Line Coverage Branch Coverage Complexity
EndpointPollingJob
74%
23/31
50%
7/14
15
 
 1  
 /*
 2  
  * $Id: EndpointPollingJob.java 11613 2008-04-20 20:30:10Z rossmason $
 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.jobs;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.ThreadSafeAccess;
 16  
 import org.mule.api.transport.PropertyScope;
 17  
 import org.mule.module.client.MuleClient;
 18  
 import org.mule.transport.quartz.QuartzConnector;
 19  
 import org.mule.transport.quartz.QuartzMessageReceiver;
 20  
 import org.mule.transport.quartz.i18n.QuartzMessages;
 21  
 import org.mule.transport.AbstractConnector;
 22  
 import org.mule.transport.AbstractMessageReceiver;
 23  
 import org.mule.RegistryContext;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 import org.quartz.Job;
 28  
 import org.quartz.JobDataMap;
 29  
 import org.quartz.JobExecutionContext;
 30  
 import org.quartz.JobExecutionException;
 31  
 
 32  
 /**
 33  
  * Will receive on an endpoint and dispatch it to the component set via the Receiver information.
 34  
  */
 35  6
 public class EndpointPollingJob implements Job
 36  
 {
 37  
     /**
 38  
      * The logger used for this class
 39  
      */
 40  6
     protected transient Log logger = LogFactory.getLog(getClass());
 41  
 
 42  
     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
 43  
     {
 44  6
         JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
 45  
 
 46  
 
 47  6
         String receiverKey = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_RECEIVER_PROPERTY);
 48  6
         if (receiverKey == null)
 49  
         {
 50  0
             throw new JobExecutionException(QuartzMessages.receiverNotInJobDataMap().getMessage());
 51  
         }
 52  
 
 53  6
         String connectorName = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_CONNECTOR_PROPERTY);
 54  6
         if (connectorName == null)
 55  
         {
 56  0
             throw new JobExecutionException(QuartzMessages.connectorNotInJobDataMap().getMessage());
 57  
         }
 58  
 
 59  6
         AbstractConnector connector = (AbstractConnector) RegistryContext.getRegistry().lookupConnector(connectorName);
 60  6
         if (connector == null)
 61  
         {
 62  0
             throw new JobExecutionException(QuartzMessages.noConnectorFound(connectorName).getMessage());
 63  
         }
 64  
 
 65  6
         AbstractMessageReceiver receiver = (AbstractMessageReceiver) connector.lookupReceiver(receiverKey);
 66  6
         if (receiver == null)
 67  
         {
 68  0
             throw new JobExecutionException(
 69  
                     QuartzMessages.noReceiverInConnector(receiverKey, connectorName).getMessage());
 70  
         }
 71  
 
 72  
 
 73  6
         EndpointPollingJobConfig jobConfig = (EndpointPollingJobConfig) jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG);
 74  6
         if (jobConfig == null)
 75  
         {
 76  0
             throw new JobExecutionException(
 77  
                     QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
 78  
         }
 79  
 
 80  
 
 81  
         try
 82  
         {
 83  6
             MuleClient client = new MuleClient();
 84  6
             logger.debug("Attempting to receive event on: " + jobConfig.getEndpointRef());
 85  6
             MuleMessage result = client.request(jobConfig.getEndpointRef(), jobConfig.getTimeout());
 86  6
             if (result != null)
 87  
             {
 88  6
                 if (logger.isDebugEnabled())
 89  
                 {
 90  0
                     logger.debug("Received event on: " + jobConfig.getEndpointRef());
 91  
                 }
 92  
                 //we need to do this because
 93  6
                 result = (MuleMessage)((ThreadSafeAccess)result).newThreadCopy();
 94  
 
 95  
                 //Add the context properties to the message.
 96  6
                 result.addProperties(jobDataMap, PropertyScope.INVOCATION);
 97  
 
 98  6
                 receiver.routeMessage(result);
 99  
             }
 100  
         }
 101  0
         catch (MuleException e)
 102  
         {
 103  0
             throw new JobExecutionException(e);
 104  6
         }
 105  6
     }
 106  
 }