4141import org .w3c .dom .Node ;
4242
4343/**
44- * WebDAV dead properties storage backed by a DataSource. Usually table and column names
45- * are configurable, but for simplicity this is not the case.
44+ * WebDAV dead properties storage using a DataSource.
45+ * <p>
4646 * The schema is:
4747 * table properties ( path, namespace, name, node )
4848 * path: the resource path
@@ -55,38 +55,58 @@ public class DataSourcePropertyStore implements WebdavServlet.PropertyStore {
5555 protected static final StringManager sm = StringManager .getManager (DataSourcePropertyStore .class );
5656 private final Log log = LogFactory .getLog (DataSourcePropertyStore .class );
5757
58- private static String ADD_PROPERTY_STMT = "INSERT INTO properties (path, namespace, name, node) VALUES (?, ?, ?, ?)" ;
59- private static String SET_PROPERTY_STMT = "UPDATE properties SET node = ? WHERE path = ? AND namespace = ? AND name = ?" ;
60- private static String REMOVE_ALL_PROPERTIES_STMT = "DELETE FROM properties WHERE path = ?" ;
61- private static String REMOVE_PROPERTY_STMT = "DELETE FROM properties WHERE path = ? AND namespace = ? AND name = ?" ;
62- private static String GET_PROPERTY_STMT = "SELECT node FROM properties WHERE path = ? AND namespace = ? AND name = ?" ;
63- private static String GET_PROPERTIES_NAMES_STMT = "SELECT namespace, name FROM properties WHERE path = ?" ;
64- private static String GET_PROPERTIES_STMT = "SELECT namespace, name, node FROM properties WHERE path = ?" ;
65- private static String GET_PROPERTIES_NODES_STMT = "SELECT node FROM properties WHERE path = ?" ;
66-
6758 /**
6859 * DataSource JNDI name, will be prefixed with java:comp/env for the lookup.
6960 */
7061 private String dataSourceName = "WebdavPropertyStore" ;
7162
63+ /**
64+ * Table name.
65+ */
66+ private String tableName = "properties" ;
67+
68+ private String addPropertyStatement ;
69+ private String setPropertyStatement ;
70+ private String removeAllPropertiesStatement ;
71+ private String removePropertyStatement ;
72+ private String getPropertyStatement ;
73+ private String getPropertiesNameStatement ;
74+ private String getPropertiesStatement ;
75+ private String getPropertiesNodeStatement ;
76+
7277 private final ReentrantReadWriteLock dbLock = new ReentrantReadWriteLock ();
7378 private final Lock dbReadLock = dbLock .readLock ();
7479 private final Lock dbWriteLock = dbLock .writeLock ();
7580
7681 /**
77- * @return the dataSourceName
82+ * @return the DataSource JNDI name, will be prefixed with java:comp/env for the lookup.
7883 */
7984 public String getDataSourceName () {
8085 return this .dataSourceName ;
8186 }
8287
8388 /**
84- * @param dataSourceName the dataSourceName to set
89+ * @param dataSourceName the DataSource JNDI name, will be prefixed with
90+ * java:comp/env for the lookup.
8591 */
8692 public void setDataSourceName (String dataSourceName ) {
8793 this .dataSourceName = dataSourceName ;
8894 }
8995
96+ /**
97+ * @return the table name that will be used in the database
98+ */
99+ public String getTableName () {
100+ return this .tableName ;
101+ }
102+
103+ /**
104+ * @param tableName the table name to use in the database
105+ */
106+ public void setTableName (String tableName ) {
107+ this .tableName = tableName ;
108+ }
109+
90110 /**
91111 * DataSource instance being used.
92112 */
@@ -101,6 +121,14 @@ public void init() {
101121 throw new IllegalArgumentException (sm .getString ("webdavservlet.dataSourceStore.noDataSource" , dataSourceName ), e );
102122 }
103123 }
124+ addPropertyStatement = "INSERT INTO " + tableName + " (path, namespace, name, node) VALUES (?, ?, ?, ?)" ;
125+ setPropertyStatement = "UPDATE " + tableName + " SET node = ? WHERE path = ? AND namespace = ? AND name = ?" ;
126+ removeAllPropertiesStatement = "DELETE FROM " + tableName + " WHERE path = ?" ;
127+ removePropertyStatement = "DELETE FROM " + tableName + " WHERE path = ? AND namespace = ? AND name = ?" ;
128+ getPropertyStatement = "SELECT node FROM " + tableName + " WHERE path = ? AND namespace = ? AND name = ?" ;
129+ getPropertiesNameStatement = "SELECT namespace, name FROM " + tableName + " WHERE path = ?" ;
130+ getPropertiesStatement = "SELECT namespace, name, node FROM " + tableName + " WHERE path = ?" ;
131+ getPropertiesNodeStatement = "SELECT node FROM " + tableName + " WHERE path = ?" ;
104132 }
105133
106134 @ Override
@@ -118,7 +146,7 @@ public void copy(String source, String destination) {
118146 }
119147 dbWriteLock .lock ();
120148 try (Connection connection = dataSource .getConnection ();
121- PreparedStatement statement = connection .prepareStatement (GET_PROPERTIES_STMT )) {
149+ PreparedStatement statement = connection .prepareStatement (getPropertiesStatement )) {
122150 statement .setString (1 , source );
123151 if (statement .execute ()) {
124152 ResultSet rs = statement .getResultSet ();
@@ -127,7 +155,7 @@ public void copy(String source, String destination) {
127155 String name = rs .getString (2 );
128156 String node = rs .getString (3 );
129157 boolean found = false ;
130- try (PreparedStatement statement2 = connection .prepareStatement (GET_PROPERTY_STMT )) {
158+ try (PreparedStatement statement2 = connection .prepareStatement (getPropertyStatement )) {
131159 statement2 .setString (1 , destination );
132160 statement2 .setString (2 , namespace );
133161 statement2 .setString (3 , name );
@@ -139,15 +167,15 @@ public void copy(String source, String destination) {
139167 }
140168 }
141169 if (found ) {
142- try (PreparedStatement statement2 = connection .prepareStatement (SET_PROPERTY_STMT )) {
170+ try (PreparedStatement statement2 = connection .prepareStatement (setPropertyStatement )) {
143171 statement2 .setString (1 , node );
144172 statement2 .setString (2 , destination );
145173 statement2 .setString (3 , namespace );
146174 statement2 .setString (4 , name );
147175 statement2 .execute ();
148176 }
149177 } else {
150- try (PreparedStatement statement2 = connection .prepareStatement (ADD_PROPERTY_STMT )) {
178+ try (PreparedStatement statement2 = connection .prepareStatement (addPropertyStatement )) {
151179 statement2 .setString (1 , destination );
152180 statement2 .setString (2 , namespace );
153181 statement2 .setString (3 , name );
@@ -171,7 +199,7 @@ public void delete(String resource) {
171199 }
172200 dbWriteLock .lock ();
173201 try (Connection connection = dataSource .getConnection ();
174- PreparedStatement statement = connection .prepareStatement (REMOVE_ALL_PROPERTIES_STMT )) {
202+ PreparedStatement statement = connection .prepareStatement (removeAllPropertiesStatement )) {
175203 statement .setString (1 , resource );
176204 statement .execute ();
177205 } catch (SQLException e ) {
@@ -190,7 +218,7 @@ public boolean propfind(String resource, Node property, boolean nameOnly, XMLWri
190218 // Add the names of all properties
191219 dbReadLock .lock ();
192220 try (Connection connection = dataSource .getConnection ();
193- PreparedStatement statement = connection .prepareStatement (GET_PROPERTIES_NAMES_STMT )) {
221+ PreparedStatement statement = connection .prepareStatement (getPropertiesNameStatement )) {
194222 statement .setString (1 , resource );
195223 if (statement .execute ()) {
196224 ResultSet rs = statement .getResultSet ();
@@ -209,7 +237,7 @@ public boolean propfind(String resource, Node property, boolean nameOnly, XMLWri
209237 // Add a single property
210238 dbReadLock .lock ();
211239 try (Connection connection = dataSource .getConnection ();
212- PreparedStatement statement = connection .prepareStatement (GET_PROPERTY_STMT )) {
240+ PreparedStatement statement = connection .prepareStatement (getPropertyStatement )) {
213241 statement .setString (1 , resource );
214242 statement .setString (2 , property .getNamespaceURI ());
215243 statement .setString (3 , property .getLocalName ());
@@ -230,7 +258,7 @@ public boolean propfind(String resource, Node property, boolean nameOnly, XMLWri
230258 // Add all properties
231259 dbReadLock .lock ();
232260 try (Connection connection = dataSource .getConnection ();
233- PreparedStatement statement = connection .prepareStatement (GET_PROPERTIES_NODES_STMT )) {
261+ PreparedStatement statement = connection .prepareStatement (getPropertiesNodeStatement )) {
234262 statement .setString (1 , resource );
235263 if (statement .execute ()) {
236264 ResultSet rs = statement .getResultSet ();
@@ -284,7 +312,7 @@ public void proppatch(String resource, ArrayList<ProppatchOperation> operations)
284312 String serializedNode = strWriter .toString ();
285313 boolean found = false ;
286314 try {
287- try (PreparedStatement statement = connection .prepareStatement (GET_PROPERTY_STMT )) {
315+ try (PreparedStatement statement = connection .prepareStatement (getPropertyStatement )) {
288316 statement .setString (1 , resource );
289317 statement .setString (2 , node .getNamespaceURI ());
290318 statement .setString (3 , node .getLocalName ());
@@ -296,15 +324,15 @@ public void proppatch(String resource, ArrayList<ProppatchOperation> operations)
296324 }
297325 }
298326 if (found ) {
299- try (PreparedStatement statement = connection .prepareStatement (SET_PROPERTY_STMT )) {
327+ try (PreparedStatement statement = connection .prepareStatement (setPropertyStatement )) {
300328 statement .setString (1 , serializedNode );
301329 statement .setString (2 , resource );
302330 statement .setString (3 , node .getNamespaceURI ());
303331 statement .setString (4 , node .getLocalName ());
304332 statement .execute ();
305333 }
306334 } else {
307- try (PreparedStatement statement = connection .prepareStatement (ADD_PROPERTY_STMT )) {
335+ try (PreparedStatement statement = connection .prepareStatement (addPropertyStatement )) {
308336 statement .setString (1 , resource );
309337 statement .setString (2 , node .getNamespaceURI ());
310338 statement .setString (3 , node .getLocalName ());
@@ -320,7 +348,7 @@ public void proppatch(String resource, ArrayList<ProppatchOperation> operations)
320348 }
321349 if (operation .getUpdateType () == PropertyUpdateType .REMOVE ) {
322350 Node node = operation .getPropertyNode ();
323- try (PreparedStatement statement = connection .prepareStatement (REMOVE_PROPERTY_STMT )) {
351+ try (PreparedStatement statement = connection .prepareStatement (removePropertyStatement )) {
324352 statement .setString (1 , resource );
325353 statement .setString (2 , node .getNamespaceURI ());
326354 statement .setString (3 , node .getLocalName ());
0 commit comments