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.io.FileFilter;
046: import java.io.IOException;
047: import java.io.ObjectInput;
048: import java.io.ObjectOutput;
049: import java.net.URL;
050: import java.util.Enumeration;
051: import java.util.Iterator;
052: import java.util.Map;
053: import java.util.Set;
054: import java.util.Vector;
055: import java.util.jar.JarFile;
056: import org.netbeans.api.db.explorer.DatabaseException;
057: import org.netbeans.api.db.explorer.JDBCDriver;
058: import org.netbeans.api.db.explorer.JDBCDriverManager;
059: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
060: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
061: import org.netbeans.modules.db.util.DriverListUtil;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.Repository;
064: import org.openide.options.SystemOption;
065: import org.openide.util.Exceptions;
066: import org.openide.util.NbBundle;
067:
068: /** Root system option. It stores a list of available drivers and open connections.
069: * These connections will be restored at startup, drivers will be placed in Drivers
070: * directory owned by Database node.
071: */
072: public class DatabaseOption extends SystemOption {
073:
074: private static boolean debugMode;
075: private static Vector drivers;
076: private static Vector connections;
077: private static int fetchlimit = 100;
078: private static int fetchstep = 200;
079: private static boolean autoConn = true;
080:
081: public static final String PROP_DEBUG_MODE = "debugMode"; //NOI18N
082:
083: static final long serialVersionUID = -13629330831657810L;
084:
085: public DatabaseOption() {
086: super ();
087: drivers = new Vector();
088: connections = new Vector();
089: debugMode = false;
090:
091: deleteAdaptorsFolder();
092: }
093:
094: /** Returns vector of registered drivers */
095: public Vector getAvailableDrivers() {
096: if (drivers.size() == 0) {
097: //get serialized drivers
098: Map xxx = (Map) DatabaseNodeInfo
099: .getGlobalNodeInfo(DatabaseNode.DRIVER_LIST);
100: drivers = createDrivers(xxx);
101: }
102:
103: return drivers;
104: }
105:
106: public boolean getDebugMode() {
107: return debugMode;
108: }
109:
110: public void setDebugMode(boolean flag) {
111: if (debugMode == flag)
112: return;
113:
114: debugMode = flag;
115: firePropertyChange(PROP_DEBUG_MODE, !debugMode ? Boolean.TRUE
116: : Boolean.FALSE, debugMode ? Boolean.TRUE
117: : Boolean.FALSE);
118: }
119:
120: /** Sets vector of available drivers.
121: * @param c Vector with drivers
122: */
123: public void setAvailableDrivers(Vector c) {
124: drivers = c;
125: }
126:
127: /**
128: * Returns vector of saved connections. Do not use this method to work with
129: * the list of connections, use {@link org.netbeans.modules.db.explorer.ConnectionList}
130: * instead.
131: */
132: Vector getConnections() {
133: if (connections == null)
134: connections = new Vector();
135:
136: return connections;
137: }
138:
139: public void save() {
140: firePropertyChange(null, null, null);
141: }
142:
143: /** Name of the option */
144: public String displayName() {
145: return NbBundle.getBundle(
146: "org.netbeans.modules.db.resources.Bundle").getString(
147: "OptionName"); //NOI18N
148: }
149:
150: /** Description of object */
151: public String toString() {
152: return (drivers != null ? drivers.size() : 0) + " drivers, "
153: + (connections != null ? connections.size() : 0)
154: + " connections"; //NOI18N
155: }
156:
157: /** Writes data
158: * @param out ObjectOutputStream
159: */
160: public void writeExternal(ObjectOutput out) throws IOException {
161: super .writeExternal(out);
162:
163: out.writeObject(null);
164: out.writeObject(getConnections());
165: }
166:
167: /** Reads data
168: * @param in ObjectInputStream
169: */
170: public void readExternal(ObjectInput in) throws IOException,
171: ClassNotFoundException {
172: super .readExternal(in);
173:
174: drivers = (Vector) in.readObject();
175: if (drivers != null)
176: lookForDrivers();
177:
178: connections = (Vector) in.readObject();
179: }
180:
181: private Vector createDrivers(Map drvMap) {
182: Vector def = (Vector) drvMap.get("defaultdriverlist"); //NOI18N
183: Vector rvec = null;
184: if (def != null && def.size() > 0) {
185: rvec = new Vector(def.size());
186: Enumeration defe = def.elements();
187: while (defe.hasMoreElements()) {
188: Object rit = defe.nextElement();
189: String name = (String) ((Map) rit).get("name"); //NOI18N
190: String drv = (String) ((Map) rit).get("driver"); //NOI18N
191: String prefix = (String) ((Map) rit).get("prefix"); //NOI18N
192: String adaptor = (String) ((Map) rit).get("adaptor"); //NOI18N
193: rit = new DatabaseDriver(name, drv, prefix, adaptor);
194: if (rit != null)
195: rvec.add(rit);
196: }
197: } else
198: rvec = new Vector();
199:
200: return rvec;
201: }
202:
203: private void lookForDrivers() {
204: StringBuffer sb = new StringBuffer();
205: sb.append(File.separator);
206: sb.append("lib");
207: sb.append(File.separator);
208: sb.append("ext");
209: String libext = sb.toString();
210: String nbhome = System.getProperty("netbeans.home");
211:
212: preinstallDrivers(nbhome + libext);
213: }
214:
215: private void preinstallDrivers(String dirPath) {
216: File dir = new File(dirPath);
217: if (!dir.isDirectory())
218: return;
219:
220: File[] files = dir.listFiles(new FileFilter() {
221: public boolean accept(File f) {
222: return (f.isDirectory() || f.getName().endsWith(".jar") || f
223: .getName().endsWith(".zip")); //NOI18N
224: }
225: });
226:
227: for (int i = 0; i < files.length; i++) {
228: JarFile jf;
229: String drv;
230:
231: try {
232: jf = new JarFile(files[i]);
233: Set drvs = DriverListUtil.getDrivers();
234: Iterator it = drvs.iterator();
235:
236: while (it.hasNext()) {
237: drv = (String) it.next();
238: if (jf.getEntry(drv.replace('.', '/') + ".class") != null) {//NOI18N
239: String driverName = DriverListUtil
240: .findFreeName(DriverListUtil
241: .getName(drv));
242: JDBCDriver driver = JDBCDriver.create(
243: driverName, driverName, drv,
244: new URL[] { files[i].toURI().toURL() });
245: try {
246: JDBCDriverManager.getDefault().addDriver(
247: driver);
248: } catch (DatabaseException e) {
249: Exceptions.printStackTrace(e);
250: }
251: }
252: }
253: jf.close();
254: } catch (IOException exc) {
255: //PENDING
256: }
257: }
258: }
259:
260: private void deleteAdaptorsFolder() {
261: FileObject fo = Repository.getDefault().getDefaultFileSystem()
262: .findResource("Database"); //NOI18N
263: try {
264: if (fo != null)
265: fo.delete();
266: } catch (IOException exc) {
267: //delete action failed - ignore
268: }
269: }
270: }
|