1 16 17 20 21 package com.klopotek.utils.log; 22 23 import java.sql.*; 24 import java.util.*; 25 import org.apache.log4j.*; 26 import org.apache.log4j.helpers.*; 27 import org.apache.log4j.spi.*; 28 29 120 public class JDBCAppender extends AppenderSkeleton 121 { 122 125 public static final String URL_OPTION = "url"; 126 127 130 public static final String USERNAME_OPTION = "username"; 131 132 135 public static final String PASSWORD_OPTION = "password"; 136 137 140 public static final String TABLE_OPTION = "table"; 141 142 145 public static final String CONNECTOR_OPTION = "connector"; 146 147 150 public static final String COLUMNS_OPTION = "columns"; 151 152 155 public static final String SQL_OPTION = "sql"; 156 157 160 public static final String BUFFER_OPTION = "buffer"; 161 162 165 public static final String COMMIT_OPTION = "commit"; 166 167 168 private String url = null; 170 private String username = null; 171 private String password = null; 172 private String table = null; 173 private String connection_class = null; 174 private String sql = null; 175 private boolean docommit = true; 176 private int buffer_size = 1; 177 private JDBCConnectionHandler connectionHandler = null; 178 179 private ArrayList buffer = new ArrayList(); 182 183 private Connection con = null; 185 186 private JDBCLogger jlogger = new JDBCLogger(); 188 189 private boolean connected = false; 192 private boolean configured = false; 194 private boolean ready = false; 196 197 200 public void finalize() 201 { 202 close(); 203 super.finalize(); 204 } 205 206 209 public String [] getOptionStrings() 210 { 211 return new String []{CONNECTOR_OPTION, URL_OPTION, USERNAME_OPTION, PASSWORD_OPTION, SQL_OPTION, TABLE_OPTION, COLUMNS_OPTION, BUFFER_OPTION, COMMIT_OPTION}; 213 } 214 215 216 219 public void setOption(String _option, String _value) 220 { 221 _option = _option.trim(); 222 _value = _value.trim(); 223 224 if(_option == null || _value == null) return; 225 if(_option.length() == 0 || _value.length() == 0) return; 226 227 _value = _value.trim(); 228 229 if(_option.equals(CONNECTOR_OPTION)) 230 { 231 if(!connected) connection_class = _value; 232 } 233 else if(_option.equals(URL_OPTION)) 234 { 235 if(!connected) url = _value; 236 } 237 else if(_option.equals(USERNAME_OPTION)) 238 { 239 if(!connected) username = _value; 240 } 241 else if(_option.equals(PASSWORD_OPTION)) 242 { 243 if(!connected) password = _value; 244 } 245 else if(_option.equals(SQL_OPTION)) 246 { 247 sql = _value; 248 } 249 else if(_option.equals(TABLE_OPTION)) 250 { 251 if(sql != null) return; 252 table = _value; 253 } 254 else if(_option.equals(COLUMNS_OPTION)) 255 { 256 if(sql != null) return; 257 258 String name = null; 259 int logtype = -1; 260 String value = null; 261 String column = null; 262 String arg = null; 263 int num_args = 0; 264 int num_columns = 0; 265 StringTokenizer st_col; 266 StringTokenizer st_arg; 267 268 st_col = new StringTokenizer(_value, " "); 270 271 num_columns = st_col.countTokens(); 272 273 if(num_columns < 1) 274 { 275 errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION value : " + _value + " !"); 276 return; 277 } 278 279 for(int i=1; i<=num_columns; i++) 280 { 281 column = st_col.nextToken(); 282 283 st_arg = new StringTokenizer(column, "~"); 285 286 num_args = st_arg.countTokens(); 287 288 if(num_args < 2) 289 { 290 errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION value : " + _value + " !"); 291 return; 292 } 293 294 for(int j=1; j<=num_args; j++) 295 { 296 arg = st_arg.nextToken(); 297 298 if(j == 1) name = arg; 299 else if(j == 2) 300 { 301 try 302 { 303 logtype = Integer.parseInt(arg); 304 } 305 catch(Exception e) 306 { 307 logtype = LogType.parseLogType(arg); 308 } 309 310 if(!LogType.isLogType(logtype)) 311 { 312 errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION LogType : " + arg + " !"); 313 return; 314 } 315 } 316 else if(j == 3) value = arg; 317 } 318 319 if(!setLogType(name, logtype, value)) return; 320 } 321 } 322 else if(_option.equals(BUFFER_OPTION)) 323 { 324 try 325 { 326 buffer_size = Integer.parseInt(_value); 327 } 328 catch(Exception e) 329 { 330 errorHandler.error("JDBCAppender::setOption(), Invalid BUFFER_OPTION value : " + _value + " !"); 331 return; 332 } 333 } 334 else if(_option.equals(COMMIT_OPTION)) 335 { 336 docommit = _value.equals("Y"); 337 } 338 339 if(_option.equals(SQL_OPTION) || _option.equals(TABLE_OPTION)) 340 { 341 if(!configured) configure(); 342 } 343 } 344 345 348 public boolean requiresLayout() 349 { 350 return true; 351 } 352 353 354 357 public void close() 358 { 359 flush_buffer(); 360 if(connection_class == null) 361 { 362 try{con.close();}catch(Exception e){errorHandler.error("JDBCAppender::close(), " + e);} 363 } 364 this.closed = true; 365 } 366 367 368 371 public boolean setLogType(String _name, int _logtype, Object _value) 372 { 373 if(sql != null) return true; 374 375 if(!configured) 376 { 377 if(!configure()) return false; 378 } 379 380 try 381 { 382 jlogger.setLogType(_name, _logtype, _value); 383 } 384 catch(Exception e) 385 { 386 errorHandler.error("JDBCAppender::setLogType(), " + e); 387 return false; 388 } 389 390 return true; 391 } 392 393 394 397 public void append(LoggingEvent event) 398 { 399 if(!ready) 400 { 401 if(!ready()) 402 { 403 errorHandler.error("JDBCAppender::append(), Not ready to append !"); 404 return; 405 } 406 } 407 408 buffer.add(event); 409 410 if(buffer.size() >= buffer_size) flush_buffer(); 411 } 412 413 414 417 public void flush_buffer() 418 { 419 try 420 { 421 int size = buffer.size(); 422 423 if(size < 1) return; 424 425 for(int i=0; i<size; i++) 426 { 427 LoggingEvent event = (LoggingEvent)buffer.get(i); 428 429 jlogger.append(layout.format(event)); 431 } 432 433 buffer.clear(); 434 435 if(docommit) con.commit(); 436 } 437 catch(Exception e) 438 { 439 errorHandler.error("JDBCAppender::flush_buffer(), " + e + " : " + jlogger.getErrorMsg()); 440 try{con.rollback();} catch(Exception ex){} 441 return; 442 } 443 } 444 445 446 449 public boolean ready() 450 { 451 if(ready) return true; 452 453 if(!configured) return false; 454 455 ready = jlogger.ready(); 456 457 if(!ready){errorHandler.error(jlogger.getErrorMsg());} 458 459 return ready; 460 } 461 462 463 466 protected void connect() throws Exception 467 { 468 if(connected) return; 469 470 try 471 { 472 if(connection_class == null) 473 { 474 if(url == null) throw new Exception ("JDBCAppender::connect(), No URL defined."); 475 476 if(username == null) throw new Exception ("JDBCAppender::connect(), No USERNAME defined."); 477 478 if(password == null) throw new Exception ("JDBCAppender::connect(), No PASSWORD defined."); 479 480 connectionHandler = new DefaultConnectionHandler(); 481 } 482 else 483 { 484 connectionHandler = (JDBCConnectionHandler)(Class.forName(connection_class).newInstance()); 485 } 486 487 if(url != null && username != null && password != null) 488 { 489 con = connectionHandler.getConnection(url, username, password); 490 } 491 else 492 { 493 con = connectionHandler.getConnection(); 494 } 495 496 if(con.isClosed()) 497 { 498 throw new Exception ("JDBCAppender::connect(), JDBCConnectionHandler returns no connected Connection !"); 499 } 500 } 501 catch(Exception e) 502 { 503 throw new Exception ("JDBCAppender::connect(), " + e); 504 } 505 506 connected = true; 507 } 508 509 512 protected boolean configure() 513 { 514 if(configured) return true; 515 516 if(!connected) 517 { 518 if((connection_class == null) && (url == null || username == null || password == null)) 519 { 520 errorHandler.error("JDBCAppender::configure(), Missing database-options or connector-option !"); 521 return false; 522 } 523 524 try 525 { 526 connect(); 527 } 528 catch(Exception e) 529 { 530 connection_class = null; 531 url = null; 532 errorHandler.error("JDBCAppender::configure(), " + e); 533 return false; 534 } 535 } 536 537 if(sql == null && table == null) 538 { 539 errorHandler.error("JDBCAppender::configure(), No SQL_OPTION or TABLE_OPTION given !"); 540 return false; 541 } 542 543 if(!jlogger.isConfigured()) 544 { 545 try 546 { 547 jlogger.setConnection(con); 548 549 if(sql == null) 550 { 551 jlogger.configureTable(table); 552 } 553 else jlogger.configureSQL(sql); 554 } 555 catch(Exception e) 556 { 557 errorHandler.error("JDBCAppender::configure(), " + e); 558 return false; 559 } 560 } 561 562 if(layout == null) 564 { 565 layout = new PatternLayout("%m"); 566 } 567 568 configured = true; 569 570 return true; 571 } 572 } 573 574 577 class DefaultConnectionHandler implements JDBCConnectionHandler 578 { 579 Connection con = null; 580 581 public Connection getConnection() 582 { 583 return con; 584 } 585 586 public Connection getConnection(String _url, String _username, String _password) 587 { 588 try 589 { 590 if(con != null && !con.isClosed()) con.close(); 591 con = DriverManager.getConnection(_url, _username, _password); 592 con.setAutoCommit(false); 593 } 594 catch(Exception e){} 595 596 return con; 597 } 598 } 599 600 601 602 603 604 605 | Popular Tags |