1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.webapp; |
20 | |
|
21 | |
import java.util.Enumeration; |
22 | |
|
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
import org.apache.myfaces.config.ManagedBeanBuilder; |
26 | |
import org.apache.myfaces.util.ContainerUtils; |
27 | |
import org.apache.myfaces.shared_impl.util.ClassUtils; |
28 | |
|
29 | |
import javax.faces.FactoryFinder; |
30 | |
import javax.servlet.ServletContext; |
31 | |
import javax.servlet.ServletContextEvent; |
32 | |
import javax.servlet.ServletContextListener; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class StartupServletContextListener extends AbstractMyFacesListener |
53 | |
implements ServletContextListener |
54 | |
{ |
55 | 0 | static final String FACES_INIT_DONE = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE"; |
56 | |
|
57 | |
static final String FACES_INIT_PLUGINS = "org.apache.myfaces.FACES_INIT_PLUGINS"; |
58 | |
|
59 | |
private static final byte FACES_INIT_PHASE_PREINIT = 0; |
60 | |
private static final byte FACES_INIT_PHASE_POSTINIT = 1; |
61 | |
private static final byte FACES_INIT_PHASE_PREDESTROY = 2; |
62 | |
private static final byte FACES_INIT_PHASE_POSTDESTROY = 3; |
63 | |
|
64 | 0 | private static final Log log = LogFactory.getLog(StartupServletContextListener.class); |
65 | |
|
66 | |
private FacesInitializer _facesInitializer; |
67 | |
private ServletContext _servletContext; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private void dispatchInitializationEvent(ServletContextEvent event, int operation) { |
77 | 0 | String [] pluginEntries = (String []) _servletContext.getAttribute(FACES_INIT_PLUGINS); |
78 | |
|
79 | 0 | if(pluginEntries == null) { |
80 | 0 | String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS); |
81 | 0 | log.info("Checking for plugins:"+FACES_INIT_PLUGINS); |
82 | 0 | if(plugins == null) return; |
83 | 0 | log.info("Plugins found"); |
84 | 0 | pluginEntries = plugins.split(","); |
85 | 0 | _servletContext.setAttribute(FACES_INIT_PLUGINS, pluginEntries); |
86 | |
} |
87 | |
|
88 | |
|
89 | 0 | for(String plugin: pluginEntries) { |
90 | 0 | log.info("Processing plugin:"+plugin); |
91 | |
try { |
92 | |
|
93 | |
|
94 | |
|
95 | 0 | Class pluginClass = ClassUtils.getContextClassLoader().loadClass(plugin); |
96 | 0 | if (pluginClass == null) |
97 | |
{ |
98 | 0 | pluginClass = this.getClass().getClassLoader().loadClass(plugin); |
99 | |
} |
100 | 0 | StartupListener initializer = (StartupListener) pluginClass.newInstance(); |
101 | |
|
102 | 0 | switch(operation) { |
103 | |
case FACES_INIT_PHASE_PREINIT: |
104 | 0 | initializer.preInit(event); |
105 | 0 | break; |
106 | |
case FACES_INIT_PHASE_POSTINIT: |
107 | 0 | initializer.postInit(event); |
108 | 0 | break; |
109 | |
case FACES_INIT_PHASE_PREDESTROY: |
110 | 0 | initializer.preDestroy(event); |
111 | 0 | break; |
112 | |
default: |
113 | 0 | initializer.postDestroy(event); |
114 | |
break; |
115 | |
} |
116 | |
|
117 | |
|
118 | 0 | } catch (ClassNotFoundException e) { |
119 | 0 | log.error(e); |
120 | 0 | } catch (IllegalAccessException e) { |
121 | 0 | log.error(e); |
122 | 0 | } catch (InstantiationException e) { |
123 | 0 | log.error(e); |
124 | 0 | } |
125 | |
|
126 | |
} |
127 | 0 | log.info("Processing plugins done"); |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
public void contextInitialized(ServletContextEvent event) |
132 | |
{ |
133 | 0 | if (_servletContext != null) |
134 | |
{ |
135 | 0 | throw new IllegalStateException("context is already initialized"); |
136 | |
} |
137 | 0 | _servletContext = event.getServletContext(); |
138 | 0 | Boolean b = (Boolean) _servletContext.getAttribute(FACES_INIT_DONE); |
139 | |
|
140 | 0 | if (b == null || b.booleanValue() == false) |
141 | |
{ |
142 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_PREINIT); |
143 | 0 | getFacesInitializer().initFaces(_servletContext); |
144 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTINIT); |
145 | 0 | _servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | log.info("MyFaces already initialized"); |
150 | |
} |
151 | 0 | } |
152 | |
|
153 | |
protected FacesInitializer getFacesInitializer() |
154 | |
{ |
155 | 0 | if (_facesInitializer == null) |
156 | |
{ |
157 | 0 | if (ContainerUtils.isJsp21()) |
158 | |
{ |
159 | 0 | _facesInitializer = new Jsp21FacesInitializer(); |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | _facesInitializer = new Jsp20FacesInitializer(); |
164 | |
} |
165 | |
} |
166 | |
|
167 | 0 | return _facesInitializer; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public void setFacesInitializer(FacesInitializer facesInitializer) |
176 | |
{ |
177 | 0 | if (_facesInitializer != null && _facesInitializer != facesInitializer && _servletContext != null) |
178 | |
{ |
179 | 0 | _facesInitializer.destroyFaces(_servletContext); |
180 | |
} |
181 | 0 | _facesInitializer = facesInitializer; |
182 | 0 | if (_servletContext != null) |
183 | |
{ |
184 | 0 | facesInitializer.initFaces(_servletContext); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
public void contextDestroyed(ServletContextEvent event) |
189 | |
{ |
190 | 0 | doPredestroy(event); |
191 | |
|
192 | 0 | if (_facesInitializer != null && _servletContext != null) |
193 | |
{ |
194 | 0 | _facesInitializer.destroyFaces(_servletContext); |
195 | |
} |
196 | 0 | FactoryFinder.releaseFactories(); |
197 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTDESTROY); |
198 | |
|
199 | 0 | _servletContext = null; |
200 | 0 | } |
201 | |
|
202 | |
private void doPredestroy(ServletContextEvent event) { |
203 | |
|
204 | 0 | ServletContext ctx = event.getServletContext(); |
205 | |
|
206 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_PREDESTROY); |
207 | 0 | Enumeration<String> attributes = ctx.getAttributeNames(); |
208 | |
|
209 | 0 | while(attributes.hasMoreElements()) |
210 | |
{ |
211 | 0 | String name = attributes.nextElement(); |
212 | 0 | Object value = ctx.getAttribute(name); |
213 | 0 | doPreDestroy(value, name, ManagedBeanBuilder.APPLICATION); |
214 | 0 | } |
215 | 0 | } |
216 | |
} |