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

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


The following commit(s) were added to refs/heads/main by this push:
     new ce9762060e Add tableName configuration
ce9762060e is described below

commit ce9762060e2875f0a0bdf94f6b05fc84d8e0e2e5
Author: remm <r...@apache.org>
AuthorDate: Thu Dec 5 23:20:32 2024 +0100

    Add tableName configuration
---
 .../catalina/servlets/DataSourcePropertyStore.java | 78 +++++++++++++++-------
 .../catalina/servlets/TestWebdavPropertyStore.java | 15 +++--
 webapps/docs/changelog.xml                         |  5 ++
 3 files changed, 68 insertions(+), 30 deletions(-)

diff --git a/java/org/apache/catalina/servlets/DataSourcePropertyStore.java 
b/java/org/apache/catalina/servlets/DataSourcePropertyStore.java
index 397f365c81..9044809870 100644
--- a/java/org/apache/catalina/servlets/DataSourcePropertyStore.java
+++ b/java/org/apache/catalina/servlets/DataSourcePropertyStore.java
@@ -41,8 +41,8 @@ import org.apache.tomcat.util.res.StringManager;
 import org.w3c.dom.Node;
 
 /**
- * WebDAV dead properties storage backed by a DataSource. Usually table and 
column names
- * are configurable, but for simplicity this is not the case.
+ * WebDAV dead properties storage using a DataSource.
+ * <p>
  * The schema is:
  * table properties ( path, namespace, name, node )
  * path: the resource path
@@ -55,38 +55,58 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
     protected static final StringManager sm = 
StringManager.getManager(DataSourcePropertyStore.class);
     private final Log log = LogFactory.getLog(DataSourcePropertyStore.class);
 
-    private static String ADD_PROPERTY_STMT = "INSERT INTO properties (path, 
namespace, name, node) VALUES (?, ?, ?, ?)";
-    private static String SET_PROPERTY_STMT = "UPDATE properties SET node = ? 
WHERE path = ? AND namespace = ? AND name = ?";
-    private static String REMOVE_ALL_PROPERTIES_STMT = "DELETE FROM properties 
WHERE path = ?";
-    private static String REMOVE_PROPERTY_STMT = "DELETE FROM properties WHERE 
path = ? AND namespace = ? AND name = ?";
-    private static String GET_PROPERTY_STMT = "SELECT node FROM properties 
WHERE path = ? AND namespace = ? AND name = ?";
-    private static String GET_PROPERTIES_NAMES_STMT = "SELECT namespace, name 
FROM properties WHERE path = ?";
-    private static String GET_PROPERTIES_STMT = "SELECT namespace, name, node 
FROM properties WHERE path = ?";
-    private static String GET_PROPERTIES_NODES_STMT = "SELECT node FROM 
properties WHERE path = ?";
-
     /**
      * DataSource JNDI name, will be prefixed with java:comp/env for the 
lookup.
      */
     private String dataSourceName = "WebdavPropertyStore";
 
+    /**
+     * Table name.
+     */
+    private String tableName = "properties";
+
+    private String addPropertyStatement;
+    private String setPropertyStatement;
+    private String removeAllPropertiesStatement;
+    private String removePropertyStatement;
+    private String getPropertyStatement;
+    private String getPropertiesNameStatement;
+    private String getPropertiesStatement;
+    private String getPropertiesNodeStatement;
+
     private final ReentrantReadWriteLock dbLock = new ReentrantReadWriteLock();
     private final Lock dbReadLock = dbLock.readLock();
     private final Lock dbWriteLock = dbLock.writeLock();
 
     /**
-     * @return the dataSourceName
+     * @return the DataSource JNDI name, will be prefixed with java:comp/env 
for the lookup.
      */
     public String getDataSourceName() {
         return this.dataSourceName;
     }
 
     /**
-     * @param dataSourceName the dataSourceName to set
+     * @param dataSourceName the DataSource JNDI name, will be prefixed with
+     *  java:comp/env for the lookup.
      */
     public void setDataSourceName(String dataSourceName) {
         this.dataSourceName = dataSourceName;
     }
 
+    /**
+     * @return the table name that will be used in the database
+     */
+    public String getTableName() {
+        return this.tableName;
+    }
+
+    /**
+     * @param tableName the table name to use in the database
+     */
+    public void setTableName(String tableName) {
+        this.tableName = tableName;
+    }
+
     /**
      * DataSource instance being used.
      */
@@ -101,6 +121,14 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                 throw new 
IllegalArgumentException(sm.getString("webdavservlet.dataSourceStore.noDataSource",
 dataSourceName), e);
             }
         }
