KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > AntTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: AntTask.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.io.File JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.avalon.framework.parameters.ParameterException;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.lenya.cms.publishing.PublishingEnvironment;
33 import org.apache.lenya.util.NamespaceMap;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.ProjectHelper;
37 import org.apache.tools.ant.XmlLogger;
38
39 /**
40  * An object of this class is used to execute Ant tasks.
41  * The task parameters are:
42  *
43  * <ul>
44  * <li><code><strong>publication-id</strong></code>: the publication ID</li>
45  * <li><code><strong>buildfile</strong> (optional)</code>: the location of the build file
46  * relative to the publication directory. If this parameter is
47  * not provided, the file is loaded from {@link #DEFAULT_BUILDFILE}.</li>
48  * <li><code><strong>target</strong> (optional)</code>: the build target. If this parameter
49          is not provided, the default target is executed.</li>
50  * <li><code><strong>ant.*</strong></code>: the command-line parameters for Ant <strong>(not implemented yet!)</strong></li>
51  * <li><code><strong>properties.*</strong></code>: the project properties</li>
52  * </ul>
53  */

54 public class AntTask extends AbstractTask {
55     /**
56      * Executes an Ant target.
57      *
58      * @param buildFile The build XML file.
59      * @param target The name of the target to execute.
60      * @param arguments A map mapping the command-line arguments to their values.
61      * @param properties A map mapping the project properties to their values.
62      * @param servletContextPath The context-path of the servlet
63      * @param contextPrefix The context-prefix of the servlet
64      * @param publicationId The publication-id
65      * @param publicationDirectory The directory of the publication
66      * @param logFile The file where the log should go to
67      *
68      * @throws ExecutionException if the execution failed
69      */

70     public void executeAntTarget(
71         String JavaDoc servletContextPath,
72         String JavaDoc contextPrefix,
73         String JavaDoc publicationId,
74         File JavaDoc publicationDirectory,
75         File JavaDoc buildFile,
76         String JavaDoc target,
77         Map JavaDoc arguments,
78         Map JavaDoc properties,
79         File JavaDoc logFile)
80         throws ExecutionException {
81         Project project = new Project();
82         project.setCoreLoader(getClass().getClassLoader());
83
84         Throwable JavaDoc error = null;
85
86         try {
87             // create task log directory if it doesn't exist
88
File JavaDoc logDirectory = logFile.getParentFile();
89
90             if (!logDirectory.exists()) {
91                 logDirectory.mkdirs();
92             }
93
94             project.setUserProperty(
95                 "XmlLogger.file",
96                 logFile.getAbsolutePath());
97
98             XmlLogger logger = new XmlLogger();
99             project.addBuildListener(logger);
100             project.fireBuildStarted();
101
102             project.init();
103             project.setBaseDir(publicationDirectory);
104
105             ProjectHelper helper = ProjectHelper.getProjectHelper();
106             helper.parse(project, buildFile);
107
108             project.setUserProperty(
109                 PUBLICATION_DIRECTORY,
110                 publicationDirectory.getAbsolutePath());
111             project.setUserProperty(PUBLICATION_ID, publicationId);
112             project.setUserProperty(SERVLET_CONTEXT_PATH, servletContextPath);
113             project.setUserProperty(CONTEXT_PREFIX, contextPrefix);
114
115             for (Iterator JavaDoc keys = properties.keySet().iterator();
116                 keys.hasNext();
117                 ) {
118                 String JavaDoc key = (String JavaDoc)keys.next();
119                 project.setUserProperty(key, (String JavaDoc)properties.get(key));
120             }
121
122             if (target == null) {
123                 target = project.getDefaultTarget();
124             }
125
126             project.executeTarget(target);
127         } catch (BuildException e) {
128             error = e;
129             throw new ExecutionException(e);
130         } finally {
131             project.fireBuildFinished(error);
132         }
133     }
134
135     /**
136      * Returns the filename of the logfile to write.
137      *
138      * @param publicationDirectory for which publication the
139      * filename of the logfile is requested
140      *
141      * @return the file path for the log file
142      */

143     protected File JavaDoc getDefaultLogFile(File JavaDoc publicationDirectory) {
144         Calendar JavaDoc now = new GregorianCalendar JavaDoc();
145
146         return new File JavaDoc(
147             publicationDirectory,
148             LOG_PATH + dateFormat.format(now.getTime()) + ".xml");
149     }
150
151     public static final DateFormat JavaDoc dateFormat =
152         new SimpleDateFormat JavaDoc("yyyy-MM-dd-HH-mm-ss-SSS");
153     public static final String JavaDoc PUBLICATION_DIRECTORY = "pub.dir";
154     public static final String JavaDoc PUBLICATION_ID = "pub.id";
155     public static final String JavaDoc SERVLET_CONTEXT_PATH = "servlet.context";
156     public static final String JavaDoc CONTEXT_PREFIX = "context.prefix";
157     public static final String JavaDoc BUILDFILE = "buildfile";
158     public static final String JavaDoc TARGET = "target";
159     public static final String JavaDoc ANT_PREFIX = "ant";
160     public static final String JavaDoc PROPERTIES_PREFIX = "properties";
161     public static final String JavaDoc DEFAULT_BUILDFILE = "config/tasks/targets.xml";
162     public static final String JavaDoc LOG_PATH =
163         "logs/tasks/".replace('/', File.separatorChar);
164     public static final String JavaDoc PARAMETER_LOGFILE = "logfile";
165
166     /**
167      * Execute the task. All parameters must have been set with parameterize().
168      *
169      * @param servletContextPath The servlet context path.
170      *
171      * @throws ExecutionException when the execution of the task failed.
172      */

173     public void execute(String JavaDoc servletContextPath) throws ExecutionException {
174         String JavaDoc publicationId;
175         File JavaDoc publicationDirectory;
176         String JavaDoc contextPrefix;
177         File JavaDoc buildFile;
178         String JavaDoc target;
179         Map JavaDoc arguments;
180         Map JavaDoc properties;
181         File JavaDoc logFile;
182
183         try {
184             String JavaDoc buildFileName =
185                 getParameters().getParameter(
186                     "buildfile",
187                     DEFAULT_BUILDFILE).replace(
188                     '/',
189                     File.separatorChar);
190
191             publicationId =
192                 getParameters().getParameter(PARAMETER_PUBLICATION_ID);
193             contextPrefix = getParameters().getParameter(PARAMETER_CONTEXT_PREFIX);
194             
195
196             if (publicationId.equals("")) {
197                 publicationDirectory = new File JavaDoc(".");
198                 buildFile = new File JavaDoc(buildFileName);
199             } else {
200                 PublishingEnvironment environment =
201                     new PublishingEnvironment(
202                         servletContextPath,
203                         publicationId);
204                 publicationDirectory = environment.getPublicationDirectory();
205                 buildFile = new File JavaDoc(publicationDirectory, buildFileName);
206             }
207
208             target = getParameters().getParameter(TARGET, null);
209
210             Map JavaDoc parametersMap = Parameters.toProperties(getParameters());
211
212             NamespaceMap antMap = new NamespaceMap(parametersMap, ANT_PREFIX);
213             arguments = antMap.getMap();
214
215             NamespaceMap propertiesMap =
216                 new NamespaceMap(parametersMap, PROPERTIES_PREFIX);
217             properties = propertiesMap.getMap();
218
219             // set logfile
220
String JavaDoc logFilename =
221                 getParameters().getParameter(
222                     PARAMETER_LOGFILE,
223                     getDefaultLogFile(publicationDirectory).getAbsolutePath());
224             logFile = new File JavaDoc(logFilename);
225         } catch (ParameterException e) {
226             throw new ExecutionException(e);
227         }
228
229         executeAntTarget(
230             servletContextPath,
231             contextPrefix,
232             publicationId,
233             publicationDirectory,
234             buildFile,
235             target,
236             arguments,
237             properties,
238             logFile);
239     }
240 }
241
Popular Tags