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