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-2007 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.visualweb.dataconnectivity.naming;
043:
044: import org.netbeans.api.db.explorer.ConnectionManager;
045: import org.netbeans.api.db.explorer.JDBCDriver;
046: import org.netbeans.api.db.explorer.JDBCDriverListener;
047: import org.netbeans.api.db.explorer.JDBCDriverManager;
048: import org.netbeans.modules.derby.api.DerbyDatabases;
049: import org.netbeans.modules.visualweb.dataconnectivity.utils.SampleDatabaseCreator;
050: import org.openide.util.NbPreferences;
051:
052: /**
053: * Waits for the JDBCDriverManager to register the Derby driver
054: * @author John Baker
055: */
056: public class DerbyWaiter {
057:
058: private static final String DRIVER_CLASS_NET = "org.apache.derby.jdbc.ClientDriver"; // NOI18N
059:
060: private boolean registered;
061: private boolean isMigration; // if user is migrating settings
062:
063: private final JDBCDriverListener jdbcDriverListener = new JDBCDriverListener() {
064: public void driversChanged() {
065: createDatabase("travel", "startup/samples/travel.zip"); //NOI18N
066: createDatabase("vir", "startup/samples/vir.zip"); //NOI18N
067: registerConnections();
068: }
069: };
070:
071: public DerbyWaiter(boolean migration) {
072: isMigration = migration;
073: String sampleDatabaseRegistered = NbPreferences.forModule(
074: SampleDatabaseCreator.class).get(
075: "VISUALWEB_SAMPLE_DATABASE_REGISTERED", ""); // NOI18N
076: if (!migration && sampleDatabaseRegistered.equals("")) { // NOI18N
077: if (JDBCDriverManager.getDefault().getDrivers(
078: DRIVER_CLASS_NET).length == 0) {
079: JDBCDriverManager.getDefault().addDriverListener(
080: jdbcDriverListener);
081: } else {
082: createDatabase("travel", "startup/samples/travel.zip"); //NOI18N
083: createDatabase("vir", "startup/samples/vir.zip"); //NOI18N
084: registerConnections();
085:
086: }
087: }
088: }
089:
090: private void createDatabase(String databaseName,
091: String databaseZipFileLocation) {
092: if (!DerbyDatabases.databaseExists(databaseName)) {
093: SampleDatabaseCreator.createDatabase(databaseName,
094: databaseZipFileLocation); //NOI18N
095: }
096: }
097:
098: private synchronized void registerConnections() {
099: if (registered) {
100: return;
101: }
102:
103: JDBCDriver[] drvsArray = JDBCDriverManager.getDefault()
104: .getDrivers(DRIVER_CLASS_NET);
105: if (isMigration) {
106: DatabaseSettingsImporter.getInstance()
107: .locateAndRegisterDrivers();
108: DatabaseSettingsImporter.getInstance()
109: .locateAndRegisterConnections(true);
110: if (drvsArray.length > 0) {
111: SampleDatabaseCreator.createAll("travel", "travel",
112: "travel", "TRAVEL",
113: "startup/samples/travel.zip", true,
114: "localhost", 1527); //NOI18N
115: SampleDatabaseCreator.createAll("vir", "vir", "vir",
116: "VIR", "startup/samples/vir.zip", true,
117: "localhost", 1527); //NOI18N
118: }
119: return;
120: }
121:
122: // Register sample database
123: if (drvsArray.length > 0) {
124: if (ConnectionManager
125: .getDefault()
126: .getConnection(
127: "jdbc:derby://localhost:1527/travel [travel on TRAVEL]") == null) {
128: SampleDatabaseCreator.createAll("travel", "travel",
129: "travel", "TRAVEL",
130: "startup/samples/travel.zip", true,
131: "localhost", 1527); //NOI18N
132: }
133: if (ConnectionManager.getDefault().getConnection(
134: "jdbc:derby://localhost:1527/vir [vir on VIR]") == null) {
135: SampleDatabaseCreator.createAll("vir", "vir", "vir",
136: "VIR", "startup/samples/vir.zip", true,
137: "localhost", 1527); //NOI18N
138: }
139:
140: registered = true;
141: JDBCDriverManager.getDefault().removeDriverListener(
142: jdbcDriverListener);
143: }
144: }
145: }
|