1 19 20 package org.netbeans.modules.derby; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import javax.management.IntrospectionException ; 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 48 public class DerbyOptions { 49 private static final DerbyOptions INSTANCE = new DerbyOptions(); 50 55 public static final String NETBEANS_DERBY_SYSTEM_HOME = "netbeans.derby.system.home"; 57 static final String PROP_DERBY_LOCATION = "location"; static final String PROP_DERBY_SYSTEM_HOME = "systemHome"; 60 static final String INST_DIR = "db-derby-10.1.1.0"; 62 public static final String DRIVER_CLASS_NET = "org.apache.derby.jdbc.ClientDriver"; public static final String DRIVER_CLASS_EMBEDDED = "org.apache.derby.jdbc.EmbeddedDriver"; 65 private static final String DRIVER_PATH_NET = "lib/derbyclient.jar"; private static final String DRIVER_PATH_EMBEDDED = "lib/derby.jar"; 68 public static final String DRIVER_DISP_NAME_NET = "Java DB (Network)"; public static final String DRIVER_DISP_NAME_EMBEDDED = "Java DB (Embedded)"; 73 private static final String DRIVER_NAME_NET = "apache_derby_net"; private static final String DRIVER_NAME_EMBEDDED = "apache_derby_embedded"; 76 public static DerbyOptions getDefault() { 77 return INSTANCE; 78 } 79 80 protected final String putProperty(String key, String value, boolean notify) { 81 String 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 getProperty(String key) { 91 return NbPreferences.forModule(DerbyOptions.class).get(key, null); 92 } 93 94 public String displayName() { 95 return NbBundle.getMessage(DerbyOptions.class, "LBL_DerbyOptions"); 96 } 97 98 102 public String getLocation() { 103 String location = (String )getProperty(PROP_DERBY_LOCATION); 104 if (location == null) { 105 location = ""; } 107 return location; 108 } 109 110 114 public boolean isLocationNull() { 115 return getProperty(PROP_DERBY_LOCATION) == null; 116 } 117 118 126 public void setLocation(String location) { 127 if (location != null && location.length() > 0) { 128 File locationFile = new File (location).getAbsoluteFile(); 129 if (!locationFile.exists()) { 130 String message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryDoesNotExist", locationFile); 131 IllegalArgumentException e = new IllegalArgumentException (message); 132 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null); 133 throw e; 134 } 135 if (!isDerbyInstallLocation(locationFile)) { 136 String message = NbBundle.getMessage(DerbyOptions.class, "ERR_InvalidDerbyLocation", locationFile); 137 IllegalArgumentException e = new IllegalArgumentException (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 157 public String getSystemHome() { 158 String systemHome = (String )getProperty(PROP_DERBY_SYSTEM_HOME); 159 if (systemHome == null) { 160 systemHome = System.getProperty(NETBEANS_DERBY_SYSTEM_HOME); 161 } 162 if (systemHome == null) { 163 systemHome = ""; } 165 return systemHome; 166 } 167 168 public void setSystemHome(String derbySystemHome) { 169 if (derbySystemHome != null && derbySystemHome.length() > 0) { 170 File derbySystemHomeFile = new File (derbySystemHome).getAbsoluteFile(); 171 if (!derbySystemHomeFile.exists() || !derbySystemHomeFile.isDirectory()) { 172 String message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryDoesNotExist", derbySystemHomeFile); 173 IllegalArgumentException e = new IllegalArgumentException (message); 174 ErrorManager.getDefault().annotate(e, ErrorManager.USER, message, message, null, null); 175 throw e; 176 } 177 if (!derbySystemHomeFile.canWrite()) { 178 String message = NbBundle.getMessage(DerbyOptions.class, "ERR_DirectoryIsNotWritable", derbySystemHomeFile); 179 IllegalArgumentException e = new IllegalArgumentException (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 getDefaultInstallLocation() { 192 File 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 location) { 203 File libDir = new File (location, "lib"); if (!libDir.exists()) { 205 return false; 206 } 207 File [] 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 newLocation) { 225 try { 226 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 e) { 235 ErrorManager.getDefault().notify(e); 236 } 237 } 238 239 private static void registerDriver(String driverName, String driverDisplayName, String driverClass, String driverRelativeFile, String newLocation) { 240 JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers(driverClass); 242 for (int i = 0; i < drivers.length; i++) { 243 JDBCDriver driver = drivers[i]; 244 URL [] urls = driver.getURLs(); 245 String 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 file = FileUtil.toFile(fo); 256 if (file != null) { 257 String 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 if (newLocation != null && newLocation.length() >= 0) { 278 File newDriverFile = new File (newLocation, driverRelativeFile); 279 if (newDriverFile.exists()) { 280 try { 281 JDBCDriver newDriver = JDBCDriver.create(driverName, driverDisplayName, driverClass, new URL [] { newDriverFile.toURI().toURL() }); 282 JDBCDriverManager.getDefault().addDriver(newDriver); 283 } catch (MalformedURLException 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 { 293 return new BeanNode<DerbyOptions>(DerbyOptions.getDefault()); 294 } 295 } 296 | Popular Tags |