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
19
20 package org.apache.shale.examples.sqlbrowser;
21
22 import java.io.File;
23 import java.sql.Connection;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.faces.FacesException;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import javax.sql.DataSource;
33
34 /***
35 * <p>A servlet context listener that sets up the embedded example database.</p>
36 */
37 public class Listener implements ServletContextListener {
38
39
40
41
42
43 /***
44 * <p>The application scope attribute under which we store a data source
45 * for the embedded database.</p>
46 */
47 public static final String INTERNAL_DATA_SOURCE = "internal";
48
49
50
51
52
53
54 /***
55 * <p>The internal data source we have configured for Derby.</p>
56 */
57 private InternalDataSource ds = null;
58
59
60 /***
61 * <p>The log instance for this class.</p>
62 */
63 private transient Logger logger =
64 Logger.getLogger(this.getClass().getName());
65
66
67
68
69
70
71 /***
72 * <p>Perform cleanup actions upon application shutdown.</p>
73 */
74 public void contextDestroyed(ServletContextEvent event) {
75
76 if (logger.isLoggable(Level.INFO)) {
77 logger.log(Level.INFO, "Shutting down database");
78 }
79
80
81 try {
82 ds.shutdown();
83 } catch (SQLException e) {
84 if (logger.isLoggable(Level.SEVERE)) {
85 while (e != null) {
86 logger.log(Level.SEVERE, e.getMessage(), e);
87 e = e.getNextException();
88 }
89 }
90 }
91
92 }
93
94
95 /***
96 * <p>Perform setup actions upon application startup.</p>
97 */
98 public void contextInitialized(ServletContextEvent event) {
99
100
101 ServletContext context = event.getServletContext();
102 File tempDir = (File) context.getAttribute("javax.servlet.context.tempdir");
103 String url = tempDir.getAbsolutePath() + File.separator + "DATABASE";
104 if (logger.isLoggable(Level.INFO)) {
105 logger.log(Level.INFO, "Creating database " + url);
106 }
107
108
109 try {
110 ds = new InternalDataSource(url);
111 populate(ds);
112 } catch (SQLException e) {
113 if (logger.isLoggable(Level.SEVERE)) {
114 while (e != null) {
115 logger.log(Level.SEVERE, e.getMessage(), e);
116 e = e.getNextException();
117 }
118 }
119 throw new FacesException("SQLException occurred during startup (see log for details): " + e.getMessage());
120 }
121 context.setAttribute(INTERNAL_DATA_SOURCE, ds);
122
123 }
124
125
126
127
128
129 /***
130 * <p>Initialization commands for the internal database. If an exception
131 * occurs on the first one (presumed to be a CREATE TABLE), then the
132 * database is assumed to be already populated.</p>
133 */
134 private static final String populate[] = {
135 "create table zip_codes (" +
136 "zip_code varchar(10)," +
137 "city varchar(30)," +
138 "state varchar(2)" +
139 ")",
140 "insert into zip_codes (zip_code, city, state) " +
141 "values ('97005', 'Beaverton', 'OR')",
142 "insert into zip_codes (zip_code, city, state) " +
143 "values ('97006', 'Beaverton', 'OR')",
144 "insert into zip_codes (zip_code, city, state) " +
145 "values ('97007', 'Beaverton', 'OR')",
146 "insert into zip_codes (zip_code, city, state) " +
147 "values ('97008', 'Beaverton', 'OR')",
148 "insert into zip_codes (zip_code, city, state) " +
149 "values ('97075', 'Beaverton', 'OR')",
150 "insert into zip_codes (zip_code, city, state) " +
151 "values ('97076', 'Beaverton', 'OR')",
152 "insert into zip_codes (zip_code, city, state) " +
153 "values ('97077', 'Beaverton', 'OR')",
154 "insert into zip_codes (zip_code, city, state) " +
155 "values ('97078', 'Beaverton', 'OR')",
156 "insert into zip_codes (zip_code, city, state) " +
157 "values ('97034', 'Lake Oswego', 'OR')",
158 "insert into zip_codes (zip_code, city, state) " +
159 "values ('97035', 'Lake Oswego', 'OR')",
160 "insert into zip_codes (zip_code, city, state) " +
161 "values ('97062', 'Tualatin', 'OR')",
162 "insert into zip_codes (zip_code, city, state) " +
163 "values ('97068', 'West Linn', 'OR')",
164 "insert into zip_codes (zip_code, city, state) " +
165 "values ('97140', 'Sherwood', 'OR')",
166 "insert into zip_codes (zip_code, city, state) " +
167 "values ('97223', 'Tigard', 'OR')",
168 "insert into zip_codes (zip_code, city, state) " +
169 "values ('97224', 'Tigard', 'OR')",
170 "insert into zip_codes (zip_code, city, state) " +
171 "values ('97281', 'Tigard', 'OR')",
172 };
173
174
175
176
177
178
179 /***
180 * <p>Populate the internal database with dummy data.</p>
181 *
182 * @param ds <code>DataSource</code> for the internal database
183 *
184 * @exception SQLException if a database error occurs
185 */
186 private void populate(DataSource ds) throws SQLException {
187
188 int i = -1;
189 Connection conn = null;
190 Statement stmt = null;
191 try {
192 conn = ds.getConnection();
193 for (i = 0; i < populate.length; i++) {
194 System.err.println("populate(): Executing: " + populate[i]);
195 stmt = conn.createStatement();
196 stmt.executeUpdate(populate[i]);
197 stmt.close();
198 }
199 } catch (SQLException e) {
200 if (i == 0) {
201 return;
202 }
203 throw e;
204 } finally {
205 if (stmt != null) {
206 try {
207 stmt.close();
208 } catch (SQLException e) {
209 ;
210 }
211 }
212 if (conn != null) {
213 try {
214 conn.close();
215 } catch (SQLException e) {
216 ;
217 }
218 }
219 }
220
221 }
222
223
224 }