KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > runtime > DatabaseRuntimeManager


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.runtime;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.spi.db.explorer.DatabaseRuntime;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.Repository;
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.FolderLookup;
33 import org.openide.util.Lookup;
34
35
36 /**
37  * This class managers the list of registered database runtimes. Database runtimes
38  * encapsulate instances of a database server which can be automatically started
39  * by the IDE when a connection is being made to this server.
40  *
41  * @see org.netbeans.spi.db.explorer.DatabaseRuntime
42  *
43  * @author Nam Nguyen, Andrei Badea
44  */

45 public final class DatabaseRuntimeManager {
46     
47     private static final ErrorManager LOGGER = ErrorManager.getDefault().getInstance(DatabaseRuntimeManager.class.getName());
48     private static final boolean LOG = LOGGER.isLoggable(ErrorManager.INFORMATIONAL);
49     
50     /**
51      * The path where the runtimes are registered in the SystemFileSystem.
52      */

53     private static final String JavaDoc RUNTIMES_PATH = "Databases/Runtimes"; // NOI18N
54

55     /**
56      * The singleton database runtime manager instance.
57      */

58     private static DatabaseRuntimeManager DEFAULT = null;
59     
60     /**
61      * The Lookup.Result instance containing all the DatabaseRuntime instances.
62      */

63     private Lookup.Result result = getLookupResult();
64     
65     /**
66      * Returns the singleton database runtime manager instance.
67      */

68     public static synchronized DatabaseRuntimeManager getDefault() {
69         if (DEFAULT == null) {
70             DEFAULT = new DatabaseRuntimeManager();
71         }
72         return DEFAULT;
73     }
74     
75     public DatabaseRuntime[] getRuntimes() {
76         Collection JavaDoc runtimes = result.allInstances();
77         return (DatabaseRuntime[])runtimes.toArray(new DatabaseRuntime[runtimes.size()]);
78     }
79     
80     /**
81      * Returns the runtimes registered for the specified JDBC driver.
82      *
83      * @param jdbcDriverClassName the JDBC driver to search for; must not be null.
84      *
85      * @return the runtime registered for the specified JDBC driver or null
86      * if no runtime is registered for this driver.
87      *
88      * @throws NullPointerException if the specified JDBC driver is null.
89      */

90     public DatabaseRuntime[] getRuntimes(String JavaDoc jdbcDriverClassName) {
91         if (jdbcDriverClassName == null) {
92             throw new NullPointerException JavaDoc();
93         }
94         List JavaDoc/*<DatabaseRuntime>*/ runtimeList = new LinkedList JavaDoc();
95         for (Iterator JavaDoc i = result.allInstances().iterator(); i.hasNext();) {
96             DatabaseRuntime runtime = (DatabaseRuntime)i.next();
97             if (LOG) {
98                 LOGGER.log(ErrorManager.INFORMATIONAL, "Runtime: " + runtime.getClass().getName() + " for driver " + runtime.getJDBCDriverClass()); // NOI18N
99
}
100             if (jdbcDriverClassName.equals(runtime.getJDBCDriverClass())) {
101                 runtimeList.add(runtime);
102             }
103         }
104         return (DatabaseRuntime[])runtimeList.toArray(new DatabaseRuntime[runtimeList.size()]);
105     }
106     
107     private synchronized Lookup.Result getLookupResult() {
108         if (result == null) {
109             FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(RUNTIMES_PATH);
110             DataFolder folder = DataFolder.findFolder(fo);
111             result = new FolderLookup(folder).getLookup().lookup(new Lookup.Template(DatabaseRuntime.class));
112         }
113         return result;
114     }
115 }
116
Popular Tags