diff -r 8b305349a578 -r 70456e481ad9 db/nbproject/project.properties --- a/db/nbproject/project.properties Fri Feb 08 22:39:38 2008 -0800 +++ b/db/nbproject/project.properties Fri Feb 08 22:43:31 2008 -0800 @@ -37,6 +37,8 @@ # Version 2 license, then the option applies only if the new code is # made subject to such option by the copyright holder. +javac.compilerargs=-Xlint -Xlint:-serial +javac.source=1.5 javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/DatabaseModule.java --- a/db/src/org/netbeans/modules/db/DatabaseModule.java Fri Feb 08 22:39:38 2008 -0800 +++ b/db/src/org/netbeans/modules/db/DatabaseModule.java Fri Feb 08 22:43:31 2008 -0800 @@ -48,7 +48,7 @@ import org.netbeans.modules.db.explorer. import org.netbeans.modules.db.explorer.DatabaseConnection; import org.netbeans.modules.db.explorer.DatabaseNodeChildren; import org.netbeans.modules.db.explorer.nodes.RootNode; -import org.netbeans.modules.db.runtime.DatabaseRuntimeManager; +import org.netbeans.modules.db.server.ServerProviderManager; import org.netbeans.spi.db.explorer.DatabaseRuntime; import org.openide.modules.ModuleInstall; @@ -74,7 +74,7 @@ public class DatabaseModule extends Modu } // stop all running runtimes - DatabaseRuntime[] runtimes = DatabaseRuntimeManager.getDefault().getRuntimes(); + DatabaseRuntime[] runtimes = ServerProviderManager.getDefault().getRuntimes(); for (int i = 0; i < runtimes.length; i++) { if (runtimes[i].isRunning()) { try { diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java Fri Feb 08 22:39:38 2008 -0800 +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java Fri Feb 08 22:43:31 2008 -0800 @@ -80,7 +80,7 @@ import org.netbeans.modules.db.explorer. import org.netbeans.modules.db.explorer.nodes.DatabaseNode; import org.netbeans.modules.db.explorer.nodes.RootNode; -import org.netbeans.modules.db.runtime.DatabaseRuntimeManager; +import org.netbeans.modules.db.server.ServerProviderManager; import org.netbeans.spi.db.explorer.DatabaseRuntime; import org.openide.explorer.ExplorerManager; import org.openide.nodes.Node; @@ -612,7 +612,7 @@ public class DatabaseConnection implemen } private void startRuntimes() { - DatabaseRuntime[] runtimes = DatabaseRuntimeManager.getDefault().getRuntimes(drv); + DatabaseRuntime[] runtimes = ServerProviderManager.getDefault().getRuntimes(drv); for (int i = 0; i < runtimes.length; i++) { DatabaseRuntime runtime = runtimes[i]; diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/actions/ServerConnectAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/src/org/netbeans/modules/db/explorer/actions/ServerConnectAction.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,56 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.db.explorer.actions; + + +import org.openide.nodes.Node; + + +public class ServerConnectAction extends DatabaseAction +{ + // TODO - fix serialversionuid + static final long serialVersionUID =6900032866933824412L; + public void performAction(Node[] activatedNodes) + { + // TODO - bring up connect to server action + } +} diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java --- a/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java Fri Feb 08 22:39:38 2008 -0800 +++ b/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java Fri Feb 08 22:43:31 2008 -0800 @@ -52,6 +52,8 @@ import org.netbeans.modules.db.explorer. import org.netbeans.modules.db.explorer.DatabaseNodeChildren; import org.netbeans.modules.db.explorer.ConnectionList; import org.netbeans.modules.db.explorer.nodes.*; +import org.netbeans.modules.db.server.ServerProviderManager; +import org.netbeans.spi.db.explorer.ServerProvider; public class RootNodeInfo extends DatabaseNodeInfo implements ConnectionOwnerOperations { static final long serialVersionUID =-8079386805046070315L; @@ -63,15 +65,18 @@ public class RootNodeInfo extends Databa } return rootInfo; } + @Override public void initChildren(Vector children) throws DatabaseException { try { + addServerProviders(children); + DatabaseConnection[] cinfos = ConnectionList.getDefault().getConnections(); for (int i = 0; i < cinfos.length; i++) { DatabaseConnection cinfo = cinfos[i]; ConnectionNodeInfo ninfo = createConnectionNodeInfo(cinfo); children.add(ninfo); } - + Repository r = Repository.getDefault(); FileSystem rfs = r.getDefaultFileSystem(); FileObject rootFolder = rfs.getRoot(); @@ -88,6 +93,18 @@ public class RootNodeInfo extends Databa } } + private void addServerProviders(Vector children) throws DatabaseException { + ServerProvider[] providers = + ServerProviderManager.getDefault().getServerProviders(); + + for ( ServerProvider provider : providers ) { + if ( provider.canRegister() ) { + ServerNodeInfo ninfo = createServerNodeInfo(provider); + children.add(ninfo); + } + } + } + private ConnectionNodeInfo createConnectionNodeInfo(DatabaseConnection dbconn) throws DatabaseException { ConnectionNodeInfo ninfo = (ConnectionNodeInfo)createNodeInfo(this, DatabaseNode.CONNECTION); ninfo.setUser(dbconn.getUser()); @@ -98,6 +115,12 @@ public class RootNodeInfo extends Databa return ninfo; } + private ServerNodeInfo createServerNodeInfo(ServerProvider provider) throws DatabaseException { + ServerNodeInfo ninfo = (ServerNodeInfo)createNodeInfo(this, DatabaseNode.SERVER); + ninfo.setProvider(provider); + ninfo.setName(provider.getDisplayName()); + return ninfo; + } public void refreshChildren() throws DatabaseException { // refresh action is empty } diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/infos/ServerNodeInfo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/src/org/netbeans/modules/db/explorer/infos/ServerNodeInfo.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,93 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.db.explorer.infos; + + + +import java.util.Vector; +import org.netbeans.api.db.explorer.DatabaseException; +import org.netbeans.spi.db.explorer.ServerProvider; +import org.openide.nodes.Children; +import org.openide.nodes.Node; + +public class ServerNodeInfo extends DatabaseNodeInfo { + + // TODO - fix serialVersionUID + static final long serialVersionUID =-8322295510950137669L; + + ServerProvider provider; + + /** + * Get the actions for this server node by querying the provider + * + * @return + */ + public Vector getActions() { + assert provider != null; + + Vector actions = super.getActions(); + + actions.addAll(provider.getActions()); + + return actions; + } + + + public void refreshChildren() throws DatabaseException { + Children children = getNode().getChildren(); + Node[] nodes = children.getNodes(); + for (int i = 0; i < nodes.length; i++) { + DatabaseNodeInfo info = (DatabaseNodeInfo)nodes[i].getCookie(DatabaseNodeInfo.class); + info.refreshChildren(); + } + } + + void setProvider(ServerProvider provider) { + this.provider = provider; + } + + public ServerProvider getProvider() { + return provider; + } + + +} diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/nodes/DatabaseNode.java --- a/db/src/org/netbeans/modules/db/explorer/nodes/DatabaseNode.java Fri Feb 08 22:39:38 2008 -0800 +++ b/db/src/org/netbeans/modules/db/explorer/nodes/DatabaseNode.java Fri Feb 08 22:43:31 2008 -0800 @@ -70,6 +70,7 @@ public class DatabaseNode extends Abstra /** Properties */ public static final String ROOT = "root"; //NOI18N public static final String DRIVER_LIST = "driverlist"; //NOI18N + public static final String SERVER = "server"; // NO18N public static final String DRIVER = "driver"; //NOI18N public static final String CONNECTION = "connection"; //NOI18N public static final String CATALOG = "catalog"; //NOI18N diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/explorer/nodes/ServerNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/src/org/netbeans/modules/db/explorer/nodes/ServerNode.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,79 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.db.explorer.nodes; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.IOException; + + +import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; +import org.netbeans.modules.db.explorer.infos.DriverListNodeInfo; +import org.netbeans.modules.db.explorer.infos.ServerNodeInfo; +import org.openide.util.HelpCtx; + +public class ServerNode extends LeafNode implements PropertyChangeListener { + + @Override + public void setInfo(DatabaseNodeInfo info) { + super.setInfo(info); + } + + @Override + public String getShortDescription() { + ServerNodeInfo serverinfo = (ServerNodeInfo)getInfo(); + return serverinfo.getProvider().getShortDescription(); + } + + /** Help context where to find more about the paste type action. + * @return the help context for this action + */ + @Override + public HelpCtx getHelpCtx() { + // TODO - set up help for this node + return new HelpCtx(ServerNode.class); + } + + public void propertyChange(PropertyChangeEvent arg0) { + } + +} diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/resources/explorer.plist --- a/db/src/org/netbeans/modules/db/resources/explorer.plist Fri Feb 08 22:39:38 2008 -0800 +++ b/db/src/org/netbeans/modules/db/resources/explorer.plist Fri Feb 08 22:43:31 2008 -0800 @@ -52,8 +52,9 @@ infoclass = org.netbeans.modules.db.explorer.infos.RootNodeInfo; children_ordering = { org.openide.loaders.DataFolder$FolderNode = 0; - org.netbeans.modules.db.explorer.nodes.DriverListNode = 1; - org.netbeans.modules.db.explorer.nodes.ConnectionNode = 2; + org.netbeans.modules.db.explorer.nodes.ServerNode = 1; + org.netbeans.modules.db.explorer.nodes.DriverListNode = 2; + org.netbeans.modules.db.explorer.nodes.ConnectionNode = 3; }; iconbase = "org/netbeans/modules/db/resources/database"; children = (driverlist); @@ -75,6 +76,38 @@ } ); }; + /** + * Server section. + * This section describes dynamic subnodes in "Databases" node. A server + * allows basic operations against a particular database server, such as + * create a database or open a connection. + * + * A server node is added dynamically if a provider is registered for + * the server (under Databases/Servers in the layer.xml), and if a server + * is detected. + * + * Actions can be added by the provider in their layer file. This is + * described in more detail in the API documentation. + */ + server = { + name = Server; + displayname = "{0}"; + activedisplayname = "{0}"; + class = org.netbeans.modules.db.explorer.nodes.ServerNode; + infoclass = org.netbeans.modules.db.explorer.infos.ServerNodeInfo; + iconbase = "org/netbeans/modules/db/resources/database"; + activeiconbase = "org/netbeans/modules/db/resources/database"; + children = (); + options = { + cut = NO; + copy = NO; + delete = NO; + }; + actions = ( + ); + children_ordering = { + }; + }; /** * Driver list section. diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/runtime/DatabaseRuntimeManager.java --- a/db/src/org/netbeans/modules/db/runtime/DatabaseRuntimeManager.java Fri Feb 08 22:39:38 2008 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun - * Microsystems, Inc. All Rights Reserved. - * - * If you wish your version of this file to be governed by only the CDDL - * or only the GPL Version 2, indicate your decision by adding - * "[Contributor] elects to include this software in this distribution - * under the [CDDL or GPL Version 2] license." If you do not indicate a - * single choice of license, a recipient has the option to distribute - * your version of this file under either the CDDL, the GPL Version 2 or - * to extend the choice of license to its licensees as provided above. - * However, if you add GPL Version 2 code and therefore, elected the GPL - * Version 2 license, then the option applies only if the new code is - * made subject to such option by the copyright holder. - */ - -package org.netbeans.modules.db.runtime; - -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.netbeans.spi.db.explorer.DatabaseRuntime; -import org.openide.util.Lookup; -import org.openide.util.lookup.Lookups; - - -/** - * This class managers the list of registered database runtimes. Database runtimes - * encapsulate instances of a database server which can be automatically started - * by the IDE when a connection is being made to this server. - * - * @see org.netbeans.spi.db.explorer.DatabaseRuntime - * - * @author Nam Nguyen, Andrei Badea - */ -public final class DatabaseRuntimeManager { - - private static final Logger LOGGER = Logger.getLogger(DatabaseRuntimeManager.class.getName()); - private static final boolean LOG = LOGGER.isLoggable(Level.FINE); - - /** - * The path where the runtimes are registered in the SystemFileSystem. - */ - private static final String RUNTIMES_PATH = "Databases/Runtimes"; // NOI18N - - /** - * The singleton database runtime manager instance. - */ - private static DatabaseRuntimeManager DEFAULT = null; - - /** - * The Lookup.Result instance containing all the DatabaseRuntime instances. - */ - private Lookup.Result result = getLookupResult(); - - /** - * Returns the singleton database runtime manager instance. - */ - public static synchronized DatabaseRuntimeManager getDefault() { - if (DEFAULT == null) { - DEFAULT = new DatabaseRuntimeManager(); - } - return DEFAULT; - } - - public DatabaseRuntime[] getRuntimes() { - Collection runtimes = result.allInstances(); - return (DatabaseRuntime[])runtimes.toArray(new DatabaseRuntime[runtimes.size()]); - } - - /** - * Returns the runtimes registered for the specified JDBC driver. - * - * @param jdbcDriverClassName the JDBC driver to search for; must not be null. - * - * @return the runtime registered for the specified JDBC driver or null - * if no runtime is registered for this driver. - * - * @throws NullPointerException if the specified JDBC driver is null. - */ - public DatabaseRuntime[] getRuntimes(String jdbcDriverClassName) { - if (jdbcDriverClassName == null) { - throw new NullPointerException(); - } - List/**/ runtimeList = new LinkedList(); - for (Iterator i = result.allInstances().iterator(); i.hasNext();) { - DatabaseRuntime runtime = (DatabaseRuntime)i.next(); - if (LOG) { - LOGGER.log(Level.FINE, "Runtime: " + runtime.getClass().getName() + " for driver " + runtime.getJDBCDriverClass()); // NOI18N - } - if (jdbcDriverClassName.equals(runtime.getJDBCDriverClass())) { - runtimeList.add(runtime); - } - } - return (DatabaseRuntime[])runtimeList.toArray(new DatabaseRuntime[runtimeList.size()]); - } - - private synchronized Lookup.Result getLookupResult() { - return Lookups.forPath(RUNTIMES_PATH).lookupResult(DatabaseRuntime.class); - } -} diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/modules/db/server/ServerProviderManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/src/org/netbeans/modules/db/server/ServerProviderManager.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,162 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.db.server; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.netbeans.spi.db.explorer.DatabaseRuntime; +import org.netbeans.spi.db.explorer.ServerProvider; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; + + +/** + * This class managers the list of registered database runtimes. Database runtimes + * encapsulate instances of a database server which can be automatically started + * by the IDE when a connection is being made to this server. + * + * @see org.netbeans.spi.db.explorer.DatabaseRuntime + * @see org.netbeans.spi.db.explorer.ServerProvider + * + * @author Nam Nguyen, Andrei Badea, David Van Couvering + */ +public final class ServerProviderManager { + + private static final Logger LOGGER = Logger.getLogger(ServerProviderManager.class.getName()); + private static final boolean LOG = LOGGER.isLoggable(Level.FINE); + + /** + * The path where the runtimes are registered in the SystemFileSystem. + */ + private static final String RUNTIMES_PATH = "Databases/Runtimes"; // NOI18N + + /** + * The singleton database runtime manager instance. + */ + private static ServerProviderManager DEFAULT = null; + + /** + * The Lookup.Result instance containing all the DatabaseRuntime instances. + */ + private Lookup.Result runtimeResult = getRuntimesLookupResult(); + + /** + * The Lookup.Result instance containing all the ServerProvider instances, + * which also provide DatabaseRuntime functionality + */ + private Lookup.Result providerResult = getServerProvidersLookupResult(); + + /** + * Returns the singleton database runtime manager instance. + */ + public static synchronized ServerProviderManager getDefault() { + if (DEFAULT == null) { + DEFAULT = new ServerProviderManager(); + } + return DEFAULT; + } + + /** + * Get all database runtimes + * + * @return all instances that implement the DatabaseRuntime interface + * (which is a subset of the full ServerProvider interface) + * + * @see org.netbeans.spi.db.explorer.DatabaseRuntime + * @see org.netbeans.spi.db.explorer.ServerProvider + */ + public DatabaseRuntime[] getRuntimes() { + ArrayList all = new ArrayList(); + all.addAll(runtimeResult.allInstances()); + all.addAll(providerResult.allInstances()); + return (DatabaseRuntime[])all.toArray(new DatabaseRuntime[all.size()]); + } + + /** + * Returns the runtimes registered for the specified JDBC driver. + * + * @param jdbcDriverClassName the JDBC driver to search for; must not be null. + * + * @return the runtime registered for the specified JDBC driver or null + * if no runtime is registered for this driver. + * + * @throws NullPointerException if the specified JDBC driver is null. + */ + public DatabaseRuntime[] getRuntimes(String jdbcDriverClassName) { + if (jdbcDriverClassName == null) { + throw new NullPointerException(); + } + List runtimeList = new LinkedList(); + DatabaseRuntime[] runtimes = getRuntimes(); + for (DatabaseRuntime runtime : runtimes ) { + if (LOG) { + LOGGER.log(Level.FINE, "Runtime: " + runtime.getClass().getName() + " for driver " + runtime.getJDBCDriverClass()); // NOI18N + } + if (jdbcDriverClassName.equals(runtime.getJDBCDriverClass())) { + runtimeList.add(runtime); + } + } + return (DatabaseRuntime[])runtimeList.toArray(new DatabaseRuntime[runtimeList.size()]); + } + + /** + * Get the list of server providers + * + * @return all instances that implement the full ServerProvider interface + */ + public ServerProvider[] getServerProviders() { + Collection providers = providerResult.allInstances(); + return (ServerProvider[])providers.toArray(new ServerProvider[providers.size()]); + } + + private synchronized Lookup.Result getRuntimesLookupResult() { + return Lookups.forPath(RUNTIMES_PATH).lookupResult(DatabaseRuntime.class); + } + + private synchronized Lookup.Result getServerProvidersLookupResult() { + return Lookups.forPath(RUNTIMES_PATH).lookupResult(ServerProvider.class); + } +} diff -r 8b305349a578 -r 70456e481ad9 db/src/org/netbeans/spi/db/explorer/ServerProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/src/org/netbeans/spi/db/explorer/ServerProvider.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,85 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.spi.db.explorer; + +import java.util.List; + +/** + * Represents an instance of a database server that can be managed. + * + *

