KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > tags > core > ThreadTag


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.jelly.tags.core;
17
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.UnsupportedEncodingException JavaDoc;
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 /** A tag that spawns the contained script in a separate thread
30   *
31   * @author <a HREF="mailto:vinayc@apache.org">Vinay Chandran</a>
32   */

33 public class ThreadTag extends TagSupport {
34     /** Thread Name */
35     private String JavaDoc name = null;
36     /** the destination of output */
37     private XMLOutput xmlOutput;
38     /** Should we close the underlying output */
39     private boolean closeOutput;
40     /** The Log to which logging calls will be made. */
41     private static final Log log = LogFactory.getLog(ThreadTag.class);
42
43     public ThreadTag() {
44     }
45
46     // Tag interface
47
//-------------------------------------------------------------------------
48
public void doTag(final XMLOutput output) throws JellyTagException {
49         if ( xmlOutput == null ) {
50             // lets default to system.out
51
try {
52                 xmlOutput = XMLOutput.createXMLOutput( System.out );
53             } catch (UnsupportedEncodingException JavaDoc e) {
54                 throw new JellyTagException(e);
55             }
56         }
57
58         // lets create a child context
59
final JellyContext newContext = context.newJellyContext();
60
61         Thread JavaDoc thread = new Thread JavaDoc(
62             new Runnable JavaDoc() {
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 JavaDoc 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     /**
86      * Sets the name of the thread.
87      * @param name The name to set
88      */

89     public void setName(String JavaDoc name) {
90         this.name = name;
91     }
92
93     /**
94      * Sets the destination of output
95      */

96     public void setXmlOutput(XMLOutput xmlOutput) {
97         this.closeOutput = false;
98         this.xmlOutput = xmlOutput;
99     }
100
101     /**
102      * Set the file which is generated from the output
103      * @param name The output file name
104      */

105     public void setFile(String JavaDoc name) throws IOException JavaDoc {
106         this.closeOutput = true;
107         setXmlOutput( XMLOutput.createXMLOutput(new FileOutputStream JavaDoc(name)) );
108     }
109 }
110
Popular Tags