%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
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; |
|
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 | /** 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 | 38 | public class ThreadTag extends TagSupport { |
34 | /** Thread Name */ |
|
35 | 0 | private String 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 | 38 | private static final Log log = LogFactory.getLog(ThreadTag.class); |
42 | ||
43 | 0 | public ThreadTag() { |
44 | 0 | } |
45 | ||
46 | // Tag interface |
|
47 | //------------------------------------------------------------------------- |
|
48 | public void doTag(final XMLOutput output) throws JellyTagException { |
|
49 | 0 | if ( xmlOutput == null ) { |
50 | // lets default to system.out |
|
51 | try { |
|
52 | 0 | xmlOutput = XMLOutput.createXMLOutput( System.out ); |
53 | 0 | } catch (UnsupportedEncodingException e) { |
54 | 0 | throw new JellyTagException(e); |
55 | } |
|
56 | } |
|
57 | ||
58 | // lets create a child context |
|
59 | 0 | final JellyContext newContext = context.newJellyContext(); |
60 | ||
61 | 0 | 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 | 0 | if ( name != null ) { |
80 | 0 | thread.setName( name ); |
81 | } |
|
82 | 0 | thread.start(); |
83 | 0 | } |
84 | ||
85 | /** |
|
86 | * Sets the name of the thread. |
|
87 | * @param name The name to set |
|
88 | */ |
|
89 | public void setName(String name) { |
|
90 | 0 | this.name = name; |
91 | 0 | } |
92 | ||
93 | /** |
|
94 | * Sets the destination of output |
|
95 | */ |
|
96 | public void setXmlOutput(XMLOutput xmlOutput) { |
|
97 | 0 | this.closeOutput = false; |
98 | 0 | this.xmlOutput = xmlOutput; |
99 | 0 | } |
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 name) throws IOException { |
|
106 | 0 | this.closeOutput = true; |
107 | 0 | setXmlOutput( XMLOutput.createXMLOutput(new FileOutputStream(name)) ); |
108 | 0 | } |
109 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |