1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.context; |
20 | |
|
21 | |
import java.lang.reflect.Field; |
22 | |
|
23 | |
import javax.faces.FacesException; |
24 | |
import javax.faces.context.ExternalContext; |
25 | |
import javax.faces.context.FacesContext; |
26 | |
import javax.faces.context.FacesContextFactory; |
27 | |
import javax.faces.lifecycle.Lifecycle; |
28 | |
import javax.servlet.ServletContext; |
29 | |
import javax.servlet.ServletRequest; |
30 | |
import javax.servlet.ServletResponse; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
import org.apache.myfaces.context.servlet.FacesContextImpl; |
35 | |
import org.apache.myfaces.context.servlet.ServletExternalContextImpl; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class FacesContextFactoryImpl |
43 | |
extends FacesContextFactory implements ReleaseableFacesContextFactory |
44 | |
{ |
45 | 0 | private static final Log log = LogFactory.getLog(FacesContextFactoryImpl.class); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private final ThreadLocal<ExternalContext> _firstExternalContextInstance; |
52 | |
|
53 | |
@SuppressWarnings("unchecked") |
54 | |
public FacesContextFactoryImpl() |
55 | |
{ |
56 | 0 | super(); |
57 | 0 | ThreadLocal<ExternalContext> firstExternalContextInstance = null; |
58 | |
try |
59 | |
{ |
60 | 0 | Field externalContextFirstInstance = ExternalContext.class.getDeclaredField("_firstInstance"); |
61 | 0 | externalContextFirstInstance.setAccessible(true); |
62 | |
|
63 | 0 | if (externalContextFirstInstance != null) |
64 | |
{ |
65 | 0 | if (firstExternalContextInstance == null) |
66 | |
{ |
67 | 0 | firstExternalContextInstance = |
68 | |
(ThreadLocal<ExternalContext>) externalContextFirstInstance.get(null); |
69 | |
} |
70 | |
} |
71 | |
} |
72 | 0 | catch (SecurityException e) |
73 | |
{ |
74 | |
|
75 | 0 | if (log.isDebugEnabled()) |
76 | 0 | log.debug("Cannot access private field _firstInstance from ExternalContext ",e); |
77 | |
} |
78 | 0 | catch (Exception e) |
79 | |
{ |
80 | |
|
81 | 0 | if (log.isDebugEnabled()) |
82 | 0 | log.debug("Cannot found private field _firstInstance from ExternalContext ",e); |
83 | 0 | } |
84 | |
|
85 | 0 | _firstExternalContextInstance = firstExternalContextInstance; |
86 | |
|
87 | 0 | } |
88 | |
|
89 | |
public FacesContext getFacesContext(Object context, |
90 | |
Object request, |
91 | |
Object response, |
92 | |
Lifecycle lifecycle) |
93 | |
throws FacesException |
94 | |
{ |
95 | 0 | if (context == null) { |
96 | 0 | throw new NullPointerException("context"); |
97 | |
} |
98 | 0 | if (request == null) { |
99 | 0 | throw new NullPointerException("request"); |
100 | |
} |
101 | 0 | if (response == null) { |
102 | 0 | throw new NullPointerException("response"); |
103 | |
} |
104 | 0 | if (lifecycle == null) { |
105 | 0 | throw new NullPointerException("lifecycle"); |
106 | |
} |
107 | |
|
108 | 0 | FacesContext facesContext = null; |
109 | |
|
110 | |
|
111 | 0 | ReleaseableExternalContext externalContext = new ServletExternalContextImpl((ServletContext)context, |
112 | |
(ServletRequest) request, (ServletResponse) response); |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 0 | facesContext = new FacesContextImpl(externalContext,this); |
127 | |
|
128 | 0 | if (facesContext != null) |
129 | |
{ |
130 | 0 | if (_firstExternalContextInstance != null) |
131 | |
{ |
132 | |
|
133 | |
|
134 | 0 | _firstExternalContextInstance.set((ExternalContext) externalContext); |
135 | |
} |
136 | 0 | return facesContext; |
137 | |
} |
138 | |
|
139 | 0 | throw new FacesException("Unsupported context type " + context.getClass().getName()); |
140 | |
} |
141 | |
|
142 | |
public void release() |
143 | |
{ |
144 | 0 | if (_firstExternalContextInstance != null) |
145 | |
{ |
146 | 0 | _firstExternalContextInstance.remove(); |
147 | |
} |
148 | 0 | } |
149 | |
} |