KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > DerbyOptions


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.derby;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import javax.management.IntrospectionException JavaDoc;
27 import org.netbeans.api.db.explorer.ConnectionManager;
28 import org.netbeans.api.db.explorer.DatabaseConnection;
29 import org.netbeans.api.db.explorer.DatabaseException;
30 import org.netbeans.api.db.explorer.JDBCDriver;
31 import org.netbeans.api.db.explorer.JDBCDriverManager;
32 import org.netbeans.modules.derby.DerbyOptions;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.Repository;
38 import org.openide.filesystems.URLMapper;
39 import org.openide.modules.InstalledFileLocator;
40 import org.openide.nodes.BeanNode;
41 import org.openide.util.NbBundle;
42 import org.openide.util.NbPreferences;
43
44 /**
45  *
46  * @author Andrei Badea
47  */

48 public class DerbyOptions {
49     private static final DerbyOptions INSTANCE = new DerbyOptions();
50     /**
51      * This system property allows setting a default value for the Derby system home directory.
52      * Its value will be returned by the {@link getSystemHome} method if the
53      * systemHome property is null. See issue 76908.
54      */

55     public static final String JavaDoc NETBEANS_DERBY_SYSTEM_HOME = "netbeans.derby.system.home"; // NOI18N
56

57     static final String JavaDoc PROP_DERBY_LOCATION = "location"; // NOI18N
58
static final String JavaDoc PROP_DERBY_SYSTEM_HOME = "systemHome"; // NOI18N
59

60     static final String JavaDoc INST_DIR = "db-derby-10.1.1.0"; // NOI18N
61

62     public static final String JavaDoc DRIVER_CLASS_NET = "org.apache.derby.jdbc.ClientDriver"; // NOI18N
63
public static final String JavaDoc DRIVER_CLASS_EMBEDDED = "org.apache.derby.jdbc.EmbeddedDriver"; // NOI18N
64

65     private static final String JavaDoc DRIVER_PATH_NET = "lib/derbyclient.jar"; // NOI18N
66
private static final String JavaDoc DRIVER_PATH_EMBEDDED = "lib/derby.jar"; // NOI18N
67

68     // XXX these should actually be localized, but we'd have to localize
69
// DriverListUtil in the db module first
70
public static final String JavaDoc DRIVER_DISP_NAME_NET = "Java DB (Network)"; // NOI18N
71
public static final String JavaDoc DRIVER_DISP_NAME_EMBEDDED = "Java DB (Embedded)"; // NOI18N
72

73     private static final String JavaDoc DRIVER_NAME_NET = "apache_derby_net"; // NOI18N
74
private static final String JavaDoc DRIVER_NAME_EMBEDDED = "apache_derby_embedded"; // NOI18N
75

76     public static DerbyOptions getDefault() {
77         return INSTANCE;
78     }
79     
80     protected final String JavaDoc putProperty(String JavaDoc key, String JavaDoc value, boolean notify) {
81         String JavaDoc retval = NbPreferences.forModule(DerbyOptions.class).get(key, null);
82         if (value != null) {
83             NbPreferences.forModule(DerbyOptions.class).put(key, value);
84         } else {
85             NbPreferences.forModule(DerbyOptions.class).remove(key);
86         }
87         return retval;
88     }
89
90     protected final String JavaDoc getProperty(String JavaDoc key) {
91         return NbPreferences.forModule(DerbyOptions.class).get(key, null);
92     }
93     
94     public String JavaDoc displayName() {
95         return NbBundle.getMessage(DerbyOptions.class, "LBL_DerbyOptions");
96     }
97     
98     /**
99      * Returns the Derby location or an empty string if the Derby location
100      * is not set. Never returns null.
101      */

102     public String JavaDoc getLocation() {
103         String JavaDoc location = (String JavaDoc)getProperty(PROP_DERBY_LOCATION);
104         if (location == null) {
105             location = ""; // NOI18N
106
}
107         return location;
108     }
109
110     /**
111      * Returns true if the Derby location is null. This method is needed
112      * since getLocation() will never return a null value.
113      */

114     public boolean isLocationNull() {
115         return getProperty(PROP_DERBY_LOCATION) == null;
116     }
117     
118     /**
119      * Sets the Derby location.
120      *
121      * @param location the Derby location. A null value is valid and
122      * will be returned by getLocation() as an empty
123      * string (meaning "not set"). An empty string is valid
124      * and has the meaning "set to the default location".
125      */

126     public void setLocation(String JavaDoc location) {
127         if (location != null && location.length() > 0) {
128             File JavaDoc locationFile = new File JavaDoc(location).getAbsoluteFile();
129             if (!locationFile.exists()) {
130                 String JavaDoc message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryDoesNotExist", locationFile);
131                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(message);
132                 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null);
133                 throw e;
134             }
135             if (!isDerbyInstallLocation(locationFile)) {
136                 String JavaDoc message = NbBundle.getMessage(DerbyOptions.class, "ERR_InvalidDerbyLocation", locationFile);
137                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(message);
138                 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null);
139                 throw e;
140             }
141         }
142         
143         synchronized (this) {
144             stopDerbyServer();
145             if (location != null && location.length() <= 0) {
146                 location = getDefaultInstallLocation();
147             }
148             registerDrivers(location);
149             putProperty(PROP_DERBY_LOCATION, location, true);
150         }
151     }
152     
153     /**
154      * Returns the Derby system home or an emtpy string if the system home
155      * is not set. Never returns null.
156      */

157     public String JavaDoc getSystemHome() {
158         String JavaDoc systemHome = (String JavaDoc)getProperty(PROP_DERBY_SYSTEM_HOME);
159         if (systemHome == null) {
160             systemHome = System.getProperty(NETBEANS_DERBY_SYSTEM_HOME);
161         }
162         if (systemHome == null) {
163             systemHome = ""; // NOI18N
164
}
165         return systemHome;
166     }
167     
168     public void setSystemHome(String JavaDoc derbySystemHome) {
169         if (derbySystemHome != null && derbySystemHome.length() > 0) {
170             File JavaDoc derbySystemHomeFile = new File JavaDoc(derbySystemHome).getAbsoluteFile();
171             if (!derbySystemHomeFile.exists() || !derbySystemHomeFile.isDirectory()) {
172                 String JavaDoc message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryDoesNotExist", derbySystemHomeFile);
173                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(message);
174                 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null);
175                 throw e;
176             }
177             if (!derbySystemHomeFile.canWrite()) {
178                 String JavaDoc message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryIsNotWritable", derbySystemHomeFile);
179                 IllegalArgumentException JavaDoc e = new IllegalArgumentException JavaDoc(message);
180                 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null);
181                 throw e;
182             }
183         }
184         
185         synchronized (this) {
186             stopDerbyServer();
187             putProperty(PROP_DERBY_SYSTEM_HOME, derbySystemHome, true);
188         }
189     }
190     
191     static String JavaDoc getDefaultInstallLocation() {
192         File JavaDoc location = InstalledFileLocator.getDefault().locate(INST_DIR, null, false);
193         if (location == null) {
194             return null;
195         }
196         if (!isDerbyInstallLocation(location)) {
197             return null;
198         }
199         return location.getAbsolutePath();
200     }
201     
202     private static boolean isDerbyInstallLocation(File JavaDoc location) {
203         File JavaDoc libDir = new File JavaDoc(location, "lib"); // NOI18N
204
if (!libDir.exists()) {
205             return false;
206         }
207         File JavaDoc[] libs = libDir.listFiles();
208         if (libs == null || libs.length <= 0) {
209             return false;
210         }
211         return true;
212     }
213     
214     private static void stopDerbyServer() {
215         DatabaseConnection[] dbconn = ConnectionManager.getDefault().getConnections();
216         for (int i = 0; i < dbconn.length; i++) {
217             if (RegisterDerby.getDefault().acceptsDatabaseURL(dbconn[i].getDatabaseURL())) {
218                 ConnectionManager.getDefault().disconnect(dbconn[i]);
219             }
220         }
221         RegisterDerby.getDefault().stop();
222     }
223     
224     private static void registerDrivers(final String JavaDoc newLocation) {
225         try {
226             // registering the drivers in an atomic action so the Drivers node
227
// is refreshed only once
228
Repository.getDefault().getDefaultFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
229                 public void run() {
230                     registerDriver(DRIVER_NAME_NET, DRIVER_DISP_NAME_NET, DRIVER_CLASS_NET, DRIVER_PATH_NET, newLocation);
231                     registerDriver(DRIVER_NAME_EMBEDDED, DRIVER_DISP_NAME_EMBEDDED, DRIVER_CLASS_EMBEDDED, DRIVER_PATH_EMBEDDED, newLocation);
232                 }
233             });
234         } catch (IOException JavaDoc e) {
235             ErrorManager.getDefault().notify(e);
236         }
237     }
238
239     private static void registerDriver(String JavaDoc driverName, String JavaDoc driverDisplayName, String JavaDoc driverClass, String JavaDoc driverRelativeFile, String JavaDoc newLocation) {
240         // try to remove the driver first if it exists was registered from the current location
241
JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers(driverClass);
242         for (int i = 0; i < drivers.length; i++) {
243             JDBCDriver driver = drivers[i];
244             URL JavaDoc[] urls = driver.getURLs();
245             String JavaDoc currentLocation = DerbyOptions.getDefault().getLocation();
246             if (currentLocation == null) {
247                 continue;
248             }
249             
250             boolean fromCurrentLocation = true;
251             
252             for (int j = 0; j < urls.length; j++) {
253                 FileObject fo = URLMapper.findFileObject(urls[j]);
254                 if (fo != null) {
255                     File JavaDoc file = FileUtil.toFile(fo);
256                     if (file != null) {
257                         String JavaDoc driverFile = file.getAbsolutePath();
258                         if (driverFile.startsWith(currentLocation)) {
259                             continue;
260                         }
261                     }
262                 }
263                 fromCurrentLocation = false;
264                 break;
265             }
266             
267             if (fromCurrentLocation) {
268                 try {
269                     JDBCDriverManager.getDefault().removeDriver(driver);
270                 } catch (DatabaseException e) {
271                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
272                 }
273             }
274         }
275         
276         // register the new driver if it exists at the new location
277
if (newLocation != null && newLocation.length() >= 0) {
278             File JavaDoc newDriverFile = new File JavaDoc(newLocation, driverRelativeFile);
279             if (newDriverFile.exists()) {
280                 try {
281                     JDBCDriver newDriver = JDBCDriver.create(driverName, driverDisplayName, driverClass, new URL JavaDoc[] { newDriverFile.toURI().toURL() });
282                     JDBCDriverManager.getDefault().addDriver(newDriver);
283                 } catch (MalformedURLException JavaDoc e) {
284                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
285                 } catch (DatabaseException e) {
286                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
287                 }
288             }
289         }
290     }
291     
292     private static BeanNode createViewNode() throws java.beans.IntrospectionException JavaDoc {
293         return new BeanNode<DerbyOptions>(DerbyOptions.getDefault());
294     }
295 }
296
Popular Tags