KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > AuthTokenTable


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 JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Timestamp JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.juddi.datatype.publisher.Publisher;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class AuthTokenTable
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(AuthTokenTable.class);
35
36   static String JavaDoc insertSQL = null;
37   static String JavaDoc selectPublisherSQL = null;
38   static String JavaDoc deleteSQL = null;
39   static String JavaDoc touchSQL = null;
40   static String JavaDoc selectLastUsedSQL = null;
41   static String JavaDoc invalidateSQL = null;
42   static String JavaDoc selectTokenStateSQL = null;
43
44   static
45   {
46     // buffer used to build SQL statements
47
StringBuffer JavaDoc sql = null;
48
49     // build insertSQL
50
sql = new StringBuffer JavaDoc(350);
51     sql.append("INSERT INTO AUTH_TOKEN (");
52     sql.append("AUTH_TOKEN,");
53     sql.append("PUBLISHER_ID,");
54     sql.append("PUBLISHER_NAME,");
55     sql.append("CREATED,");
56     sql.append("LAST_USED,");
57     sql.append("NUMBER_OF_USES,");
58     sql.append("TOKEN_STATE) ");
59     sql.append("VALUES (?,?,?,?,?,0,1)");
60     insertSQL = sql.toString();
61
62     // build selectPublisherSQL
63
sql = new StringBuffer JavaDoc(200);
64     sql.append("SELECT ");
65     sql.append("PUBLISHER_ID,");
66     sql.append("PUBLISHER_NAME ");
67     sql.append("FROM AUTH_TOKEN ");
68     sql.append("WHERE AUTH_TOKEN=?");
69     selectPublisherSQL = sql.toString();
70
71     // build deleteSQL
72
sql = new StringBuffer JavaDoc(100);
73     sql.append("DELETE FROM AUTH_TOKEN ");
74     sql.append("WHERE AUTH_TOKEN=?");
75     deleteSQL = sql.toString();
76
77     // build touchSQL
78
sql = new StringBuffer JavaDoc(150);
79     sql.append("UPDATE AUTH_TOKEN ");
80     sql.append("SET LAST_USED=?,");
81     sql.append("NUMBER_OF_USES=NUMBER_OF_USES+1 ");
82     sql.append("WHERE AUTH_TOKEN=? ");
83     touchSQL = sql.toString();
84
85     // build selectLastUsedSQL
86
sql = new StringBuffer JavaDoc(200);
87     sql.append("SELECT ");
88     sql.append("LAST_USED ");
89     sql.append("FROM AUTH_TOKEN ");
90     sql.append("WHERE AUTH_TOKEN=?");
91     selectLastUsedSQL = sql.toString();
92
93     // build invalidateSQL
94
sql = new StringBuffer JavaDoc(100);
95     sql.append("UPDATE AUTH_TOKEN ");
96     sql.append("SET LAST_USED=?,");
97     sql.append("NUMBER_OF_USES=NUMBER_OF_USES+1,");
98     sql.append("TOKEN_STATE=0 ");
99     sql.append("WHERE AUTH_TOKEN=? ");
100     invalidateSQL = sql.toString();
101
102     // build selectTokenStateSQL
103
sql = new StringBuffer JavaDoc(200);
104     sql.append("SELECT ");
105     sql.append("TOKEN_STATE ");
106     sql.append("FROM AUTH_TOKEN ");
107     sql.append("WHERE AUTH_TOKEN=?");
108     selectTokenStateSQL = sql.toString();
109   }
110
111   /**
112    * Insert new row into the AUTH_TOKEN table.
113    *
114    * @param authToken
115    * @param publisher
116    * @param connection JDBC connection
117    * @throws SQLException
118    */

