1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.lang.reflect.Constructor; |
21 | |
import javax.faces.FacesException; |
22 | |
import javax.faces.context.ExternalContext; |
23 | |
import javax.faces.context.FacesContext; |
24 | |
import javax.faces.context.FacesContextFactory; |
25 | |
import javax.faces.lifecycle.Lifecycle; |
26 | |
import javax.servlet.ServletContext; |
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class MockFacesContextFactory extends FacesContextFactory { |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 18 | public MockFacesContextFactory() { |
47 | |
|
48 | 18 | Class clazz = null; |
49 | |
|
50 | |
|
51 | |
try { |
52 | 18 | clazz = this.getClass().getClassLoader().loadClass("org.apache.shale.test.mock.MockFacesContext12"); |
53 | 18 | constructor = clazz.getConstructor(facesContextSignature); |
54 | 18 | jsf12 = true; |
55 | |
} catch (NoClassDefFoundError e) { |
56 | |
|
57 | |
clazz = null; |
58 | |
constructor = null; |
59 | |
} catch (ClassNotFoundException e) { |
60 | |
|
61 | |
clazz = null; |
62 | |
constructor = null; |
63 | |
} catch (RuntimeException e) { |
64 | |
throw e; |
65 | |
} catch (Exception e) { |
66 | |
throw new FacesException(e); |
67 | 18 | } |
68 | |
|
69 | |
|
70 | |
try { |
71 | 18 | if (clazz == null) { |
72 | |
clazz = this.getClass().getClassLoader().loadClass("org.apache.shale.test.mock.MockFacesContext"); |
73 | |
constructor = clazz.getConstructor(facesContextSignature); |
74 | |
jsf12 = false; |
75 | |
} |
76 | |
} catch (RuntimeException e) { |
77 | |
throw e; |
78 | |
} catch (Exception e) { |
79 | |
throw new FacesException(e); |
80 | 18 | } |
81 | |
|
82 | 18 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | 18 | private Constructor constructor = null; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 1 | private static Class[] externalContextSignature = new Class[] { |
102 | 6 | ServletContext.class, HttpServletRequest.class, HttpServletResponse.class |
103 | |
}; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | 1 | private static Class[] facesContextSignature = new Class[] { |
110 | |
ExternalContext.class, Lifecycle.class |
111 | |
}; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 18 | private boolean jsf12 = false; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public FacesContext getFacesContext(Object context, Object request, |
125 | |
Object response, |
126 | |
Lifecycle lifecycle) throws FacesException { |
127 | |
|
128 | |
|
129 | 18 | Class clazz = MockExternalContext.class; |
130 | 18 | if (jsf12) { |
131 | |
try { |
132 | 18 | clazz = this.getClass().getClassLoader().loadClass |
133 | |
("org.apache.shale.test.mock.MockExternalContext12"); |
134 | |
} catch (RuntimeException e) { |
135 | |
throw e; |
136 | |
} catch (Exception e) { |
137 | |
throw new FacesException(e); |
138 | 18 | } |
139 | |
} |
140 | |
|
141 | |
|
142 | 18 | Constructor mecConstructor = null; |
143 | |
try { |
144 | 18 | mecConstructor = clazz.getConstructor(externalContextSignature); |
145 | |
} catch (RuntimeException e) { |
146 | |
throw e; |
147 | |
} catch (Exception e) { |
148 | |
throw new FacesException(e); |
149 | 18 | } |
150 | |
|
151 | |
|
152 | 18 | MockExternalContext externalContext = null; |
153 | |
try { |
154 | 18 | externalContext = (MockExternalContext) mecConstructor.newInstance |
155 | |
(new Object[] { context, request, response }); |
156 | |
} catch (RuntimeException e) { |
157 | |
throw e; |
158 | |
} catch (Exception e) { |
159 | |
throw new FacesException(e); |
160 | 18 | } |
161 | |
|
162 | |
|
163 | |
try { |
164 | 18 | return (MockFacesContext) |
165 | |
constructor.newInstance(new Object[] { externalContext, lifecycle }); |
166 | |
} catch (RuntimeException e) { |
167 | |
throw e; |
168 | |
} catch (Exception e) { |
169 | |
throw new FacesException(e); |
170 | |
} |
171 | |
|
172 | |
} |
173 | |
|
174 | |
|
175 | |
} |