View Javadoc

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