source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java@ 26911

Last change on this file since 26911 was 26911, checked in by davidb, 11 years ago

Introduction of a shutdown method (which shuts down the complete server, not just a connection to a particular database). This function is used in the cleanUp sequence of calls, triggered when the servlet is stopped. Done to avoid 'warning thread still running [derby.antiGC] ... possible memory leak' message reported in catalina.out

File size: 15.1 KB
Line 
1/*
2 * DerbyWrapper.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import javax.swing.*;
22
23import org.greenstone.gsdl3.service.Authentication;
24
25import java.io.File;
26import java.sql.Connection;
27import java.sql.DriverManager;
28import java.sql.ResultSet;
29import java.sql.SQLException;
30import java.sql.SQLWarning;
31import java.sql.Statement;
32import java.util.ArrayList;
33import java.util.HashMap;
34import java.util.Properties;
35
36public class DerbyWrapper
37{
38 static final String PROTOCOL = "jdbc:derby:";
39 static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
40 static final String USERSDB = "usersDB";
41 static final String USERS = "users";
42 static final String ROLES = "roles";
43 private Connection conn = null;
44 private Statement state = null;
45 private String protocol_str;
46
47 public DerbyWrapper()
48 {
49 }
50
51 public DerbyWrapper(String dbpath)
52 {
53 connectDatabase(dbpath, false);
54 }
55
56 public void connectDatabase(String dbpath, boolean create_database)
57 {
58 try
59 {
60 Class.forName(DRIVER).newInstance();
61 //System.out.println("Loaded the embedded driver.");
62 protocol_str = PROTOCOL + dbpath;
63 if (create_database)
64 {
65 conn = DriverManager.getConnection(protocol_str + ";create=true");
66 }
67 else
68 {
69 conn = DriverManager.getConnection(protocol_str);
70 }
71 state = conn.createStatement();
72 }
73 catch (Throwable e)
74 {
75 System.out.println("exception thrown:");
76 if (e instanceof SQLException)
77 {
78 printSQLError((SQLException) e);
79 }
80 else
81 {
82 e.printStackTrace();
83 }
84 }
85 }
86
87 public void closeDatabase()
88 {
89 //state = null;
90 //conn = null;
91 boolean gotSQLExc = false;
92 try
93 {
94 // shutdown the database
95 DriverManager.getConnection(protocol_str + ";shutdown=true");
96
97 }
98 catch (SQLException se)
99 {
100 // this is good (i.e. what Derby is designed to do on a successful shutdown)
101 gotSQLExc = true;
102 }
103 catch (Exception e) {
104 e.printStackTrace();
105 }
106
107 if (!gotSQLExc)
108 {
109 System.err.println("Warning: Derby Database did not shut down normally");
110 }
111 }
112
113 public static void shutdownDatabaseServer()
114 {
115 boolean gotSQLExc = false;
116
117 try {
118 // shutdown the whole server
119 DriverManager.getConnection(PROTOCOL + ";shutdown=true");
120
121 }
122 catch (SQLException se) {
123 // this is good (i.e. what Derby is designed to do on a successful shutdown)
124 gotSQLExc = true;
125 //System.out.println("Shutdown returned: " + se);
126 }
127 catch (Exception e) {
128
129 e.printStackTrace();
130 }
131 if (!gotSQLExc) {
132 System.err.println("Warning: Derby did not shut down normally");
133 }
134
135 }
136
137 public void createDatabase()
138 {
139 try
140 {
141 conn.setAutoCommit(false);
142 state.execute("create table users (username varchar(40) not null, password varchar(40) not null, accountstatus varchar(10), comment varchar(100), email varchar(40), primary key(username))");
143 state.execute("create table roles (username varchar(40) not null, role varchar(40) not null, primary key (username, role))");
144 state.execute("insert into " + USERS + " values ('admin', '" + Authentication.hashPassword("admin") + "', 'true', 'change the password for this account as soon as possible', '')");
145 state.execute("insert into " + ROLES + " values ('admin', 'administrator')");
146 state.execute("insert into " + ROLES + " values ('admin', 'all-collections-editor')");
147 conn.commit();
148 }
149 catch (Exception ex)
150 {
151 ex.printStackTrace();
152 }
153 }
154
155 public UserQueryResult listAllUser() throws SQLException
156 {
157 UserQueryResult userQueryResult = new UserQueryResult();
158 String sql_list_all_user = "SELECT username, password, accountstatus, email, comment FROM " + USERS;
159
160 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
161 ResultSet rs = state.executeQuery(sql_list_all_user);
162 while (rs.next())
163 {
164 HashMap<String, String> user = new HashMap<String, String>();
165 user.put("username", rs.getString("username"));
166 user.put("password", rs.getString("password"));
167 user.put("as", rs.getString("accountstatus"));
168 user.put("comment", rs.getString("comment"));
169 user.put("email", rs.getString("email"));
170
171 users.add(user);
172 }
173
174 for (HashMap<String, String> user : users)
175 {
176 ResultSet gs = state.executeQuery("SELECT role FROM roles WHERE username = '" + user.get("username") + "'");
177 String group = "";
178 while (gs.next())
179 {
180 if (!group.equals(""))
181 {
182 group += ",";
183 }
184 group += gs.getString("role");
185 }
186 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
187 }
188
189 if (userQueryResult.getSize() == 0)
190 {
191 System.out.println("couldn't find any users");
192 return null;
193 }
194 else
195 {
196 return userQueryResult;
197 }
198 }
199
200 public boolean addUser(String username, String password, String groups, String accountstatus, String comment, String email)
201 {
202 try
203 {
204 conn.setAutoCommit(false);
205 String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + accountstatus + "', '" + comment + "', '" + email + "')";
206 state.execute(sql_insert_user);
207
208 String[] groupArray = groups.split(",");
209 for (String g : groupArray)
210 {
211 String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
212 state.execute(sql_insert_group);
213 }
214
215 conn.commit();
216 }
217 catch (Throwable e)
218 {
219 System.out.println("exception thrown:");
220 if (e instanceof SQLException)
221 {
222 printSQLError((SQLException) e);
223 }
224 else
225 {
226 e.printStackTrace();
227 }
228 closeDatabase();
229 System.out.println("Error:" + e.getMessage());
230 return false;
231 }
232
233 return true;
234 }
235
236 public boolean deleteUser(String del_username)
237 {
238 try
239 {
240 conn.setAutoCommit(false);
241 String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
242 String sql_delete_groups = "delete from " + ROLES + " where username='" + del_username + "'";
243 state.execute(sql_delete_user);
244 state.execute(sql_delete_groups);
245 conn.commit();
246 }
247 catch (Throwable e)
248 {
249 System.out.println("exception thrown:");
250 if (e instanceof SQLException)
251 {
252 printSQLError((SQLException) e);
253 }
254 else
255 {
256 e.printStackTrace();
257 }
258 closeDatabase();
259 return false;
260 }
261 return true;
262 }
263
264 public boolean deleteAllUser() throws SQLException
265 {
266 conn.setAutoCommit(false);
267 try
268 {
269 state.execute("delete from " + USERS);
270 state.execute("delete from " + ROLES);
271 conn.commit();
272 }
273 catch (Throwable e)
274 {
275 System.out.println("exception thrown:");
276 if (e instanceof SQLException)
277 {
278 printSQLError((SQLException) e);
279 }
280 else
281 {
282 e.printStackTrace();
283 }
284 closeDatabase();
285 return false;
286 }
287 return true;
288 }
289
290 public UserQueryResult findUser(String username, String password)
291 {
292 UserQueryResult userQueryResult = new UserQueryResult();
293
294 try
295 {
296 conn.setAutoCommit(false);
297 }
298 catch (Exception ex)
299 {
300 ex.printStackTrace();
301 return null;
302 }
303
304 String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
305 String append_sql = "";
306
307 if (username != null)
308 {
309 append_sql = " WHERE username = '" + username + "'";
310 }
311 if (password != null)
312 {
313 if (append_sql.equals(""))
314 {
315 append_sql = " WHERE password = '" + password + "'";
316 }
317 else
318 {
319 append_sql += " and password = '" + password + "'";
320 }
321 }
322 if (!append_sql.equals(""))
323 {
324 sql_find_user += append_sql;
325 }
326
327 try
328 {
329 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
330 ResultSet rs = state.executeQuery(sql_find_user);
331 while (rs.next())
332 {
333 HashMap<String, String> user = new HashMap<String, String>();
334 user.put("username", rs.getString("username"));
335 user.put("password", rs.getString("password"));
336 user.put("as", rs.getString("accountstatus"));
337 user.put("comment", rs.getString("comment"));
338 user.put("email", rs.getString("email"));
339
340 users.add(user);
341 }
342 conn.commit();
343
344 for (HashMap<String, String> user : users)
345 {
346 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
347
348 String group = "";
349 while (gs.next())
350 {
351 if (!group.equals(""))
352 {
353 group += ",";
354 }
355 group += gs.getString("role");
356 }
357
358 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
359 }
360 }
361 catch (Exception ex)
362 {
363 ex.printStackTrace();
364 return null;
365 }
366
367 if (userQueryResult.getSize() > 0)
368 {
369 return userQueryResult;
370 }
371 else
372 {
373 return null;
374 }
375 }
376
377 // findUser(null) will return all users, which is why a UserQueryResult
378 // (a vector of UserTermInfo) is returned
379 public UserQueryResult findUser(String username) throws SQLException
380 {
381 UserQueryResult userQueryResult = new UserQueryResult();
382
383 conn.setAutoCommit(false);
384 String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
385 String append_sql = "";
386
387 if (username != null)
388 {
389 append_sql = " WHERE username = '" + username + "'";
390 }
391 if (!append_sql.equals(""))
392 {
393 sql_find_user += append_sql;
394 }
395
396 ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
397 ResultSet rs = state.executeQuery(sql_find_user);
398 while (rs.next())
399 {
400 HashMap<String, String> user = new HashMap<String, String>();
401 user.put("username", rs.getString("username"));
402 user.put("password", rs.getString("password"));
403 user.put("as", rs.getString("accountstatus"));
404 user.put("comment", rs.getString("comment"));
405 user.put("email", rs.getString("email"));
406
407 users.add(user);
408 }
409 conn.commit();
410
411 for (HashMap<String, String> user : users)
412 {
413 ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
414
415 String group = "";
416 while (gs.next())
417 {
418 if (!group.equals(""))
419 {
420 group += ",";
421 }
422 group += gs.getString("role");
423 }
424
425 userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
426 }
427
428 if (userQueryResult.getSize() > 0)
429 {
430 return userQueryResult;
431 }
432 else
433 {
434 System.out.println("couldn't find the user");
435 return null;
436 }
437 }
438
439 public String modifyUserInfo(String username, String new_password, String groups, String accountstatus, String comment, String email)
440 {
441 try
442 {
443 conn.setAutoCommit(false);
444 String sql_modify_user_info = "update " + USERS + " set ";
445
446 boolean needComma = false;
447 if (new_password != null && !new_password.equals(""))
448 {
449 sql_modify_user_info += "password='" + new_password + "'";
450 needComma = true;
451 }
452
453 if (accountstatus != null && comment != null)
454 {
455 sql_modify_user_info += (needComma ? "," : "") + " accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
456 needComma = true;
457 }
458
459 if(email != null)
460 {
461 sql_modify_user_info += (needComma ? "," : "") + " email='" + email + "'";
462 }
463
464 sql_modify_user_info += " where username='" + username + "'";
465 state.execute(sql_modify_user_info);
466
467 String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
468 state.execute(sql_delete_groups);
469
470 String[] groupsArray = groups.split(",");
471 for (String g : groupsArray)
472 {
473 String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
474 state.execute(sql_insert_group);
475 }
476
477 conn.commit();
478 }
479 catch (Throwable e)
480 {
481 System.out.println("exception thrown:");
482 if (e instanceof SQLException)
483 {
484 printSQLError((SQLException) e);
485 }
486 else
487 {
488 e.printStackTrace();
489 }
490 closeDatabase();
491 return "Error:" + e.getMessage();
492 }
493 return "succeed";
494 }
495
496 public void db2txt()
497 {
498 System.err.println(db2txtString());
499 }
500
501 public String db2txtString()
502 {
503 //String db2txt = "Error in converting db2txt string.";
504 String db2txt = "";
505 try
506 {
507 conn.setAutoCommit(false); // An exception at this line can happen when the GS3 tomcat server is already running
508 // and GS3 is already accessing the usersDB when this function independently tries to
509 // connect to it (via usersDB2txt.java's main(). For an explanation of the possible
510 // reasons, see http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
511 // section "Embedded Derby supports multiple users in one JVM".
512 String sql_list_all_user = "select username, password, accountstatus, comment, email from " + USERS;
513 ResultSet rs = state.executeQuery(sql_list_all_user);
514
515 ArrayList<HashMap<String, String>> infoMap = new ArrayList<HashMap<String, String>>();
516
517 while (rs.next())
518 {
519 HashMap<String, String> userMap = new HashMap<String, String>();
520 userMap.put("username", rs.getString("username"));
521 userMap.put("password", rs.getString("password"));
522 userMap.put("status", rs.getString("accountstatus"));
523 userMap.put("comment", rs.getString("comment"));
524 userMap.put("email", rs.getString("email"));
525 infoMap.add(userMap);
526 }
527 conn.commit();
528
529 StringBuffer buffer = new StringBuffer();//("-------------------------------------");
530 for (HashMap<String, String> user : infoMap)
531 {
532 ResultSet groupsSet = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
533 String returnedGroups = "";
534 while (groupsSet.next())
535 {
536 if (!returnedGroups.equals(""))
537 {
538 returnedGroups += ",";
539 }
540 returnedGroups += groupsSet.getString("role");
541 }
542 conn.commit();
543
544 buffer.append("USERNAME = " + user.get("username"));
545 buffer.append("\nPASSWORD = " + user.get("password"));
546 buffer.append("\nGROUPS = " + returnedGroups);
547 buffer.append("\nSTATUS = " + user.get("status"));
548 buffer.append("\nCOMMENT = " + user.get("comment"));
549 buffer.append("\nEMAIL = " + user.get("email"));
550 buffer.append("\n-------------------------------------\n");
551 }
552 db2txt = buffer.toString();
553
554 conn.commit();
555 closeDatabase();
556 }
557 catch (Exception ex)
558 {
559 ex.printStackTrace();
560 } finally {
561 return db2txt;
562 }
563 }
564
565 static void printSQLError(SQLException e)
566 {
567 while (e != null)
568 {
569 System.out.println(e.toString());
570 e = e.getNextException();
571 }
572 }
573}
Note: See TracBrowser for help on using the repository browser.