com.lamatek.valves
Class DBAccessLogValve

java.lang.Object
  extended by ValveBase
      extended by com.lamatek.valves.DBAccessLogValve

public class DBAccessLogValve
extends ValveBase

This class performs a similar function to the org.apache.catalina.valves.AccessLogValve class in that it logs each access for the Context in which it is defined. However this logger will log accesses to a database, rather than a text file. Defining a DBAccessLogValve is very similar to defining a standard AccessLogValve, using the same pattern setup, however the user must specify database & connection properties, rather than text file properties. (i.e.)

 <Valve className="com.lamatek.valves.DBAccessLogValve"
      pattern="%h,%H,%m,%U%q,%p,%s,%b,%D,%t,%v,%{Referer}i,%{User-Agent}i" resolveHosts="true"
      tableName="access_log"
      columnNames="remote_host,protocol,method,request,port,http_status,bytes,process_time,date,website,referer,useragent"
      resource="jdbc/sql"
      lookup="java:/comp/env"
 />
The following attributes are allowed (bold are required):

Attribute Values
className This value must be com.lamatek.valves.DBAccessLogValve
pattern This attribute defines which values are to be stored. This must be a comma separated list of markers. See below for a complete list of available markers.
columnNames This is a comma separated list of column names within the target database table. Each name must be valid and exist in the database, and must accept text (all data is stored as text for compatibility). The number of columnNames MUST match the number of markers listed in the pattern attribute and must be specified in the same order.
tableName The name of the table that is to receive the access data.
resource This value is used for Connection Pool environments and is the name of the resource to use to grab connections. If this value is specified, any values for driver, url, username and password are ignored and are not required.
lookup Used for Connection Pool environments. This is the JNDI lookup name for the resource. If not specified, java:/comp/env will be used.
driver For non-pooled environments. This is the fully qualified class name for the JDBC driver used to make connections. Required for non-pooled environments.
url For non-pooled environments. This is the connection URL for the desired target table. Required for non-pooled environments.
username For non-pooled environments. This is a valid username with permissions to the target table. Required for non-pooled environments.
password For non-pooled environments. This is the password for the specified username. Required for non-pooled environments.
resolveHosts Set to true to convert the IP address of the remote host into the corresponding host name via a DNS lookup. Set to false to skip this lookup, and report the remote IP address instead.

Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:

        %a - Remote IP address 
        %A - Local IP address 
        %b - Bytes sent, excluding HTTP headers, or '-' if zero 
        %B - Bytes sent, excluding HTTP headers 
        %h - Remote host name (or IP address if resolveHosts is false) 
        %H - Request protocol 
        %l - Remote logical username from identd (always returns '-') 
        %m - Request method (GET, POST, etc.) 
        %p - Local port on which this request was received 
        %q - Query string (prepended with a '?' if it exists) 
        %r - First line of the request (method and request URI) 
        %s - HTTP status code of the response 
        %S - User session ID 
        %t - Date and time, in Common Log Format 
        %u - Remote user that was authenticated (if any), else '-' 
        %U - Requested URL path 
        %v - Local server name 
        %D - Time taken to process the request, in millis 
        %T - Time taken to process the request, in seconds
There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest. It is modeled after the apache syntax:
        %{xxx}i for incoming headers 
        %{xxx}c for a specific cookie 
        %{xxx}r xxx is an attribute in the ServletRequest 
        %{xxx}s xxx is an attribute in the HttpSession 
The shorthand pattern name common (which is also the default) corresponds to %h %l %u %t "%r" %s %b".

The shorthand pattern name combined appends the values of the Referer and User-Agent headers, each in double quotes, to the common pattern described in the previous paragraph.


Field Summary
protected static java.lang.String info
           
protected  LifecycleSupport lifecycle
           
protected static java.lang.String[] months
           
 
Constructor Summary
DBAccessLogValve()
          Creates a new DBAccessLogValve with default values.
 
Method Summary
 void addLifecycleListener(LifecycleListener listener)
          Adds a LifeCycleListener.
 LifecycleListener[] findLifecycleListeners()
          Returns all registered LifecycleListeners.
 java.lang.String getColumnNames()
          Returns the comma separated list of column names that correlates to the current pattern.
 java.lang.String getCondition()
          Returns the conditional String for this instance.
 java.lang.String getDriver()
          Returns the currently assigned driver classname.
 java.lang.String getInfo()
           
 java.lang.String getLookup()
          Returns the JNDI lookup name for the resource specified.
 java.lang.String getPassword()
          Returns the database password currently assigned.
 java.lang.String getPattern()
          Returns the pattern currently used by this instance.
 java.lang.String getResource()
          Gets the resource name for connection pooling environments from which database connections are pulled.
 java.lang.String getTableName()
          Returns the table name where accesses are being logged.
 java.lang.String getUrl()
          Returns the url to the database that contains the logging table.
 java.lang.String getUsername()
          Returns the database username currently assigned.
 void invoke(Request request, Response response, ValveContext context)
          This method is called by Context anytime an access is made.
 boolean isResolveHosts()
          Reports if IP resolution is enabled.
protected  void log(java.lang.String message, java.util.Date date)
          This message performs the database logging functionality.
 void removeLifecycleListener(LifecycleListener listener)
          Unregisters the given LifecycleListener.
 void setColumnNames(java.lang.String columnNames)
          Sets the current column names within the specified table.
 void setCondition(java.lang.String condition)
          Sets the conditional String for this instance.
 void setDriver(java.lang.String driver)
          Sets the JDBC driver classname.
 void setLookup(java.lang.String lookup)
          Sets the JNDI lookup name to locate the current resource.
 void setPassword(java.lang.String password)
          Sets the password to use for database access.
 void setPattern(java.lang.String pattern)
          Sets the pattern for this instance, which determines which access data will be logged.
 void setResolveHosts(boolean resolveHosts)
          Denotes whether to attempt to resolve IPs to host names.
 void setResource(java.lang.String resource)
          Sets the resource name for connection pooling environments from which database connections should be pulled.
 void setTableName(java.lang.String tableName)
          Sets the table name where access data should be stored.
 void setUrl(java.lang.String url)
          Sets database url that contains the logging table..
 void setUsername(java.lang.String username)
          Sets the usernamed to use for database access.
 void start()
          Prepares for the beginning of active use of the public methods of this component
 void stop()
          Gracefully terminate the active use of the public methods of this component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

