01: /*
02: * DerbyShutdownHook.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.shutdown;
13:
14: import java.sql.SQLException;
15: import workbench.db.ConnectionMgr;
16: import workbench.db.ConnectionProfile;
17: import workbench.db.DbDriver;
18: import workbench.db.WbConnection;
19: import workbench.log.LogMgr;
20: import workbench.util.ExceptionUtil;
21: import workbench.util.StringUtil;
22:
23: /**
24: * A Shutdown hook for Apache Derby.
25: *
26: * @author support@sql-workbench.net
27: */
28: public class DerbyShutdownHook implements DbShutdownHook {
29:
30: public void shutdown(WbConnection conn) throws SQLException {
31: if (ConnectionMgr.getInstance().isActive(conn))
32: return;
33:
34: conn.close();
35:
36: if (!canShutdown(conn))
37: return;
38:
39: ConnectionProfile prof = conn.getProfile();
40:
41: String drvClass = prof.getDriverclass();
42: String drvName = prof.getDriverName();
43:
44: String url = prof.getUrl();
45: int pos = url.indexOf(";");
46: if (pos < 0)
47: pos = url.length();
48: String command = url.substring(0, pos) + ";shutdown=true";
49:
50: try {
51: DbDriver drv = ConnectionMgr.getInstance()
52: .findDriverByName(drvClass, drvName);
53: LogMgr
54: .logInfo("ConnectionMgr.shutdownDerby()",
55: "Local Derby connection detected. Shutting down engine...");
56: drv.commandConnect(command);
57: } catch (SQLException e) {
58: // This exception is expected!
59: // Cloudscape/Derby reports the shutdown success through an exception
60: LogMgr.logInfo("ConnectionMgr.shutdownDerby()",
61: ExceptionUtil.getDisplay(e));
62: } catch (Throwable th) {
63: LogMgr.logError("ConnectionMgr.shutdownDerby()",
64: "Error when shutting down Cloudscape/Derby", th);
65: }
66: }
67:
68: private boolean canShutdown(WbConnection conn) {
69: String cls = conn.getProfile().getDriverclass();
70:
71: // Never send a shutdown to a Derby server connection
72: if (!cls.equals("org.apache.derby.jdbc.EmbeddedDriver"))
73: return false;
74:
75: String url = conn.getUrl();
76: int pos = StringUtil.indexOf(url, ':', 2);
77: if (pos < 0)
78: return true;
79:
80: String prefix = url.substring(pos + 1);
81:
82: // Do not shutdown Cloudscape server connections!
83: if (url.startsWith(prefix + "net:"))
84: return false;
85:
86: // Derby network URL starts with a //
87: if (url.startsWith(prefix + "//"))
88: return false;
89:
90: return true;
91: }
92: }
|