This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch vsnetbeans_1603
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/vsnetbeans_1603 by this push:
     new f7f6f605b2e Proper escaping of TNS_ADMIN path. Select JDBC driver with 
defined implementation. (#5363)
f7f6f605b2e is described below

commit f7f6f605b2e63e1010bba642e0afd7e633d3ce5d
Author: Svatopluk Dedic <svatopluk.de...@oracle.com>
AuthorDate: Fri Jan 27 10:59:14 2023 +0100

    Proper escaping of TNS_ADMIN path. Select JDBC driver with defined 
implementation. (#5363)
    
    * Proper escaping of TNS_ADMIN path. Select JDBC driver with defined 
implementation.
    
    * Issue warning if driver w/o code locations is selected.
---
 .../cloud/oracle/actions/DownloadWalletAction.java | 75 +++++++++++++++++++---
 1 file changed, 65 insertions(+), 10 deletions(-)

diff --git 
a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
 
b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
index 81a909b86ea..3e0df5933d3 100644
--- 
a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
+++ 
b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
@@ -20,12 +20,15 @@ package org.netbeans.modules.cloud.oracle.actions;
 
 import java.awt.event.ActionEvent;
 import java.io.IOException;
+import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.text.MessageFormat;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.stream.Collectors;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
@@ -44,6 +47,8 @@ import org.openide.awt.ActionReference;
 import org.openide.awt.ActionReferences;
 import org.openide.awt.ActionRegistration;
 import org.openide.awt.StatusDisplayer;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.BaseUtilities;
 import org.openide.util.ContextAwareAction;
 import org.openide.util.Exceptions;
 import org.openide.util.Lookup;
@@ -71,11 +76,15 @@ import org.openide.util.NbBundle;
     "CTL_DownloadWalletAction=Download Wallet",
     "MSG_WalletDownloaded=Database Wallet was downloaded to {0}",
     "MSG_WalletDownloadedPassword=Database Wallet was downloaded. \nGenerated 
wallet password is: {0}",
-    "MSG_WalletNoConnection=Wallet doesn't contain any connection"
+    "MSG_WalletNoConnection=Wallet doesn't contain any connection",
+    "WARN_DriverWithoutJars=No matching JDBC drivers are configured with code 
location(s). Driver {0} will be associated with the connection, but the " +
+            "connection may fail because driver's code is not loadable. 
Continue ?"
+    
 })
 public class DownloadWalletAction extends AbstractAction implements 
ContextAwareAction {
-
-    private static final String URL_TEMPLATE = 
"jdbc:oracle:thin:@{0}?TNS_ADMIN=\"{1}\""; //NOI18N
+    private static final Logger LOG = 
Logger.getLogger(DownloadWalletAction.class.getName());
+    
+    private static final String URL_TEMPLATE = 
"jdbc:oracle:thin:@{0}?TNS_ADMIN={1}"; //NOI18N
     private final DatabaseItem context;
     private OCIProfile session;
 
@@ -107,7 +116,27 @@ public class DownloadWalletAction extends AbstractAction 
implements ContextAware
             if (p.getDbUser() != null && p.getDbPassword() != null) {
 
                 JDBCDriver[] drivers = 
JDBCDriverManager.getDefault().getDrivers("oracle.jdbc.OracleDriver"); //NOI18N
+                JDBCDriver jarsPresent = null;
+
                 if (drivers.length > 0) {
+
+                    // prefer a driver that actually defines some JARs.
+                    for (JDBCDriver d : drivers) {
+                        if (isAvailable(d)) {
+                            jarsPresent = d;
+                            break;
+                        }
+                    }
+                    if (jarsPresent == null) {
+                        jarsPresent = drivers[0];
+                        LOG.log(Level.WARNING, "Unable to find driver JARs for 
wallet {0}, using fallback driver: {1}", new Object[] { walletPath, 
jarsPresent.getName() });
+                        NotifyDescriptor.Confirmation msg = new 
NotifyDescriptor.Confirmation(Bundle.WARN_DriverWithoutJars(jarsPresent.getName()),
 
+                            NotifyDescriptor.WARNING_MESSAGE, 
NotifyDescriptor.YES_NO_OPTION);
+                        Object choice = 
DialogDisplayer.getDefault().notify(msg);
+                        if (choice != NotifyDescriptor.YES_OPTION && choice != 
NotifyDescriptor.OK_OPTION) {
+                            return;
+                        }
+                    }
                     String connectionName = context.getConnectionName();
                     if (connectionName == null) {
                         Optional<String> n = 
parseConnectionNames(walletPath).stream().findFirst();
@@ -118,17 +147,20 @@ public class DownloadWalletAction extends AbstractAction 
implements ContextAware
                             return;
                         }
                     }
-                    String dbUrl = MessageFormat.format(URL_TEMPLATE, 
connectionName, walletPath);
+                    String dbUrl = MessageFormat.format(URL_TEMPLATE, 
connectionName, BaseUtilities.escapeParameters(new String[] { 
walletPath.toString() }));
                     DatabaseConnection dbConn = DatabaseConnection.create(
-                            drivers[0],
-                            dbUrl,
-                            p.getDbUser(),
-                            p.getDbUser(),
-                            new String(p.getDbPassword()),
-                            true,
+                            drivers[0], 
+                            dbUrl, 
+                            p.getDbUser(), 
+                            p.getDbUser(), 
+                            new String(p.getDbPassword()), 
+                            true, 
                             context.getName());
                     ConnectionManager.getDefault().addConnection(dbConn);
                 }
+
+                // PENDING: what should happen, if the driver is not found at 
all - display an info message ?
+
                 DialogDisplayer.getDefault().notifyLater(
                         new NotifyDescriptor.Message(
                                 Bundle.MSG_WalletDownloadedPassword(
@@ -140,6 +172,29 @@ public class DownloadWalletAction extends AbstractAction 
implements ContextAware
             Exceptions.printStackTrace(ex);
         }
     }
+    
+    static boolean isAvailable(JDBCDriver driver) {
+        URL[] urls = driver.getURLs();
+        for (URL u : urls) {
+            if (URLMapper.findFileObject(u) == null) {
+                return false;
+            }
+        }
+        if (urls.length > 0) {
+            // true, some jar is defined && exists.
+            return true;
+        } else {
+            // if the JDBC drive does not list jars, its class must be 
reachable. DbDriverManager uses no-arg classloader constructor, so it is
+            // using systemClassLoader as a parent for no-URL URLClassloader.
+            try {
+                Class.forName(driver.getClassName(), true, 
ClassLoader.getSystemClassLoader());
+                return true;
+            } catch (ClassNotFoundException | SecurityException | LinkageError 
ex) {
+                // expected, class is not avaialble
+                return false;
+            }
+        }
+    }
 
     protected List<String> parseConnectionNames(Path wallet) {
         Path tns = wallet.resolve("tnsnames.ora"); //NOI18N


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to