1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.frameworkAdapter.jsf;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.myfaces.orchestra.conversation.ConversationMessager;
24 import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
25 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
26 import org.apache.myfaces.orchestra.frameworkAdapter._FrameworkAdapterUtils;
27 import org.apache.myfaces.orchestra.lib.OrchestraException;
28
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.context.FacesContext;
31
32 import java.io.IOException;
33
34
35
36
37
38
39 public class JsfFrameworkAdapter extends FrameworkAdapter
40 {
41 private final Log log = LogFactory.getLog(JsfFrameworkAdapter.class);
42
43 public JsfFrameworkAdapter(String conversationMessagerClass)
44 {
45 ConversationMessager cm = _FrameworkAdapterUtils.createConversationMessager(conversationMessagerClass,
46 JsfConversationMessager.class);
47 setConversationMessager(cm);
48 }
49
50
51
52
53
54
55
56 public void beginRequest()
57 {
58 log.debug("Beginning request");
59 FrameworkAdapter.setCurrentInstance(this);
60 }
61
62
63
64
65
66
67
68 public void endRequest()
69 {
70 log.debug("Ending request");
71 FrameworkAdapter.setCurrentInstance(null);
72 }
73
74 protected ConversationMessager createDefaultConversationMessager()
75 {
76 return new JsfConversationMessager();
77 }
78
79 protected FacesContext getFacesContext()
80 {
81 FacesContext fc = FacesContext.getCurrentInstance();
82 if (fc == null)
83 {
84 throw new OrchestraException("Missing FacesContext");
85 }
86 return fc;
87 }
88
89 public String getInitParameter(String key)
90 {
91 FacesContext context = getFacesContext();
92
93 return context.getExternalContext().getInitParameter(key);
94 }
95
96 public Object getRequestParameterAttribute(String key)
97 {
98 FacesContext context = getFacesContext();
99 return context.getExternalContext().getRequestParameterMap().get(key);
100 }
101
102 public boolean containsRequestParameterAttribute(String key)
103 {
104 FacesContext context = getFacesContext();
105 return context.getExternalContext().getRequestParameterMap().containsKey(key);
106 }
107
108 public Object getRequestAttribute(String key)
109 {
110 FacesContext context = getFacesContext();
111 return context.getExternalContext().getRequestMap().get(key);
112 }
113
114 public void setRequestAttribute(String key, Object value)
115 {
116 FacesContext context = getFacesContext();
117 context.getExternalContext().getRequestMap().put(key, value);
118 }
119
120 public boolean containsRequestAttribute(String key)
121 {
122 FacesContext context = getFacesContext();
123 return context.getExternalContext().getRequestMap().containsKey(key);
124 }
125
126 public Object getSessionAttribute(String key)
127 {
128 FacesContext context = getFacesContext();
129 return context.getExternalContext().getSessionMap().get(key);
130 }
131
132 public void setSessionAttribute(String key, Object value)
133 {
134 FacesContext context = getFacesContext();
135 context.getExternalContext().getSessionMap().put(key, value);
136 }
137
138 public boolean containsSessionAttribute(String key)
139 {
140 FacesContext context = getFacesContext();
141 return context.getExternalContext().getSessionMap().containsKey(key);
142 }
143
144 protected String getRequestContextPath()
145 {
146 FacesContext context = getFacesContext();
147 return context.getExternalContext().getRequestContextPath();
148 }
149
150 public void redirect(String url) throws IOException
151 {
152 StringBuffer redir = new StringBuffer();
153 if (url.startsWith("/"))
154 {
155 redir.append(getRequestContextPath());
156 }
157 redir.append(url);
158
159
160 FacesContext context = getFacesContext();
161
162 String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
163 context.getExternalContext().redirect(actionUrl);
164 context.responseComplete();
165 }
166
167 public Object getBean(String name)
168 {
169 FacesContext context = getFacesContext();
170 if (context == null)
171 {
172 throw new IllegalStateException("getBean invoked before FacesServlet");
173 }
174
175 try
176 {
177 return context.getApplication()
178 .getVariableResolver().resolveVariable(context, name);
179 }
180 catch(RuntimeException e)
181 {
182
183
184
185 log.error("Failed to resolve variable [" + name + "]", e);
186 throw e;
187 }
188 }
189
190 public void invokeNavigation(String navigationName)
191 {
192 FacesContext context = getFacesContext();
193
194 context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
195 }
196
197
198
199
200
201
202 public String getCurrentViewId()
203 {
204 UIViewRoot vr = getFacesContext().getViewRoot();
205 if (vr == null)
206 {
207 return null;
208 }
209
210 return vr.getViewId();
211 }
212 }