KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.jcorporate.expresso.core.registry;
65
66 import com.jcorporate.expresso.core.security.User;
67
68 import java.util.Stack JavaDoc;
69
70 /**
71  * The request registry provides several useful utility functions that
72  * allow isolation of Expresso specifics away from the domain model where
73  * they don't belong. To use it, use the static methods, which in turn
74  * return threadlocal data.
75  * <p>Example Usage:</p>
76  * <p/>
77  * <code><pre>
78  * String dataContext = RequestRegistry.getDataContext();
79  * </pre></code>
80  * </p>
81  * <p>The pattern for this class is originally defined by Martin Fowler
82  * in his book <em>Patterns of Enterprise Application Architecture</em>
83  * as the registry pattern. Supplemental information for the
84  * casual reader can be found at:
85  * <a HREF="http://www.martinfowler.com/eaaCatalog/registry.html">
86  * http://www.martinfowler.com/eaaCatalog/registry.html</a>
87  * </p>
88  * <p/>
89  * In the concept of Interface vs Concrete Class. In this case, the
90  * static methods for this class are the interface to the class. Actual
91  * member manipulation is done in subclasses to the RequestRegistry.
92  * </p>
93  * <p>Dependencies: Note that ExpressoThreadContext depends heavily on
94  * the RequestRegistry. If you change the interface of this class by
95  * adding new members, you MUST change the ExpressoThreadContext
96  * and MutableRequestRegistry as well.</p>
97  *
98  * @author Michael Rimov
99  * @version 1.0
100  */

101 abstract public class RequestRegistry {
102
103     /**
104      * The data context for the Expresso request.
105      */

106     protected String JavaDoc dataContext = null;
107
108     /**
109      * The security context for the Expresso request.
110      */

111     protected User user = null;
112
113
114     /**
115      * A stack for allowing credential changes mid-request. Use superUser()
116      * and revertUser() to push and pop users off the stack, much the
117      * way that unix <code>su</code> performs.
118      */

119     private Stack JavaDoc superUserStack = null;
120
121     /**
122      * The threadlocal instance for this class.
123      */

124     static private ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc();
125
126     /**
127      * Default constructor. Adds this class to the threadlocal context.
128      */

129     protected RequestRegistry() {
130         synchronized (threadLocal) {
131             threadLocal.set(this);
132         }
133     }
134
135     /**
136      * Retrieves the current thread's data context.
137      *
138      * @return String java.lang.String
139      * @throws java.lang.IllegalStateException
140      * if this method is called
141      * before anybody constructed it.
142      */

143     static public String JavaDoc getDataContext() {
144         return getInstance().dataContext;
145     }
146
147     /**
148      * Method that gets the actual registry threadlocal instance and makes
149      * sure that it has been built prior to getting.
150      *
151      * @return RequestRegistry the registry instance for this thread.
152      * @throws java.lang.IllegalStateException
153      * if this method is called
154      * before anybody constructed it.
155      */

156     protected static RequestRegistry getInstance() throws IllegalStateException JavaDoc {
157         RequestRegistry requestRegistry = (RequestRegistry) threadLocal.get();
158         if (requestRegistry == null) {
159             throw new IllegalStateException JavaDoc("No request registry has been associated with this thread yet."
160                     + " If you are running inside a servlet engine, please add "
161                     + "RequestRegistryFilter as a servlet filter within web.xml. See the " +
162                     " web.xml in the Expresso source tree as an example.");
163         }
164         return requestRegistry;
165     }
166
167     /**
168      * Retrieves the current thread's user.
169      *
170      * @return User instance
171      * @throws java.lang.IllegalStateException
172      * if this method is called
173      * before anybody constructed it.
174      */

175     static public User getUser() {
176         return getInstance().user;
177     }
178
179     /**
180      * Changes user credentials mid-thread request. It allows for
181      * <code>su</code>-like abilities. The old user context is stored
182      * in a stack that may be reverted by calling {@link #revertUser}.
183      * restrict who can su based on a java security policy.
184      *
185      * @param newUser User the user to change thread credentials to.
186      */

187     static public void superUser(User newUser) {
188         RequestRegistry requestRegistry = getInstance();
189         Stack JavaDoc userStack = requestRegistry.getUserStack();
190         userStack.push(requestRegistry.user);
191         requestRegistry.user = newUser;
192     }
193
194     /**
195      * Retrieves the user stack, constructing a new one if necessary.
196      *
197      * @return Stack the user stack.
198      */

199     protected Stack JavaDoc getUserStack() {
200         if (superUserStack == null) {
201             superUserStack = new Stack JavaDoc();
202         }
203
204         return superUserStack;
205     }
206
207     /**
208      * Reverts the user credentials
209      *
210      * @return the now current User.
211      * @throws java.lang.ArrayIndexOutOfBoundsException
212      * if the stack
213      * was already empty.... <em>ie</em> you did not have matching superUser()
214      * to revertUser() calls.
215      */

216     static public User revertUser() {
217         RequestRegistry requestRegistry = getInstance();
218         Stack JavaDoc userStack = requestRegistry.getUserStack();
219         if (userStack.isEmpty()) {
220             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc("Stack is empty. " +
221                     "You have mismatching superUser() and revertUser() calls");
222         }
223
224         requestRegistry.user = (User) userStack.pop();
225         return requestRegistry.user;
226
227     }
228
229     /**
230      * Releases all data associated with this registry.
231      */

232     protected void releaseSettings() {
233         superUserStack = null;
234         user = null;
235         dataContext = null;
236     }
237
238
239 }
240
Popular Tags