001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: AntTask.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.task;
022:
023: import java.io.File;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.Calendar;
027: import java.util.GregorianCalendar;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: import org.apache.avalon.framework.parameters.ParameterException;
032: import org.apache.avalon.framework.parameters.Parameters;
033: import org.apache.lenya.cms.ant.LenyaProject;
034: import org.apache.lenya.cms.publishing.PublishingEnvironment;
035: import org.apache.lenya.util.NamespaceMap;
036: import org.apache.tools.ant.BuildException;
037: import org.apache.tools.ant.Project;
038: import org.apache.tools.ant.ProjectHelper;
039: import org.apache.tools.ant.XmlLogger;
040:
041: /**
042: * An object of this class is used to execute Ant tasks. The task parameters are:
043: *
044: * <ul>
045: * <li><code><strong>publication-id</strong></code>: the publication ID</li>
046: * <li><code><strong>buildfile</strong> (optional)</code>: the location of the build file
047: * relative to the publication directory. If this parameter is not provided, the file is loaded from
048: * {@link #DEFAULT_BUILDFILE}.</li>
049: * <li><code><strong>target</strong> (optional)</code>: the build target. If this parameter is
050: * not provided, the default target is executed.</li>
051: * <li><code><strong>ant.*</strong></code>: the command-line parameters for Ant <strong>(not
052: * implemented yet!) </strong></li>
053: * <li><code><strong>properties.*</strong></code>: the project properties</li>
054: * </ul>
055: * @deprecated Use the usecase framework instead.
056: */
057: public class AntTask extends AbstractTask {
058:
059: /**
060: * Executes an Ant target.
061: *
062: * @param buildFile The build XML file.
063: * @param target The name of the target to execute.
064: * @param arguments A map mapping the command-line arguments to their values.
065: * @param properties A map mapping the project properties to their values.
066: * @param servletContextPath The context-path of the servlet
067: * @param contextPrefix The context-prefix of the servlet
068: * @param publicationId The publication-id
069: * @param publicationDirectory The directory of the publication
070: * @param logFile The file where the log should go to
071: *
072: * @throws ExecutionException if the execution failed
073: */
074: public void executeAntTarget(String servletContextPath,
075: String contextPrefix, String publicationId,
076: File publicationDirectory, File buildFile, String target,
077: Map arguments, Map properties, File logFile)
078: throws ExecutionException {
079: Project project = new LenyaProject(getServiceManager());
080: project.setCoreLoader(getClass().getClassLoader());
081:
082: Throwable error = null;
083:
084: try {
085: // create task log directory if it doesn't exist
086: File logDirectory = logFile.getParentFile();
087:
088: if (!logDirectory.exists()) {
089: logDirectory.mkdirs();
090: }
091:
092: project.setUserProperty("XmlLogger.file", logFile
093: .getAbsolutePath());
094:
095: XmlLogger logger = new XmlLogger();
096: project.addBuildListener(logger);
097: project.fireBuildStarted();
098:
099: project.init();
100: project.setBaseDir(publicationDirectory);
101:
102: ProjectHelper helper = ProjectHelper.getProjectHelper();
103: helper.parse(project, buildFile);
104:
105: project.setUserProperty(PUBLICATION_DIRECTORY,
106: publicationDirectory.getAbsolutePath());
107: project.setUserProperty(PUBLICATION_ID, publicationId);
108: project.setUserProperty(SERVLET_CONTEXT_PATH,
109: servletContextPath);
110: project.setUserProperty(CONTEXT_PREFIX, contextPrefix);
111:
112: String key;
113: String value;
114: Map.Entry entry;
115:
116: for (Iterator iter = properties.entrySet().iterator(); iter
117: .hasNext();) {
118: entry = (Map.Entry) iter.next();
119: key = (String) entry.getKey();
120: value = (String) entry.getValue();
121: project.setUserProperty(key, value);
122: }
123:
124: if (target == null) {
125: target = project.getDefaultTarget();
126: }
127:
128: project.executeTarget(target);
129: } catch (BuildException e) {
130: error = e;
131: throw new ExecutionException(e);
132: } finally {
133: project.fireBuildFinished(error);
134: }
135: }
136:
137: /**
138: * Returns the filename of the logfile to write.
139: * @param publicationDirectory for which publication the filename of the logfile is requested
140: * @return the file path for the log file
141: */
142: protected File getDefaultLogFile(File publicationDirectory) {
143: Calendar now = new GregorianCalendar();
144:
145: return new File(publicationDirectory, LOG_PATH
146: + dateFormat.format(now.getTime()) + ".xml");
147: }
148:
149: /**
150: * <code>dateFormat</code> The date format
151: */
152: public static final DateFormat dateFormat = new SimpleDateFormat(
153: "yyyy-MM-dd-HH-mm-ss-SSS");
154: /**
155: * <code>PUBLICATION_DIRECTORY</code> The publication directory
156: */
157: public static final String PUBLICATION_DIRECTORY = "pub.dir";
158: /**
159: * <code>PUBLICATION_ID</code> The publication id
160: */
161: public static final String PUBLICATION_ID = "pub.id";
162: /**
163: * <code>SERVLET_CONTEXT_PATH</code> The servlet context path
164: */
165: public static final String SERVLET_CONTEXT_PATH = "servlet.context";
166: /**
167: * <code>CONTEXT_PREFIX</code> The context prefix
168: */
169: public static final String CONTEXT_PREFIX = "context.prefix";
170: /**
171: * <code>BUILDFILE</code> The build file
172: */
173: public static final String BUILDFILE = "buildfile";
174: /**
175: * <code>TARGET</code> The target
176: */
177: public static final String TARGET = "target";
178: /**
179: * <code>ANT_PREFIX</code> The ant prefix
180: */
181: public static final String ANT_PREFIX = "ant";
182: /**
183: * <code>PROPERTIES_PREFIX</code> The properties prefix
184: */
185: public static final String PROPERTIES_PREFIX = "properties";
186: /**
187: * <code>DEFAULT_BUILDFILE</code> The default build file
188: */
189: public static final String DEFAULT_BUILDFILE = "config/tasks/targets.xml";
190: /**
191: * <code>LOG_PATH</code> The log path
192: */
193: public static final String LOG_PATH = "logs/tasks/".replace('/',
194: File.separatorChar);
195: /**
196: * <code>PARAMETER_LOGFILE</code> The log file parameter
197: */
198: public static final String PARAMETER_LOGFILE = "logfile";
199:
200: /**
201: * Execute the task. All parameters must have been set with parameterize().
202: * @param servletContextPath The servlet context path.
203: * @throws ExecutionException when the execution of the task failed.
204: */
205: public void execute(String servletContextPath)
206: throws ExecutionException {
207: String publicationId;
208: File publicationDirectory;
209: String contextPrefix;
210: File buildFile;
211: String target;
212: Map arguments;
213: Map properties;
214: File logFile;
215:
216: try {
217: String buildFileName = getParameters().getParameter(
218: "buildfile", DEFAULT_BUILDFILE).replace('/',
219: File.separatorChar);
220:
221: publicationId = getParameters().getParameter(
222: PARAMETER_PUBLICATION_ID);
223: contextPrefix = getParameters().getParameter(
224: PARAMETER_CONTEXT_PREFIX);
225:
226: if (publicationId.equals("")) {
227: publicationDirectory = new File(".");
228: buildFile = new File(buildFileName);
229: } else {
230: PublishingEnvironment environment = new PublishingEnvironment(
231: servletContextPath, publicationId);
232: publicationDirectory = environment
233: .getPublicationDirectory();
234: buildFile = new File(publicationDirectory,
235: buildFileName);
236: }
237:
238: target = getParameters().getParameter(TARGET, null);
239:
240: Map parametersMap = Parameters
241: .toProperties(getParameters());
242:
243: NamespaceMap antMap = new NamespaceMap(parametersMap,
244: ANT_PREFIX);
245: arguments = antMap.getMap();
246:
247: NamespaceMap propertiesMap = new NamespaceMap(
248: parametersMap, PROPERTIES_PREFIX);
249: properties = propertiesMap.getMap();
250:
251: // set logfile
252: String logFilename = getParameters().getParameter(
253: PARAMETER_LOGFILE,
254: getDefaultLogFile(publicationDirectory)
255: .getAbsolutePath());
256: logFile = new File(logFilename);
257: } catch (ParameterException e) {
258: throw new ExecutionException(e);
259: }
260:
261: executeAntTarget(servletContextPath, contextPrefix,
262: publicationId, publicationDirectory, buildFile, target,
263: arguments, properties, logFile);
264: }
265: }
|