1 24 25 package com.mckoi.database.jdbcserver; 26 27 import com.mckoi.database.*; 28 import com.mckoi.database.jdbc.*; 29 import com.mckoi.util.StringUtil; 30 import com.mckoi.debug.*; 31 32 import java.sql.SQLException ; 33 34 47 48 public class JDBCDatabaseInterface extends AbstractJDBCDatabaseInterface { 49 50 53 private static final boolean COMMAND_LOGGING = true; 54 55 58 private String host_name; 59 60 63 public JDBCDatabaseInterface(Database database, String host_name) { 64 super(database); 65 this.host_name = host_name; 66 } 67 68 73 private boolean authenticate(Database database, String default_schema, 74 String username, String password, 75 final DatabaseCallBack database_call_back) { 76 77 80 if (getUser() == null) { 81 82 if (COMMAND_LOGGING && database.getSystem().logQueries()) { 83 StringBuffer log_str = new StringBuffer (); 85 log_str.append("[JDBC] ["); 86 log_str.append(username); 87 log_str.append("] "); 88 log_str.append('['); 89 log_str.append(host_name); 90 log_str.append("] "); 91 log_str.append("Log in.\n"); 92 database.getCommandsLog().log(new String (log_str)); 93 } 94 95 if (Debug().isInterestedIn(Lvl.INFORMATION)) { 97 Debug().write(Lvl.INFORMATION, this, 98 "Authenticate User: " + username); 99 } 100 101 DatabaseConnection.CallBack call_back = 103 new DatabaseConnection.CallBack() { 104 public void triggerNotify(String trigger_name, int trigger_event, 105 String trigger_source, int fire_count) { 106 StringBuffer message = new StringBuffer (); 107 message.append(trigger_name); 108 message.append(' '); 109 message.append(trigger_source); 110 message.append(' '); 111 message.append(fire_count); 112 113 database_call_back.databaseEvent(99, new String (message)); 114 } 115 }; 116 117 User this_user = database.authenticateUser(username, password, 119 host_name); 120 DatabaseConnection database_connection = null; 121 122 if (this_user != null) { 124 database_connection = 125 database.createNewConnection(this_user, call_back); 126 127 LockingMechanism locker = database_connection.getLockingMechanism(); 129 locker.setMode(LockingMechanism.EXCLUSIVE_MODE); 130 try { 131 132 database_connection.setAutoCommit(true); 134 135 if (database_connection.schemaExists(default_schema)) { 137 database_connection.setDefaultSchema(default_schema); 138 } 139 else { 140 Debug().write(Lvl.WARNING, this, 141 "Couldn't change to '" + default_schema + "' schema."); 142 database_connection.setDefaultSchema("APP"); 144 } 145 146 } 147 finally { 148 try { 149 database_connection.commit(); 151 } 152 catch (TransactionException e) { 153 Debug().writeException(Lvl.WARNING, e); 155 } 156 finally { 157 locker.finishMode(LockingMechanism.EXCLUSIVE_MODE); 159 } 160 } 161 162 } 163 164 if (this_user != null) { 166 init(this_user, database_connection); 167 return true; 168 } 169 else { 170 return false; 172 } 173 174 } 175 else { 176 throw new RuntimeException ("Attempt to authenticate user twice"); 177 } 178 179 } 180 181 183 public boolean login(String default_schema, String username, String password, 184 DatabaseCallBack database_call_back) 185 throws SQLException { 186 187 Database database = getDatabase(); 188 189 boolean b = authenticate(database, default_schema, username, password, 190 database_call_back); 191 return b; 192 } 193 194 195 196 197 198 public QueryResponse execQuery(SQLQuery query) throws SQLException { 199 200 checkNotDisposed(); 202 203 User user = getUser(); 204 DatabaseConnection database_connection = getDatabaseConnection(); 205 206 if (COMMAND_LOGGING && getDatabase().getSystem().logQueries()) { 208 StringBuffer log_str = new StringBuffer (); 210 log_str.append("[JDBC] ["); 211 log_str.append(user.getUserName()); 212 log_str.append("] "); 213 log_str.append('['); 214 log_str.append(host_name); 215 log_str.append("] "); 216 log_str.append("Query: "); 217 log_str.append(query.getQuery()); 218 log_str.append('\n'); 219 user.getDatabase().getCommandsLog().log(new String (log_str)); 220 } 221 222 if (Debug().isInterestedIn(Lvl.INFORMATION)) { 224 Debug().write(Lvl.INFORMATION, this, 225 "Query From User: " + user.getUserName() + "@" + host_name); 226 Debug().write(Lvl.INFORMATION, this, 227 "Query: " + query.getQuery().trim()); 228 } 229 230 LockingMechanism locker = database_connection.getLockingMechanism(); 232 int lock_mode = -1; 233 QueryResponse response = null; 234 try { 235 try { 236 237 lock_mode = LockingMechanism.EXCLUSIVE_MODE; 248 locker.setMode(lock_mode); 249 250 response = super.execQuery(query); 252 253 return response; 255 256 } 257 finally { 258 try { 259 if (lock_mode != -1) { 262 locker.finishMode(lock_mode); 263 } 264 } 265 catch (Throwable e) { 266 e.printStackTrace(System.err); 269 Debug().write(Lvl.ERROR, this, "Exception finishing locks"); 270 Debug().writeException(e); 271 } 274 } 275 276 } 277 finally { 278 281 if (database_connection.getAutoCommit()) { 284 try { 286 locker.setMode(LockingMechanism.EXCLUSIVE_MODE); 288 if (response == null) { 290 database_connection.rollback(); 292 } 293 else { 294 try { 295 database_connection.commit(); 297 } 298 catch (Throwable e) { 299 disposeResult(response.getResultID()); 301 throw handleExecuteThrowable(e, query); 303 } 304 } 305 } 306 finally { 307 locker.finishMode(LockingMechanism.EXCLUSIVE_MODE); 308 } 309 } 310 311 } 312 313 } 314 315 316 public void dispose() throws SQLException { 317 if (getUser() != null) { 318 DatabaseConnection database = getDatabaseConnection(); 319 LockingMechanism locker = database.getLockingMechanism(); 320 try { 321 locker.setMode(LockingMechanism.EXCLUSIVE_MODE); 323 database.rollback(); 325 } 326 finally { 327 locker.finishMode(LockingMechanism.EXCLUSIVE_MODE); 329 database.close(); 331 getUser().logout(); 333 internalDispose(); 335 } 336 } 337 } 338 339 } 340 | Popular Tags |