in jelly-tags/threads/src/main/java/org/apache/commons/jelly/tags/threads/ThreadTag.java [71:156]
public void doTag(final XMLOutput output) throws JellyTagException {
if (xmlOutput == null) {
// lets default to system.out
try {
xmlOutput = XMLOutput.createXMLOutput(System.out);
}
catch (UnsupportedEncodingException e) {
throw new JellyTagException(e);
}
}
// lets create a child context
final JellyContext useThisContext = newContext ? context.newJellyContext() : context;
// set the target to run
thread.setTarget(new Runnable() {
@Override
public void run() {
try {
getBody().run(useThisContext, xmlOutput);
if (closeOutput) {
xmlOutput.close();
}
else {
xmlOutput.flush();
}
}
catch (JellyTagException e) {
// jelly wraps the exceptions thrown
Throwable subException = e.getCause();
if (subException != null) {
if (subException instanceof TimeoutException) {
throw (TimeoutException)subException;
} else if (subException instanceof RequirementException) {
throw (RequirementException)subException;
}
}
log.error(e);
// wrap the exception with a RuntimeException
throw new NestedRuntimeException(e);
}
catch (Exception e) {
log.error(e);
// wrap the exception with a RuntimeException
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
else {
throw new NestedRuntimeException(e);
}
}
}
});
// set the threads priority
thread.setPriority(priority);
// set the threads name
if (name != null) {
thread.setName(name);
} else {
thread.setName("Jelly Thread #" + (threadNumber++));
}
// set whether this thread is a daemon thread
thread.setDaemon(daemon);
// save the thread in a context variable
if (var != null) {
context.setVariable(var, thread);
}
// check if this tag is nested inside a group tag. if so
// add this thread to the thread group but do not start it.
// all threads in a thread group should start together.
GroupTag gt = (GroupTag) findAncestorWithClass(GroupTag.class);
if (gt != null) {
gt.addThread(thread);
} else {
// start the thread
thread.start();
}
}