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 | |
|
36 | |
|
37 | 0 | public class Listener implements ServletContextListener { |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public static final String INTERNAL_DATA_SOURCE = "internal"; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | private InternalDataSource ds = null; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | private transient Logger logger = |
64 | |
Logger.getLogger(this.getClass().getName()); |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void contextDestroyed(ServletContextEvent event) { |
75 | |
|
76 | 0 | if (logger.isLoggable(Level.INFO)) { |
77 | 0 | logger.log(Level.INFO, "Shutting down database"); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
try { |
82 | 0 | ds.shutdown(); |
83 | 0 | } catch (SQLException e) { |
84 | 0 | if (logger.isLoggable(Level.SEVERE)) { |
85 | 0 | while (e != null) { |
86 | 0 | logger.log(Level.SEVERE, e.getMessage(), e); |
87 | 0 | e = e.getNextException(); |
88 | 0 | } |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void contextInitialized(ServletContextEvent event) { |
99 | |
|
100 | |
|
101 | 0 | ServletContext context = event.getServletContext(); |
102 | 0 | File tempDir = (File) context.getAttribute("javax.servlet.context.tempdir"); |
103 | 0 | String url = tempDir.getAbsolutePath() + File.separator + "DATABASE"; |
104 | 0 | if (logger.isLoggable(Level.INFO)) { |
105 | 0 | logger.log(Level.INFO, "Creating database " + url); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
try { |
110 | 0 | ds = new InternalDataSource(url); |
111 | 0 | populate(ds); |
112 | 0 | } catch (SQLException e) { |
113 | 0 | if (logger.isLoggable(Level.SEVERE)) { |
114 | 0 | while (e != null) { |
115 | 0 | logger.log(Level.SEVERE, e.getMessage(), e); |
116 | 0 | e = e.getNextException(); |
117 | 0 | } |
118 | |
} |
119 | 0 | throw new FacesException("SQLException occurred during startup (see log for details): " + e.getMessage()); |
120 | 0 | } |
121 | 0 | context.setAttribute(INTERNAL_DATA_SOURCE, ds); |
122 | |
|
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | 0 | 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 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private void populate(DataSource ds) throws SQLException { |
187 | |
|
188 | 0 | int i = -1; |
189 | 0 | Connection conn = null; |
190 | 0 | Statement stmt = null; |
191 | |
try { |
192 | 0 | conn = ds.getConnection(); |
193 | 0 | for (i = 0; i < populate.length; i++) { |
194 | 0 | System.err.println("populate(): Executing: " + populate[i]); |
195 | 0 | stmt = conn.createStatement(); |
196 | 0 | stmt.executeUpdate(populate[i]); |
197 | 0 | stmt.close(); |
198 | |
} |
199 | 0 | } catch (SQLException e) { |
200 | 0 | if (i == 0) { |
201 | |
return; |
202 | |
} |
203 | 0 | throw e; |
204 | |
} finally { |
205 | 0 | if (stmt != null) { |
206 | |
try { |
207 | 0 | stmt.close(); |
208 | 0 | } catch (SQLException e) { |
209 | |
; |
210 | 0 | } |
211 | |
} |
212 | 0 | if (conn != null) { |
213 | |
try { |
214 | 0 | conn.close(); |
215 | 0 | } catch (SQLException e) { |
216 | |
; |
217 | 0 | } |
218 | 0 | } |
219 | 0 | } |
220 | |
|
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
} |