View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.scheduling;
16  
17  import org.figure8.join.core.EntityObject;
18  import org.figure8.join.core.InvalidParameterException;
19  import org.figure8.join.util.LogUtil;
20  
21  import org.apache.commons.logging.Log;
22  import org.quartz.Job;
23  import org.quartz.JobDetail;
24  import org.quartz.JobDataMap;
25  
26  import java.util.List;
27  import java.util.ArrayList;
28  /**
29   * Cron object containing info on how to create and schedule a Quartz job.
30   * @author <a href="mailto:jerome.evrard@gmail.com">Jerome Evrard</a>
31   * @version $Revision: 1.3 $
32   * 
33   * @hibernate.class table="join_quartzcron"
34   * @hibernate.cache usage="read-write"
35   * 
36   * @hibernate.query name="join.quartzcron_findByName"
37   *    query="from QuartzCronInfo cron where cron.name = :cronName"
38   */
39  public class QuartzCronInfo extends EntityObject{
40  
41     // Static -------------------------------------------------------------------
42  
43     /** Get a commons logger. */
44     private static final Log log = LogUtil.getLog(QuartzCronInfo.class);
45  
46  
47     // Attributes ---------------------------------------------------------------
48  
49     /** <code>name</code>: The cron name */
50     private String name;
51     /** <code>cronExpression</code>: The cron expression that schedules job */
52     private String cronExpression;
53     /** <code>jobType</code>: The job type name */
54     private String jobType;
55     /** <code>jobClass</code>: The job class name to instanciate on job execution */
56     private String jobClass;
57     /** A list of <code>QuartzCronParameterInfo</code>s if job is {@link org.figure8.join.core.Configurable}. */
58     private List jobParameters = new ArrayList();
59  
60  
61     // Constructors -------------------------------------------------------------
62     
63     /** Creates a new QuarzCron instance. */
64     public QuartzCronInfo(){
65     }
66     
67     /**
68      * Build a new instance with mandatory attributes.
69      * @param name The cron name.
70      * @param cronExpression The cron expression.
71      * @param jobType The job type name.
72      * @param jobClass The job class name to execute.
73      */
74     public QuartzCronInfo(String name, String cronExpression, String jobType, String jobClass)
75             throws InvalidParameterException{
76        this.name = name;
77        this.cronExpression = cronExpression;
78        this.jobType = jobType;
79        // Check and set job class.
80        setJobClass(jobClass);
81     }
82  
83  
84     // Public -------------------------------------------------------------------
85  
86     /**
87      * @return Returns the name.
88      * @hibernate.property column="s_name"
89      *    not-null="true" unique="true"
90      *    length="80"
91      */
92     public String getName(){
93        return name;
94     }
95     /** @param name The name to set. */
96     public void setName(String name){
97        this.name = name;
98     }
99     
100    /**
101     * @return Returns the cronExpression.
102     * @hibernate.property column="s_cronexpression"
103     *    not-null="true" length="40"
104     */
105    public String getCronExpression(){
106       return cronExpression;
107    }
108    /** @param cronExpression The cronExpression to set. */
109    public void setCronExpression(String cronExpression){
110       this.cronExpression = cronExpression;
111    }
112 
113    /**
114     * @return Returns the jobType.
115     * @hibernate.property column="s_jobtype"
116     *    not-null="true" length="80"
117     */
118    public String getJobType(){
119       return jobType;
120    }
121    /** @param jobType The jobType to set. */
122    public void setJobType(String jobType){
123       this.jobType = jobType;
124    }
125 
126    /**
127     * @return Returns the jobClass.
128     * @hibernate.property column="s_jobclass"
129     *    not-null="true" length="200"
130     */
131    public String getJobClass(){
132       return jobClass;
133    }
134    /**
135     * Give the FQN of Java class used for instanciating a Job
136     * @param jobClass The name of java class implementing job
137     * @throws InvalidParameterException if the <b>consumerBeanClass</b> is not an instance of {@link Job}
138     */
139    public void setJobClass(String jobClass) throws InvalidParameterException{
140       this.jobClass = jobClass;
141       // Try instanciating job now.
142       checkJobClass();
143    }
144 
145    /**
146     * @hibernate.bag cascade="all-delete-orphan" inverse="true"
147     * @hibernate.collection-key column="n_cron_fk"
148     * @hibernate.collection-one-to-many class="org.figure8.join.services.scheduling.QuartzCronParameterInfo"
149     * @return A set of {@code QuartzCronParameterInfo}s for this cron definition
150     */
151    public List getJobParameterInfos(){
152       return jobParameters;
153    }
154    /** @param paramInfos A set of {@code QuartzCronParameterInfo}s */
155    public void setJobParameterInfos(List paramInfos){
156       this.jobParameters = paramInfos;
157    }
158 
159    /**
160     * Convenient method for adding a job parameter info to a cron.
161     * This method manages the 2 sides of the association.
162     * @param paramInfo The parameter info to add to quartz cron definition
163     */
164    public void addJobParameterInfo(QuartzCronParameterInfo paramInfo){
165       // Manage the 2 sides of relation.
166       paramInfo.setCronInfo(this);
167       jobParameters.add(paramInfo);
168    }
169 
170    /**
171     * Try to instanciate wrapped <b>job</b>.
172     * @throws InvalidParameterException if <b>jobClass</b> is not valid.
173     */
174    public Class checkJobClass() throws InvalidParameterException{
175       if (jobClass != null && jobClass.length() > 0){
176          try{
177             // Load class from context class loader and retrieve new instance.
178             Class clazz = Thread.currentThread().getContextClassLoader().loadClass(jobClass);
179             if (!Job.class.isAssignableFrom(clazz))
180                throw new ClassCastException("'" + clazz +"' does not implement Job");
181             return clazz;
182          }
183          catch (Exception e){
184             // Log and wrap within an InvalidParameterException.
185             log.error("Exception while trying to load job class for cron '" + name + "'");
186             log.error("Here's the exception message : " + e.getMessage());
187             throw new InvalidParameterException("'" + jobClass +
188                     "' cannot be found or does not implement Job", e);
189          }
190       }
191       return null;
192    }
193 
194    /**
195     * Create a Quartz JobDetail object from this Cron informations
196     * @throws InvalidParameterException if the job class cannot be found
197     * @return The newly created {@code JobDetail} object or null if no job class is specified
198     */
199    public JobDetail createJobDetail() throws InvalidParameterException{
200       // First check and get job class.
201       Class clazz = checkJobClass();
202       if (clazz != null){
203          // Create a new job detail and its data map.
204          JobDetail detail = new JobDetail(name, null, clazz);
205          JobDataMap data = new JobDataMap();
206          // Browse cron parameters and add them as data.
207          for (int i=0; i<jobParameters.size(); i++){
208             QuartzCronParameterInfo paramInfo = (QuartzCronParameterInfo)jobParameters.get(i);
209             data.put(paramInfo.getName(), paramInfo);
210          }
211          detail.setJobDataMap(data);
212          return detail;
213       }
214       return null;
215    }
216 }