2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
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 * <p>ServletContextListener that loads dialog configuration resources
36 * at application startup, and cleans up the libraries we depend on at
37 * application shutdown.</p>
38 *
39 * $Id: SCXMLLifecycleListener.java 476713 2006-11-19 05:10:57Z rahul $
40 *
41 * @since 1.0.4
42 */
43 public class SCXMLLifecycleListener implements ServletContextListener {
44
45
46
47
48
49 /***
50 * <p>The default configuration resource we will process (if present),
51 * even if it is not explicitly configured.</p>
52 */
53 private static final String DEFAULT_CONFIGURATION_RESOURCE =
54 "/WEB-INF/dialog-config.xml";
55
56
57 /***
58 * <p>Resource name for dialog configuration resource(s) embedded in
59 * JAR files inside the application.</p>
60 */
61 private static final String EMBEDDED_CONFIGURATION_RESOURCE =
62 "META-INF/dialog-config.xml";
63
64
65
66
67
68 /***
69 * <p>The <code>Log</code> instance we will use for this listener.</p>
70 */
71 private Log log = LogFactory.getLog(SCXMLLifecycleListener.class);
72
73
74
75
76
77 /***
78 * <p>Process the application shutdown event, cleanup resources.</p>
79 *
80 * @param event Shutdown event to be processed
81 */
82 public void contextDestroyed(ServletContextEvent event) {
83
84 if (log.isInfoEnabled()) {
85 log.info("Finalizing Dialog SCXML Implementation");
86 }
87
88
89 event.getServletContext().removeAttribute(Globals.DIALOGS);
90
91
92 PropertyUtils.clearDescriptors();
93 LogFactory.release(Thread.currentThread().getContextClassLoader());
94 log = null;
95
96 }
97
98
99 /***
100 * <p>Process the application startup event, parse dialog
101 * configurations.</p>
102 *
103 * @param event Startup event to be processed
104 */
105 public void contextInitialized(ServletContextEvent event) {
106
107 if (log.isInfoEnabled()) {
108 log.info("Initializing Dialog SCXML Implementation");
109 }
110
111
112 boolean didDefault = false;
113 ConfigurationParser parser = new ConfigurationParser();
114 parser.setDialogs(new HashMap());
115
116
117 ClassLoader loader = Thread.currentThread().getContextClassLoader();
118 if (loader == null) {
119 loader = this.getClass().getClassLoader();
120 }
121 try {
122 Enumeration resources = loader.getResources(EMBEDDED_CONFIGURATION_RESOURCE);
123 while (resources.hasMoreElements()) {
124 URL resource = (URL) resources.nextElement();
125 if (log.isDebugEnabled()) {
126 log.debug("Parsing configuration resource '"
127 + resource + "'");
128 }
129 parser.setResource(resource);
130 parser.parse();
131 }
132 } catch (RuntimeException e) {
133 throw e;
134 } catch (Exception e) {
135 throw new FacesException(e);
136 }
137
138
139 String resources =
140 event.getServletContext().getInitParameter(Globals.CONFIGURATION);
141 if (resources == null) {
142 resources = "";
143 }
144 int comma = 0;
145 String path = null;
146 try {
147 while (true) {
148 comma = resources.indexOf(",");
149 if (comma < 0) {
150 path = resources.trim();
151 resources = "";
152 } else {
153 path = resources.substring(0, comma).trim();
154 resources = resources.substring(comma + 1);
155 }
156 if (path.length() < 1) {
157 break;
158 }
159 URL resource = event.getServletContext().getResource(path);
160 if (log.isDebugEnabled()) {
161 log.debug("Parsing configuration resource '"
162 + resource + "'");
163 }
164 parser.setResource(resource);
165 parser.parse();
166 if (DEFAULT_CONFIGURATION_RESOURCE.equals(path)) {
167 didDefault = true;
168 }
169 }
170 } catch (RuntimeException e) {
171 throw e;
172 } catch (Exception e) {
173 throw new FacesException(e);
174 }
175
176
177
178 if (!didDefault) {
179 try {
180 URL resource =
181 event.getServletContext().getResource(DEFAULT_CONFIGURATION_RESOURCE);
182 if (resource != null) {
183 if (log.isDebugEnabled()) {
184 log.debug("Parsing configuration resource '"
185 + resource + "'");
186 }
187 parser.setResource(resource);
188 parser.parse();
189 }
190 } catch (RuntimeException e) {
191 throw e;
192 } catch (Exception e) {
193 throw new FacesException(e);
194 }
195 }
196
197
198 Map dialogs = parser.getDialogs();
199
200 if (dialogs.size() == 0) {
201 if (log.isWarnEnabled()) {
202 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 event.getServletContext().setAttribute(Globals.DIALOGS, dialogs);
208
209 }
210
211
212 }