View Javadoc

1   /*
2    * $Id: DelegatingJob.java 7963 2007-08-21 08:53:15Z 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.jobs;
12  
13  import org.mule.MuleManager;
14  import org.mule.providers.quartz.QuartzConnector;
15  import org.mule.providers.quartz.i18n.QuartzMessages;
16  import org.mule.umo.manager.ObjectNotFoundException;
17  
18  import org.quartz.Job;
19  import org.quartz.JobDataMap;
20  import org.quartz.JobExecutionContext;
21  import org.quartz.JobExecutionException;
22  
23  /**
24   * Extracts the Job object to invoke from the context. The Job itself can be
25   * scheduled by dispatching an event over a quartz endpoint. The job can either be
26   * set as a property on the event (this property can be a container reference or the
27   * actual job object) or the payload of the event can be the Job (in which case when
28   * the job is fired it will have a NullPayload)
29   * 
30   * @see org.mule.providers.NullPayload
31   */
32  public class DelegatingJob implements Job
33  {
34      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
35      {
36          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
37          Object tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT);
38          if (tempJob == null)
39          {
40              tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF);
41              if (tempJob == null)
42              {
43                  throw new JobExecutionException(QuartzMessages.invalidPayloadType().getMessage());
44              }
45              else
46              {
47                  try
48                  {
49                      tempJob = MuleManager.getInstance().getContainerContext().getComponent(tempJob);
50                  }
51                  catch (ObjectNotFoundException e)
52                  {
53                      throw new JobExecutionException(e);
54                  }
55                  if (!(tempJob instanceof Job))
56                  {
57                      throw new JobExecutionException(QuartzMessages.invalidJobObject().getMessage());
58                  }
59              }
60          }
61          else if (!(tempJob instanceof Job))
62          {
63              throw new JobExecutionException(QuartzMessages.invalidJobObject().toString());
64          }
65          ((Job)tempJob).execute(jobExecutionContext);
66      }
67  }