001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer;
043:
044: import java.io.File;
045: import java.sql.Connection;
046: import java.sql.SQLException;
047: import java.util.Properties;
048: import org.openide.util.Exceptions;
049:
050: /**
051: * This class receives notifications about connection events which
052: * are used to apply the hacks for Derby. It gathers these hacks
053: * together instead of having them spread all over the code.
054: *
055: * <p>In the future and if necessary a ConnectionEventListener interface should be defined,
056: * which this class should implement. It should be possible to register
057: * implementations of the CEL inteface, for example in the layer.</p>
058: *
059: * @author Andrei Badea
060: */
061: public class DerbyConectionEventListener {
062:
063: // XXX this class should be called DerbyConnectionEventListener (double 'n' in connection)
064:
065: private static final DerbyConectionEventListener DEFAULT = new DerbyConectionEventListener();
066:
067: private static final String DERBY_DATABASE_FORCE_LOCK = "derby.database.forceDatabaseLock"; // NOI18N
068: private static final String DERBY_SYSTEM_HOME = "derby.system.home"; // NOI18N
069: private static final String DERBY_SYSTEM_SHUTDOWN_STATE = "XJ015"; // NOI18N
070:
071: public static DerbyConectionEventListener getDefault() {
072: return DEFAULT;
073: }
074:
075: /**
076: * Called before a database connection is connected.
077: *
078: * @param dbconn the database connection.
079: */
080: public void beforeConnect(DatabaseConnection dbconn) {
081: if (!dbconn.getDriver().equals(
082: "org.apache.derby.jdbc.EmbeddedDriver")) { // NOI18N
083: return;
084: }
085:
086: // force the database lock -- useful on Linux, see issue 63957
087: if (System.getProperty(DERBY_DATABASE_FORCE_LOCK) == null) {
088: System.setProperty(DERBY_DATABASE_FORCE_LOCK, "true"); // NOI18N
089: }
090:
091: // set the system directory, see issue 64316
092: if (System.getProperty(DERBY_SYSTEM_HOME) == null) { // NOI18N
093: File derbySystemHome = new File(System
094: .getProperty("netbeans.user"), "derby"); // NOI18N
095: derbySystemHome.mkdirs();
096: System.setProperty(DERBY_SYSTEM_HOME, derbySystemHome
097: .getAbsolutePath()); // NOI18N
098: }
099: }
100:
101: /**
102: * Called after a database connection was disconnected.
103: *
104: * @param dbconn the database connection.
105: * @param conn the closed {@link java.sql.Connection}. This parameter is needed since dbconn.getConnection()
106: * returns null at the moment when afterDisconnect is called.
107: */
108: public void afterDisconnect(DatabaseConnection dbconn,
109: Connection conn) {
110: if (!dbconn.getDriver().equals(
111: "org.apache.derby.jdbc.EmbeddedDriver")) { // NOI18N
112: return;
113: }
114:
115: // shutdown the Derby database instance
116: try {
117: DbDriverManager.getDefault().getSameDriverConnection(conn,
118: "jdbc:derby:;shutdown=true", new Properties()); // NOI18N
119: } catch (SQLException e) {
120: if (!DERBY_SYSTEM_SHUTDOWN_STATE.equals(e.getSQLState())) { // NOI18N
121: Exceptions.printStackTrace(e);
122: }
123: }
124: }
125: }
|