001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021: package org.josso.gateway.audit.service.handler;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.josso.gateway.audit.SSOAuditTrail;
026:
027: import java.util.Enumeration;
028: import java.util.Properties;
029:
030: /**
031: * This audit trail handler sends all received trails to configured logger.
032: *
033: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
034: * @version $Id: LoggerAuditTrailHandler.java 508 2008-02-18 13:32:29Z sgonzalez $
035: */
036:
037: public class LoggerAuditTrailHandler extends BaseAuditTrailHandler {
038:
039: private Log logger = LogFactory
040: .getLog(LoggerAuditTrailHandler.class);
041:
042: // The logger category :
043: private String category;
044:
045: public int handle(SSOAuditTrail trail) {
046:
047: StringBuffer line = new StringBuffer();
048:
049: // Append TIME : CATEGORY - SEVERITY -
050: line.append(trail.getTime()).append(" - ").append(
051: trail.getCategory()).append(" - ").append(
052: trail.getSeverity());
053:
054: // Append SUBJECT - ACTION=OUTCOME
055: line.append(" - ").append(
056: trail.getSubject() == null ? "" : trail.getSubject())
057: .append(" - ").append(trail.getAction()).append("=")
058: .append(trail.getOutcome());
059:
060: // Append properties PROPERTIES:p1=v1,p2=v2
061: Properties properties = trail.getProperties();
062: Enumeration names = properties.propertyNames();
063:
064: if (names.hasMoreElements()) {
065: line.append(" - ");
066: }
067:
068: while (names.hasMoreElements()) {
069: String key = (String) names.nextElement();
070: String value = properties.getProperty(key);
071: line.append(key).append("=").append(value);
072:
073: if (names.hasMoreElements())
074: line.append(",");
075: }
076:
077: // Log error information !?
078: // Append error informatino if any : ERROR:<message><classname>
079: if (trail.getError() != null) {
080: line.append(" - ERROR:").append(
081: trail.getError().getMessage()).append(":").append(
082: trail.getError().getClass().getName());
083: // Append error cause informatino if any : ERROR_CAUSE:<message><classname>
084: if (trail.getError().getCause() != null) {
085: line.append(" ERROR_CAUSE:").append(
086: trail.getError().getCause().getMessage())
087: .append(":").append(
088: trail.getError().getClass().getName());
089: }
090: }
091:
092: // Logging the propper line :
093: logger.info(line);
094:
095: return CONTINUE_PROCESS;
096: }
097:
098: public String getCategory() {
099: return category;
100: }
101:
102: public void setCategory(String category) {
103: this.category = category;
104: logger = LogFactory.getLog(category);
105: }
106: }
|