KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > registry > ExpressoThread


1 package com.jcorporate.expresso.core.registry;
2
3 /**
4  * Provides a thread with an Expresso Thread Context already set for you.
5  * <p>If your architecture allows you to do it, then deriving from
6  * <tt>ExpressoThread</tt> instead of <tt>Thread</tt> allows you to have
7  * a predefined environment with a build RequestRegistry.
8  * <p>You create an ExpressoThread which takes a snapshot of the credentials
9  * of the thread in which it was created. Then when it runs() the
10  * original credentials are applied to the new thread.</p>
11  * <p><b>Note:</b> To get the benefits of this class, you must call
12  * super.run() inside your own run method before proceeding.</p>
13  * <p>If you are using a <tt>Runnable<tt> interface, you may use the code:
14  * <code><pre>
15  * ExpressoThread et = new ExpressoThread(myRunnableInstance);
16  * et.start();
17  * </pre></code>
18  * which will get the RequestRegistry set before the runnable instance begins.
19  * </p>
20  *
21  * @author Michael Rimov
22  */

23 public class ExpressoThread
24         extends Thread JavaDoc {
25     /**
26      * The actual context that gets transferred from one thread to another.
27      */

28     private final ExpressoThreadContext context;
29
30     /**
31      * Default constructor.
32      */

33     public ExpressoThread() {
34         super();
35         context = new ExpressoThreadContext();
36     }
37
38     /**
39      * Protected getter to allow for future extension.
40      *
41      * @return ExpressoThreadContext the thread context created in
42      * the constructor.
43      */

44     protected ExpressoThreadContext getThreadContext() {
45         return context;
46     }
47
48     /**
49      * Constructor.
50      * See {@link Thread#Thread(Runnable)}
51      *
52      * @param target Runnable
53      */

54     public ExpressoThread(Runnable JavaDoc target) {
55         super(target);
56         context = new ExpressoThreadContext();
57     }
58
59     /**
60      * Constructor.
61      * See {@link Thread#Thread(String)}
62      *
63      * @param name Runnable
64      */

65     public ExpressoThread(String JavaDoc name) {
66         super(name);
67         context = new ExpressoThreadContext();
68     }
69
70     /**
71      * Constructor.
72      * See {@link Thread#Thread(String)}
73      *
74      * @param group Parent ThreadGroup
75      * @param target Runnable
76      */

77     public ExpressoThread(ThreadGroup JavaDoc group, Runnable JavaDoc target) {
78         super(group, target);
79         context = new ExpressoThreadContext();
80     }
81
82     /**
83      * Constructor.
84      * See {@link Thread#Thread(Runnable,String)}
85      *
86      * @param name the Thread Name
87      * @param target Runnable the target to execute.
88      */

89     public ExpressoThread(Runnable JavaDoc target, String JavaDoc name) {
90         super(target, name);
91         context = new ExpressoThreadContext();
92     }
93
94     /**
95      * Constructor.
96      * See {@link Thread#Thread(ThreadGroup,String)}
97      *
98      * @param group Parent ThreadGroup
99      * @param name the Thread Name
100      */

101     public ExpressoThread(ThreadGroup JavaDoc group, String JavaDoc name) {
102         super(group, name);
103         context = new ExpressoThreadContext();
104     }
105
106     /**
107      * Constructor.
108      * See {@link Thread#Thread(ThreadGroup,Runnable,String)}
109      *
110      * @param group Parent ThreadGroup
111      * @param target Runnable the target to execute.
112      * @param name the Thread Name
113      */

114     public ExpressoThread(ThreadGroup JavaDoc group, Runnable JavaDoc target, String JavaDoc 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 JavaDoc group, Runnable JavaDoc target, String JavaDoc name,
129                           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 }
142
Popular Tags