1 package org.oddjob.ant; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.OutputStream ; 8 import java.io.PrintStream ; 9 import java.io.StringReader ; 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import org.apache.tools.ant.BuildEvent; 17 import org.apache.tools.ant.BuildException; 18 import org.apache.tools.ant.BuildListener; 19 import org.apache.tools.ant.BuildLogger; 20 import org.apache.tools.ant.DefaultLogger; 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.ProjectComponent; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.util.StringUtils; 25 import org.oddjob.OddjobException; 26 import org.oddjob.arooa.ArooaContext; 27 import org.oddjob.arooa.ArooaHandler; 28 import org.oddjob.framework.SimpleJob; 29 import org.oddjob.util.OddjobConfigException; 30 31 54 55 public class AntJob extends SimpleJob { 56 static final long serialVersionUID = 20050202; 57 58 private static Map messageLevels = new HashMap (); 59 60 static { 61 messageLevels.put("DEBUG", new Integer (Project.MSG_DEBUG)); 62 messageLevels.put("ERR", new Integer (Project.MSG_ERR)); 63 messageLevels.put("INFO", new Integer (Project.MSG_INFO)); 64 messageLevels.put("VERBOSE", new Integer (Project.MSG_VERBOSE)); 65 messageLevels.put("WARN", new Integer (Project.MSG_WARN)); 66 } 67 68 75 private transient Project project; 76 77 private transient List tasks = new ArrayList (); 78 79 85 private transient String messageLevel; 86 87 88 private transient OutputStream output; 89 90 93 public ArooaHandler handlerFor(ArooaContext context) { 94 return new AntJobComponentHandler(getProject()); 95 } 96 97 102 public void addComponent(ProjectComponent component) { 103 tasks.add(component); 104 } 105 106 public void setOutput(OutputStream out) { 107 this.output = out; 108 } 109 114 public OutputStream getOutput() { 115 return output; 116 } 117 118 123 public Project getProject() { 124 if (project == null) { 125 arooaRuntime().configure(); 126 } 127 if (project == null) { 128 project = new Project(); 129 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 130 if (cl != null) { 131 logger().debug("Setting classloader to: " + cl); 132 project.setCoreLoader(cl); 133 } 134 project.init(); 135 } 136 return project; 137 } 138 139 146 public void setProject(Project project) throws OddjobException { 147 if (this.project != null) { 148 return; 149 } 150 this.project = project; 151 } 152 153 public void setMessageLevel(String messageLevel) { 154 messageLevel = messageLevel.toUpperCase(); 155 if (messageLevels.get(messageLevel) == null) { 156 throw new OddjobConfigException("Message level of [" + messageLevel + "] not known."); 157 } 158 this.messageLevel = messageLevel; 159 } 160 161 public String getMessageLevel() { 162 return messageLevel; 163 } 164 165 int messageLevel() { 166 if (messageLevel == null) { 167 return Project.MSG_INFO; 168 } 169 return ((Integer )messageLevels.get(messageLevel)).intValue(); 170 } 171 172 175 protected int execute() throws Exception { 176 177 BuildLogger logger = new DefaultLogger(); 178 if (output != null) { 179 logger.setOutputPrintStream(new PrintStream (output)); 180 logger.setErrorPrintStream(new PrintStream (output)); 181 logger.setMessageOutputLevel(messageLevel()); 182 project.addBuildListener(logger); 183 } 184 DebugBuildListener debug = new DebugBuildListener(); 185 debug.setMessageOutputLevel(messageLevel()); 186 project.addBuildListener(debug); 187 try { 188 for (Iterator it = tasks.iterator(); it.hasNext();) { 189 ProjectComponent pc = (ProjectComponent)it.next(); 190 if (pc instanceof Task) { 191 Task task = (Task)pc; 192 logger().debug("Starting task [" + task.getTaskName() + "]"); 193 task.execute(); 194 } 195 } 196 return 0; 197 } 198 catch (BuildException e) { 199 logger().debug("Build excpetion.", e); 200 return 1; 201 } 202 finally { 203 if (output != null) { 204 project.removeBuildListener(logger); 205 } 206 project.removeBuildListener(debug); 207 } 208 } 209 210 213 public void onDestroy() { 214 tasks.clear(); 215 } 216 217 private void writeObject(ObjectOutputStream s) 218 throws IOException { 219 s.defaultWriteObject(); 220 } 221 222 private void readObject(ObjectInputStream s) 223 throws IOException , ClassNotFoundException { 224 s.defaultReadObject(); 225 tasks = new ArrayList (); 226 } 227 228 class DebugBuildListener implements BuildListener { 229 233 public static final int LEFT_COLUMN_SIZE = 12; 234 235 236 protected int msgOutputLevel = Project.MSG_ERR; 237 238 239 protected final String lSep = StringUtils.LINE_SEP; 240 241 257 public void setMessageOutputLevel(int level) { 258 this.msgOutputLevel = level; 259 } 260 261 266 public void buildStarted(BuildEvent event) { 267 } 268 269 274 public void buildFinished(BuildEvent event) { 275 } 276 277 282 public void targetStarted(BuildEvent event) { 283 } 284 285 290 public void targetFinished(BuildEvent event) { 291 } 292 293 298 public void taskStarted(BuildEvent event) { 299 } 300 301 306 public void taskFinished(BuildEvent event) { 307 } 308 309 317 public void messageLogged(BuildEvent event) { 318 int priority = event.getPriority(); 319 if (priority <= msgOutputLevel) { 321 322 StringBuffer message = new StringBuffer (); 323 if (event.getTask() != null) { 324 String name = event.getTask().getTaskName(); 326 String label = "[" + name + "] "; 327 int size = LEFT_COLUMN_SIZE - label.length(); 328 StringBuffer tmp = new StringBuffer (); 329 for (int i = 0; i < size; i++) { 330 tmp.append(" "); 331 } 332 tmp.append(label); 333 label = tmp.toString(); 334 335 try { 336 BufferedReader r = 337 new BufferedReader ( 338 new StringReader (event.getMessage())); 339 String line = r.readLine(); 340 boolean first = true; 341 while (line != null) { 342 if (!first) { 343 message.append(StringUtils.LINE_SEP); 344 } 345 first = false; 346 message.append(label).append(line); 347 line = r.readLine(); 348 } 349 } catch (IOException e) { 350 message.append(label).append(event.getMessage()); 352 } 353 } else { 354 message.append(event.getMessage()); 355 } 356 357 String msg = message.toString(); 358 logger().debug("(Message) " + msg); 359 } 360 } 361 362 } 363 } 364 | Popular Tags |