1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import java.io.File ; 23 import java.text.DateFormat ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Calendar ; 26 import java.util.GregorianCalendar ; 27 import java.util.Iterator ; 28 import java.util.Map ; 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 54 public class AntTask extends AbstractTask { 55 70 public void executeAntTarget( 71 String servletContextPath, 72 String contextPrefix, 73 String publicationId, 74 File publicationDirectory, 75 File buildFile, 76 String target, 77 Map arguments, 78 Map properties, 79 File logFile) 80 throws ExecutionException { 81 Project project = new Project(); 82 project.setCoreLoader(getClass().getClassLoader()); 83 84 Throwable error = null; 85 86 try { 87 File 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 keys = properties.keySet().iterator(); 116 keys.hasNext(); 117 ) { 118 String key = (String )keys.next(); 119 project.setUserProperty(key, (String )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 143 protected File getDefaultLogFile(File publicationDirectory) { 144 Calendar now = new GregorianCalendar (); 145 146 return new File ( 147 publicationDirectory, 148 LOG_PATH + dateFormat.format(now.getTime()) + ".xml"); 149 } 150 151 public static final DateFormat dateFormat = 152 new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss-SSS"); 153 public static final String PUBLICATION_DIRECTORY = "pub.dir"; 154 public static final String PUBLICATION_ID = "pub.id"; 155 public static final String SERVLET_CONTEXT_PATH = "servlet.context"; 156 public static final String CONTEXT_PREFIX = "context.prefix"; 157 public static final String BUILDFILE = "buildfile"; 158 public static final String TARGET = "target"; 159 public static final String ANT_PREFIX = "ant"; 160 public static final String PROPERTIES_PREFIX = "properties"; 161 public static final String DEFAULT_BUILDFILE = "config/tasks/targets.xml"; 162 public static final String LOG_PATH = 163 "logs/tasks/".replace('/', File.separatorChar); 164 public static final String PARAMETER_LOGFILE = "logfile"; 165 166 173 public void execute(String servletContextPath) throws ExecutionException { 174 String publicationId; 175 File publicationDirectory; 176 String contextPrefix; 177 File buildFile; 178 String target; 179 Map arguments; 180 Map properties; 181 File logFile; 182 183 try { 184 String 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 ("."); 198 buildFile = new File (buildFileName); 199 } else { 200 PublishingEnvironment environment = 201 new PublishingEnvironment( 202 servletContextPath, 203 publicationId); 204 publicationDirectory = environment.getPublicationDirectory(); 205 buildFile = new File (publicationDirectory, buildFileName); 206 } 207 208 target = getParameters().getParameter(TARGET, null); 209 210 Map 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 String logFilename = 221 getParameters().getParameter( 222 PARAMETER_LOGFILE, 223 getDefaultLogFile(publicationDirectory).getAbsolutePath()); 224 logFile = new File (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 |