View Javadoc

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