KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > injbossaop > lib > SimpleInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.injbossaop.lib;
23
24 import org.jboss.aop.joinpoint.ConstructorInvocation;
25 import org.jboss.aop.joinpoint.FieldReadInvocation;
26 import org.jboss.aop.joinpoint.FieldWriteInvocation;
27 import org.jboss.aop.joinpoint.Invocation;
28 import org.jboss.aop.joinpoint.MethodInvocation;
29 import org.jboss.aop.advice.Interceptor;
30 import org.jboss.aop.PointcutDef;
31 import org.jboss.aop.TypeDef;
32 import org.jboss.aop.Bind;
33 import org.jboss.aop.InterceptorDef;
34 import org.jboss.aop.pointcut.Pointcut;
35 import org.jboss.aop.pointcut.Typedef;
36
37 /**
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 37406 $
41  */

42 @InterceptorDef
43 @Bind (pointcut="org.jboss.injbossaop.lib.SimpleInterceptor.valueConstructors OR org.jboss.injbossaop.lib.SimpleInterceptor.valueMessage OR org.jboss.injbossaop.lib.SimpleInterceptor.service OR org.jboss.injbossaop.lib.SimpleInterceptor.sessionValue OR org.jboss.injbossaop.lib.SimpleInterceptor.mbeans")
44 public class SimpleInterceptor implements Interceptor
45 {
46    @PointcutDef ("execution(org.jboss.injbossaop.lib.ExampleValue->new(..))")
47    public static Pointcut valueConstructors;
48
49    @PointcutDef ("execution(* org.jboss.injbossaop.lib.ExampleValue->getMessage())")
50    public static Pointcut valueMessage;
51
52    @TypeDef ("class($instanceof{javax.servlet.http.HttpServlet}) AND class(org.jboss.injbossaop.web.*)")
53    public static Typedef servlets;
54
55    @PointcutDef ("execution(* $typedef{org.jboss.injbossaop.lib.SimpleInterceptor.servlets}->service(..))")
56    public static Pointcut service;
57
58    @TypeDef ("class($instanceof{javax.ejb.SessionBean}) AND class(org.jboss.injbossaop.ejb.*)")
59    public static Typedef sessionBeans;
60
61    @PointcutDef ("execution(* $typedef{org.jboss.injbossaop.lib.SimpleInterceptor.sessionBeans}->getValue(..))")
62    public static Pointcut sessionValue;
63
64    @PointcutDef ("all(org.jboss.injbossaop.mbean.*)")
65    public static Pointcut mbeans;
66
67    public String JavaDoc getName() { return "SimpleInterceptor"; }
68
69    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
70    {
71       try
72       {
73          System.out.println("<<< Entering SimpleInterceptor: " + invocationInfo(invocation));
74          return invocation.invokeNext();
75       }
76       finally
77       {
78          System.out.println(">>> Leaving SimpleInterceptor");
79       }
80    }
81
82    private String JavaDoc invocationInfo(Invocation invocation)
83    {
84       StringBuffer JavaDoc info = new StringBuffer JavaDoc("\n\tinvocation class: " + invocation.getClass().getName());
85
86       if (invocation instanceof MethodInvocation)
87       {
88          MethodInvocation mi = (MethodInvocation)invocation;
89          info.append("\n\ttype: Method Invocation");
90          info.append("\n\tmethod: " + mi.getMethod().getName());
91          info.append("\n\tClass containing method: " + mi.getTargetObject().getClass().getName());
92       }
93       else if (invocation instanceof ConstructorInvocation)
94       {
95          ConstructorInvocation ci = (ConstructorInvocation)invocation;
96          info.append("\n\ttype: Constructor Invocation");
97          info.append("\n\tconstructor: " + ci.getConstructor());
98       }
99       else if (invocation instanceof FieldWriteInvocation)
100       {
101          FieldWriteInvocation fi = (FieldWriteInvocation)invocation;
102          info.append("\n\ttype: Field Write Invocation");
103          info.append("\n\tfield: " + fi.getField());
104
105       }
106       else if (invocation instanceof FieldReadInvocation)
107       {
108          FieldReadInvocation fi = (FieldReadInvocation)invocation;
109          info.append("\n\ttype: Field Write Invocation");
110          info.append("\n\tfield: " + fi.getField());
111       }
112
113       return info.toString();
114    }
115 }
116
Popular Tags