1 16 package org.apache.cocoon; 17 18 import java.io.File ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.apache.tools.ant.AntClassLoader; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.DynamicConfigurator; 29 import org.apache.tools.ant.ExitException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.types.CommandlineJava; 33 import org.apache.tools.ant.types.Path; 34 import org.apache.tools.ant.types.Reference; 35 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NamedNodeMap ; 39 import org.w3c.dom.Node ; 40 41 47 public class CocoonTask extends Task implements DynamicConfigurator { 48 49 private CommandlineJava cmdl = new CommandlineJava(); 50 private boolean failOnError = false; 51 private Throwable caught = null; 52 53 private String uriGroup = null; 54 private Document xconf; 55 private Element root; 56 private ElementWrapper _wrapper; 57 58 private static final String CLASS_DELEGATE = "org.apache.cocoon.bean.helpers.AntDelegate"; 59 60 public CocoonTask() { 61 try { 62 DocumentBuilder builder = 63 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 64 xconf = builder.newDocument(); 65 root = xconf.createElement("cocoon"); 66 xconf.appendChild(root); 67 _wrapper = new ElementWrapper(root); 68 cmdl.setClassname(CLASS_DELEGATE); 69 } 70 catch (ParserConfigurationException e) { 71 throw new BuildException(e); 72 } 73 } 74 75 80 public Path createClasspath() { 81 return cmdl.createClasspath(getProject()).createPath(); 82 } 83 84 89 public void setClasspathRef(Reference r) { 90 createClasspath().setRefid(r); 91 } 92 93 98 public void setClasspath(Path s) { 99 createClasspath().append(s); 100 } 101 102 public void setUrigroup(String group) { 103 this.uriGroup = group; 104 } 105 106 109 private static class ElementWrapper 110 implements DynamicConfigurator { 111 112 private Node node; 113 114 115 private ElementWrapper(Node node) { 116 this.node = node; 117 } 118 119 120 private ElementWrapper(Node parent, String childName) { 121 Document document = parent.getOwnerDocument(); 122 if (document == null) { 123 document = (Document )parent; } 125 node = document.createElement(childName); 126 parent.appendChild(node); 127 } 128 129 public void setDynamicAttribute(String name, String value) 132 throws BuildException { 133 Element element = (Element )node; 135 element.setAttribute(name, value); 136 } 137 138 public Object createDynamicElement(String name) 139 throws BuildException { 140 return new ElementWrapper(node, name); 141 } 142 } 143 144 public File getLibDir() throws BuildException { 145 Element root = xconf.getDocumentElement(); 146 String contextDir = null; 147 if (root!=null) { 148 if (hasAttribute(root, "context-dir")){ 149 contextDir = getAttributeValue(root, "context-dir"); 150 } 151 } 152 if (contextDir != null) { 153 return new File (contextDir + "/WEB-INF/lib"); 154 } else { 155 throw new BuildException("No context directory specified. Cannot find Cocoon"); 156 } 157 } 158 159 private static String getAttributeValue(Node node, String attr) throws IllegalArgumentException { 160 NamedNodeMap nodes = node.getAttributes(); 161 if (nodes != null) { 162 Node attribute = nodes.getNamedItem(attr); 163 if (attribute != null && attribute.getNodeValue() != null) { 164 return attribute.getNodeValue(); 165 } 166 } 167 throw new IllegalArgumentException ("Missing " + attr + " attribute on <" + node.getNodeName() + "> node"); 168 } 169 170 private static boolean hasAttribute(Node node, String attr) { 171 NamedNodeMap nodes = node.getAttributes(); 172 if (nodes != null) { 173 Node attribute = nodes.getNamedItem(attr); 174 return (attribute != null); 175 } 176 return false; 177 } 178 179 public void setDynamicAttribute(String name, String value) 182 throws BuildException { 183 root.setAttribute(name, value); 184 } 185 186 public Object createDynamicElement(String name) 187 throws BuildException { 188 return _wrapper.createDynamicElement(name); 189 } 190 191 199 public void execute() throws BuildException { 200 int err= -1; 202 203 if (cmdl.getClasspath() == null) { 204 throw new BuildException("Could not find a classpath that points to the Cocoon classes"); 205 } 206 try { 207 try { 208 execute(cmdl); 209 err = 0; 210 } catch (ExitException ex) { 211 err = ex.getStatus(); 212 } 213 } catch (BuildException e) { 214 if (failOnError) { 215 throw e; 216 } else { 217 log(e.getMessage(), Project.MSG_ERR); 218 err = 0; 219 } 220 } catch (Throwable t) { 221 if (failOnError) { 222 throw new BuildException(t); 223 } else { 224 log(t.getMessage(), Project.MSG_ERR); 225 err = 0; 226 } 227 } 228 } 229 230 public void execute(CommandlineJava command) throws BuildException { 231 final String classname = command.getJavaCommand().getExecutable(); 232 233 AntClassLoader loader = null; 234 try { 235 if (command.getSystemProperties() != null) { 236 command.getSystemProperties().setSystem(); 237 } 238 239 final Class [] param = {Class.forName("org.w3c.dom.Document"), Class.forName("java.lang.String")}; 240 Class target = null; 241 if (command.getClasspath() == null) { 242 target = Class.forName(classname); 243 } else { 244 loader = new AntClassLoader(getProject().getCoreLoader(), getProject(), 245 command.getClasspath(), false); 246 loader.setIsolated(true); 247 loader.setThreadContextLoader(); 248 target = loader.forceLoadClass(classname); 249 Class.forName(classname, true, loader); 250 } 251 Method method = target.getMethod("process", param); 252 if (method == null) { 253 throw new BuildException("Could not find process() method in " 254 + classname); 255 } 256 257 run(method); 258 259 if (caught != null) { 260 throw caught; 261 } 262 263 } catch (ClassNotFoundException e) { 264 throw new BuildException("Could not find " + classname + "." 265 + " Make sure you have it in your" 266 + " classpath"); 267 } catch (SecurityException e) { 268 throw e; 269 } catch (Throwable e) { 270 throw new BuildException(e); 271 } finally { 272 if (loader != null) { 273 loader.resetThreadContextLoader(); 274 loader.cleanup(); 275 } 276 if (command.getSystemProperties() != null) { 277 command.getSystemProperties().restoreSystem(); 278 } 279 } 280 } 281 282 public void run(Method method) { 283 final Object [] argument = {xconf, uriGroup}; 284 try { 285 method.invoke(null, argument); 286 } catch (InvocationTargetException e) { 287 Throwable t = e.getTargetException(); 288 if (!(t instanceof InterruptedException )) { 289 caught = t; 290 } 291 } catch (Throwable t) { 292 caught = t; 293 } finally { 294 synchronized (this) { 295 notifyAll(); 296 } 297 } 298 } 299 } 300 | Popular Tags |