+        addPropertyStatement = "INSERT INTO " + tableName + " (path, 
namespace, name, node) VALUES (?, ?, ?, ?)";
+        setPropertyStatement = "UPDATE " + tableName + " SET node = ? WHERE 
path = ? AND namespace = ? AND name = ?";
+        removeAllPropertiesStatement = "DELETE FROM " + tableName + " WHERE 
path = ?";
+        removePropertyStatement = "DELETE FROM " + tableName + " WHERE path = 
? AND namespace = ? AND name = ?";
+        getPropertyStatement = "SELECT node FROM " + tableName + " WHERE path 
= ? AND namespace = ? AND name = ?";
+        getPropertiesNameStatement = "SELECT namespace, name FROM " + 
tableName + " WHERE path = ?";
+        getPropertiesStatement = "SELECT namespace, name, node FROM " + 
tableName + " WHERE path = ?";
+        getPropertiesNodeStatement = "SELECT node FROM " + tableName + " WHERE 
path = ?";
     }
 
     @Override
@@ -118,7 +146,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
         }
         dbWriteLock.lock();
         try (Connection connection = dataSource.getConnection();
-                PreparedStatement statement = 
connection.prepareStatement(GET_PROPERTIES_STMT)) {
+                PreparedStatement statement = 
connection.prepareStatement(getPropertiesStatement)) {
             statement.setString(1, source);
             if (statement.execute()) {
                 ResultSet rs = statement.getResultSet();
@@ -127,7 +155,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                     String name = rs.getString(2);
                     String node = rs.getString(3);
                     boolean found = false;
-                    try (PreparedStatement statement2 = 
connection.prepareStatement(GET_PROPERTY_STMT)) {
+                    try (PreparedStatement statement2 = 
connection.prepareStatement(getPropertyStatement)) {
                         statement2.setString(1, destination);
                         statement2.setString(2, namespace);
                         statement2.setString(3, name);
@@ -139,7 +167,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                         }
                     }
                     if (found) {
-                        try (PreparedStatement statement2 = 
connection.prepareStatement(SET_PROPERTY_STMT)) {
+                        try (PreparedStatement statement2 = 
connection.prepareStatement(setPropertyStatement)) {
                             statement2.setString(1, node);
                             statement2.setString(2, destination);
                             statement2.setString(3, namespace);
@@ -147,7 +175,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                             statement2.execute();
                         }
                     } else {
-                        try (PreparedStatement statement2 = 
connection.prepareStatement(ADD_PROPERTY_STMT)) {
+                        try (PreparedStatement statement2 = 
connection.prepareStatement(addPropertyStatement)) {
                             statement2.setString(1, destination);
                             statement2.setString(2, namespace);
                             statement2.setString(3, name);
@@ -171,7 +199,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
         }
         dbWriteLock.lock();
         try (Connection connection = dataSource.getConnection();
-                PreparedStatement statement = 
connection.prepareStatement(REMOVE_ALL_PROPERTIES_STMT)) {
+                PreparedStatement statement = 
connection.prepareStatement(removeAllPropertiesStatement)) {
             statement.setString(1, resource);
             statement.execute();
         } catch (SQLException e) {
@@ -190,7 +218,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
             // Add the names of all properties
             dbReadLock.lock();
             try (Connection connection = dataSource.getConnection();
-                    PreparedStatement statement = 
connection.prepareStatement(GET_PROPERTIES_NAMES_STMT)) {
+                    PreparedStatement statement = 
connection.prepareStatement(getPropertiesNameStatement)) {
                 statement.setString(1, resource);
                 if (statement.execute()) {
                     ResultSet rs = statement.getResultSet();
@@ -209,7 +237,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
             // Add a single property
             dbReadLock.lock();
             try (Connection connection = dataSource.getConnection();
-                    PreparedStatement statement = 
connection.prepareStatement(GET_PROPERTY_STMT)) {
+                    PreparedStatement statement = 
connection.prepareStatement(getPropertyStatement)) {
                 statement.setString(1, resource);
                 statement.setString(2, property.getNamespaceURI());
                 statement.setString(3, property.getLocalName());
@@ -230,7 +258,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
             // Add all properties
             dbReadLock.lock();
             try (Connection connection = dataSource.getConnection();
-                    PreparedStatement statement = 
connection.prepareStatement(GET_PROPERTIES_NODES_STMT)) {
+                    PreparedStatement statement = 
connection.prepareStatement(getPropertiesNodeStatement)) {
                 statement.setString(1, resource);
                 if (statement.execute()) {
                     ResultSet rs = statement.getResultSet();
@@ -284,7 +312,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                         String serializedNode = strWriter.toString();
                         boolean found = false;
                         try {
-                            try (PreparedStatement statement = 
connection.prepareStatement(GET_PROPERTY_STMT)) {
+                            try (PreparedStatement statement = 
connection.prepareStatement(getPropertyStatement)) {
                                 statement.setString(1, resource);
                                 statement.setString(2, node.getNamespaceURI());
                                 statement.setString(3, node.getLocalName());
@@ -296,7 +324,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                                 }
                             }
                             if (found) {
-                                try (PreparedStatement statement = 
connection.prepareStatement(SET_PROPERTY_STMT)) {
+                                try (PreparedStatement statement = 
connection.prepareStatement(setPropertyStatement)) {
                                     statement.setString(1, serializedNode);
                                     statement.setString(2, resource);
                                     statement.setString(3, 
node.getNamespaceURI());
@@ -304,7 +332,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                                     statement.execute();
                                 }
                             } else {
-                                try (PreparedStatement statement = 
connection.prepareStatement(ADD_PROPERTY_STMT)) {
+                                try (PreparedStatement statement = 
connection.prepareStatement(addPropertyStatement)) {
                                     statement.setString(1, resource);
                                     statement.setString(2, 
node.getNamespaceURI());
                                     statement.setString(3, 
node.getLocalName());
@@ -320,7 +348,7 @@ public class DataSourcePropertyStore implements 
WebdavServlet.PropertyStore {
                     }
                     if (operation.getUpdateType() == 
PropertyUpdateType.REMOVE) {
                         Node node = operation.getPropertyNode();
-                        try (PreparedStatement statement = 
connection.prepareStatement(REMOVE_PROPERTY_STMT)) {
+                        try (PreparedStatement statement = 
connection.prepareStatement(removePropertyStatement)) {
                             statement.setString(1, resource);
                             statement.setString(2, node.getNamespaceURI());
                             statement.setString(3, node.getLocalName());
diff --git a/test/org/apache/catalina/servlets/TestWebdavPropertyStore.java 
b/test/org/apache/catalina/servlets/TestWebdavPropertyStore.java
index 3f03ff710e..8f1bd2d6d2 100644
--- a/test/org/apache/catalina/servlets/TestWebdavPropertyStore.java
+++ b/test/org/apache/catalina/servlets/TestWebdavPropertyStore.java
@@ -70,11 +70,12 @@ public class TestWebdavPropertyStore extends 
LoggingBaseTest {
             "</V:someprop>";
 
     public static final String SIMPLE_SCHEMA =
-            "create table properties (\n" +
-            "  path         varchar(256) not null,\n" +
-            "  namespace    varchar(64) not null,\n" +
-            "  name         varchar(64) not null,\n" +
-            "  node         varchar(1024) not null" +
+            "CREATE TABLE webdavproperties (\n" +
+            "  path         VARCHAR(1024) NOT NULL,\n" +
+            "  namespace    VARCHAR(64) NOT NULL,\n" +
+            "  name         VARCHAR(64) NOT NULL,\n" +
+            "  node         VARCHAR(2048) NOT NULL,\n" +
+            "  PRIMARY KEY (path, namespace, name)\n" +
             ")";
 
     public static class CustomDataSourcePropertyStore extends 
DataSourcePropertyStore {
@@ -176,7 +177,9 @@ public class TestWebdavPropertyStore extends 
LoggingBaseTest {
         PropertyStore propertyStore = (PropertyStore) 
Class.forName(storeName).getDeclaredConstructor().newInstance();
         if (propertyStore instanceof CustomDataSourcePropertyStore) {
             ((CustomDataSourcePropertyStore) propertyStore).setDataSource(new 
DerbyDataSource());
+            ((CustomDataSourcePropertyStore) 
propertyStore).setTableName("webdavproperties");
         }
+        propertyStore.init();
 
         // Add properties
         ArrayList<ProppatchOperation> operations = new ArrayList<>();
@@ -241,5 +244,7 @@ public class TestWebdavPropertyStore extends 
LoggingBaseTest {
         Assert.assertFalse(propertyStore.propfind("/other/path2", node1, 
false, xmlWriter9));
         Assert.assertTrue(xmlWriter9.toString().isEmpty());
 
+        propertyStore.destroy();
+
     }
 }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 3087e7fa09..4e5ddc1d36 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -134,6 +134,11 @@
         <code>true</code>. (markt)
       </fix>
       <!-- Entries for backport and removal before 12.0.0-M1 below this line 
-->
+      <update>
+        Add <code>tableName</code> configuration on the
+        <code>DataSourcePropertyStore</code> that may be used by the WebDAV
+        Servlet. (remm)
+      </update>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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

Reply via email to