KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > DerbyConectionEventListener


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer;
21
22 import java.io.File JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26 import org.openide.ErrorManager;
27
28 /**
29  * This class receives notifications about connection events which
30  * are used to apply the hacks for Derby. It gathers these hacks
31  * together instead of having them spread all over the code.
32  *
33  * <p>In the future and if necessary a ConnectionEventListener interface should be defined,
34  * which this class should implement. It should be possible to register
35  * implementations of the CEL inteface, for example in the layer.</p>
36  *
37  * @author Andrei Badea
38  */

39 public class DerbyConectionEventListener {
40     
41     // XXX this class should be called DerbyConnectionEventListener (double 'n' in connection)
42

43     private static final DerbyConectionEventListener DEFAULT = new DerbyConectionEventListener();
44     
45     private static final String JavaDoc DERBY_DATABASE_FORCE_LOCK = "derby.database.forceDatabaseLock"; // NOI18N
46
private static final String JavaDoc DERBY_SYSTEM_HOME = "derby.system.home"; // NOI18N
47
private static final String JavaDoc DERBY_SYSTEM_SHUTDOWN_STATE = "XJ015"; // NOI18N
48

49     public static DerbyConectionEventListener getDefault() {
50         return DEFAULT;
51     }
52
53     /**
54      * Called before a database connection is connected.
55      *
56      * @param dbconn the database connection.
57      */

58     public void beforeConnect(DatabaseConnection dbconn) {
59         if (!dbconn.getDriver().equals("org.apache.derby.jdbc.EmbeddedDriver")) { // NOI18N
60
return;
61         }
62         
63         // force the database lock -- useful on Linux, see issue 63957
64
if (System.getProperty(DERBY_DATABASE_FORCE_LOCK) == null) {
65             System.setProperty(DERBY_DATABASE_FORCE_LOCK, "true"); // NOI18N
66
}
67         
68         // set the system directory, see issue 64316
69
if (System.getProperty(DERBY_SYSTEM_HOME) == null) { // NOI18N
70
File JavaDoc derbySystemHome = new File JavaDoc(System.getProperty("netbeans.user"), "derby"); // NOI18N
71
derbySystemHome.mkdirs();
72             System.setProperty(DERBY_SYSTEM_HOME, derbySystemHome.getAbsolutePath()); // NOI18N
73
}
74     }
75     
76     /**
77      * Called after a database connection was disconnected.
78      *
79      * @param dbconn the database connection.
80      * @param conn the closed {@link java.sql.Connection}. This parameter is needed since dbconn.getConnection()
81      * returns null at the moment when afterDisconnect is called.
82      */

83     public void afterDisconnect(DatabaseConnection dbconn, Connection JavaDoc conn) {
84         if (!dbconn.getDriver().equals("org.apache.derby.jdbc.EmbeddedDriver")) { // NOI18N
85
return;
86         }
87         
88         // shutdown the Derby database instance
89
try {
90             DbDriverManager.getDefault().getSameDriverConnection(conn, "jdbc:derby:;shutdown=true", new Properties JavaDoc()); // NOI18N
91
} catch (SQLException JavaDoc e) {
92             if (!DERBY_SYSTEM_SHUTDOWN_STATE.equals(e.getSQLState())) { // NOI18N
93
ErrorManager.getDefault().notify(e);
94             }
95         }
96     }
97 }
98
Popular Tags