1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.dialog.basic; |
19 | |
|
20 | |
import java.net.URL; |
21 | |
import java.util.Enumeration; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
import javax.faces.FacesException; |
25 | |
import javax.servlet.ServletContextEvent; |
26 | |
import javax.servlet.ServletContextListener; |
27 | |
import org.apache.commons.beanutils.PropertyUtils; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
import org.apache.shale.dialog.basic.config.ConfigurationParser; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class BasicLifecycleListener implements ServletContextListener { |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private static final String DEFAULT_CONFIGURATION_RESOURCE = |
50 | |
"/WEB-INF/dialog-config.xml"; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private static final String EMBEDDED_CONFIGURATION_RESOURCE = |
58 | |
"META-INF/dialog-config.xml"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | private Log log = LogFactory.getLog(BasicLifecycleListener.class); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void contextDestroyed(ServletContextEvent event) { |
79 | |
|
80 | 0 | if (log.isInfoEnabled()) { |
81 | 0 | log.info("Finalizing Dialog Basic Implementation"); |
82 | |
} |
83 | |
|
84 | |
|
85 | 0 | event.getServletContext().removeAttribute(Globals.DIALOGS); |
86 | |
|
87 | |
|
88 | 0 | PropertyUtils.clearDescriptors(); |
89 | 0 | LogFactory.release(Thread.currentThread().getContextClassLoader()); |
90 | 0 | log = null; |
91 | |
|
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void contextInitialized(ServletContextEvent event) { |
101 | |
|
102 | 0 | if (log.isInfoEnabled()) { |
103 | 0 | log.info("Initializing Dialog Basic Implementation"); |
104 | |
} |
105 | |
|
106 | |
|
107 | 0 | boolean didDefault = false; |
108 | 0 | ConfigurationParser parser = new ConfigurationParser(); |
109 | 0 | parser.setDialogs(new HashMap()); |
110 | |
|
111 | |
|
112 | 0 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
113 | 0 | if (loader == null) { |
114 | 0 | loader = this.getClass().getClassLoader(); |
115 | |
} |
116 | |
try { |
117 | 0 | Enumeration resources = loader.getResources(EMBEDDED_CONFIGURATION_RESOURCE); |
118 | 0 | while (resources.hasMoreElements()) { |
119 | 0 | URL resource = (URL) resources.nextElement(); |
120 | 0 | if (log.isDebugEnabled()) { |
121 | 0 | log.debug("Parsing configuration resource '" |
122 | |
+ resource + "'"); |
123 | |
} |
124 | 0 | parser.setResource(resource); |
125 | 0 | parser.parse(); |
126 | 0 | } |
127 | 0 | } catch (RuntimeException e) { |
128 | 0 | throw e; |
129 | 0 | } catch (Exception e) { |
130 | 0 | throw new FacesException(e); |
131 | 0 | } |
132 | |
|
133 | |
|
134 | 0 | String resources = |
135 | |
event.getServletContext().getInitParameter(Globals.CONFIGURATION); |
136 | 0 | if (resources == null) { |
137 | 0 | resources = ""; |
138 | |
|
139 | |
|
140 | |
} |
141 | 0 | int comma = 0; |
142 | 0 | String path = null; |
143 | |
try { |
144 | |
while (true) { |
145 | 0 | comma = resources.indexOf(","); |
146 | 0 | if (comma < 0) { |
147 | 0 | path = resources.trim(); |
148 | 0 | resources = ""; |
149 | 0 | } else { |
150 | 0 | path = resources.substring(0, comma).trim(); |
151 | 0 | resources = resources.substring(comma + 1); |
152 | |
} |
153 | 0 | if (path.length() < 1) { |
154 | 0 | break; |
155 | |
} |
156 | 0 | URL resource = event.getServletContext().getResource(path); |
157 | 0 | if (log.isDebugEnabled()) { |
158 | 0 | log.debug("Parsing configuration resource '" |
159 | |
+ resource + "'"); |
160 | |
} |
161 | 0 | parser.setResource(resource); |
162 | 0 | parser.parse(); |
163 | 0 | if (DEFAULT_CONFIGURATION_RESOURCE.equals(path)) { |
164 | 0 | didDefault = true; |
165 | |
} |
166 | 0 | } |
167 | 0 | } catch (RuntimeException e) { |
168 | 0 | throw e; |
169 | 0 | } catch (Exception e) { |
170 | 0 | throw new FacesException(e); |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | 0 | if (!didDefault) { |
176 | |
try { |
177 | 0 | URL resource = |
178 | |
event.getServletContext().getResource(DEFAULT_CONFIGURATION_RESOURCE); |
179 | 0 | if (resource != null) { |
180 | 0 | if (log.isDebugEnabled()) { |
181 | 0 | log.debug("Parsing configuration resource '" |
182 | |
+ resource + "'"); |
183 | |
} |
184 | 0 | parser.setResource(resource); |
185 | 0 | parser.parse(); |
186 | |
} |
187 | 0 | } catch (RuntimeException e) { |
188 | 0 | throw e; |
189 | 0 | } catch (Exception e) { |
190 | 0 | throw new FacesException(e); |
191 | 0 | } |
192 | |
} |
193 | |
|
194 | |
|
195 | 0 | Map dialogs = parser.getDialogs(); |
196 | 0 | if (dialogs.size() == 0) { |
197 | 0 | if (log.isWarnEnabled()) { |
198 | 0 | log.warn("No dialog configuration information present. No default configuration found at: " + |
199 | |
DEFAULT_CONFIGURATION_RESOURCE + ". No embedded configuration found at: " + |
200 | |
EMBEDDED_CONFIGURATION_RESOURCE); |
201 | |
} |
202 | |
} |
203 | 0 | event.getServletContext().setAttribute(Globals.DIALOGS, dialogs); |
204 | |
|
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
} |