View Javadoc

1   /*
2    * $Id: CustomJob.java 19191 2010-08-25 21:05:23Z tcarlson $
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 implements Job
35  {
36      public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
37      {
38          MuleContext muleContext = lookupMuleContext(jobExecutionContext);
39          
40          JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
41          Object tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT);
42          if (tempJob == null)
43          {
44              tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF);
45              if (tempJob == null)
46              {
47                  throw new JobExecutionException(QuartzMessages.invalidPayloadType().getMessage());
48              }
49              else
50              {
51                  tempJob = muleContext.getRegistry().lookupObject((String) tempJob);
52                  if (tempJob == null)
53                  {
54                      throw new JobExecutionException("Job not found: " + tempJob);
55                  }
56                  if (!(tempJob instanceof Job))
57                  {
58                      throw new JobExecutionException(QuartzMessages.invalidJobObject().getMessage());
59                  }
60              }
61          }
62          else if (!(tempJob instanceof Job))
63          {
64              throw new JobExecutionException(QuartzMessages.invalidJobObject().toString());
65          }
66          ((Job)tempJob).execute(jobExecutionContext);
67      }
68  
69      private MuleContext lookupMuleContext(JobExecutionContext jobExecutionContext) throws JobExecutionException
70      {
71          try
72          {
73              SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
74              return (MuleContext) schedulerContext.get(MuleProperties.MULE_CONTEXT_PROPERTY);
75          }
76          catch (SchedulerException e)
77          {
78              throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e);
79          }
80      }
81  }