View Javadoc
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.MuleContext;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.transport.quartz.QuartzConnector;
12  import org.mule.transport.quartz.i18n.QuartzMessages;
13  
14  import org.quartz.Job;
15  import org.quartz.JobDataMap;
16  import org.quartz.JobExecutionContext;
17  import org.quartz.JobExecutionException;
18  import org.quartz.SchedulerContext;
19  import org.quartz.SchedulerException;
20  
21  /**
22   * Extracts the Job object to invoke from the context. The Job itself can be
23   * scheduled by dispatching an event over a quartz endpoint. The job can either be
24   * set as a property on the event (this property can be a container reference or the
25   * actual job object) or the payload of the event can be the Job (in which case when
26   * the job is fired it will have a NullPayload)
27   * 
28   * @see org.mule.transport.NullPayload
29   */
30  public class CustomJob implements Job
31  {
32      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
33      {
34          MuleContext muleContext = lookupMuleContext(jobExecutionContext);
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                  tempJob = muleContext.getRegistry().lookupObject((String) tempJob);
48                  if (tempJob == null)
49                  {
50                      throw new JobExecutionException("Job not found: " + tempJob);
51                  }
52                  if (!(tempJob instanceof Job))
53                  {
54                      throw new JobExecutionException(QuartzMessages.invalidJobObject().getMessage());
55                  }
56              }
57          }
58          else if (!(tempJob instanceof Job))
59          {
60              throw new JobExecutionException(QuartzMessages.invalidJobObject().toString());
61          }
62          ((Job)tempJob).execute(jobExecutionContext);
63      }
64  
65      private MuleContext lookupMuleContext(JobExecutionContext jobExecutionContext) throws JobExecutionException
66      {
67          try
68          {
69              SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
70              return (MuleContext) schedulerContext.get(MuleProperties.MULE_CONTEXT_PROPERTY);
71          }
72          catch (SchedulerException e)
73          {
74              throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e);
75          }
76      }
77  }