01: /*
02: * Created on 07-May-2006
03: */
04: package uk.org.ponder.rsac;
05:
06: import org.springframework.context.ApplicationContext;
07:
08: /** Provides a global point of access to application-scope beans, bound to the
09: * current thread. This is for use <b>only in extreme emergencies</b> - it
10: * represents a terrible architectural risk. (In RSF it is used to allow
11: * ViewParameters, which are tiny, uniquitous objects, to clone themselves).
12: * Bean names should never ordinarily occur in Java code!
13: * @author Antranig Basman (amb26@ponder.org.uk)
14: *
15: */
16:
17: public class GlobalBeanAccessor {
18: private static ThreadLocal contexts = new ThreadLocal();
19:
20: /** Returns the (application-scope) bean with the given name, bound to
21: * the context for the current thread. Only use this method in a genuine
22: * emergency!
23: */
24: public static Object getBean(String name) {
25: return ((ApplicationContext) contexts.get()).getBean(name);
26: }
27:
28: /** Returns the request-scope bean with the given name, bound to the
29: * standard RSAC locator within the application context bound to the
30: * current thread. Only use this method in a genuine emergency!
31: */
32: public static Object getRequestBean(String name) {
33: RSACBeanLocator rsacbl = (RSACBeanLocator) getBean("RSACBeanLocator");
34: return rsacbl.getBeanLocator().locateBean(name);
35: }
36:
37: public static void startRequest(ApplicationContext context) {
38: contexts.set(context);
39: }
40:
41: public static void endRequest() {
42: contexts.set(null);
43: }
44: }
|