119   public static void insert(String JavaDoc authToken,Publisher publisher,Connection JavaDoc connection)
120     throws SQLException JavaDoc
121   {
122     PreparedStatement JavaDoc statement = null;
123     Timestamp JavaDoc timestamp = new Timestamp JavaDoc(System.currentTimeMillis());
124
125     try
126     {
127       statement = connection.prepareStatement(insertSQL);
128       statement.setString(1,authToken);
129       statement.setString(2,publisher.getPublisherID());
130       statement.setString(3,publisher.getName());
131       statement.setTimestamp(4,timestamp);
132       statement.setTimestamp(5,timestamp);
133
134       log.debug("insert into AUTH_TOKEN table:\n\n\t" + insertSQL +
135         "\n\t AUTH_TOKEN=" + authToken +
136         "\n\t PUBLISHER_ID=" + publisher.getPublisherID() +
137         "\n\t PUBLISHER_NAME=" + publisher.getName() +
138         "\n\t CREATED=" + timestamp.toString() +
139         "\n\t LAST_USED=" + timestamp.toString() +
140         "\n\t NUMBER_OF_USES=1" +
141         "\n\t TOKEN_STATE=1\n");
142
143       statement.executeUpdate();
144     }
145     catch(SQLException JavaDoc sqlex)
146     {
147       log.error(sqlex.getMessage());
148       throw sqlex;
149     }
150     finally
151     {
152       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
153     }
154   }
155
156   /**
157    * Select one row from the AUTH_TOKEN table.
158    *
159    * @param authToken
160    * @param connection JDBC connection
161    * @return Publisher The publisher's info
162    * @throws SQLException
163    */

164   public static Publisher selectPublisher(String JavaDoc authToken,Connection JavaDoc connection)
165     throws SQLException JavaDoc
166   {
167     Publisher publisher = null;
168     PreparedStatement JavaDoc statement = null;
169     ResultSet JavaDoc resultSet = null;
170
171     try
172     {
173       statement = connection.prepareStatement(selectPublisherSQL);
174       statement.setString(1,authToken);
175
176       log.debug("select from AUTH_TOKEN table:\n\n\t" + selectPublisherSQL +
177         "\n\t AUTH_TOKEN=" + authToken + "\n");
178
179       resultSet = statement.executeQuery();
180       if (resultSet.next())
181       {
182         publisher = new Publisher();
183         publisher.setPublisherID(resultSet.getString(1));//("PUBLISHER_ID"));
184
publisher.setName(resultSet.getString(2));//("PUBLISHER_NAME"));
185
}
186
187       return publisher;
188     }
189     catch(SQLException JavaDoc sqlex)
190     {
191       log.error(sqlex.getMessage());
192       throw sqlex;
193     }
194     finally
195     {
196       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
197       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
198     }
199   }
200
201   /**
202    * Delete row from the AUTH_TOKEN table.
203    *
204    * @param authToken
205    * @param connection
206    * @throws SQLException
207    */

208   public static void delete(String JavaDoc authToken,Connection JavaDoc connection)
209     throws SQLException JavaDoc
210   {
211     PreparedStatement JavaDoc statement = null;
212
213     try
214     {
215       // prepare the delete
216
statement = connection.prepareStatement(deleteSQL);
217       statement.setString(1,authToken);
218
219       log.debug("delete from AUTH_TOKEN table:\n\n\t" + deleteSQL +
220         "\n\t AUTH_TOKEN=" + authToken + "\n");
221
222       // execute
223
statement.executeUpdate();
224     }
225     catch(SQLException JavaDoc sqlex)
226     {
227       log.error(sqlex.getMessage());
228       throw sqlex;
229     }
230     finally
231     {
232       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
233     }
234   }
235
236   /**
237    * Update the PUBLISHER table for a particular PublisherID.
238    *
239    * @param connection JDBC connection
240    * @throws SQLException
241    */

242   public static void touch(String JavaDoc authToken,Connection JavaDoc connection)
243     throws SQLException JavaDoc
244   {
245     PreparedStatement JavaDoc statement = null;
246     Timestamp JavaDoc timestamp = new Timestamp JavaDoc(System.currentTimeMillis());
247
248     try
249     {
250       // create a statement to query with
251
statement = connection.prepareStatement(touchSQL);
252       statement.setTimestamp(1,timestamp);
253       statement.setString(2,authToken);
254
255       log.debug("update AUTH_TOKEN table:\n\n\t" + touchSQL +
256        "\n\t AUTH_TOKEN=" + authToken +
257        "\n\t LAST_USED=" + timestamp.toString() + "\n");
258
259       // execute
260
statement.executeUpdate();
261     }
262     catch(SQLException JavaDoc sqlex)
263     {
264       log.error(sqlex.getMessage());
265       throw sqlex;
266     }
267     finally
268     {
269       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
270     }
271   }
272
273   /**
274    * Update the PUBLISHER table for a particular PublisherID.
275    *
276    * @param connection JDBC connection
277    * @throws SQLException
278    */

