001: package com.pentaho.security.utils;
002:
003: import java.lang.reflect.Field;
004: import java.lang.reflect.Method;
005:
006: import org.apache.commons.lang.builder.ReflectionToStringBuilder;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.pentaho.messages.Messages;
010: import org.springframework.aop.AfterReturningAdvice;
011: import org.springframework.aop.MethodBeforeAdvice;
012: import org.springframework.aop.ThrowsAdvice;
013:
014: /**
015: * Logger that uses AOP to log debugging information.
016: *
017: * <p><strong>Do not use this in production! It logs passwords in plain text!</strong></p>
018: * @author mlowery
019: */
020: public class LoggingInterceptor implements MethodBeforeAdvice,
021: AfterReturningAdvice, ThrowsAdvice {
022:
023: public void before(Method method, Object[] args, Object target)
024: throws Throwable {
025: Log logger = LogFactory.getLog(target.getClass());
026: if (logger.isDebugEnabled()) {
027: logger
028: .debug(Messages
029: .getString("LoggingInterceptor.DEBUG_BEGIN_METHOD")); //$NON-NLS-1$
030: log(method, args, target);
031: }
032: }
033:
034: public void afterReturning(Object returnValue, Method method,
035: Object[] args, Object target) throws Throwable {
036: Log logger = LogFactory.getLog(target.getClass());
037: if (logger.isDebugEnabled()) {
038: logger.debug(Messages
039: .getString("LoggingInterceptor.DEBUG_END_METHOD")); //$NON-NLS-1$
040: log(method, args, target);
041: logger
042: .debug(Messages
043: .getString(
044: "LoggingInterceptor.DEBUG_RETURN_VALUE", returnValue.getClass().getName(), toString(returnValue))); //$NON-NLS-1$
045: }
046: }
047:
048: public void afterThrowing(Method method, Object[] args,
049: Object target, Throwable exception) {
050: Log logger = LogFactory.getLog(target.getClass());
051: if (logger.isDebugEnabled()) {
052: logger
053: .debug(Messages
054: .getString("LoggingInterceptor.DEBUG_EXCEPTION_IN_METHOD")); //$NON-NLS-1$
055: log(method, args, target);
056: logger
057: .debug(Messages
058: .getString(
059: "LoggingInterceptor.DEBUG_EXCEPTION", exception.getClass().getName(), exception.getMessage())); //$NON-NLS-1$
060: logger
061: .debug(
062: Messages
063: .getString("LoggingInterceptor.DEBUG_STACK_TRACE"), exception); //$NON-NLS-1$
064: }
065: }
066:
067: private void log(Method method, Object[] args, Object target) {
068: Log logger = LogFactory.getLog(target.getClass());
069: if (logger.isDebugEnabled()) {
070: logger
071: .debug(Messages
072: .getString(
073: "LoggingInterceptor.DEBUG_METHOD_NAME", method.getName())); //$NON-NLS-1$
074: logger
075: .debug(Messages
076: .getString(
077: "LoggingInterceptor.DEBUG_TARGET_OBJECT", target.getClass().getName(), toString(target))); //$NON-NLS-1$
078: logger
079: .debug(Messages
080: .getString(
081: "LoggingInterceptor.DEBUG_METHOD_ARGS", arrayToString(args))); //$NON-NLS-1$
082: }
083: }
084:
085: /**
086: * Returns a string representation of the given object. This is useful when third-party objects do not have
087: * <code>toString()</code> implementations that meet your needs.
088: */
089: protected String toString(Object object) {
090: /**
091: * This impl uses reflection to print fields and it also skips sensitive fields.
092: */
093: return (new ReflectionToStringBuilder(object) {
094: protected boolean accept(Field f) {
095: return super .accept(f)
096: && !f.getName().equals("password") && !f.getName().equals("credentials"); //$NON-NLS-1$ //$NON-NLS-2$
097: }
098: }).toString();
099: }
100:
101: /**
102: * Returns a string representation of the given array. Instead of overriding this method, try overriding
103: * <code>toString(object)</code> instead.
104: */
105: protected String arrayToString(Object[] objects) {
106: StringBuffer buf = new StringBuffer();
107: if (null == objects) {
108: return "null"; //$NON-NLS-1$
109: } else {
110: for (int i = 0; i < objects.length; i++) {
111: if (i > 0) {
112: buf.append(", "); //$NON-NLS-1$
113: }
114: buf.append(toString(objects[i]));
115: }
116: return buf.toString();
117: }
118: }
119:
120: }
|