Lines 44-55
Link Here
|
44 |
|
44 |
|
45 |
package org.netbeans.modules.db.explorer; |
45 |
package org.netbeans.modules.db.explorer; |
46 |
|
46 |
|
|
|
47 |
|
48 |
import java.net.URL; |
47 |
import java.sql.Connection; |
49 |
import java.sql.Connection; |
48 |
import java.sql.Driver; |
50 |
import java.sql.Driver; |
49 |
import java.sql.DriverManager; |
51 |
import java.sql.DriverManager; |
50 |
import java.sql.SQLException; |
52 |
import java.sql.SQLException; |
51 |
import java.util.HashSet; |
53 |
import java.util.HashSet; |
52 |
import java.util.Iterator; |
54 |
import java.util.Iterator; |
|
|
55 |
import java.util.List; |
53 |
import java.util.Map; |
56 |
import java.util.Map; |
54 |
import java.util.Properties; |
57 |
import java.util.Properties; |
55 |
import java.util.Set; |
58 |
import java.util.Set; |
Lines 57-62
Link Here
|
57 |
import java.util.logging.Level; |
60 |
import java.util.logging.Level; |
58 |
import java.util.logging.Logger; |
61 |
import java.util.logging.Logger; |
59 |
import org.netbeans.api.db.explorer.JDBCDriver; |
62 |
import org.netbeans.api.db.explorer.JDBCDriver; |
|
|
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.FileUtil; |
65 |
import org.openide.util.Utilities; |
60 |
|
66 |
|
61 |
/** |
67 |
/** |
62 |
* Class to load drivers and create connections. It can find drivers and connections from |
68 |
* Class to load drivers and create connections. It can find drivers and connections from |
Lines 223-237
Link Here
|
223 |
*/ |
229 |
*/ |
224 |
public Driver getDriver(JDBCDriver jdbcDriver) throws SQLException { |
230 |
public Driver getDriver(JDBCDriver jdbcDriver) throws SQLException { |
225 |
ClassLoader l = getClassLoader(jdbcDriver); |
231 |
ClassLoader l = getClassLoader(jdbcDriver); |
|
|
232 |
Object driver; |
226 |
try { |
233 |
try { |
227 |
return (Driver)Class.forName(jdbcDriver.getClassName(), true, l).newInstance(); |
234 |
driver = Class.forName(jdbcDriver.getClassName(), true, l).newInstance(); |
228 |
} catch (Exception e) { |
235 |
} catch (Exception e) { |
229 |
SQLException sqlex = createDriverNotFoundException(); |
236 |
SQLException sqlex = createDriverNotFoundException(); |
230 |
sqlex.initCause(e); |
237 |
sqlex.initCause(e); |
231 |
throw sqlex; |
238 |
throw sqlex; |
232 |
} |
239 |
} |
|
|
240 |
if (driver instanceof Driver) { |
241 |
return (Driver) driver; |
242 |
} else if (driver instanceof javax.sql.CommonDataSource) { |
243 |
Driver d = findDriverInMetaInfServices(l, jdbcDriver); |
244 |
if (d != null) { |
245 |
return d; |
246 |
} else { |
247 |
throw createDriverNotFoundException(); |
248 |
} |
249 |
} else { |
250 |
throw createDriverNotFoundException(); |
251 |
} |
233 |
} |
252 |
} |
234 |
|
253 |
|
|
|
254 |
/** |
255 |
* Check whether there is a java.sql.Driver specified in META-INF/services, |
256 |
* and if so, try to create it. |
257 |
* |
258 |
* @return The Driver, or null if it is not specified or cannot be created. |
259 |
*/ |
260 |
private Driver findDriverInMetaInfServices(ClassLoader loader, |
261 |
JDBCDriver jdbcDriver) { |
262 |
String className = findSpecifiedDriverClassName(jdbcDriver); |
263 |
try { |
264 |
Driver realDriver = (Driver) Class.forName( |
265 |
className, true, loader).newInstance(); |
266 |
LOGGER.log(Level.INFO, |
267 |
"Class {0} is not valid driver." //NOI18N |
268 |
+ " Using {1} instead", //NOI18N |
269 |
new Object[]{jdbcDriver.getClassName(), className}); |
270 |
return realDriver; |
271 |
} catch (Exception e) { |
272 |
LOGGER.log(Level.FINE, null, e); |
273 |
return null; |
274 |
} |
275 |
} |
276 |
|
277 |
/** |
278 |
* Find JDBC Driver class name as specified in driver JAR file. |
279 |
*/ |
280 |
private String findSpecifiedDriverClassName(JDBCDriver jdbcDriver) { |
281 |
String className = null; |
282 |
URL[] urls = jdbcDriver.getURLs(); |
283 |
for (URL url : urls) { |
284 |
if (url != null) { |
285 |
if (FileUtil.isArchiveFile(url)) { |
286 |
try { |
287 |
FileObject archiveFile = FileUtil.toFileObject( |
288 |
Utilities.toFile(url.toURI())); |
289 |
FileObject root = FileUtil.getArchiveRoot(archiveFile); |
290 |
FileObject servicesDriver = root.getFileObject( |
291 |
"META-INF/services/java.sql.Driver"); //NOI18N |
292 |
List<String> lines = servicesDriver.asLines(); |
293 |
if (!lines.isEmpty() && !lines.get(0).trim().isEmpty()) { |
294 |
className = lines.get(0).trim(); |
295 |
break; |
296 |
} |
297 |
} catch (Exception e) { |
298 |
LOGGER.log(Level.FINE, null, e); |
299 |
} |
300 |
} |
301 |
} |
302 |
} |
303 |
return className; |
304 |
} |
305 |
|
235 |
/** |
306 |
/** |
236 |
* Gets a driver, but can skip DriverManager and doesn't throw SQLException if a driver can't be found. |
307 |
* Gets a driver, but can skip DriverManager and doesn't throw SQLException if a driver can't be found. |
237 |
*/ |
308 |
*/ |