KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
2 *
3 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The end-user documentation included with the redistribution,
18 * if any, must include the following acknowledgment:
19 * "This product includes software developed by Jcorporate Ltd.
20 * (http://www.jcorporate.com/)."
21 * Alternately, this acknowledgment may appear in the software itself,
22 * if and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. "Jcorporate" and product names such as "Expresso" must
25 * not be used to endorse or promote products derived from this
26 * software without prior written permission. For written permission,
27 * please contact info@jcorporate.com.
28 *
29 * 5. Products derived from this software may not be called "Expresso",
30 * or other Jcorporate product names; nor may "Expresso" or other
31 * Jcorporate product names appear in their name, without prior
32 * written permission of Jcorporate Ltd.
33 *
34 * 6. No product derived from this software may compete in the same
35 * market space, i.e. framework, without prior written permission
36 * of Jcorporate Ltd. For written permission, please contact
37 * partners@jcorporate.com.
38 *
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
45 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Jcorporate Ltd. Contributions back
55 * to the project(s) are encouraged when you make modifications.
56 * Please send them to support@jcorporate.com. For more information
57 * on Jcorporate Ltd. and its products, please see
58 * <http://www.jcorporate.com/>.
59 *
60 * Portions of this software are based upon other open source
61 * products and are subject to their respective licenses.
62  */

63
64
65 package com.jcorporate.expresso.core.registry;
66
67 import com.jcorporate.expresso.core.security.User;
68
69
70 /**
71  * Thread context is closely tied with the RequestRegistry. What it does
72  * is glean the RequestRegistry from one thread and allow that information
73  * to be passed to another.
74  * <p>Typical Usage:
75  * <code><pre>
76  * //Sets the internal members
77  * ExpressoThreadContext context = new ExpressoThreadContext();
78  * <p/>
79  * //Sample thread
80  * Thread myThread = new MyThread();
81  * myThread.setThreadContext(context);
82  * <p/>
83  * .....
84  * <p/>
85  * Then in 'MyThread'....
86  * <p/>
87  * void run() {
88  * context.applySettingsToCurrentThread();
89  * ....
90  * }
91  * </pre></code>
92  * </p>
93  * <p>If you change the API for the RequestRegistry by adding more items
94  * , then you need to change this class as well to allow transport of the
95  * request registry between threads.</p>
96  *
97  * @author Michael Rimov
98  */

99 public class ExpressoThreadContext {
100     /**
101      * The Security credentials of the user who's permissions are being
102      * used for this thread.
103      */

104     private User user;
105
106     /**
107      * The data context that is being used for defaults for this thread.
108      */

109     private String JavaDoc dataContext;
110
111     /**
112      * Default constructor.
113      */

114     public ExpressoThreadContext() {
115         super();
116         setDataContext(RequestRegistry.getDataContext());
117         setUser(RequestRegistry.getUser());
118     }
119
120
121     /**
122      * Takes the settings that were in the thread that this object
123      * resided in when it was created and applies it to the thread that
124      * it is currently running in.
125      */

126     public void applyToCurrentThread() {
127         new MutableRequestRegistry(getDataContext(), getUser());
128     }
129
130     /**
131      * Protected setter. Sets the user for the thread context.
132      *
133      * @param user User
134      */

135     protected void setUser(User user) {
136         this.user = user;
137     }
138
139     /**
140      * Sets the data context. Protected.
141      *
142      * @param dataContext String
143      */

144     protected void setDataContext(String JavaDoc dataContext) {
145         this.dataContext = dataContext;
146     }
147
148     /**
149      * Retreive the user.
150      *
151      * @return User
152      */

153     public User getUser() {
154         return user;
155     }
156
157     /**
158      * Retrieve the data context.
159      *
160      * @return String
161      */

162     public String JavaDoc getDataContext() {
163         return dataContext;
164     }
165 }
166
Popular Tags