1 17 18 19 package org.apache.catalina.valves; 20 21 22 import java.io.IOException ; 23 import java.sql.Connection ; 24 import java.sql.Driver ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 import java.sql.Timestamp ; 28 import java.util.Properties ; 29 30 import javax.servlet.ServletException ; 31 32 import org.apache.catalina.Lifecycle; 33 import org.apache.catalina.LifecycleException; 34 import org.apache.catalina.LifecycleListener; 35 import org.apache.catalina.connector.Request; 36 import org.apache.catalina.connector.Response; 37 import org.apache.catalina.util.LifecycleSupport; 38 import org.apache.catalina.util.StringManager; 39 40 111 112 public final class JDBCAccessLogValve 113 extends ValveBase 114 implements Lifecycle { 115 116 118 119 140 public JDBCAccessLogValve() { 141 super(); 142 driverName = null; 143 connectionURL = null; 144 tableName = "access"; 145 remoteHostField = "remoteHost"; 146 userField = "userName"; 147 timestampField = "timestamp"; 148 virtualHostField = "virtualHost"; 149 methodField = "method"; 150 queryField = "query"; 151 statusField = "status"; 152 bytesField = "bytes"; 153 refererField = "referer"; 154 userAgentField = "userAgent"; 155 pattern = "common"; 156 resolveHosts = false; 157 conn = null; 158 ps = null; 159 currentTimeMillis = new java.util.Date ().getTime(); 160 } 161 162 163 165 166 169 protected String connectionName = null; 170 171 172 175 protected String connectionPassword = null; 176 177 180 protected Driver driver = null; 181 182 183 private String driverName; 184 private String connectionURL; 185 private String tableName; 186 private String remoteHostField; 187 private String userField; 188 private String timestampField; 189 private String virtualHostField; 190 private String methodField; 191 private String queryField; 192 private String statusField; 193 private String bytesField; 194 private String refererField; 195 private String userAgentField; 196 private String pattern; 197 private boolean resolveHosts; 198 199 200 private Connection conn; 201 private PreparedStatement ps; 202 203 204 private long currentTimeMillis; 205 206 207 210 protected static String info = 211 "org.apache.catalina.valves.JDBCAccessLogValve/1.1"; 212 213 214 217 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 218 219 220 223 private StringManager sm = StringManager.getManager(Constants.Package); 224 225 226 229 private boolean started = false; 230 231 232 234 238 public String getConnectionName() { 239 return connectionName; 240 } 241 242 247 public void setConnectionName(String connectionName) { 248 this.connectionName = connectionName; 249 } 250 251 256 public void setDriverName(String driverName) { 257 this.driverName = driverName; 258 } 259 260 264 public String getConnectionPassword() { 265 return connectionPassword; 266 } 267 268 273 public void setConnectionPassword(String connectionPassword) { 274 this.connectionPassword = connectionPassword; 275 } 276 277 282 public void setConnectionURL(String connectionURL) { 283 this.connectionURL = connectionURL; 284 } 285 286 287 292 public void setTableName(String tableName) { 293 this.tableName = tableName; 294 } 295 296 297 302 public void setRemoteHostField(String remoteHostField) { 303 this.remoteHostField = remoteHostField; 304 } 305 306 307 312 public void setUserField(String userField) { 313 this.userField = userField; 314 } 315 316 317 322 public void setTimestampField(String timestampField) { 323 this.timestampField = timestampField; 324 } 325 326 327 333 public void setVirtualHostField(String virtualHostField) { 334 this.virtualHostField = virtualHostField; 335 } 336 337 338 343 public void setMethodField(String methodField) { 344 this.methodField = methodField; 345 } 346 347 348 354 public void setQueryField(String queryField) { 355 this.queryField = queryField; 356 } 357 358 359 364 public void setStatusField(String statusField) { 365 this.statusField = statusField; 366 } 367 368 369 374 public void setBytesField(String bytesField) { 375 this.bytesField = bytesField; 376 } 377 378 379 384 public void setRefererField(String refererField) { 385 this.refererField = refererField; 386 } 387 388 389 394 public void setUserAgentField(String userAgentField) { 395 this.userAgentField = userAgentField; 396 } 397 398 399 407 public void setPattern(String pattern) { 408 this.pattern = pattern; 409 } 410 411 412 418 public void setResolveHosts(String resolveHosts) { 419 this.resolveHosts = new Boolean (resolveHosts).booleanValue(); 420 } 421 422 423 425 426 436 public void invoke(Request request, Response response) 437 throws IOException , ServletException { 438 439 getNext().invoke(request, response); 440 441 String remoteHost = ""; 442 if(resolveHosts) 443 remoteHost = request.getRemoteHost(); 444 else 445 remoteHost = request.getRemoteAddr(); 446 String user = ""; 447 if(request != null) 448 user = request.getRemoteUser(); 449 String query=""; 450 if(request != null) 451 query = request.getRequestURI(); 452 int bytes = response.getContentCount(); 453 if(bytes < 0) 454 bytes = 0; 455 int status = response.getStatus(); 456 if (pattern.equals("combined")) { 457 String virtualHost = ""; 458 if(request != null) 459 virtualHost = request.getServerName(); 460 String method = ""; 461 if(request != null) 462 method = request.getMethod(); 463 String referer = ""; 464 if(request != null) 465 referer = request.getHeader("referer"); 466 String userAgent = ""; 467 if(request != null) 468 userAgent = request.getHeader("user-agent"); 469 } 470 synchronized (this) { 471 int numberOfTries = 2; 472 while (numberOfTries>0) { 473 try { 474 open(); 475 476 ps.setString(1, remoteHost); 477 ps.setString(2, user); 478 ps.setTimestamp(3, new Timestamp (getCurrentTimeMillis())); 479 ps.setString(4, query); 480 ps.setInt(5, status); 481 ps.setInt(6, bytes); 482 if (pattern.equals("combined")) { 483 484 String virtualHost = ""; 485 if(request != null) 486 virtualHost = request.getServerName(); 487 String method = ""; 488 if(request != null) 489 method = request.getMethod(); 490 String referer = ""; 491 if(request != null) 492 referer = request.getHeader("referer"); 493 String userAgent = ""; 494 if(request != null) 495 userAgent = request.getHeader("user-agent"); 496 ps.setString(7, virtualHost); 497 ps.setString(8, method); 498 ps.setString(9, referer); 499 ps.setString(10, userAgent); 500 } 501 ps.executeUpdate(); 502 return; 503 } catch (SQLException e) { 504 container.getLogger().error(sm.getString("jdbcAccessLogValve.exception"), e); 506 507 if (conn != null) 509 close(); 510 } 511 numberOfTries--; 512 } 513 } 514 515 } 516 517 518 523 public void addLifecycleListener(LifecycleListener listener) { 524 525 lifecycle.addLifecycleListener(listener); 526 527 } 528 529 530 534 public LifecycleListener[] findLifecycleListeners() { 535 536 return lifecycle.findLifecycleListeners(); 537 538 } 539 540 541 546 public void removeLifecycleListener(LifecycleListener listener) { 547 548 lifecycle.removeLifecycleListener(listener); 549 550 } 551 552 558 protected void open() throws SQLException { 559 560 if (conn != null) 562 return ; 563 564 if (driver == null) { 566 try { 567 Class clazz = Class.forName(driverName); 568 driver = (Driver ) clazz.newInstance(); 569 } catch (Throwable e) { 570 throw new SQLException (e.getMessage()); 571 } 572 } 573 574 Properties props = new Properties (); 576 props.put("autoReconnect", "true"); 577 if (connectionName != null) 578 props.put("user", connectionName); 579 if (connectionPassword != null) 580 props.put("password", connectionPassword); 581 conn = driver.connect(connectionURL, props); 582 conn.setAutoCommit(true); 583 if (pattern.equals("common")) { 584 ps = conn.prepareStatement 585 ("INSERT INTO " + tableName + " (" 586 + remoteHostField + ", " + userField + ", " 587 + timestampField +", " + queryField + ", " 588 + statusField + ", " + bytesField 589 + ") VALUES(?, ?, ?, ?, ?, ?)"); 590 } else if (pattern.equals("combined")) { 591 ps = conn.prepareStatement 592 ("INSERT INTO " + tableName + " (" 593 + remoteHostField + ", " + userField + ", " 594 + timestampField + ", " + queryField + ", " 595 + statusField + ", " + bytesField + ", " 596 + virtualHostField + ", " + methodField + ", " 597 + refererField + ", " + userAgentField 598 + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 599 } 600 } 601 602 605 protected void close() { 606 607 if (conn == null) 609 return; 610 611 try { 613 ps.close(); 614 } catch (Throwable f) { 615 ; 616 } 617 this.ps = null; 618 619 620 621 try { 623 conn.close(); 624 } catch (SQLException e) { 625 container.getLogger().error(sm.getString("jdbcAccessLogValeve.close"), e); } finally { 627 this.conn = null; 628 } 629 630 } 631 637 public void start() throws LifecycleException { 638 639 if (started) 640 throw new LifecycleException 641 (sm.getString("accessLogValve.alreadyStarted")); 642 lifecycle.fireLifecycleEvent(START_EVENT, null); 643 started = true; 644 645 try { 646 open() ; 647 } catch (SQLException e) { 648 throw new LifecycleException(e); 649 } 650 651 } 652 653 654 660 public void stop() throws LifecycleException { 661 662 if (!started) 663 throw new LifecycleException 664 (sm.getString("accessLogValve.notStarted")); 665 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 666 started = false; 667 668 close() ; 669 670 } 671 672 673 public long getCurrentTimeMillis() { 674 long systime = System.currentTimeMillis(); 675 if ((systime - currentTimeMillis) > 1000) { 676 currentTimeMillis = new java.util.Date (systime).getTime(); 677 } 678 return currentTimeMillis; 679 } 680 681 } 682 | Popular Tags |