📄 jdbcaccesslogvalve.java
字号:
}
/**
* Sets the name of the field containing the URL part of the HTTP query.
*
* @param queryField The name of the field containing the URL part of
* the HTTP query.
*/
public void setQueryField(String queryField) {
this.queryField = queryField;
}
/**
* Sets the name of the field containing the HTTP response status code.
*
* @param statusField The name of the HTTP response status code field.
*/
public void setStatusField(String statusField) {
this.statusField = statusField;
}
/**
* Sets the name of the field containing the number of bytes returned.
*
* @param bytesField The name of the returned bytes field.
*/
public void setBytesField(String bytesField) {
this.bytesField = bytesField;
}
/**
* Sets the name of the field containing the referer.
*
* @param refererField The referer field name.
*/
public void setRefererField(String refererField) {
this.refererField = refererField;
}
/**
* Sets the name of the field containing the user agent.
*
* @param userAgentField The name of the user agent field.
*/
public void setUserAgentField(String userAgentField) {
this.userAgentField = userAgentField;
}
/**
* Sets the logging pattern. The patterns supported correspond to the
* file-based "common" and "combined". These are translated into the use
* of tables containing either set of fields.
* <P><I>TO DO: more flexible field choices.</I></P>
*
* @param pattern The name of the logging pattern.
*/
public void setPattern(String pattern) {
this.pattern = pattern;
}
/**
* Determines whether IP host name resolution is done.
*
* @param resolveHosts "true" or "false", if host IP resolution
* is desired or not.
*/
public void setResolveHosts(String resolveHosts) {
this.resolveHosts = new Boolean(resolveHosts).booleanValue();
}
// --------------------------------------------------------- Public Methods
/**
* This method is invoked by Tomcat on each query.
*
* @param request The Request object.
* @param response The Response object.
* @param context The ValveContext object.
* @exception IOException Should not be thrown.
* @exception ServletException Database SQLException is wrapped
* in a ServletException.
*/
public void invoke(Request request, Response response,
ValveContext context)
throws IOException, ServletException {
context.invokeNext(request, response);
ServletRequest req = request.getRequest();
HttpServletRequest hreq = null;
if(req instanceof HttpServletRequest)
hreq = (HttpServletRequest) req;
String remoteHost = "";
if(resolveHosts)
remoteHost = req.getRemoteHost();
else
remoteHost = req.getRemoteAddr();
String user = "";
if(hreq != null)
user = hreq.getRemoteUser();
String query="";
if(hreq != null)
query = hreq.getRequestURI();
int bytes = response.getContentCount();
if(bytes < 0)
bytes = 0;
int status = ((HttpResponse)response).getStatus();
if (pattern.equals("combined")) {
String virtualHost = "";
if(hreq != null)
virtualHost = hreq.getServerName();
String method = "";
if(hreq != null)
method = hreq.getMethod();
String referer = "";
if(hreq != null)
referer = hreq.getHeader("referer");
String userAgent = "";
if(hreq != null)
userAgent = hreq.getHeader("user-agent");
}
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries>0) {
try {
open();
ps.setString(1, remoteHost);
ps.setString(2, user);
ps.setTimestamp(3, new Timestamp(getCurrentTimeMillis()));
ps.setString(4, query);
ps.setInt(5, status);
ps.setInt(6, bytes);
if (pattern.equals("combined")) {
String virtualHost = "";
if(hreq != null)
virtualHost = hreq.getServerName();
String method = "";
if(hreq != null)
method = hreq.getMethod();
String referer = "";
if(hreq != null)
referer = hreq.getHeader("referer");
String userAgent = "";
if(hreq != null)
userAgent = hreq.getHeader("user-agent");
ps.setString(7, virtualHost);
ps.setString(8, method);
ps.setString(9, referer);
ps.setString(10, userAgent);
}
ps.executeUpdate();
return;
} catch (SQLException e) {
// Log the problem for posterity
log(sm.getString("jdbcAccessLogValve.exception"), e);
// Close the connection so that it gets reopened next time
if (conn != null)
close();
}
numberOfTries--;
}
}
}
/**
* Adds a Lifecycle listener.
*
* @param listener The listener to add.
*/
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
/**
* Get the lifecycle listeners associated with this lifecycle. If this
* Lifecycle has no listeners registered, a zero-length array is returned.
*/
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}
/**
* Removes a Lifecycle listener.
*
* @param listener The listener to remove.
*/
public void removeLifecycleListener(LifecycleListener listener) {
lifecycle.removeLifecycleListener(listener);
}
/**
* Open (if necessary) and return a database connection for use by
* this AccessLogValve.
*
* @exception SQLException if a database error occurs
*/
protected void open() throws SQLException {
// Do nothing if there is a database connection already open
if (conn != null)
return ;
// Instantiate our database driver if necessary
if (driver == null) {
try {
Class clazz = Class.forName(driverName);
driver = (Driver) clazz.newInstance();
} catch (Throwable e) {
throw new SQLException(e.getMessage());
}
}
// Open a new connection
Properties props = new Properties();
props.put("autoReconnect", "true");
if (connectionName != null)
props.put("user", connectionName);
if (connectionPassword != null)
props.put("password", connectionPassword);
conn = driver.connect(connectionURL, props);
conn.setAutoCommit(true);
if (pattern.equals("common")) {
ps = conn.prepareStatement
("INSERT INTO " + tableName + " ("
+ remoteHostField + ", " + userField + ", "
+ timestampField +", " + queryField + ", "
+ statusField + ", " + bytesField
+ ") VALUES(?, ?, ?, ?, ?, ?)");
} else if (pattern.equals("combined")) {
ps = conn.prepareStatement
("INSERT INTO " + tableName + " ("
+ remoteHostField + ", " + userField + ", "
+ timestampField + ", " + queryField + ", "
+ statusField + ", " + bytesField + ", "
+ virtualHostField + ", " + methodField + ", "
+ refererField + ", " + userAgentField
+ ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
}
/**
* Close the specified database connection.
*
* @param dbConnection The connection to be closed
*/
protected void close() {
// Do nothing if the database connection is already closed
if (conn == null)
return;
// Close our prepared statements (if any)
try {
ps.close();
} catch (Throwable f) {
;
}
this.ps = null;
// Close this database connection, and log any errors
try {
conn.close();
} catch (SQLException e) {
log(sm.getString("jdbcAccessLogValeve.close"), e); // Just log it here
} finally {
this.conn = null;
}
}
/**
* Invoked by Tomcat on startup. The database connection is set here.
*
* @exception LifecycleException Can be thrown on lifecycle
* inconsistencies or on database errors (as a wrapped SQLException).
*/
public void start() throws LifecycleException {
if (started)
throw new LifecycleException
(sm.getString("accessLogValve.alreadyStarted"));
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
try {
open() ;
} catch (SQLException e) {
throw new LifecycleException(e);
}
}
/**
* Invoked by tomcat on shutdown. The database connection is closed here.
*
* @exception LifecycleException Can be thrown on lifecycle
* inconsistencies or on database errors (as a wrapped SQLException).
*/
public void stop() throws LifecycleException {
if (!started)
throw new LifecycleException
(sm.getString("accessLogValve.notStarted"));
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
started = false;
close() ;
}
public long getCurrentTimeMillis() {
long systime = System.currentTimeMillis();
if ((systime - currentTimeMillis) > 1000) {
currentTimeMillis = new java.util.Date(systime).getTime();
}
return currentTimeMillis;
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
*/
protected void log(String message) {
Logger logger = null;
String name = null;
if (container != null) {
logger = container.getLogger();
name = container.getName();
}
if (logger != null) {
logger.log(connectionURL+"[" + name + "]: " + message);
} else {
System.out.println(connectionURL+"[" + name + "]: " + message);
}
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
* @param throwable Associated exception
*/
protected void log(String message, Throwable throwable) {
Logger logger = null;
String name = null;
if (container != null) {
logger = container.getLogger();
name = container.getName();
}
if (logger != null) {
logger.log(connectionURL+"[" + name + "]: " + message, throwable);
} else {
System.out.println(connectionURL+"[" + name + "]: " + message);
throwable.printStackTrace(System.out);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -