1 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 28 31 class AuthTokenTable 32 { 33 private static Log log = LogFactory.getLog(AuthTokenTable.class); 35 36 static String insertSQL = null; 37 static String selectPublisherSQL = null; 38 static String deleteSQL = null; 39 static String touchSQL = null; 40 static String selectLastUsedSQL = null; 41 static String invalidateSQL = null; 42 static String selectTokenStateSQL = null; 43 44 static 45 { 46 StringBuffer sql = null; 48 49 sql = new StringBuffer (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 sql = new StringBuffer (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 sql = new StringBuffer (100); 73 sql.append("DELETE FROM AUTH_TOKEN "); 74 sql.append("WHERE AUTH_TOKEN=?"); 75 deleteSQL = sql.toString(); 76 77 sql = new StringBuffer (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 sql = new StringBuffer (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 sql = new StringBuffer (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 sql = new StringBuffer (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 119 public static void insert(String authToken,Publisher publisher,Connection connection) 120 throws SQLException 121 { 122 PreparedStatement statement = null; 123 Timestamp timestamp = new Timestamp (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 sqlex) 146 { 147 log.error(sqlex.getMessage()); 148 throw sqlex; 149 } 150 finally 151 { 152 try { statement.close(); } catch (Exception e) { } 153 } 154 } 155 156 164 public static Publisher selectPublisher(String authToken,Connection connection) 165 throws SQLException 166 { 167 Publisher publisher = null; 168 PreparedStatement statement = null; 169 ResultSet 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.setName(resultSet.getString(2)); } 186 187 return publisher; 188 } 189 catch(SQLException sqlex) 190 { 191 log.error(sqlex.getMessage()); 192 throw sqlex; 193 } 194 finally 195 { 196 try { resultSet.close(); } catch (Exception e) { } 197 try { statement.close(); } catch (Exception e) { } 198 } 199 } 200 201 208 public static void delete(String authToken,Connection connection) 209 throws SQLException 210 { 211 PreparedStatement statement = null; 212 213 try 214 { 215 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 statement.executeUpdate(); 224 } 225 catch(SQLException sqlex) 226 { 227 log.error(sqlex.getMessage()); 228 throw sqlex; 229 } 230 finally 231 { 232 try { statement.close(); } catch (Exception e) { } 233 } 234 } 235 236 242 public static void touch(String authToken,Connection connection) 243 throws SQLException 244 { 245 PreparedStatement statement = null; 246 Timestamp timestamp = new Timestamp (System.currentTimeMillis()); 247 248 try 249 { 250 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 statement.executeUpdate(); 261 } 262 catch(SQLException sqlex) 263 { 264 log.error(sqlex.getMessage()); 265 throw sqlex; 266 } 267 finally 268 { 269 try { statement.close(); } catch (Exception e) { } 270 } 271 } 272 273 279 public static long selectLastUsed(String authToken,Connection connection) 280 throws SQLException 281 { 282 PreparedStatement statement = null; 283 ResultSet resultSet = null; 284 long lastUsed = -1; 285 286 try 287 { 288 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 timestamp = resultSet.getTimestamp("LAST_USED"); 299 if (timestamp != null) 300 lastUsed = timestamp.getTime(); 301 } 302 } 303 catch(SQLException sqlex) 304 { 305 log.error(sqlex.getMessage()); 306 throw sqlex; 307 } 308 finally 309 { 310 try { statement.close(); } catch (Exception e) { } 311 } 312 313 return lastUsed; 314 } 315 316 323 public static void invalidate(String authToken,Connection connection) 324 throws SQLException 325 { 326 PreparedStatement statement = null; 327 Timestamp timestamp = new Timestamp (System.currentTimeMillis()); 328 329 try 330 { 331 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 statement.executeUpdate(); 342 } 343 catch(SQLException sqlex) 344 { 345 log.error(sqlex.getMessage()); 346 throw sqlex; 347 } 348 finally 349 { 350 try { statement.close(); } catch (Exception e) { } 351 } 352 } 353 354 362 public static long selectTokenState(String authToken, Connection connection) 363 throws SQLException 364 { 365 PreparedStatement statement = null; 366 ResultSet resultSet = null; 367 long tokenState = -1; 368 369 try 370 { 371 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 sqlex) 384 { 385 log.error(sqlex.getMessage()); 386 throw sqlex; 387 } 388 finally 389 { 390 try { statement.close(); } catch (Exception e) { } 391 } 392 393 return tokenState; 394 } 395 } 396 | Popular Tags |