This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 230841
Collapse All | Expand All

(-)a/db/src/org/netbeans/api/db/explorer/JDBCDriver.java (+112 lines)
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
(-)a/derby/src/org/netbeans/modules/derby/DerbyOptions.java (-3 / +15 lines)
Lines 407-414 Link Here
407
                    outStreamd = derbyLib.getOutputStream(ld);
407
                    outStreamd = derbyLib.getOutputStream(ld);
408
                    osw = new OutputStreamWriter(outStreamd);
408
                    osw = new OutputStreamWriter(outStreamd);
409
                    outd = new BufferedWriter(osw);
409
                    outd = new BufferedWriter(osw);
410
                    outd.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE library PUBLIC \"-//NetBeans//DTD Library Declaration 1.0//EN\" \"http://www.netbeans.org/dtds/library-declaration-1_0.dtd\">\n");//NOI18N
410
                    outd.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //NOI18N
411
                    outd.write("<library version=\"1.0\">\n<name>JAVADB_DRIVER_LABEL</name>\n");//NOI18N
411
                    outd.write("<library version=\"3.0\" xmlns=\"http://www.netbeans.org/ns/library-declaration/3\">\n"); //NOI18N
412
                    outd.write("<name>JAVADB_DRIVER_LABEL</name>\n");//NOI18N
412
                    outd.write("<type>j2se</type>\n");//NOI18N
413
                    outd.write("<type>j2se</type>\n");//NOI18N
413
                    outd.write("<localizing-bundle>org.netbeans.modules.derby.Bundle</localizing-bundle>\n");//NOI18N
414
                    outd.write("<localizing-bundle>org.netbeans.modules.derby.Bundle</localizing-bundle>\n");//NOI18N
414
                    outd.write("<volume>\n<type>classpath</type>\n"); //NOI18N
415
                    outd.write("<volume>\n<type>classpath</type>\n"); //NOI18N
Lines 417-423 Link Here
417
                    outd.write("<resource>jar:" + new File(location.getAbsolutePath() + "/lib/derbynet.jar").toURI().toURL() + "!/</resource>\n"); //NOI18N
418
                    outd.write("<resource>jar:" + new File(location.getAbsolutePath() + "/lib/derbynet.jar").toURI().toURL() + "!/</resource>\n"); //NOI18N
418
                    outd.write("</volume>\n<volume>\n<type>src</type>\n</volume>\n"); //NOI18N
419
                    outd.write("</volume>\n<volume>\n<type>src</type>\n</volume>\n"); //NOI18N
419
                    outd.write("<volume>\n<type>javadoc</type>\n");  //NOI18N
420
                    outd.write("<volume>\n<type>javadoc</type>\n");  //NOI18N
420
                    outd.write("</volume>\n</library>"); //NOI18N
421
                    outd.write("</volume>\n"); //NOI18N
422
                    outd.write("<properties>\n"); //NOI18N
423
                    outd.write("  <property>\n"); //NOI18N
424
                    outd.write("    <name>maven-dependencies</name>\n"); //NOI18N
425
                    outd.write("    <value>\n"); //NOI18N
426
                    outd.write("      org.apache.derby:derby:10.10.1.1:jar\n"); //NOI18N
427
                    outd.write("      org.apache.derby:derbyclient:10.10.1.1:jar\n"); //NOI18N
428
                    outd.write("      org.apache.derby:derbynet:10.10.1.1:jar\n"); //NOI18N
429
                    outd.write("    </value>\n"); //NOI18N
430
                    outd.write("  </property>\n"); //NOI18N
431
                    outd.write("</properties>\n"); //NOI18N
432
                    outd.write("</library>"); //NOI18N
421
                }
433
                }
422
            } finally {
434
            } finally {
423
                if (null != outd) {
435
                if (null != outd) {

Return to bug 230841