View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.datastore.jdbc;
17  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Timestamp;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.datatype.publisher.Publisher;
27  import org.apache.juddi.registry.RegistryEngine;
28  import org.apache.juddi.util.Config;
29  
30  /***
31   * @author Steve Viens (sviens@apache.org)
32   */
33  class AuthTokenTable
34  {
35    // private reference to the jUDDI logger
36    private static Log log = LogFactory.getLog(AuthTokenTable.class);
37  
38    static String insertSQL = null;
39    static String selectPublisherSQL = null;
40    static String deleteSQL = null;
41    static String touchSQL = null;
42    static String selectLastUsedSQL = null;
43    static String invalidateSQL = null;
44    static String selectTokenStateSQL = null;
45    static String tablePrefix = "";
46  
47    static
48    {
49      tablePrefix = Config.getStringProperty(
50          RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
51      // buffer used to build SQL statements
52      StringBuffer sql = null;
53  
54      // build insertSQL
55      sql = new StringBuffer(350);
56      sql.append("INSERT INTO ").append(tablePrefix).append("AUTH_TOKEN (");
57      sql.append("AUTH_TOKEN,");
58      sql.append("PUBLISHER_ID,");
59      sql.append("PUBLISHER_NAME,");
60      sql.append("CREATED,");
61      sql.append("LAST_USED,");
62      sql.append("NUMBER_OF_USES,");
63      sql.append("TOKEN_STATE) ");
64      sql.append("VALUES (?,?,?,?,?,0,1)");
65      insertSQL = sql.toString();
66  
67      // build selectPublisherSQL
68      sql = new StringBuffer(200);
69      sql.append("SELECT ");
70      sql.append("PUBLISHER_ID,");
71      sql.append("PUBLISHER_NAME ");
72      sql.append("FROM ").append(tablePrefix).append("AUTH_TOKEN ");
73      sql.append("WHERE AUTH_TOKEN=?");
74      selectPublisherSQL = sql.toString();
75  
76      // build deleteSQL
77      sql = new StringBuffer(100);
78      sql.append("DELETE FROM ").append(tablePrefix).append("AUTH_TOKEN ");
79      sql.append("WHERE AUTH_TOKEN=?");
80      deleteSQL = sql.toString();
81  
82      // build touchSQL
83      sql = new StringBuffer(150);
84      sql.append("UPDATE ").append(tablePrefix).append("AUTH_TOKEN ");
85      sql.append("SET LAST_USED=?,");
86      sql.append("NUMBER_OF_USES=NUMBER_OF_USES+1 ");
87      sql.append("WHERE AUTH_TOKEN=? ");
88      touchSQL = sql.toString();
89  
90      // build selectLastUsedSQL
91      sql = new StringBuffer(200);
92      sql.append("SELECT ");
93      sql.append("LAST_USED ");
94      sql.append("FROM ").append(tablePrefix).append("AUTH_TOKEN ");
95      sql.append("WHERE AUTH_TOKEN=?");
96      selectLastUsedSQL = sql.toString();
97  
98      // build invalidateSQL
99      sql = new StringBuffer(100);
100     sql.append("UPDATE ").append(tablePrefix).append("AUTH_TOKEN ");
101     sql.append("SET LAST_USED=?,");
102     sql.append("NUMBER_OF_USES=NUMBER_OF_USES+1,");
103     sql.append("TOKEN_STATE=0 ");
104     sql.append("WHERE AUTH_TOKEN=? ");
105     invalidateSQL = sql.toString();
106 
107     // build selectTokenStateSQL
108     sql = new StringBuffer(200);
109     sql.append("SELECT ");
110     sql.append("TOKEN_STATE ");
111     sql.append("FROM ").append(tablePrefix).append("AUTH_TOKEN ");
112     sql.append("WHERE AUTH_TOKEN=?");
113     selectTokenStateSQL = sql.toString();
114   }
115 
116   /***
117    * Insert new row into the AUTH_TOKEN table.
118    *
119    * @param authToken
120    * @param publisher
121    * @param connection JDBC connection
122    * @throws SQLException
123    */
124   public static void insert(String authToken,Publisher publisher,Connection connection)
125     throws SQLException
126   {
127     PreparedStatement statement = null;
128     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
129 
130     try
131     {
132       statement = connection.prepareStatement(insertSQL);
133       statement.setString(1,authToken);
134       statement.setString(2,publisher.getPublisherID());
135       statement.setString(3,publisher.getName());
136       statement.setTimestamp(4,timestamp);
137       statement.setTimestamp(5,timestamp);
138 
139       if (log.isDebugEnabled()) {
140           log.debug("insert into " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + insertSQL +
141             "\n\t AUTH_TOKEN=" + authToken +
142             "\n\t PUBLISHER_ID=" + publisher.getPublisherID() +
143             "\n\t PUBLISHER_NAME=" + publisher.getName() +
144             "\n\t CREATED=" + timestamp.toString() +
145             "\n\t LAST_USED=" + timestamp.toString() +
146             "\n\t NUMBER_OF_USES=1" +
147             "\n\t TOKEN_STATE=1\n");
148       }
149 
150       statement.executeUpdate();
151     }
152     catch(SQLException sqlex)
153     {
154       log.error(sqlex.getMessage());
155       throw sqlex;
156     }
157     finally
158     {
159       try { statement.close(); } catch (Exception e) { /* ignored */ }
160     }
161   }
162 
163   /***
164    * Select one row from the AUTH_TOKEN table.
165    *
166    * @param authToken
167    * @param connection JDBC connection
168    * @return Publisher The publisher's info
169    * @throws SQLException
170    */
171   public static Publisher selectPublisher(String authToken,Connection connection)
172     throws SQLException
173   {
174     Publisher publisher = null;
175     PreparedStatement statement = null;
176     ResultSet resultSet = null;
177 
178     try
179     {
180       statement = connection.prepareStatement(selectPublisherSQL);
181       statement.setString(1,authToken);
182 
183       if (log.isDebugEnabled()){
184           log.debug("select from " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + selectPublisherSQL +
185             "\n\t AUTH_TOKEN=" + authToken + "\n");
186       }
187 
188       resultSet = statement.executeQuery();
189       if (resultSet.next())
190       {
191         publisher = new Publisher();
192         publisher.setPublisherID(resultSet.getString(1));//("PUBLISHER_ID"));
193         publisher.setName(resultSet.getString(2));//("PUBLISHER_NAME"));
194       }
195 
196       return publisher;
197     }
198     catch(SQLException sqlex)
199     {
200       log.error(sqlex.getMessage());
201       throw sqlex;
202     }
203     finally
204     {
205       try { resultSet.close(); } catch (Exception e) { /* ignored */ }
206       try { statement.close(); } catch (Exception e) { /* ignored */ }
207     }
208   }
209 
210   /***
211    * Delete row from the AUTH_TOKEN table.
212    *
213    * @param authToken
214    * @param connection
215    * @throws SQLException
216    */
217   public static void delete(String authToken,Connection connection)
218     throws SQLException
219   {
220     PreparedStatement statement = null;
221 
222     try
223     {
224       // prepare the delete
225       statement = connection.prepareStatement(deleteSQL);
226       statement.setString(1,authToken);
227 
228       if (log.isDebugEnabled()) {
229           log.debug("delete from " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + deleteSQL +
230             "\n\t AUTH_TOKEN=" + authToken + "\n");
231       }
232 
233       // execute
234       statement.executeUpdate();
235     }
236     catch(SQLException sqlex)
237     {
238       log.error(sqlex.getMessage());
239       throw sqlex;
240     }
241     finally
242     {
243       try { statement.close(); } catch (Exception e) { /* ignored */ }
244     }
245   }
246 
247   /***
248    * Update the PUBLISHER table for a particular PublisherID.
249    *
250    * @param connection JDBC connection
251    * @throws SQLException
252    */
253   public static void touch(String authToken,Connection connection)
254     throws SQLException
255   {
256     PreparedStatement statement = null;
257     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
258 
259     try
260     {
261       // create a statement to query with
262       statement = connection.prepareStatement(touchSQL);
263       statement.setTimestamp(1,timestamp);
264       statement.setString(2,authToken);
265 
266       if (log.isDebugEnabled()) {
267           log.debug("update " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + touchSQL +
268            "\n\t AUTH_TOKEN=" + authToken +
269            "\n\t LAST_USED=" + timestamp.toString() + "\n");
270       }
271 
272       // execute
273       statement.executeUpdate();
274     }
275     catch(SQLException sqlex)
276     {
277       log.error(sqlex.getMessage());
278       throw sqlex;
279     }
280     finally
281     {
282       try { statement.close(); } catch (Exception e) { /* ignored */ }
283     }
284   }
285 
286   /***
287    * Update the PUBLISHER table for a particular PublisherID.
288    *
289    * @param connection JDBC connection
290    * @throws SQLException
291    */
292   public static long selectLastUsed(String authToken,Connection connection)
293     throws SQLException
294   {
295     PreparedStatement statement = null;
296     ResultSet resultSet = null;
297     long lastUsed = -1;
298 
299     try
300     {
301       // create a statement to query with
302       statement = connection.prepareStatement(selectLastUsedSQL);
303       statement.setString(1,authToken);
304 
305       if (log.isDebugEnabled()) {
306           log.debug("select LAST_USED from " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + selectLastUsedSQL +
307            "\n\t AUTH_TOKEN=" + authToken + "\n");
308       }
309 
310       resultSet = statement.executeQuery();
311       if (resultSet.next())
312       {
313         Timestamp timestamp = resultSet.getTimestamp("LAST_USED");
314         if (timestamp != null)
315           lastUsed = timestamp.getTime();
316       }
317     }
318     catch(SQLException sqlex)
319     {
320       log.error(sqlex.getMessage());
321       throw sqlex;
322     }
323     finally
324     {
325       try { statement.close(); } catch (Exception e) { /* ignored */ }
326     }
327 
328     return lastUsed;
329   }
330 
331   /***
332    * Update the AUTH_TOKEN table's LAST_USED property for a
333    * particular authToken.
334    *
335    * @param connection JDBC connection
336    * @throws SQLException
337    */
338   public static void invalidate(String authToken,Connection connection)
339     throws SQLException
340   {
341     PreparedStatement statement = null;
342     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
343 
344     try
345     {
346       // create a statement to query with
347       statement = connection.prepareStatement(invalidateSQL);
348       statement.setTimestamp(1,timestamp);
349       statement.setString(2,authToken);
350 
351       if (log.isDebugEnabled()) {
352           log.debug("update " + tablePrefix + "AUTH_TOKEN table:\n\n\t" + invalidateSQL +
353             "\n\t LAST_USED=" + timestamp.toString() +
354             "\n\t AUTH_TOKEN=" + authToken + "\n");
355       }
356 
357       // execute
358       statement.executeUpdate();
359     }
360     catch(SQLException sqlex)
361     {
362       log.error(sqlex.getMessage());
363       throw sqlex;
364     }
365     finally
366     {
367       try { statement.close(); } catch (Exception e) { /* ignored */ }
368     }
369   }
370 
371   /***
372    * Query the token's state. This is used to determine if the authToken
373    * has been discarded, for example.
374    *
375    * @param authToken
376    * @param connection
377    * @throws SQLException
378    */
379   public static long selectTokenState(String authToken, Connection connection)
380       throws SQLException
381   {
382     PreparedStatement statement = null;
383     ResultSet resultSet = null;
384     long tokenState = -1;
385 
386     try
387     {
388       // create a statement to query with
389       statement = connection.prepareStatement(selectTokenStateSQL);
390       statement.setString(1,authToken);
391 
392       if (log.isDebugEnabled()) {
393           log.debug("SQL Statement: [" + selectTokenStateSQL + "], parameter authToken: [" + authToken + "]");
394       }
395 
396       resultSet = statement.executeQuery();
397       if (resultSet.next())
398       {
399         tokenState = resultSet.getLong("TOKEN_STATE");
400       }
401     }
402     catch(SQLException sqlex)
403     {
404       log.error(sqlex.getMessage());
405       throw sqlex;
406     }
407     finally
408     {
409       try { statement.close(); } catch (Exception e) { /* ignored */ }
410     }
411 
412     return tokenState;
413   }
414 }