1 16 package org.apache.commons.jelly.tags.core; 17 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.UnsupportedEncodingException ; 21 22 import org.apache.commons.jelly.JellyContext; 23 import org.apache.commons.jelly.JellyTagException; 24 import org.apache.commons.jelly.TagSupport; 25 import org.apache.commons.jelly.XMLOutput; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 33 public class ThreadTag extends TagSupport { 34 35 private String name = null; 36 37 private XMLOutput xmlOutput; 38 39 private boolean closeOutput; 40 41 private static final Log log = LogFactory.getLog(ThreadTag.class); 42 43 public ThreadTag() { 44 } 45 46 public void doTag(final XMLOutput output) throws JellyTagException { 49 if ( xmlOutput == null ) { 50 try { 52 xmlOutput = XMLOutput.createXMLOutput( System.out ); 53 } catch (UnsupportedEncodingException e) { 54 throw new JellyTagException(e); 55 } 56 } 57 58 final JellyContext newContext = context.newJellyContext(); 60 61 Thread thread = new Thread ( 62 new Runnable () { 63 public void run() { 64 try { 65 getBody().run(newContext, xmlOutput); 66 if (closeOutput) { 67 xmlOutput.close(); 68 } 69 else { 70 xmlOutput.flush(); 71 } 72 } 73 catch (Exception e) { 74 if (log.isErrorEnabled()) log.error("error running thread tag", e); 75 } 76 } 77 } 78 ); 79 if ( name != null ) { 80 thread.setName( name ); 81 } 82 thread.start(); 83 } 84 85 89 public void setName(String name) { 90 this.name = name; 91 } 92 93 96 public void setXmlOutput(XMLOutput xmlOutput) { 97 this.closeOutput = false; 98 this.xmlOutput = xmlOutput; 99 } 100 101 105 public void setFile(String name) throws IOException { 106 this.closeOutput = true; 107 setXmlOutput( XMLOutput.createXMLOutput(new FileOutputStream (name)) ); 108 } 109 } 110 | Popular Tags |