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.
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.
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.
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.
22
package org.jboss.remoting.transporter;
24
import org.jboss.remoting.InvocationRequest;
25
import org.jboss.remoting.ServerInvocationHandler;
26
import org.jboss.remoting.ServerInvoker;
27
import org.jboss.remoting.callback.InvokerCallbackHandler;
28
import org.jboss.remoting.invocation.NameBasedInvocation;
29
import org.jboss.remoting.util.SecurityUtility;
31
import javax.management.MBeanServer;
32
import java.lang.reflect.Method;
33
import java.security.AccessController;
34
import java.security.PrivilegedActionException;
35
import java.security.PrivilegedExceptionAction;
38
* Simple handler that uses reflection to make calls on target POJO (as supplied in the constructor)
39
* when receive invocation requests.
41
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
43
public class TransporterHandler implements ServerInvocationHandler
45
private Object targetPOJO = null;
47
public TransporterHandler(Object target)
49
this.targetPOJO = target;
53
* Takes the invocation request, which should have a internal parameter of NameBasedInvocation,
54
* and convert that to a method call on the target POJO (using reflection). Then return the Object
55
* returned from the method call on the target POJO.
61
public Object invoke(InvocationRequest invocation) throws Throwable
63
Object request = invocation.getParameter();
65
// Am expecting a NameBasedInvocation as the parameter
66
NameBasedInvocation nbInvocation = (NameBasedInvocation) request;
68
final String methodName = nbInvocation.getMethodName();
69
Object[] params = nbInvocation.getParameters();
70
String[] sig = nbInvocation.getSignature();
71
final Class[] classSig = new Class[sig.length];
72
for(int x = 0; x < sig.length; x++)
74
Class signature = getPrimitiveType(sig[x]);
77
classSig[x] = signature;
81
classSig[x] = Class.forName(sig[x]);
85
// use reflection to make the call
86
Method method = getMethod(targetPOJO.getClass(), methodName, classSig);
87
Object responseObject = method.invoke(targetPOJO, params);
89
return responseObject;
92
private Class getPrimitiveType(String name)
94
if (name.equals("byte")) return Byte.TYPE;
95
if (name.equals("short")) return Short.TYPE;
96
if (name.equals("int")) return Integer.TYPE;
97
if (name.equals("long")) return Long.TYPE;
98
if (name.equals("char")) return Character.TYPE;
99
if (name.equals("float")) return Float.TYPE;
100
if (name.equals("double")) return Double.TYPE;
101
if (name.equals("boolean")) return Boolean.TYPE;
102
if (name.equals("void")) return Void.TYPE;
108
* set the mbean server that the handler can reference
112
public void setMBeanServer(MBeanServer server)
118
* set the invoker that owns this handler
122
public void setInvoker(ServerInvoker invoker)
128
* Adds a callback handler that will listen for callbacks from
129
* the server invoker handler.
131
* @param callbackHandler
133
public void addListener(InvokerCallbackHandler callbackHandler)
139
* Removes the callback handler that was listening for callbacks
140
* from the server invoker handler.
142
* @param callbackHandler
144
public void removeListener(InvokerCallbackHandler callbackHandler)
149
static private Method getMethod(final Class c, final String name, final Class[] parameterTypes)
150
throws NoSuchMethodException
152
if (SecurityUtility.skipAccessControl())
154
return c.getMethod(name, parameterTypes);
159
return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
161
public Object run() throws NoSuchMethodException
163
return c.getMethod(name, parameterTypes);
167
catch (PrivilegedActionException e)
169
throw (NoSuchMethodException) e.getCause();
b'\\ No newline at end of file'