KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > interceptor > DynamicInterceptor


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.mx.interceptor;
23
24 import org.jboss.mx.server.Invocation;
25 import org.jboss.mx.server.MBeanInvoker;
26
27 /**
28  * Interceptor that provides access to the org.jboss.mx.server.Interceptable hooks
29  * for dynamically adding and removing interceptors to an MBean.
30  *
31  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
32  * @version $Revision: 39975 $
33  */

34 public class DynamicInterceptor extends AbstractInterceptor
35 {
36    /** methods implemented from org.jboss.mx.server.Interceptable */
37    public static final String JavaDoc ADD_INTERCEPTOR = "addOperationInterceptor";
38    public static final String JavaDoc REMOVE_INTERCEPTOR = "removeOperationInterceptor";
39
40    /** the invoker implementing Interceptable */
41    MBeanInvoker invoker;
42    
43    /**
44     * CTOR
45     */

46    public DynamicInterceptor(MBeanInvoker invoker)
47    {
48       // initialize logger and name
49
super();
50       setName("DynamicInterceptor");
51       
52       this.invoker = invoker;
53    }
54
55    /**
56     * Do the trick
57     */

58    public Object JavaDoc invoke(Invocation invocation)
59       throws Throwable JavaDoc
60    {
61       String JavaDoc type = invocation.getType();
62       
63       // implement Interceptable by delegating to MBeanInvoker
64
if (type.equals(Invocation.OP_INVOKE))
65       {
66          String JavaDoc name = invocation.getName();
67          
68          if (name.equals(ADD_INTERCEPTOR))
69          {
70             Object JavaDoc args[] = invocation.getArgs();
71             Object JavaDoc retn = invocation.getReturnTypeClass();
72             
73             if ((args.length == 1) && (args[0] instanceof Interceptor) && (retn == null))
74             {
75                invoker.addOperationInterceptor((Interceptor)args[0]);
76                return null;
77             }
78          }
79          else if (name.equals(REMOVE_INTERCEPTOR))
80          {
81             Object JavaDoc args[] = invocation.getArgs();
82             Object JavaDoc retn = invocation.getReturnTypeClass();
83             
84             if ((args.length == 1) && (args[0] instanceof Interceptor) && (retn == null))
85             {
86                invoker.removeOperationInterceptor((Interceptor)args[0]);
87                return null;
88             }
89          }
90       }
91
92       // call the next in the interceptor chain,
93
// if nobody follows dispatch the call
94
Interceptor next = invocation.nextInterceptor();
95       if (next != null)
96       {
97          return next.invoke(invocation);
98       }
99       else
100       {
101          return invocation.dispatch();
102       }
103    }
104 }
105
Popular Tags