001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Apr 15, 2005
014: * @author Marc Batchelor
015: */
016: package org.pentaho.core.audit;
017:
018: import java.math.BigDecimal;
019:
020: import org.pentaho.core.runtime.IRuntimeContext;
021: import org.pentaho.core.session.IPentahoSession;
022: import org.pentaho.messages.Messages;
023: import org.pentaho.util.logging.ILogger;
024: import org.pentaho.util.logging.Logger;
025:
026: public class AuditHelper {
027:
028: public static void audit(IRuntimeContext runtimeContext,
029: IPentahoSession session, String messageType,
030: String message, String value, BigDecimal duration,
031: ILogger logger) {
032: try {
033:
034: String instanceId = (runtimeContext == null) ? "" : runtimeContext.getInstanceId(); //$NON-NLS-1$
035: String userId = session.getName();
036: String actionName = (runtimeContext == null) ? null
037: : runtimeContext.getActionName();
038: String objectType = (runtimeContext == null) ? "" : runtimeContext.getCurrentComponentName(); //$NON-NLS-1$
039: String processId = (runtimeContext == null) ? null
040: : runtimeContext.getProcessId();
041:
042: audit(instanceId, userId, actionName, objectType,
043: processId, messageType, message, value, duration,
044: logger);
045:
046: } catch (Exception e) {
047: logTheAuditError(logger, e);
048: }
049: }
050:
051: private static void logTheAuditError(ILogger logger, Exception e) {
052: String msg = null;
053: try {
054: msg = ((e.getMessage() != null) ? e.getMessage()
055: : Messages
056: .getErrorString("AUDITHELPER.ERROR_0001_AUDIT_ENTRY_ERROR")); //$NON-NLS-1$
057: } catch (Throwable ignored) {
058: msg = Messages
059: .getErrorString("AUDITHELPER.ERROR_0001_AUDIT_ENTRY_ERROR"); //$NON-NLS-1$
060: }
061: if ((msg.toLowerCase().indexOf("not found") >= 0)) { //$NON-NLS-1$
062: e = null; // Prevent Stack Trace
063: }
064: if (logger != null) {
065: logger.error(msg, e);
066: } else {
067: Logger.error(AuditHelper.class.getName(), msg, e);
068: }
069: }
070:
071: public static void audit(String instanceId, String userId,
072: String actionName, String objectType, String processId,
073: String messageType, String message, String value,
074: BigDecimal duration, ILogger logger) {
075: try {
076:
077: if ((processId == null) || (instanceId == null)
078: || (actionName == null) || actionName.equals("")) { //$NON-NLS-1$
079: if (processId == null) {
080: processId = ""; //$NON-NLS-1$
081: // TODO log this as an error
082: }
083: if (instanceId == null) {
084: instanceId = ""; //$NON-NLS-1$
085: // TODO log this as an error
086: }
087: if (actionName == null) {
088: actionName = ""; //$NON-NLS-1$
089: // TODO log this as an error
090: }
091: }
092: AuditEntry.auditJobDuration(processId, instanceId,
093: actionName, objectType, userId, messageType,
094: message, value, duration);
095: } catch (Exception e) {
096: logTheAuditError(logger, e);
097: }
098: }
099:
100: }
|