1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.myfaces.orchestra.frameworkAdapter.local;
24
25 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
26 import org.springframework.context.ApplicationContextAware;
27 import org.springframework.context.ConfigurableApplicationContext;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.beans.BeansException;
30
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 /***
36 * A FrameworkAdapter which uses local maps to simulate a servlet environment.
37 * <p>
38 * This is intended when Orchestra functionality is desired outside of any real
39 * request/response type system. One example is where a job scheduler (eg quartz)
40 * is used to execute background threads that want to execute the same beans that
41 * can also be used. In this case:
42 * <ol>
43 * <li>Create a new Thread object
44 * <li>Create a new LocalFrameworkAdapter instance
45 * <li>Within the new thread, call FrameworkAdapter.setInstance.
46 * </ol>
47 * When Orchestra methods are invoked from within the new thread, calls made to the
48 * framework adapter will be handled by the created LocalFrameworkAdapter instance,
49 * and will not interact with the user session, the original request or response.
50 * Because the session is new, no conversations will be inherited from the environment
51 * in which that new thread was created.
52 * <p>
53 * This class is not expected to be used very often.
54 * <p>
55 * Note that this adapter relies on Spring and thus you have to ensure
56 * {@link #setApplicationContext} is called.
57 * <p>
58 * Note also that because this is intended for use only without a real request
59 * and response, there is no corresponding Filter class.
60 * <p>
61 * This class does not override the inherited createConversationMessager method, so
62 * it is mandatory for anyone using this class to explicitly call method
63 * setConversationMessager before using an instance of this adapter.
64 */
65 public class LocalFrameworkAdapter extends FrameworkAdapter implements ApplicationContextAware
66 {
67 private ConfigurableApplicationContext configurableApplicationContext;
68
69 private final Map sessionMap = new HashMap();
70 private final Map requestMap = new HashMap();
71 private final Map requestParameterMap = new HashMap();
72 private final Map initMap = new HashMap();
73
74 public String getInitParameter(String key)
75 {
76 return (String) initMap.get(key);
77 }
78
79 public Object getRequestParameterAttribute(String key)
80 {
81 return requestParameterMap.get(key);
82 }
83
84 public boolean containsRequestParameterAttribute(String key)
85 {
86 return requestParameterMap.containsKey(key);
87 }
88
89 public void setRequestParameterAttribute(String key , Object value)
90 {
91 requestParameterMap.put(key, value);
92 }
93
94 public Object getRequestAttribute(String key)
95 {
96 return requestMap.get(key);
97 }
98
99 public void setRequestAttribute(String key, Object value)
100 {
101 requestMap.put(key, value);
102 }
103
104 public void clearRequestMap()
105 {
106 requestMap.clear();
107 }
108
109 public boolean containsRequestAttribute(String key)
110 {
111 return requestMap.containsKey(key);
112 }
113
114 public Object getSessionAttribute(String key)
115 {
116 return sessionMap.get(key);
117 }
118
119 public void setSessionAttribute(String key, Object value)
120 {
121 sessionMap.put(key, value);
122 }
123
124 public boolean containsSessionAttribute(String key)
125 {
126 return sessionMap.containsKey(key);
127 }
128
129 protected ConfigurableApplicationContext getApplicationContext()
130 {
131 return configurableApplicationContext;
132 }
133
134 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
135 {
136 setApplicationContext((ConfigurableApplicationContext) applicationContext);
137 }
138
139 public void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext)
140 {
141 this.configurableApplicationContext = configurableApplicationContext;
142 }
143
144 public void redirect(String url) throws IOException
145 {
146 }
147
148 public Object getBean(String name)
149 {
150 if (!getApplicationContext().containsBean(name))
151 {
152 return null;
153 }
154 return getApplicationContext().getBean(name);
155 }
156
157 public void invokeNavigation(String navigationName)
158 {
159 }
160 }