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

remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new c177e9d  Use DataSource in DataSourceUserDatabase constructor
c177e9d is described below

commit c177e9dadf9e1438da5649116a4dd5d45ad403d1
Author: remm <r...@apache.org>
AuthorDate: Thu Sep 9 11:38:23 2021 +0200

    Use DataSource in DataSourceUserDatabase constructor
    
    Easy to do since there's no support for a local DataSource.
    Also allows much easier and complete testing since a JNDI environment is
    no longer needed.
---
 .../apache/catalina/users/DataSourceUserDatabase.java    | 16 ++++++++--------
 .../catalina/users/DataSourceUserDatabaseFactory.java    | 14 ++++++++++----
 java/org/apache/catalina/users/mbeans-descriptors.xml    |  3 ++-
 webapps/docs/changelog.xml                               |  8 ++++++++
 4 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/java/org/apache/catalina/users/DataSourceUserDatabase.java 
b/java/org/apache/catalina/users/DataSourceUserDatabase.java
index 99cf584..01f6718 100644
--- a/java/org/apache/catalina/users/DataSourceUserDatabase.java
+++ b/java/org/apache/catalina/users/DataSourceUserDatabase.java
@@ -27,7 +27,6 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
-import javax.naming.Context;
 import javax.sql.DataSource;
 
 import org.apache.catalina.Group;
@@ -45,16 +44,16 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
     private static final Log log = 
LogFactory.getLog(DataSourceUserDatabase.class);
     private static final StringManager sm = 
StringManager.getManager(DataSourceUserDatabase.class);
 
-    public DataSourceUserDatabase(Context namingContext, String id) {
-        this.namingContext = namingContext;
+    public DataSourceUserDatabase(DataSource dataSource, String id) {
+        this.dataSource = dataSource;
         this.id = id;
     }
 
 
     /**
-     * Associated naming context (will be used to bet the DataSource).
+     * DataSource to use.
      */
-    protected final Context namingContext;
+    protected final DataSource dataSource;
 
 
     /**
@@ -243,7 +242,7 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
      * @param dataSourceName the name of the JNDI JDBC DataSource
      */
     public void setDataSourceName(String dataSourceName) {
-      this.dataSourceName = dataSourceName;
+        this.dataSourceName = dataSourceName;
     }
 
     /**
@@ -1509,9 +1508,10 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
      * @return Connection to the database
      */
     protected Connection openConnection() {
+        if (dataSource == null) {
+            return null;
+        }
         try {
-            Context context = namingContext;
-            DataSource dataSource = (DataSource) 
context.lookup(dataSourceName);
             Connection connection = dataSource.getConnection();
             connectionSuccess = true;
             return connection;
diff --git a/java/org/apache/catalina/users/DataSourceUserDatabaseFactory.java 
b/java/org/apache/catalina/users/DataSourceUserDatabaseFactory.java
index d288eff..1a69b7f 100644
--- a/java/org/apache/catalina/users/DataSourceUserDatabaseFactory.java
+++ b/java/org/apache/catalina/users/DataSourceUserDatabaseFactory.java
@@ -24,6 +24,7 @@ import javax.naming.Name;
 import javax.naming.RefAddr;
 import javax.naming.Reference;
 import javax.naming.spi.ObjectFactory;
+import javax.sql.DataSource;
 
 
 /**
@@ -78,16 +79,21 @@ public class DataSourceUserDatabaseFactory implements 
ObjectFactory {
             return null;
         }
 
-        // Create and configure a MemoryUserDDataSourceUserDatabaseatabase 
instance based on the
-        // RefAddr values associated with this Reference
-        DataSourceUserDatabase database = new DataSourceUserDatabase(nameCtx, 
name.toString());
+        DataSource dataSource = null;
+        String dataSourceName = null;
         RefAddr ra = null;
 
         ra = ref.get("dataSourceName");
         if (ra != null) {
-            database.setDataSourceName(ra.getContent().toString());
+            dataSourceName = ra.getContent().toString();
+            dataSource = (DataSource) nameCtx.lookup(dataSourceName);
         }
 
+        // Create and configure a DataSourceUserDatabase instance based on the
+        // RefAddr values associated with this Reference
+        DataSourceUserDatabase database = new 
DataSourceUserDatabase(dataSource, name.toString());
+        database.setDataSourceName(dataSourceName);
+
         ra = ref.get("readonly");
         if (ra != null) {
             
database.setReadonly(Boolean.parseBoolean(ra.getContent().toString()));
diff --git a/java/org/apache/catalina/users/mbeans-descriptors.xml 
b/java/org/apache/catalina/users/mbeans-descriptors.xml
index 2175b40..e73df9e 100644
--- a/java/org/apache/catalina/users/mbeans-descriptors.xml
+++ b/java/org/apache/catalina/users/mbeans-descriptors.xml
@@ -298,7 +298,8 @@
 
     <attribute   name="dataSourceName"
           description="The name of the JNDI JDBC DataSource"
-                 type="java.lang.String"/>
+                 type="java.lang.String"
+            writeable="false"/>
 
     <attribute   name="groups"
           description="Names of all defined groups"
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index aa8c1ca..c76bd90 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -105,6 +105,14 @@
   issues do not "pop up" wrt. others).
 -->
 <section name="Tomcat 9.0.54 (remm)" rtext="in development">
+  <subsection name="Catalina">
+    <changelog>
+      <fix>
+        Provide the DataSource in the constructor of
+        <code>DataSourceUserDatabase</code>, since it is always global. (remm)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Coyote">
     <changelog>
       <fix>

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

Reply via email to