KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > jobs > AntJob


1 package org.quartz.jobs;
2
3 import java.io.File JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import org.apache.tools.ant.BuildException;
9 import org.apache.tools.ant.Project;
10 import org.apache.tools.ant.ProjectHelper;
11 import org.quartz.Job;
12 import org.quartz.JobDataMap;
13 import org.quartz.JobExecutionContext;
14 import org.quartz.JobExecutionException;
15
16 /**
17  * A Job executing ANT scripts.<br>
18  * Brief usage example:
19  *
20  * <p><blockquote><pre>
21  * ...
22  *
23  * JobDetail job = new JobDetail();
24  * job.setGroup("ANT");
25  * job.setName("Example");
26  * job.setJobClass(AntJob.class);
27  *
28  * Map dataMap = job.getJobDataMap();
29  * dataMap.put("execdir", "pathToYourAntScripts");
30  * dataMap.put("buildfile", "yourAntScript.xml"); //default is build.xml
31  * dataMap.put("target", "targetToExecute"); //default is script's dafault target
32  *
33  * //set the 'lib.dir' ant property to './lib'
34  * dataMap.put("$P_lib.dir", "./lib");
35  *
36  * //set the 'debug' ant (user) property to 'true'
37  * dataMap.put("$U_debug", "true");
38  *
39  * ...
40  * </pre></blockquote>
41  * <p>
42  *
43  * @author Dimitar Marinov
44  */

45 public class AntJob implements Job {
46
47     /**
48      * Creates a new <code>AntJob</code>.
49      */

50     public AntJob() {
51         super();
52     }
53
54     /**
55      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
56      */

57     public void execute(JobExecutionContext context)
58         throws JobExecutionException {
59
60         JobDataMap dataMap = context.getMergedJobDataMap();
61         String JavaDoc execDir = dataMap.getString("execdir");
62         String JavaDoc buildFileName = dataMap.getString("buildfile");
63         String JavaDoc target = dataMap.getString("target");
64
65         // Create a new project, and perform some default initialization
66
Project project = new Project();
67         try {
68             project.init();
69         } catch (BuildException e) {
70             throw new JobExecutionException(e.getMessage());
71         }
72         
73         // Prepare the buildfile. If none is given, "build.xml" is used.
74
if (buildFileName == null) {
75             buildFileName = "build.xml";
76         }
77         File JavaDoc buildFile = new File JavaDoc(execDir + File.separator + buildFileName);
78         
79         // Set system properties
80
Properties JavaDoc sysProps = System.getProperties();
81         Iterator JavaDoc it = sysProps.entrySet().iterator();
82         while (it.hasNext()) {
83             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
84             project.setProperty(entry.getKey().toString(), entry.getValue().toString());
85         }
86         
87         // Loop through the parameter property map
88
it = dataMap.keySet().iterator();
89         while (it.hasNext()) {
90             // Get the property's name and value
91
String JavaDoc propName = (String JavaDoc) it.next();
92             String JavaDoc propValue = dataMap.getString(propName);
93             if (propName.startsWith("$P_")) {
94                 project.setProperty(propName.substring(3), propValue);
95             } else if (propName.startsWith("$U_")) {
96                 project.setUserProperty(propName.substring(3), propValue);
97             }
98         }
99         
100         try {
101             ProjectHelper.getProjectHelper().parse(project, buildFile);
102             
103             // Set ANT built-in properties
104
project.setProperty("basedir", project.getBaseDir().getPath());
105             project.setProperty("ant.project.name", project.getName());
106             project.setProperty("ant.file", buildFile.getPath());
107             
108             // If no target is specified, run the default one.
109
if (target == null) {
110                 target = project.getDefaultTarget();
111             }
112             
113             // Run the target
114
project.executeTarget(target);
115             
116         } catch (BuildException e) {
117             throw new JobExecutionException(e.getMessage());
118         }
119     }
120 }
Popular Tags