001: package com.jcorporate.expresso.core.registry;
002:
003: /**
004: * Provides a thread with an Expresso Thread Context already set for you.
005: * <p>If your architecture allows you to do it, then deriving from
006: * <tt>ExpressoThread</tt> instead of <tt>Thread</tt> allows you to have
007: * a predefined environment with a build RequestRegistry.
008: * <p>You create an ExpressoThread which takes a snapshot of the credentials
009: * of the thread in which it was created. Then when it runs() the
010: * original credentials are applied to the new thread.</p>
011: * <p><b>Note:</b> To get the benefits of this class, you must call
012: * super.run() inside your own run method before proceeding.</p>
013: * <p>If you are using a <tt>Runnable<tt> interface, you may use the code:
014: * <code><pre>
015: * ExpressoThread et = new ExpressoThread(myRunnableInstance);
016: * et.start();
017: * </pre></code>
018: * which will get the RequestRegistry set before the runnable instance begins.
019: * </p>
020: *
021: * @author Michael Rimov
022: */
023: public class ExpressoThread extends Thread {
024: /**
025: * The actual context that gets transferred from one thread to another.
026: */
027: private final ExpressoThreadContext context;
028:
029: /**
030: * Default constructor.
031: */
032: public ExpressoThread() {
033: super ();
034: context = new ExpressoThreadContext();
035: }
036:
037: /**
038: * Protected getter to allow for future extension.
039: *
040: * @return ExpressoThreadContext the thread context created in
041: * the constructor.
042: */
043: protected ExpressoThreadContext getThreadContext() {
044: return context;
045: }
046:
047: /**
048: * Constructor.
049: * See {@link Thread#Thread(Runnable)}
050: *
051: * @param target Runnable
052: */
053: public ExpressoThread(Runnable target) {
054: super (target);
055: context = new ExpressoThreadContext();
056: }
057:
058: /**
059: * Constructor.
060: * See {@link Thread#Thread(String)}
061: *
062: * @param name Runnable
063: */
064: public ExpressoThread(String name) {
065: super (name);
066: context = new ExpressoThreadContext();
067: }
068:
069: /**
070: * Constructor.
071: * See {@link Thread#Thread(String)}
072: *
073: * @param group Parent ThreadGroup
074: * @param target Runnable
075: */
076: public ExpressoThread(ThreadGroup group, Runnable target) {
077: super (group, target);
078: context = new ExpressoThreadContext();
079: }
080:
081: /**
082: * Constructor.
083: * See {@link Thread#Thread(Runnable,String)}
084: *
085: * @param name the Thread Name
086: * @param target Runnable the target to execute.
087: */
088: public ExpressoThread(Runnable target, String name) {
089: super (target, name);
090: context = new ExpressoThreadContext();
091: }
092:
093: /**
094: * Constructor.
095: * See {@link Thread#Thread(ThreadGroup,String)}
096: *
097: * @param group Parent ThreadGroup
098: * @param name the Thread Name
099: */
100: public ExpressoThread(ThreadGroup group, String name) {
101: super (group, name);
102: context = new ExpressoThreadContext();
103: }
104:
105: /**
106: * Constructor.
107: * See {@link Thread#Thread(ThreadGroup,Runnable,String)}
108: *
109: * @param group Parent ThreadGroup
110: * @param target Runnable the target to execute.
111: * @param name the Thread Name
112: */
113: public ExpressoThread(ThreadGroup group, Runnable target,
114: String name) {
115: super (group, target, name);
116: context = new ExpressoThreadContext();
117: }
118:
119: /**
120: * Constructor.
121: * See {@link Thread#Thread(ThreadGroup,Runnable,String,long)}
122: *
123: * @param group Parent ThreadGroup
124: * @param target Runnable the target to execute.
125: * @param name the Thread Name
126: * @param stackSize the size of the stack
127: */
128: public ExpressoThread(ThreadGroup group, Runnable target,
129: String name, long stackSize) {
130: super (group, target, name, stackSize);
131: context = new ExpressoThreadContext();
132: }
133:
134: /**
135: * Call super.run() from your own threads running inside Expresso to
136: */
137: public void run() {
138: getThreadContext().applyToCurrentThread();
139: super.run();
140: }
141: }
|