001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.core;
017:
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.UnsupportedEncodingException;
021:
022: import org.apache.commons.jelly.JellyContext;
023: import org.apache.commons.jelly.JellyTagException;
024: import org.apache.commons.jelly.TagSupport;
025: import org.apache.commons.jelly.XMLOutput;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /** A tag that spawns the contained script in a separate thread
030: *
031: * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
032: */
033: public class ThreadTag extends TagSupport {
034: /** Thread Name */
035: private String name = null;
036: /** the destination of output */
037: private XMLOutput xmlOutput;
038: /** Should we close the underlying output */
039: private boolean closeOutput;
040: /** The Log to which logging calls will be made. */
041: private static final Log log = LogFactory.getLog(ThreadTag.class);
042:
043: public ThreadTag() {
044: }
045:
046: // Tag interface
047: //-------------------------------------------------------------------------
048: public void doTag(final XMLOutput output) throws JellyTagException {
049: if (xmlOutput == null) {
050: // lets default to system.out
051: try {
052: xmlOutput = XMLOutput.createXMLOutput(System.out);
053: } catch (UnsupportedEncodingException e) {
054: throw new JellyTagException(e);
055: }
056: }
057:
058: // lets create a child context
059: final JellyContext newContext = context.newJellyContext();
060:
061: Thread thread = new Thread(new Runnable() {
062: public void run() {
063: try {
064: getBody().run(newContext, xmlOutput);
065: if (closeOutput) {
066: xmlOutput.close();
067: } else {
068: xmlOutput.flush();
069: }
070: } catch (Exception e) {
071: if (log.isErrorEnabled())
072: log.error("error running thread tag", e);
073: }
074: }
075: });
076: if (name != null) {
077: thread.setName(name);
078: }
079: thread.start();
080: }
081:
082: /**
083: * Sets the name of the thread.
084: * @param name The name to set
085: */
086: public void setName(String name) {
087: this .name = name;
088: }
089:
090: /**
091: * Sets the destination of output
092: */
093: public void setXmlOutput(XMLOutput xmlOutput) {
094: this .closeOutput = false;
095: this .xmlOutput = xmlOutput;
096: }
097:
098: /**
099: * Set the file which is generated from the output
100: * @param name The output file name
101: */
102: public void setFile(String name) throws IOException {
103: this .closeOutput = true;
104: setXmlOutput(XMLOutput.createXMLOutput(new FileOutputStream(
105: name)));
106: }
107: }
|