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