Lines 44-61
Link Here
|
44 |
|
44 |
|
45 |
package org.netbeans.api.db.explorer; |
45 |
package org.netbeans.api.db.explorer; |
46 |
|
46 |
|
|
|
47 |
import java.net.MalformedURLException; |
47 |
import java.net.URL; |
48 |
import java.net.URL; |
48 |
import java.sql.Driver; |
49 |
import java.sql.Driver; |
49 |
import java.sql.SQLException; |
50 |
import java.sql.SQLException; |
|
|
51 |
import java.util.ArrayList; |
50 |
import java.util.Arrays; |
52 |
import java.util.Arrays; |
|
|
53 |
import java.util.HashSet; |
54 |
import java.util.List; |
51 |
import java.util.Objects; |
55 |
import java.util.Objects; |
|
|
56 |
import java.util.Set; |
57 |
import java.util.logging.Level; |
58 |
import java.util.logging.Logger; |
59 |
import java.util.regex.Matcher; |
60 |
import java.util.regex.Pattern; |
61 |
import org.netbeans.api.project.libraries.Library; |
62 |
import org.netbeans.api.project.libraries.LibraryManager; |
52 |
import org.netbeans.modules.db.explorer.DbDriverManager; |
63 |
import org.netbeans.modules.db.explorer.DbDriverManager; |
|
|
64 |
import org.netbeans.spi.project.libraries.LibraryFactory; |
65 |
import org.netbeans.spi.project.libraries.LibraryImplementation3; |
66 |
import org.netbeans.spi.project.libraries.support.LibrariesSupport; |
53 |
|
67 |
|
54 |
/** |
68 |
/** |
55 |
* Encapsulates a JDBC driver. |
69 |
* Encapsulates a JDBC driver. |
56 |
*/ |
70 |
*/ |
57 |
public final class JDBCDriver { |
71 |
public final class JDBCDriver { |
58 |
|
72 |
|
|
|
73 |
private static Pattern jarFileNamePattern = null; |
59 |
private URL[] urls; |
74 |
private URL[] urls; |
60 |
private String clazz; |
75 |
private String clazz; |
61 |
private String displayName; |
76 |
private String displayName; |
Lines 140-145
Link Here
|
140 |
throw new DatabaseException(sqle); |
155 |
throw new DatabaseException(sqle); |
141 |
} |
156 |
} |
142 |
} |
157 |
} |
|
|
158 |
|
159 |
/** |
160 |
* Get library corresponding to the JDBC driver. If corresponding registered |
161 |
* library is found, it will be used (it applies only for drivers that are |
162 |
* shipped with NetBeans IDE). Otherwise a new library is created. |
163 |
* |
164 |
* @return The library that contains the JDBC Driver. A existing registered |
165 |
* library, or a newly created one. If the library cannot be created (e.g. |
166 |
* there are no JAR files associated with the driver), return null. |
167 |
*/ |
168 |
public Library getDriverLibrary() { |
169 |
String libraryName = null; |
170 |
switch (getClassName()) { |
171 |
case "com.mysql.jdbc.Driver": //NOI18N |
172 |
libraryName = "MySQLDriver"; //NOI18N |
173 |
break; |
174 |
case "org.postgresql.Driver": //NOI18N |
175 |
libraryName = "PostgreSQLDriver"; //NOI18N |
176 |
break; |
177 |
case "org.apache.derby.jdbc.EmbeddedDriver": //NOI18N |
178 |
case "org.apache.derby.jdbc.ClientDriver": //NOI18N |
179 |
libraryName = "JAVADB_DRIVER_LABEL"; //NOI18N |
180 |
break; |
181 |
} |
182 |
if (libraryName != null) { |
183 |
Library lib = LibraryManager.getDefault().getLibrary(libraryName); |
184 |
if (lib != null && checkLibraryContainsDriverJARs(lib)) { |
185 |
return lib; |
186 |
} |
187 |
} |
188 |
return createLibraryForDriver(); |
189 |
} |
190 |
|
191 |
/** |
192 |
* Check whether the registered library contains all JARs needed by the |
193 |
* driver, and that it doesn't contain extra files. |
194 |
*/ |
195 |
private boolean checkLibraryContainsDriverJARs(Library lib) { |
196 |
Set<String> filesInLib = new HashSet<>(); |
197 |
for (URL url : lib.getContent("classpath")) { //NOI18N |
198 |
String jarFileName = getJarFileName(url); |
199 |
if (jarFileName != null) { |
200 |
filesInLib.add(jarFileName); |
201 |
} |
202 |
} |
203 |
for (URL url : getURLs()) { |
204 |
String jarFileName = getJarFileName(url); |
205 |
if (jarFileName == null || !filesInLib.contains(jarFileName)) { |
206 |
return false; |
207 |
} |
208 |
} |
209 |
int allowedFiles = "JAVADB_DRIVER_LABEL".equals(lib.getName()) //NOI18N |
210 |
? 3 : 1; |
211 |
return filesInLib.size() <= allowedFiles; |
212 |
} |
213 |
|
214 |
/** |
215 |
* Create a library containing all JARs of this JDBC driver. |
216 |
*/ |
217 |
private Library createLibraryForDriver() { |
218 |
List<URL> libraryContentURLs = new ArrayList<>(urls.length); |
219 |
for (URL url : urls) { |
220 |
if (url.toString().matches(".*\\.jar")) {//NOI18N |
221 |
try { |
222 |
libraryContentURLs.add( |
223 |
new URL("jar:" + url.toString() + "!/")); //NOI18N |
224 |
} catch (MalformedURLException ex) { |
225 |
Logger.getLogger(JDBCDriver.class.getName()).log(Level.INFO, |
226 |
null, ex); |
227 |
} |
228 |
} else { |
229 |
return null; |
230 |
} |
231 |
} |
232 |
if (libraryContentURLs.isEmpty()) { |
233 |
return null; |
234 |
} else { |
235 |
LibraryImplementation3 libImpl |
236 |
= LibrariesSupport.createLibraryImplementation3( |
237 |
"j2se", "classpath", "src", "javadoc"); //NOI18N |
238 |
libImpl.setContent("classpath", libraryContentURLs); //NOI18N |
239 |
return LibraryFactory.createLibrary(libImpl); |
240 |
} |
241 |
} |
242 |
|
243 |
/** |
244 |
* Extract name of JAR file (without path and extension) from library |
245 |
* content URL or driver JAR URL |
246 |
*/ |
247 |
private String getJarFileName(URL url) { |
248 |
if (jarFileNamePattern == null) { |
249 |
jarFileNamePattern = Pattern.compile( |
250 |
".*/([^/]*).jar(?:!/)?"); //NOI18N |
251 |
} |
252 |
Matcher m = jarFileNamePattern.matcher(url.toString()); |
253 |
return m.matches() ? m.group(1) : null; |
254 |
} |
143 |
|
255 |
|
144 |
public String toString() { |
256 |
public String toString() { |
145 |
return "JDBCDriver[name='" + name + // NOI18N |
257 |
return "JDBCDriver[name='" + name + // NOI18N |