KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import org.netbeans.api.db.explorer.DatabaseException;
35 import org.netbeans.api.db.explorer.JDBCDriver;
36 import org.netbeans.api.db.explorer.JDBCDriverManager;
37 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
38 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
39 import org.netbeans.modules.db.util.DriverListUtil;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.Repository;
43 import org.openide.options.SystemOption;
44 import org.openide.util.NbBundle;
45
46 /** Root system option. It stores a list of available drivers and open connections.
47 * These connections will be restored at startup, drivers will be placed in Drivers
48 * directory owned by Database node.
49 */

50 public class DatabaseOption extends SystemOption {
51     
52     private static boolean debugMode;
53     private static Vector JavaDoc drivers;
54     private static Vector JavaDoc connections;
55     private static int fetchlimit = 100;
56     private static int fetchstep = 200;
57     private static boolean autoConn = true;
58
59     public static final String JavaDoc PROP_DEBUG_MODE = "debugMode"; //NOI18N
60

61     static final long serialVersionUID =-13629330831657810L;
62     
63     public DatabaseOption() {
64         super();
65         drivers = new Vector JavaDoc();
66         connections = new Vector JavaDoc();
67         debugMode = false;
68         
69         deleteAdaptorsFolder();
70     }
71
72     /** Returns vector of registered drivers */
73     public Vector JavaDoc getAvailableDrivers() {
74         if (drivers.size() == 0) {
75             //get serialized drivers
76
Map JavaDoc xxx = (Map JavaDoc) DatabaseNodeInfo.getGlobalNodeInfo(DatabaseNode.DRIVER_LIST);
77             drivers = createDrivers(xxx);
78         }
79         
80         return drivers;
81     }
82
83     public boolean getDebugMode() {
84         return debugMode;
85     }
86
87     public void setDebugMode(boolean flag) {
88         if (debugMode == flag)
89             return;
90         
91         debugMode = flag;
92         firePropertyChange(PROP_DEBUG_MODE, !debugMode ? Boolean.TRUE : Boolean.FALSE, debugMode ? Boolean.TRUE : Boolean.FALSE);
93     }
94
95     /** Sets vector of available drivers.
96     * @param c Vector with drivers
97     */

98     public void setAvailableDrivers(Vector JavaDoc c) {
99         drivers = c;
100     }
101
102     /**
103      * Returns vector of saved connections. Do not use this method to work with
104      * the list of connections, use {@link org.netbeans.modules.db.explorer.ConnectionList}
105      * instead.
106      */

107     Vector JavaDoc getConnections() {
108         if (connections == null)
109             connections = new Vector JavaDoc();
110
111         return connections;
112     }
113
114     public void save() {
115         firePropertyChange(null, null, null);
116     }
117
118     /** Name of the option */
119     public String JavaDoc displayName() {
120         return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("OptionName"); //NOI18N
121
}
122
123     /** Description of object */
124     public String JavaDoc toString() {
125         return (drivers != null ? drivers.size() : 0) + " drivers, " + (connections != null ? connections.size() : 0) + " connections"; //NOI18N
126
}
127     
128     /** Writes data
129     * @param out ObjectOutputStream
130     */

131     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
132         super.writeExternal(out);
133         
134         out.writeObject(null);
135         out.writeObject(getConnections());
136     }
137
138     /** Reads data
139     * @param in ObjectInputStream
140     */

141     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
142         super.readExternal(in);
143         
144         drivers = (Vector JavaDoc) in.readObject();
145         if (drivers != null)
146             lookForDrivers();
147
148         connections = (Vector JavaDoc) in.readObject();
149     }
150         
151     private Vector JavaDoc createDrivers(Map JavaDoc drvMap) {
152         Vector JavaDoc def = (Vector JavaDoc) drvMap.get("defaultdriverlist"); //NOI18N
153
Vector JavaDoc rvec = null;
154         if (def != null && def.size() > 0) {
155             rvec = new Vector JavaDoc(def.size());
156             Enumeration JavaDoc defe = def.elements();
157             while (defe.hasMoreElements()) {
158                 Object JavaDoc rit = defe.nextElement();
159                 String JavaDoc name = (String JavaDoc) ((Map JavaDoc)rit).get("name"); //NOI18N
160
String JavaDoc drv = (String JavaDoc) ((Map JavaDoc)rit).get("driver"); //NOI18N
161
String JavaDoc prefix = (String JavaDoc) ((Map JavaDoc)rit).get("prefix"); //NOI18N
162
String JavaDoc adaptor = (String JavaDoc) ((Map JavaDoc)rit).get("adaptor"); //NOI18N
163
rit = new DatabaseDriver(name, drv, prefix, adaptor);
164                 if (rit != null)
165                     rvec.add(rit);
166             }
167         } else
168             rvec = new Vector JavaDoc();
169         
170         return rvec;
171     }
172     
173     private void lookForDrivers() {
174         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
175         sb.append(File.separator);
176         sb.append("lib");
177         sb.append(File.separator);
178         sb.append("ext");
179         String JavaDoc libext = sb.toString();
180         String JavaDoc nbhome = System.getProperty("netbeans.home");
181         
182         preinstallDrivers(nbhome + libext);
183     }
184     
185     private void preinstallDrivers(String JavaDoc dirPath) {
186         File JavaDoc dir = new File JavaDoc(dirPath);
187         if (!dir.isDirectory())
188             return;
189         
190         File JavaDoc[] files = dir.listFiles(new FileFilter JavaDoc() {
191             public boolean accept(File JavaDoc f) {
192                 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); //NOI18N
193
}
194         });
195         
196         for (int i = 0; i < files.length; i++) {
197             JarFile JavaDoc jf;
198             String JavaDoc drv;
199
200             try {
201                 jf = new JarFile JavaDoc(files[i]);
202                 Set JavaDoc drvs = DriverListUtil.getDrivers();
203                 Iterator JavaDoc it = drvs.iterator();
204                 while (it.hasNext()) {
205                     drv = (String JavaDoc) it.next();
206                     if (jf.getEntry(drv.replace('.', '/') + ".class") != null) {//NOI18N
207
String JavaDoc driverName = DriverListUtil.findFreeName(DriverListUtil.getName(drv));
208                         JDBCDriver driver = JDBCDriver.create(driverName, driverName, drv, new URL JavaDoc[] {files[i].toURI().toURL()});
209                         try {
210                             JDBCDriverManager.getDefault().addDriver(driver);
211                         } catch (DatabaseException e) {
212                             ErrorManager.getDefault().notify(e);
213                         }
214                     }
215                 }
216                 jf.close();
217             } catch (IOException JavaDoc exc) {
218                 //PENDING
219
}
220         }
221     }
222     
223     private void deleteAdaptorsFolder() {
224         FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Database"); //NOI18N
225
try {
226             if (fo != null)
227                 fo.delete();
228         } catch (IOException JavaDoc exc) {
229             //delete action failed - ignore
230
}
231     }
232 }
233
Popular Tags