279   public static long selectLastUsed(String JavaDoc authToken,Connection JavaDoc connection)
280     throws SQLException JavaDoc
281   {
282     PreparedStatement JavaDoc statement = null;
283     ResultSet JavaDoc resultSet = null;
284     long lastUsed = -1;
285
286     try
287     {
288       // create a statement to query with
289
statement = connection.prepareStatement(selectLastUsedSQL);
290       statement.setString(1,authToken);
291
292       log.debug("select LAST_USED from AUTH_TOKEN table:\n\n\t" + selectLastUsedSQL +
293        "\n\t AUTH_TOKEN=" + authToken + "\n");
294
295       resultSet = statement.executeQuery();
296       if (resultSet.next())
297       {
298         Timestamp JavaDoc timestamp = resultSet.getTimestamp("LAST_USED");
299         if (timestamp != null)
300           lastUsed = timestamp.getTime();
301       }
302     }
303     catch(SQLException JavaDoc sqlex)
304     {
305       log.error(sqlex.getMessage());
306       throw sqlex;
307     }
308     finally
309     {
310       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
311     }
312
313     return lastUsed;
314   }
315
316   /**
317    * Update the AUTH_TOKEN table's LAST_USED property for a
318    * particular authToken.
319    *
320    * @param connection JDBC connection
321    * @throws SQLException
322    */

323   public static void invalidate(String JavaDoc authToken,Connection JavaDoc connection)
324     throws SQLException JavaDoc
325   {
326     PreparedStatement JavaDoc statement = null;
327     Timestamp JavaDoc timestamp = new Timestamp JavaDoc(System.currentTimeMillis());
328
329     try
330     {
331       // create a statement to query with
332
statement = connection.prepareStatement(invalidateSQL);
333       statement.setTimestamp(1,timestamp);
334       statement.setString(2,authToken);
335
336       log.debug("update AUTH_TOKEN table:\n\n\t" + invalidateSQL +
337         "\n\t LAST_USED=" + timestamp.toString() +
338         "\n\t AUTH_TOKEN=" + authToken + "\n");
339
340       // execute
341
statement.executeUpdate();
342     }
343     catch(SQLException JavaDoc sqlex)
344     {
345       log.error(sqlex.getMessage());
346       throw sqlex;
347     }
348     finally
349     {
350       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
351     }
352   }
353
354   /**
355    * Query the token's state. This is used to determine if the authToken
356    * has been discarded, for example.
357    *
358    * @param authToken
359    * @param connection
360    * @throws SQLException
361    */

362   public static long selectTokenState(String JavaDoc authToken, Connection JavaDoc connection)
363       throws SQLException JavaDoc
364   {
365     PreparedStatement JavaDoc statement = null;
366     ResultSet JavaDoc resultSet = null;
367     long tokenState = -1;
368
369     try
370     {
371       // create a statement to query with
372
statement = connection.prepareStatement(selectTokenStateSQL);
373       statement.setString(1,authToken);
374
375       log.debug("SQL Statement: [" + selectTokenStateSQL + "], parameter authToken: [" + authToken + "]");
376
377       resultSet = statement.executeQuery();
378       if (resultSet.next())
379       {
380         tokenState = resultSet.getLong("TOKEN_STATE");
381       }
382     }
383     catch(SQLException JavaDoc sqlex)
384     {
385       log.error(sqlex.getMessage());
386       throw sqlex;
387     }
388     finally
389     {
390       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
391     }
392
393     return tokenState;
394   }
395 }
396
Popular Tags