View Javadoc

1   /*
2    * Created on May 12, 2004
3    *
4    */
5   package org.neo.swarm.interceptor.logging;
6   
7   import java.lang.reflect.InvocationTargetException;
8   import java.lang.reflect.UndeclaredThrowableException;
9   
10  import org.aopalliance.intercept.MethodInvocation;
11  import org.apache.log4j.Logger;
12  import org.neo.swarm.core.aop.silc.comp.MethodInterceptor;
13  import org.neo.swarm.util.serialize.Serializer;
14  import org.neo.swarm.util.serialize.XMLSerializer;
15  
16  /***
17   * Performs nonprogrammatic output on program execution. This is useful when
18   * an application enters error states and need to begin capturing data.
19   *  @author navery
20   */
21  public class TracingInterceptor implements MethodInterceptor {
22  	static Logger logger = Logger.getLogger(Tracer.class.toString());
23  	private int depth = 0;
24  	boolean enabled = true;
25  	static Serializer serializer ;
26  	
27  	TracingInterceptor(boolean enabled){
28  		this.enabled = enabled;
29  	}
30  	
31  	/* (non-Javadoc)
32  	 * @see org.neo.swarm.core.aop.container.component.MethodInterceptor#invoke(org.neo.swarm.core.aop.container.component.Invocation)
33  	 */
34  	public Object invoke(MethodInvocation invocation) throws Throwable {
35  		if (!enabled) return invocation.proceed();
36  		
37  		depth++;		
38  		String padding = getPadding(depth);
39  		String invocationString = invocation.toString();
40  		Object o = null;
41  		try {
42  		
43  			System.out.println(padding + ">> " + invocationString);
44  			 o = invocation.proceed();
45  			if (o instanceof Exception) {
46  				throw (Exception) o;
47  			}
48  			
49  			return o;
50  		} catch (Exception ex) {
51  			try {
52  			if (serializer == null ) serializer = new XMLSerializer();
53  			System.err.println("===============LOGGER REPORTING EXCEPTION ===================");
54  			System.err.println("Invocation:" + new String(serializer.serialize(invocation)));
55  			System.err.println(" Cause: "+  new String(serializer.serialize(ex)));			
56  			System.err.println(ex);
57  			if (ex instanceof InvocationTargetException) {
58  				InvocationTargetException ex2 = (InvocationTargetException) ex;
59  				if (ex2.getTargetException() instanceof UndeclaredThrowableException) {
60  					UndeclaredThrowableException ex3 = (UndeclaredThrowableException) ex2.getTargetException();
61  					Exception ex4 = (Exception) ex3.getUndeclaredThrowable();
62  					System.err.println("RealCause: " + ex4 + " msg: "+ ex4.getMessage());							
63  					throw ex4;
64  				}	
65  			}
66  				
67  			}finally {
68  				System.out.println("==========================================:");
69  			}
70  			ex.printStackTrace();
71  			throw ex;
72  			
73  		}finally {
74  			depth--;
75  			try {
76  				//System.out.println(padding + "<<  "+ invocation.getMethod().toString() + "  ret[" + o + "]");	
77  				System.out.println(padding + "<<  "+ invocationString + "  ret[" + o + "]");				
78  				//System.out.println(padding + "<<  "+ invocation.getMethod().toString() + "  ret[" + new String(serializer.serialize(o)) + "]");				
79  			} catch (Exception e) {
80  //			System.out.println(padding + "<< " + invocation.getMethod().toString() );
81  			System.out.println(padding + "<< " + invocationString);			
82  			}		
83  		}
84  	}
85  	
86  	
87  	/***
88  	 * @return Returns the enabled.
89  	 */
90  	public boolean isEnabled() {
91  		return enabled;
92  	}
93  	/***
94  	 * @param enabled The enabled to set.
95  	 */
96  	public void setEnabled(boolean enabled) {
97  		this.enabled = enabled;
98  	}
99  	private String getPadding(int depth) {
100 		StringBuffer pad = new StringBuffer();
101 		for (int i = 0; i < depth; i++) {
102 			pad.append("| | ");
103 		}
104 		return pad.toString();
105 	}	
106 }