01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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: 57200 $
33: */
34: public class DynamicInterceptor extends AbstractInterceptor {
35: /** methods implemented from org.jboss.mx.server.Interceptable */
36: public static final String ADD_INTERCEPTOR = "addOperationInterceptor";
37: public static final String REMOVE_INTERCEPTOR = "removeOperationInterceptor";
38:
39: /** the invoker implementing Interceptable */
40: MBeanInvoker invoker;
41:
42: /**
43: * CTOR
44: */
45: public DynamicInterceptor(MBeanInvoker invoker) {
46: // initialize logger and name
47: super ();
48: setName("DynamicInterceptor");
49:
50: this .invoker = invoker;
51: }
52:
53: /**
54: * Do the trick
55: */
56: public Object invoke(Invocation invocation) throws Throwable {
57: String type = invocation.getType();
58:
59: // implement Interceptable by delegating to MBeanInvoker
60: if (type.equals(Invocation.OP_INVOKE)) {
61: String name = invocation.getName();
62:
63: if (name.equals(ADD_INTERCEPTOR)) {
64: Object args[] = invocation.getArgs();
65: Object retn = invocation.getReturnTypeClass();
66:
67: if ((args.length == 1)
68: && (args[0] instanceof Interceptor)
69: && (retn == null)) {
70: invoker
71: .addOperationInterceptor((Interceptor) args[0]);
72: return null;
73: }
74: } else if (name.equals(REMOVE_INTERCEPTOR)) {
75: Object args[] = invocation.getArgs();
76: Object retn = invocation.getReturnTypeClass();
77:
78: if ((args.length == 1)
79: && (args[0] instanceof Interceptor)
80: && (retn == null)) {
81: invoker
82: .removeOperationInterceptor((Interceptor) args[0]);
83: return null;
84: }
85: }
86: }
87:
88: // call the next in the interceptor chain,
89: // if nobody follows dispatch the call
90: Interceptor next = invocation.nextInterceptor();
91: if (next != null) {
92: return next.invoke(invocation);
93: } else {
94: return invocation.dispatch();
95: }
96: }
97: }
|