1    /*
2     * Copyright 2007 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.bundle;
19   
20   import org.apache.log4j.NDC;
21   import org.quartz.Job;
22   import org.quartz.JobDataMap;
23   import org.quartz.JobExecutionContext;
24   import org.quartz.JobExecutionException;
25   
26   /**
27    * @author Christian Schatt
28    * @version $Revision: 1383 $
29    */
30   public abstract class AbstractJoblet implements Job {
31   
32       /**
33        * The method called by the quartz {@code Scheduler} to execute the
34        * {@code Joblet}.
35        * 
36        * @param jec
37        *            the {@code JobExecutionContext} is created for every
38        *            execution of the {@code Joblet} and contains the
39        *            {@code Bundle} the {@code Joblet} is executed for
40        * 
41        * @throws JobExecutionException
42        *             if an {@code Exception} is thrown while executing the
43        *             {@code Joblet}
44        * 
45        * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
46        */
47       public final void execute(final JobExecutionContext jec)
48               throws JobExecutionException {
49           JobDataMap map = jec.getJobDetail().getJobDataMap();
50           Bundle b = (Bundle) map.get("bundle");
51           if (b != null) {
52               NDC.push("job:" + b.getName());
53           } else {
54               NDC.push("job:no-bundle");
55           }
56           try {
57               run(b);
58           } catch (Exception exception) {
59               throw new JobExecutionException(exception);
60           } finally {
61               NDC.pop();
62               NDC.remove();
63           }
64       }
65   
66       /**
67        * The method where the action of the {@code Joblet} takes place,
68        * actually.
69        * 
70        * @param b
71        *            the {@code Bundle} the {@code Joblet} is executed
72        *            for or {@code null}
73        */
74       protected abstract void run(final Bundle b);
75   
76   }
77