Implementations of this class should be put in the Databases/Servers folder + * in the default filesystem.

+ * + *

If the implementation is registered and the canRegister() method returns + * true, the server is added as a node to the Databases folder

+ * + * @author David Van Couvering + */ +public interface ServerProvider extends DatabaseRuntime { + + /** + * Return true if this server can be registered. For example, + * if it's not installed and/or not running, then the provider + * may choose to return false. + * + * If it returns true, then a node for the server will be displayed + * under Services->Databases + * + * @return true if a server is available, false otherwise + */ + boolean canRegister(); + + /** + * @return a list of all actions this server supports + */ + List getActions(); + + /** + * @return the display name for this provider + */ + String getDisplayName(); + + /** + * @return a short description of this provider + */ + public String getShortDescription(); +} diff -r 8b305349a578 -r 70456e481ad9 db/test/unit/src/org/netbeans/modules/db/DatabaseModuleTest.java --- a/db/test/unit/src/org/netbeans/modules/db/DatabaseModuleTest.java Fri Feb 08 22:39:38 2008 -0800 +++ b/db/test/unit/src/org/netbeans/modules/db/DatabaseModuleTest.java Fri Feb 08 22:43:31 2008 -0800 @@ -41,7 +41,7 @@ package org.netbeans.modules.db; -import org.netbeans.modules.db.runtime.DatabaseRuntimeManager; +import org.netbeans.modules.db.server.ServerProviderManager; import org.netbeans.modules.db.test.TestBase; import org.netbeans.spi.db.explorer.DatabaseRuntime; import org.openide.filesystems.FileObject; @@ -73,7 +73,7 @@ public class DatabaseModuleTest extends new DatabaseModule().close(); - DatabaseRuntime[] runtimes = DatabaseRuntimeManager.getDefault().getRuntimes(); + DatabaseRuntime[] runtimes = ServerProviderManager.getDefault().getRuntimes(); int checked = 0; for (int i = 0; i < runtimes.length; i++) { if (runtimes[i] instanceof DatabaseRuntimeImpl) { diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/Bundle.properties --- a/derby/src/org/netbeans/modules/derby/Bundle.properties Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/Bundle.properties Fri Feb 08 22:43:31 2008 -0800 @@ -83,7 +83,11 @@ ERR_DirectoryIsNotWritable=The directory ERR_DirectoryIsNotWritable=The directory {0} is not writable. #Installer -MSG_RegisterJavaDB=Registering the Java DB database. +MSG_RegisterJavaDB=Registering the Java DB server. + +#DerbyServerProvider +LBL_JavaDBServer = Java DB +DSC_JavaDBServer = The registered Java DB Server # unused ERR_NotThere=The Java DB database installation was not found in your copy of NetBeans. diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/CreateDatabaseAction.java --- a/derby/src/org/netbeans/modules/derby/CreateDatabaseAction.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/CreateDatabaseAction.java Fri Feb 08 22:43:31 2008 -0800 @@ -105,7 +105,7 @@ public class CreateDatabaseAction extend } void makeDatabase(String dbname, String user, String password) throws Exception { - RegisterDerby.getDefault().postCreateNewDatabase(dbname, user, password); + DerbyServerProvider.getDefault().postCreateNewDatabase(dbname, user, password); } protected boolean asynchronous() { diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/DerbyOptions.java --- a/derby/src/org/netbeans/modules/derby/DerbyOptions.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/DerbyOptions.java Fri Feb 08 22:43:31 2008 -0800 @@ -250,11 +250,11 @@ public class DerbyOptions { private static void stopDerbyServer() { DatabaseConnection[] dbconn = ConnectionManager.getDefault().getConnections(); for (int i = 0; i < dbconn.length; i++) { - if (RegisterDerby.getDefault().acceptsDatabaseURL(dbconn[i].getDatabaseURL())) { + if (DerbyServerProvider.getDefault().acceptsDatabaseURL(dbconn[i].getDatabaseURL())) { ConnectionManager.getDefault().disconnect(dbconn[i]); } } - RegisterDerby.getDefault().stop(); + DerbyServerProvider.getDefault().stop(); } private static void registerDrivers(final String newLocation) { diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/DerbyServerProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/derby/src/org/netbeans/modules/derby/DerbyServerProvider.java Fri Feb 08 22:43:31 2008 -0800 @@ -0,0 +1,414 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +package org.netbeans.modules.derby; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.java.platform.JavaPlatformManager; +import org.netbeans.api.progress.ProgressHandle; +import org.netbeans.api.progress.ProgressHandleFactory; +import org.netbeans.modules.derby.api.DerbyDatabases; +import org.netbeans.spi.db.explorer.ServerProvider; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.execution.NbProcessDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.NbBundle; +import org.openide.util.RequestProcessor; +import org.openide.util.Utilities; + + +/** + * + * @author Ludo, Petr Jiricka, David Van Couvering + */ +public class DerbyServerProvider implements ServerProvider { + + // XXX this class does too much. Should maybe be split into + // DatabaseRuntimeImpl, DerbyStartStop and the rest. + + // XXX refactor this soon, it is full of race conditions! + + private static final Logger LOGGER = Logger.getLogger(DerbyServerProvider.class.getName()); + private static final boolean LOG = LOGGER.isLoggable(Level.FINE); + + private static final int START_TIMEOUT = 5; // seconds + + private static DerbyServerProvider reg=null; + ArrayList actions = new ArrayList(); + + /** Derby server process */ + static Process process = null; + + /** Creates a new instance of DerbyServerProvider */ + private DerbyServerProvider() { + actions.add(new StartAction()); + actions.add(new StopAction()); + actions.add(new CreateDatabaseAction()); + actions.add(new DerbySettingsAction()); + } + + public static synchronized DerbyServerProvider getDefault(){ + if (reg==null) + reg= new DerbyServerProvider(); + return reg; + } + + /** + * Whether this runtime accepts this connection string. + */ + public boolean acceptsDatabaseURL(String url){ + return url.trim().startsWith("jdbc:derby://localhost"); // NOI18N + } + + /** + * Is database server up and running. + */ + public boolean isRunning(){ + if (process!=null){ + try{ + int e = process.exitValue(); + process=null; + } catch (IllegalThreadStateException e){ + //not exited yet...it's ok + + } + } + return (process!=null); + + } + + public String getJDBCDriverClass() { + return DerbyOptions.DRIVER_CLASS_NET; + } + + /** + * Can the database be started from inside the IDE? + */ + public boolean canStart(){ + // issue 81619: should only try to start if the location is set + return DerbyOptions.getDefault().getLocation().length() > 0; + } + + /** + * Start the database server. + */ + public void start(){ + start(START_TIMEOUT); + } + + public boolean canRegister() { + return true; + } + + public List getActions() { + return actions; + } + + public String getDisplayName() { + return NbBundle.getBundle(DerbyServerProvider.class).getString("LBL_JavaDBServer"); + } + + public String getShortDescription() { + return NbBundle.getBundle(DerbyServerProvider.class).getString("DSC_JavaDBServer"); + } + + private String getNetworkServerClasspath() { + return + Util.getDerbyFile("lib/derby.jar").getAbsolutePath() + File.pathSeparator + + Util.getDerbyFile("lib/derbytools.jar").getAbsolutePath() + File.pathSeparator + + Util.getDerbyFile("lib/derbynet.jar").getAbsolutePath(); // NOI18N + } + + public int getPort() { + return 1527; + } + + /** Posts the creation of the new database to request processor. + */ + void postCreateNewDatabase(final String databaseName, final String user, final String password) throws Exception { + RequestProcessor.getDefault().post(new Runnable() { + public void run () { + try { + // DerbyDatabases.createDatabase would start the database too, but + // doing it beforehand to avoid having two progress bars running + if (!ensureStarted(true)) { + return; + } + + ProgressHandle ph = ProgressHandleFactory.createHandle(NbBundle.getMessage( + DerbyServerProvider.class, "MSG_CreatingDBProgressLabel", databaseName)); + ph.start(); + try { + DerbyDatabases.createDatabase(databaseName, user, password); + } finally { + ph.finish(); + } + } catch (Exception e) { + LOGGER.log(Level.WARNING, null, e); + String message = NbBundle.getMessage(DerbyServerProvider.class, "ERR_CreateDatabase", e.getMessage()); + Util.showInformation(message); + } + } + }); + } + + private String getDerbySystemHome() { + // return System.getProperty("netbeans.user") + File.separator + "derby"; + return DerbyOptions.getDefault().getSystemHome(); + } + + private void createDerbyPropertiesFile() { + File derbyProperties = new File(getDerbySystemHome(), "derby.properties"); + if (derbyProperties.exists()) + return; + Properties derbyProps = new Properties(); + // fill it + if (Utilities.isMac()) { + derbyProps.setProperty("derby.storage.fileSyncTransactionLog", "true"); + } + + // write it out + OutputStream fileos = null; + try { + File derbyPropertiesParent = derbyProperties.getParentFile(); + derbyPropertiesParent.mkdirs(); + fileos = new FileOutputStream(derbyProperties); + derbyProps.store(fileos, NbBundle.getMessage(DerbyServerProvider.class, "MSG_DerbyPropsFile")); + } catch (IOException ex) { + LOGGER.log(Level.WARNING, null, ex); + } finally { + if (fileos != null) { + try { + fileos.close(); + } catch (IOException ex) { + LOGGER.log(Level.WARNING, null, ex); + } + } + } + + } + + private File getInstallLocation() { + String location = DerbyOptions.getDefault().getLocation(); + if (location.equals("")) { // NOI18N + return null; + } + return new File(location); + } + + private String[] getEnvironment() { + String location = DerbyOptions.getDefault().getLocation(); + if (location.equals("")) { // NOI18N + return null; + } + return new String[] { "DERBY_INSTALL=" + location }; // NOI18N + } + + private JavaPlatform getJavaPlatform() { + JavaPlatformManager jpm = JavaPlatformManager.getDefault(); + return jpm.getDefaultPlatform(); + } + + /** + * Start the database server, and wait some time (in milliseconds) to make sure the server is active. + * + * @param waitTime the time to wait. If less than or equal to zero, do not + * wait at all. + * + * @return true if the server is definitely started, false otherwise (the server is + * not started or the status is unknown). If waitTime was + * less than or equal to zero, then always false. + */ + private boolean start(int waitTime){ + if (process!=null){// seems to be already running? + stop(); + } + if (!Util.checkInstallLocation()) { + return false; + } + try { + ExecSupport ee= new ExecSupport(); + ee.setStringToLookFor("" + getPort()); + FileObject javaFO = getJavaPlatform().findTool("java"); + if (javaFO == null) + throw new Exception (NbBundle.getMessage(DerbyServerProvider.class, "EXC_JavaExecutableNotFound")); + String java = FileUtil.toFile(javaFO).getAbsolutePath(); + + // create the derby.properties file + createDerbyPropertiesFile(); + + // java -Dderby.system.home="" -classpath + // "/lib/derby.jar:/lib/derbytools.jar:/lib/derbynet.jar" + // org.apache.derby.drda.NetworkServerControl start + NbProcessDescriptor desc = new NbProcessDescriptor( + java, + "-Dderby.system.home=\"" + getDerbySystemHome() + "\" " + + "-classpath \"" + getNetworkServerClasspath() + "\"" + + " org.apache.derby.drda.NetworkServerControl start" + ); + if (LOG) { + LOGGER.log(Level.FINE, "Running " + desc.getProcessName() + " " + desc.getArguments()); + } + process = desc.exec ( + null, + getEnvironment(), + true, + getInstallLocation() + ); + + ee.displayProcessOutputs(process,NbBundle.getMessage(StartAction.class, "LBL_outputtab")); + if (waitTime > 0) { + // to make sure the server is up and running + return waitStart(ee, waitTime); + } else { + return false; + } + } catch (Exception e) { + Util.showInformation(e.getLocalizedMessage()); + return false; + } + } + + private boolean waitStart(ExecSupport execSupport, int waitTime) { + boolean started = false; + String waitMessage = NbBundle.getMessage(DerbyServerProvider.class, "MSG_StartingDerby"); + ProgressHandle progress = ProgressHandleFactory.createHandle(waitMessage); + progress.start(); + try { + while (!started) { + started = execSupport.waitForMessage(waitTime * 1000); + if (!started) { + String title = NbBundle.getMessage(DerbyServerProvider.class, "LBL_DerbyDatabase"); + String message = NbBundle.getMessage(DerbyServerProvider.class, "MSG_WaitStart", waitTime); + NotifyDescriptor waitConfirmation = new NotifyDescriptor.Confirmation(message, title, NotifyDescriptor.YES_NO_OPTION); + if (DialogDisplayer.getDefault().notify(waitConfirmation) != NotifyDescriptor.YES_OPTION) { + break; + } + } + } + if (!started) { + LOGGER.log(Level.WARNING, "Derby server failed to start"); // NOI18N + } + } finally { + progress.finish(); + } + return started; + } + + /** + * Stop the database server. + */ + public void stop(){ + try { + if (process==null){//nothing to stop... + return; + } + //BufferedWriter processIn = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); + //processIn.write("q\ny\n"); + //processIn.flush(); + String java = FileUtil.toFile(getJavaPlatform().findTool("java")).getAbsolutePath(); + if (java == null) + throw new Exception (NbBundle.getMessage(DerbyServerProvider.class, "EXC_JavaExecutableNotFound")); + // java -Dderby.system.home="" -classpath + // "/lib/derby.jar:/lib/derbytools.jar:/lib/derbynet.jar" + // org.apache.derby.drda.NetworkServerControl shutdown + NbProcessDescriptor desc = new NbProcessDescriptor( + java, + "-Dderby.system.home=\"" + getDerbySystemHome() + "\" " + + "-classpath \"" + getNetworkServerClasspath() + "\"" + + " org.apache.derby.drda.NetworkServerControl shutdown" + ); + if (LOG) { + LOGGER.log(Level.FINE, "Running " + desc.getProcessName() + " " + desc.getArguments()); + } + Process shutwownProcess = desc.exec ( + null, + getEnvironment(), + true, + getInstallLocation() + ); + shutwownProcess.waitFor(); + + process.destroy(); + } + catch (Exception e) { + Util.showInformation(e.getMessage()); + } + finally { + process=null; + } + } + + /** + * Starts the server if necessary, and can wait for it to start if it was + * not already started. + * + * @param waitIfNotStarted true if to wait for a certain period of time for the server to start + * if it is not already started; false otherwise. + * + * @return true if the server is definitely known to be started, false otherwise. + */ + public boolean ensureStarted(boolean waitIfNotStarted) { + if (isRunning()) { + return true; + } + if (!canStart()) { + return false; + } + if (waitIfNotStarted) { + return start(START_TIMEOUT); + } else { + start(0); + return false; + } + } +} diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/RegisterDerby.java --- a/derby/src/org/netbeans/modules/derby/RegisterDerby.java Fri Feb 08 22:39:38 2008 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,404 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun - * Microsystems, Inc. All Rights Reserved. - * - * If you wish your version of this file to be governed by only the CDDL - * or only the GPL Version 2, indicate your decision by adding - * "[Contributor] elects to include this software in this distribution - * under the [CDDL or GPL Version 2] license." If you do not indicate a - * single choice of license, a recipient has the option to distribute - * your version of this file under either the CDDL, the GPL Version 2 or - * to extend the choice of license to its licensees as provided above. - * However, if you add GPL Version 2 code and therefore, elected the GPL - * Version 2 license, then the option applies only if the new code is - * made subject to such option by the copyright holder. - */ - -package org.netbeans.modules.derby; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.netbeans.api.db.explorer.JDBCDriver; -import org.netbeans.api.db.explorer.JDBCDriverManager; -import org.netbeans.api.java.platform.JavaPlatform; -import org.netbeans.api.java.platform.JavaPlatformManager; -import org.netbeans.api.progress.ProgressHandle; -import org.netbeans.api.progress.ProgressHandleFactory; -import org.netbeans.modules.derby.api.DerbyDatabases; -import org.netbeans.spi.db.explorer.DatabaseRuntime; -import org.openide.DialogDisplayer; -import org.openide.NotifyDescriptor; -import org.openide.execution.NbProcessDescriptor; -import org.openide.filesystems.FileObject; -import org.openide.filesystems.FileUtil; -import org.openide.util.NbBundle; -import org.openide.util.RequestProcessor; -import org.openide.util.Utilities; - - -/** - * - * @author Ludo, Petr Jiricka - */ -public class RegisterDerby implements DatabaseRuntime { - - // XXX this class does too much. Should maybe be split into - // DatabaseRuntimeImpl, DerbyStartStop and the rest. - - // XXX refactor this soon, it is full of race conditions! - - private static final Logger LOGGER = Logger.getLogger(RegisterDerby.class.getName()); - private static final boolean LOG = LOGGER.isLoggable(Level.FINE); - - private static final int START_TIMEOUT = 5; // seconds - - private static RegisterDerby reg=null; - - /** Derby server process */ - static Process process = null; - - /** Creates a new instance of RegisterDerby */ - private RegisterDerby() { - } - - public static synchronized RegisterDerby getDefault(){ - if (reg==null) - reg= new RegisterDerby(); - return reg; - } - - /** - * Whether this runtime accepts this connection string. - */ - public boolean acceptsDatabaseURL(String url){ - return url.trim().startsWith("jdbc:derby://localhost"); // NOI18N - } - - /** - * Is database server up and running. - */ - public boolean isRunning(){ - if (process!=null){ - try{ - int e = process.exitValue(); - process=null; - } catch (IllegalThreadStateException e){ - //not exited yet...it's ok - - } - } - return (process!=null); - - } - - public String getJDBCDriverClass() { - return DerbyOptions.DRIVER_CLASS_NET; - } - - /** - * Can the database be started from inside the IDE? - */ - public boolean canStart(){ - // issue 81619: should only try to start if the location is set - return DerbyOptions.getDefault().getLocation().length() > 0; - } - - /** - * Start the database server. - */ - public void start(){ - start(START_TIMEOUT); - } - - private String getNetworkServerClasspath() { - return - Util.getDerbyFile("lib/derby.jar").getAbsolutePath() + File.pathSeparator + - Util.getDerbyFile("lib/derbytools.jar").getAbsolutePath() + File.pathSeparator + - Util.getDerbyFile("lib/derbynet.jar").getAbsolutePath(); // NOI18N - } - - /** - * Returns the registered Derby driver. - */ - private JDBCDriver getRegisteredDerbyDriver() { - JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(DerbyOptions.DRIVER_CLASS_NET); - if (drvs.length > 0) { - return drvs[0]; - } - return null; - } - - public int getPort() { - return 1527; - } - - /** Posts the creation of the new database to request processor. - */ - void postCreateNewDatabase(final String databaseName, final String user, final String password) throws Exception { - RequestProcessor.getDefault().post(new Runnable() { - public void run () { - try { - // DerbyDatabases.createDatabase would start the database too, but - // doing it beforehand to avoid having two progress bars running - if (!ensureStarted(true)) { - return; - } - - ProgressHandle ph = ProgressHandleFactory.createHandle(NbBundle.getMessage( - RegisterDerby.class, "MSG_CreatingDBProgressLabel", databaseName)); - ph.start(); - try { - DerbyDatabases.createDatabase(databaseName, user, password); - } finally { - ph.finish(); - } - } catch (Exception e) { - LOGGER.log(Level.WARNING, null, e); - String message = NbBundle.getMessage(RegisterDerby.class, "ERR_CreateDatabase", e.getMessage()); - Util.showInformation(message); - } - } - }); - } - - private String getDerbySystemHome() { - // return System.getProperty("netbeans.user") + File.separator + "derby"; - return DerbyOptions.getDefault().getSystemHome(); - } - - private void createDerbyPropertiesFile() { - File derbyProperties = new File(getDerbySystemHome(), "derby.properties"); - if (derbyProperties.exists()) - return; - Properties derbyProps = new Properties(); - // fill it - if (Utilities.isMac()) { - derbyProps.setProperty("derby.storage.fileSyncTransactionLog", "true"); - } - - // write it out - OutputStream fileos = null; - try { - File derbyPropertiesParent = derbyProperties.getParentFile(); - derbyPropertiesParent.mkdirs(); - fileos = new FileOutputStream(derbyProperties); - derbyProps.store(fileos, NbBundle.getMessage(RegisterDerby.class, "MSG_DerbyPropsFile")); - } catch (IOException ex) { - LOGGER.log(Level.WARNING, null, ex); - } finally { - if (fileos != null) { - try { - fileos.close(); - } catch (IOException ex) { - LOGGER.log(Level.WARNING, null, ex); - } - } - } - - } - - private File getInstallLocation() { - String location = DerbyOptions.getDefault().getLocation(); - if (location.equals("")) { // NOI18N - return null; - } - return new File(location); - } - - private String[] getEnvironment() { - String location = DerbyOptions.getDefault().getLocation(); - if (location.equals("")) { // NOI18N - return null; - } - return new String[] { "DERBY_INSTALL=" + location }; // NOI18N - } - - private JavaPlatform getJavaPlatform() { - JavaPlatformManager jpm = JavaPlatformManager.getDefault(); - return jpm.getDefaultPlatform(); - } - - /** - * Start the database server, and wait some time (in milliseconds) to make sure the server is active. - * - * @param waitTime the time to wait. If less than or equal to zero, do not - * wait at all. - * - * @return true if the server is definitely started, false otherwise (the server is - * not started or the status is unknown). If waitTime was - * less than or equal to zero, then always false. - */ - private boolean start(int waitTime){ - if (process!=null){// seems to be already running? - stop(); - } - if (!Util.checkInstallLocation()) { - return false; - } - try { - ExecSupport ee= new ExecSupport(); - ee.setStringToLookFor("" + getPort()); - FileObject javaFO = getJavaPlatform().findTool("java"); - if (javaFO == null) - throw new Exception (NbBundle.getMessage(RegisterDerby.class, "EXC_JavaExecutableNotFound")); - String java = FileUtil.toFile(javaFO).getAbsolutePath(); - - // create the derby.properties file - createDerbyPropertiesFile(); - - // java -Dderby.system.home="" -classpath - // "/lib/derby.jar:/lib/derbytools.jar:/lib/derbynet.jar" - // org.apache.derby.drda.NetworkServerControl start - NbProcessDescriptor desc = new NbProcessDescriptor( - java, - "-Dderby.system.home=\"" + getDerbySystemHome() + "\" " + - "-classpath \"" + getNetworkServerClasspath() + "\"" + - " org.apache.derby.drda.NetworkServerControl start" - ); - if (LOG) { - LOGGER.log(Level.FINE, "Running " + desc.getProcessName() + " " + desc.getArguments()); - } - process = desc.exec ( - null, - getEnvironment(), - true, - getInstallLocation() - ); - - ee.displayProcessOutputs(process,NbBundle.getMessage(StartAction.class, "LBL_outputtab")); - if (waitTime > 0) { - // to make sure the server is up and running - return waitStart(ee, waitTime); - } else { - return false; - } - } catch (Exception e) { - Util.showInformation(e.getLocalizedMessage()); - return false; - } - } - - private boolean waitStart(ExecSupport execSupport, int waitTime) { - boolean started = false; - String waitMessage = NbBundle.getMessage(RegisterDerby.class, "MSG_StartingDerby"); - ProgressHandle progress = ProgressHandleFactory.createHandle(waitMessage); - progress.start(); - try { - while (!started) { - started = execSupport.waitForMessage(waitTime * 1000); - if (!started) { - String title = NbBundle.getMessage(RegisterDerby.class, "LBL_DerbyDatabase"); - String message = NbBundle.getMessage(RegisterDerby.class, "MSG_WaitStart", waitTime); - NotifyDescriptor waitConfirmation = new NotifyDescriptor.Confirmation(message, title, NotifyDescriptor.YES_NO_OPTION); - if (DialogDisplayer.getDefault().notify(waitConfirmation) != NotifyDescriptor.YES_OPTION) { - break; - } - } - } - if (!started) { - LOGGER.log(Level.WARNING, "Derby server failed to start"); // NOI18N - } - } finally { - progress.finish(); - } - return started; - } - - /** - * Stop the database server. - */ - public void stop(){ - try { - if (process==null){//nothing to stop... - return; - } - //BufferedWriter processIn = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); - //processIn.write("q\ny\n"); - //processIn.flush(); - String java = FileUtil.toFile(getJavaPlatform().findTool("java")).getAbsolutePath(); - if (java == null) - throw new Exception (NbBundle.getMessage(RegisterDerby.class, "EXC_JavaExecutableNotFound")); - // java -Dderby.system.home="" -classpath - // "/lib/derby.jar:/lib/derbytools.jar:/lib/derbynet.jar" - // org.apache.derby.drda.NetworkServerControl shutdown - NbProcessDescriptor desc = new NbProcessDescriptor( - java, - "-Dderby.system.home=\"" + getDerbySystemHome() + "\" " + - "-classpath \"" + getNetworkServerClasspath() + "\"" + - " org.apache.derby.drda.NetworkServerControl shutdown" - ); - if (LOG) { - LOGGER.log(Level.FINE, "Running " + desc.getProcessName() + " " + desc.getArguments()); - } - Process shutwownProcess = desc.exec ( - null, - getEnvironment(), - true, - getInstallLocation() - ); - shutwownProcess.waitFor(); - - process.destroy(); - } - catch (Exception e) { - Util.showInformation(e.getMessage()); - } - finally { - process=null; - } - } - - /** - * Starts the server if necessary, and can wait for it to start if it was - * not already started. - * - * @param waitIfNotStarted true if to wait for a certain period of time for the server to start - * if it is not already started; false otherwise. - * - * @return true if the server is definitely known to be started, false otherwise. - */ - public boolean ensureStarted(boolean waitIfNotStarted) { - if (isRunning()) { - return true; - } - if (!canStart()) { - return false; - } - if (waitIfNotStarted) { - return start(START_TIMEOUT); - } else { - start(0); - return false; - } - } -} diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/StartAction.java --- a/derby/src/org/netbeans/modules/derby/StartAction.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/StartAction.java Fri Feb 08 22:43:31 2008 -0800 @@ -65,7 +65,7 @@ public class StartAction extends Callabl // return new HelpCtx(RefreshAction.class); } public boolean isEnabled() { - return (Util.hasInstallLocation() && !RegisterDerby.getDefault().isRunning()); + return (Util.hasInstallLocation() && !DerbyServerProvider.getDefault().isRunning()); } public void performAction() { @@ -73,7 +73,7 @@ public class StartAction extends Callabl return; } if (Util.ensureSystemHome()) { - RegisterDerby.getDefault().ensureStarted(false); + DerbyServerProvider.getDefault().ensureStarted(false); } } diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/StopAction.java --- a/derby/src/org/netbeans/modules/derby/StopAction.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/StopAction.java Fri Feb 08 22:43:31 2008 -0800 @@ -59,11 +59,11 @@ public class StopAction extends Callable } public boolean isEnabled() { - return (RegisterDerby.getDefault().isRunning()==true); + return (DerbyServerProvider.getDefault().isRunning()==true); } public void performAction() { - RegisterDerby.getDefault().stop(); + DerbyServerProvider.getDefault().stop(); } diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/api/DerbyDatabases.java --- a/derby/src/org/netbeans/modules/derby/api/DerbyDatabases.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/api/DerbyDatabases.java Fri Feb 08 22:43:31 2008 -0800 @@ -58,7 +58,7 @@ import org.netbeans.modules.derby.DbURLC import org.netbeans.modules.derby.DbURLClassLoader; import org.netbeans.modules.derby.DerbyOptions; import org.netbeans.modules.derby.Util; -import org.netbeans.modules.derby.RegisterDerby; +import org.netbeans.modules.derby.DerbyServerProvider; import org.netbeans.modules.derby.spi.support.DerbySupport; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -216,7 +216,7 @@ public final class DerbyDatabases { } ensureSystemHome(); - if (!RegisterDerby.getDefault().ensureStarted(true)) { + if (!DerbyServerProvider.getDefault().ensureStarted(true)) { throw new DatabaseException("The Derby server did not start"); // NOI18N } @@ -225,7 +225,7 @@ public final class DerbyDatabases { boolean setupAuthentication = (user != null && user.length() >= 0); try { - String url = "jdbc:derby://localhost:" + RegisterDerby.getDefault().getPort() + "/" + databaseName; // NOI18N + String url = "jdbc:derby://localhost:" + DerbyServerProvider.getDefault().getPort() + "/" + databaseName; // NOI18N String urlForCreation = url + ";create=true"; // NOI18N Connection connection = driver.connect(urlForCreation, props); @@ -361,7 +361,7 @@ public final class DerbyDatabases { if (drivers.length == 0) { throw new IllegalStateException("The " + DerbyOptions.DRIVER_DISP_NAME_NET + " driver was not found"); // NOI18N } - DatabaseConnection dbconn = DatabaseConnection.create(drivers[0], "jdbc:derby://localhost:" + RegisterDerby.getDefault().getPort() + "/" + databaseName, user, schema, password, rememberPassword); // NOI18N + DatabaseConnection dbconn = DatabaseConnection.create(drivers[0], "jdbc:derby://localhost:" + DerbyServerProvider.getDefault().getPort() + "/" + databaseName, user, schema, password, rememberPassword); // NOI18N ConnectionManager.getDefault().addConnection(dbconn); return dbconn; } diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/layer.xml --- a/derby/src/org/netbeans/modules/derby/layer.xml Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/layer.xml Fri Feb 08 22:43:31 2008 -0800 @@ -44,13 +44,14 @@ made subject to such option by the copyr - - - + + + + diff -r 8b305349a578 -r 70456e481ad9 derby/src/org/netbeans/modules/derby/spi/support/DerbySupport.java --- a/derby/src/org/netbeans/modules/derby/spi/support/DerbySupport.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/src/org/netbeans/modules/derby/spi/support/DerbySupport.java Fri Feb 08 22:43:31 2008 -0800 @@ -46,7 +46,7 @@ import java.util.logging.Logger; import java.util.logging.Logger; import org.netbeans.modules.derby.DerbyOptions; import org.netbeans.modules.derby.JDKDerbyHelper; -import org.netbeans.modules.derby.RegisterDerby; +import org.netbeans.modules.derby.DerbyServerProvider; import org.openide.filesystems.FileUtil; import org.openide.util.NbBundle; @@ -121,6 +121,6 @@ public class DerbySupport { * @since 1.5 */ public static void ensureStarted() { - RegisterDerby.getDefault().ensureStarted(false); + DerbyServerProvider.getDefault().ensureStarted(false); } } diff -r 8b305349a578 -r 70456e481ad9 derby/test/unit/src/org/netbeans/modules/derby/RegisterDerbyTest.java --- a/derby/test/unit/src/org/netbeans/modules/derby/RegisterDerbyTest.java Fri Feb 08 22:39:38 2008 -0800 +++ b/derby/test/unit/src/org/netbeans/modules/derby/RegisterDerbyTest.java Fri Feb 08 22:43:31 2008 -0800 @@ -56,14 +56,14 @@ public class RegisterDerbyTest extends T } public void testAcceptsDatabaseURL() { - DatabaseRuntime runtime = RegisterDerby.getDefault(); + DatabaseRuntime runtime = DerbyServerProvider.getDefault(); assertTrue(runtime.acceptsDatabaseURL("jdbc:derby://localhost")); assertTrue("Leading spaces should be ignored", runtime.acceptsDatabaseURL(" jdbc:derby://localhost")); assertFalse(runtime.acceptsDatabaseURL("jdbc:derby://remote")); } public void testCanStart() throws Exception { - DatabaseRuntime runtime = RegisterDerby.getDefault(); + DatabaseRuntime runtime = DerbyServerProvider.getDefault(); assertTrue(DerbyOptions.getDefault().getLocation().length() == 0); assertFalse(runtime.canStart());