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.context.FacesContext;
30
31 import java.io.IOException;
32
33
34
35
36
37
38 public class JsfFrameworkAdapter extends FrameworkAdapter
39 {
40 private final Log log = LogFactory.getLog(JsfFrameworkAdapter.class);
41
42 public JsfFrameworkAdapter(String conversationMessagerClass)
43 {
44 ConversationMessager cm = _FrameworkAdapterUtils.createConversationMessager(conversationMessagerClass,
45 JsfConversationMessager.class);
46 setConversationMessager(cm);
47 }
48
49
50
51
52
53
54
55 public void beginRequest()
56 {
57 log.debug("Beginning request");
58 FrameworkAdapter.setCurrentInstance(this);
59 }
60
61
62
63
64
65
66
67 public void endRequest()
68 {
69 log.debug("Ending request");
70 FrameworkAdapter.setCurrentInstance(null);
71 }
72
73 protected ConversationMessager createDefaultConversationMessager()
74 {
75 return new JsfConversationMessager();
76 }
77
78 protected FacesContext getFacesContext()
79 {
80 FacesContext fc = FacesContext.getCurrentInstance();
81 if (fc == null)
82 {
83 throw new OrchestraException("Missing FacesContext");
84 }
85 return fc;
86 }
87
88 public String getInitParameter(String key)
89 {
90 FacesContext context = getFacesContext();
91
92 return context.getExternalContext().getInitParameter(key);
93 }
94
95 public Object getRequestParameterAttribute(String key)
96 {
97 FacesContext context = getFacesContext();
98 return context.getExternalContext().getRequestParameterMap().get(key);
99 }
100
101 public boolean containsRequestParameterAttribute(String key)
102 {
103 FacesContext context = getFacesContext();
104 return context.getExternalContext().getRequestParameterMap().containsKey(key);
105 }
106
107 public Object getRequestAttribute(String key)
108 {
109 FacesContext context = getFacesContext();
110 return context.getExternalContext().getRequestMap().get(key);
111 }
112
113 public void setRequestAttribute(String key, Object value)
114 {
115 FacesContext context = getFacesContext();
116 context.getExternalContext().getRequestMap().put(key, value);
117 }
118
119 public boolean containsRequestAttribute(String key)
120 {
121 FacesContext context = getFacesContext();
122 return context.getExternalContext().getRequestMap().containsKey(key);
123 }
124
125 public Object getSessionAttribute(String key)
126 {
127 FacesContext context = getFacesContext();
128 return context.getExternalContext().getSessionMap().get(key);
129 }
130
131 public void setSessionAttribute(String key, Object value)
132 {
133 FacesContext context = getFacesContext();
134 context.getExternalContext().getSessionMap().put(key, value);
135 }
136
137 public boolean containsSessionAttribute(String key)
138 {
139 FacesContext context = getFacesContext();
140 return context.getExternalContext().getSessionMap().containsKey(key);
141 }
142
143 protected String getRequestContextPath()
144 {
145 FacesContext context = getFacesContext();
146 return context.getExternalContext().getRequestContextPath();
147 }
148
149 public void redirect(String url) throws IOException
150 {
151 StringBuffer redir = new StringBuffer();
152 if (url.startsWith("/"))
153 {
154 redir.append(getRequestContextPath());
155 }
156 redir.append(url);
157
158
159 FacesContext context = getFacesContext();
160
161 String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
162 context.getExternalContext().redirect(actionUrl);
163 context.responseComplete();
164 }
165
166 public Object getBean(String name)
167 {
168 FacesContext context = getFacesContext();
169 if (context == null)
170 {
171 throw new IllegalStateException("getBean invoked before FacesServlet");
172 }
173
174 return context.getApplication()
175 .getVariableResolver().resolveVariable(context, name);
176 }
177
178 public void invokeNavigation(String navigationName)
179 {
180 FacesContext context = getFacesContext();
181
182 context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
183 }
184
185
186
187
188 public String getCurrentViewId()
189 {
190 return getFacesContext().getViewRoot().getViewId();
191 }
192 }