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.jmx.connector.invoker;
23:
24: import org.jboss.invocation.MarshalledInvocation;
25: import org.jboss.mx.interceptor.AbstractInterceptor;
26: import org.jboss.mx.server.Invocation;
27:
28: /**
29: * An interceptor that validates the Serializability of responses,
30: * using plugable policies.
31: *
32: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
33: * @author <a href="mailto:fabcipriano@yahoo.com.br">Fabiano C. de Oliveira</a>
34: * @version $Revision: 57209 $
35: */
36: public class SerializableInterceptor extends AbstractInterceptor {
37: /** The plugable policy to use */
38: private SerializablePolicy policy = new NoopPolicy();
39:
40: /**
41: * Configure a SerializablePolicy class
42: */
43: public void setPolicyClass(String policyClass) throws Exception {
44: try {
45: // try to load the policy Class
46: Class clazz = Thread.currentThread()
47: .getContextClassLoader().loadClass(policyClass);
48: policy = (SerializablePolicy) clazz.newInstance();
49: } catch (Exception e) // ClassNotFoundException, IllegalAccessException, InstantiationException
50: {
51: // policy class not found. Make a second try using
52: // the 'org.jboss.jmx.connector.invoker.serializablepolicy.' package prefix
53: // for the "standard" reponse policies provided with jboss.
54: // If that fails, too, rethrow the original exception.
55: try {
56: policyClass = "org.jboss.jmx.connector.invoker.serializablepolicy."
57: + policyClass;
58: Class clazz = Thread.currentThread()
59: .getContextClassLoader().loadClass(policyClass);
60: policy = (SerializablePolicy) clazz.newInstance();
61: } catch (Exception inner) {
62: throw e;
63: }
64: }
65: }
66:
67: public Object invoke(Invocation invocation) throws Throwable {
68: // Invoke the next in the sequence
69: Object result = invocation.nextInterceptor().invoke(invocation);
70:
71: // If the invocation was an 'invoke(MarshalledInvocation)'
72: // filter the result using the plugable policy
73: if ("invoke".equals(invocation.getName())) {
74: Object[] args = invocation.getArgs();
75: if ((args.length == 1)
76: && (args[0] instanceof MarshalledInvocation)) {
77: MarshalledInvocation mi = (MarshalledInvocation) args[0];
78: result = policy.filter(mi, result);
79: }
80: }
81: return result;
82: }
83:
84: /**
85: * A noop serializable policy
86: */
87: public class NoopPolicy implements SerializablePolicy {
88: public Object filter(MarshalledInvocation input, Object result)
89: throws Throwable {
90: return result;
91: }
92: }
93: }
|