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 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 super();
57 ThreadLocal<ExternalContext> firstExternalContextInstance = null;
58 try
59 {
60 Field externalContextFirstInstance = ExternalContext.class.getDeclaredField("_firstInstance");
61 externalContextFirstInstance.setAccessible(true);
62
63 if (externalContextFirstInstance != null)
64 {
65 if (firstExternalContextInstance == null)
66 {
67 firstExternalContextInstance =
68 (ThreadLocal<ExternalContext>) externalContextFirstInstance.get(null);
69 }
70 }
71 }
72 catch (SecurityException e)
73 {
74
75 if (log.isDebugEnabled())
76 log.debug("Cannot access private field _firstInstance from ExternalContext ",e);
77 }
78 catch (Exception e)
79 {
80
81 if (log.isDebugEnabled())
82 log.debug("Cannot found private field _firstInstance from ExternalContext ",e);
83 }
84
85 _firstExternalContextInstance = firstExternalContextInstance;
86
87 }
88
89 public FacesContext getFacesContext(Object context,
90 Object request,
91 Object response,
92 Lifecycle lifecycle)
93 throws FacesException
94 {
95 if (context == null) {
96 throw new NullPointerException("context");
97 }
98 if (request == null) {
99 throw new NullPointerException("request");
100 }
101 if (response == null) {
102 throw new NullPointerException("response");
103 }
104 if (lifecycle == null) {
105 throw new NullPointerException("lifecycle");
106 }
107
108 FacesContext facesContext = null;
109
110
111 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 facesContext = new FacesContextImpl(externalContext,this);
127
128 if (facesContext != null)
129 {
130 if (_firstExternalContextInstance != null)
131 {
132
133
134 _firstExternalContextInstance.set((ExternalContext) externalContext);
135 }
136 return facesContext;
137 }
138
139 throw new FacesException("Unsupported context type " + context.getClass().getName());
140 }
141
142 public void release()
143 {
144 if (_firstExternalContextInstance != null)
145 {
146 _firstExternalContextInstance.remove();
147 }
148 }
149 }