info

protected static final java.lang.String info
See Also:
Constant Field Values

lifecycle

protected LifecycleSupport lifecycle

months

protected static final java.lang.String[] months
Constructor Detail

DBAccessLogValve

public DBAccessLogValve()
Creates a new DBAccessLogValve with default values.

Method Detail

getInfo

public java.lang.String getInfo()
Returns:
The classname and version information.

getPattern

public java.lang.String getPattern()
Returns the pattern currently used by this instance. See notes above regarding pattern declaration.

Returns:
The currently assigned pattern or null.

setPattern

public void setPattern(java.lang.String pattern)
Sets the pattern for this instance, which determines which access data will be logged. See notes above regarding pattern declaration.

Parameters:
pattern - The pattern to use.

setResolveHosts

public void setResolveHosts(boolean resolveHosts)
Denotes whether to attempt to resolve IPs to host names. If set to true and an IP cannot be resolved, the IP will be reported.

Parameters:
resolveHosts - True to attempt to resolve to DNS names, or false to report IP addresses.

isResolveHosts

public boolean isResolveHosts()
Reports if IP resolution is enabled.

Returns:
True or false.

getCondition

public java.lang.String getCondition()
Returns the conditional String for this instance.

Returns:
The currently assigned conditional String or null if unassigned.

setCondition

public void setCondition(java.lang.String condition)
Sets the conditional String for this instance. If set, any request that contains an attribute value with this name will NOT be logged. For example, if set to "junk" and request where getAttribute("junk") does NOT return null will NOT be logged.

Parameters:
condition - The conditional request parameter.

invoke

public void invoke(Request request,
                   Response response,
                   ValveContext context)
            throws java.io.IOException,
                   ServletException
This method is called by Context anytime an access is made.

Parameters:
context - The ValveContext for this instance.
request - The Request being logged.
response - The Response for this request.
Throws:
java.io.IOException
ServletException

log

protected void log(java.lang.String message,
                   java.util.Date date)
This message performs the database logging functionality.

Parameters:
message - The comma delimted list of values to log.
date - The date of the request being logged.

addLifecycleListener

public void addLifecycleListener(LifecycleListener listener)
Adds a LifeCycleListener.

Parameters:
listener - The LifecycleListener to be notified.

findLifecycleListeners

public LifecycleListener[] findLifecycleListeners()
Returns all registered LifecycleListeners.

Returns:
An array of registered LifecycleListeners.

removeLifecycleListener

public void removeLifecycleListener(LifecycleListener listener)
Unregisters the given LifecycleListener.

Parameters:
listener - The LifeCycleListener to remove.

start

public void start()
           throws LifecycleException
Prepares for the beginning of active use of the public methods of this component

Throws:
LifecycleException

stop

public void stop()
          throws LifecycleException
Gracefully terminate the active use of the public methods of this component.

Throws:
LifecycleException

getDriver

public java.lang.String getDriver()
Returns the currently assigned driver classname.

Returns:
The fully qualified classname for the JDBC driver registered.

setDriver

public void setDriver(java.lang.String driver)
Sets the JDBC driver classname.

Parameters:
driver - The fully qualified classname for the JDBC driver to use.

getPassword

public java.lang.String getPassword()
Returns the database password currently assigned.

Returns:
The database password.

setPassword

public void setPassword(java.lang.String password)
Sets the password to use for database access.

Parameters:
password - The password to use for database access.

getResource

public java.lang.String getResource()
Gets the resource name for connection pooling environments from which database connections are pulled.

Returns:
The currently assigned resource name.

setResource

public void setResource(java.lang.String resource)
Sets the resource name for connection pooling environments from which database connections should be pulled.

Parameters:
resource - The resource name to pull connections from.

getTableName

public java.lang.String getTableName()
Returns the table name where accesses are being logged.

Returns:
The table name.

setTableName

public void setTableName(java.lang.String tableName)
Sets the table name where access data should be stored.

Parameters:
tableName - The table name.

getUrl

public java.lang.String getUrl()
Returns the url to the database that contains the logging table.

Returns:
The database URL as a String.

setUrl

public void setUrl(java.lang.String url)
Sets database url that contains the logging table..

Parameters:
url - The database url to connect to.

getLookup

public java.lang.String getLookup()
Returns the JNDI lookup name for the resource specified. If not specified returns java:/comp/env

Returns:
The JNDI lookup name.

setLookup

public void setLookup(java.lang.String lookup)
Sets the JNDI lookup name to locate the current resource.

Parameters:
lookup - The JNDI lookup name for the current resource.

getColumnNames

public java.lang.String getColumnNames()
Returns the comma separated list of column names that correlates to the current pattern.

Returns:
A comma separated list of table column names.

setColumnNames

public void setColumnNames(java.lang.String columnNames)
Sets the current column names within the specified table. See notes above on how to format the columnNames string.

Parameters:
columnNames - The comma separated list of table column names.

getUsername

public java.lang.String getUsername()
Returns the database username currently assigned.

Returns:
The database username.

setUsername

public void setUsername(java.lang.String username)
Sets the usernamed to use for database access.

Parameters:
username - The username to use for database access.