~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/transporter/TransporterHandler.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.remoting.transporter;
 
23
 
 
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;
 
30
 
 
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;
 
36
 
 
37
/**
 
38
 * Simple handler that uses reflection to make calls on target POJO (as supplied in the constructor)
 
39
 * when receive invocation requests.
 
40
 *
 
41
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
42
 */
 
43
public class TransporterHandler implements ServerInvocationHandler
 
44
{
 
45
   private Object targetPOJO = null;
 
46
 
 
47
   public TransporterHandler(Object target)
 
48
   {
 
49
      this.targetPOJO = target;
 
50
   }
 
51
 
 
52
   /**
 
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.
 
56
    *
 
57
    * @param invocation
 
58
    * @return
 
59
    * @throws Throwable
 
60
    */
 
61
   public Object invoke(InvocationRequest invocation) throws Throwable
 
62
   {
 
63
      Object request = invocation.getParameter();
 
64
 
 
65
      // Am expecting a NameBasedInvocation as the parameter
 
66
      NameBasedInvocation nbInvocation = (NameBasedInvocation) request;
 
67
 
 
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++)
 
73
      {
 
74
         Class signature = getPrimitiveType(sig[x]);
 
75
         if(signature != null)
 
76
         {
 
77
            classSig[x] = signature;
 
78
         }
 
79
         else
 
80
         {
 
81
            classSig[x] = Class.forName(sig[x]);
 
82
         }
 
83
      }
 
84
 
 
85
      // use reflection to make the call
 
86
      Method method = getMethod(targetPOJO.getClass(), methodName, classSig);      
 
87
      Object responseObject = method.invoke(targetPOJO, params);
 
88
 
 
89
      return responseObject;
 
90
   }
 
91
 
 
92
   private Class getPrimitiveType(String name)
 
93
   {
 
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;
 
103
      return null;
 
104
   }
 
105
 
 
106
 
 
107
   /**
 
108
    * set the mbean server that the handler can reference
 
109
    *
 
110
    * @param server
 
111
    */
 
112
   public void setMBeanServer(MBeanServer server)
 
113
   {
 
114
      //NOOP
 
115
   }
 
116
 
 
117
   /**
 
118
    * set the invoker that owns this handler
 
119
    *
 
120
    * @param invoker
 
121
    */
 
122
   public void setInvoker(ServerInvoker invoker)
 
123
   {
 
124
      //NOOP
 
125
   }
 
126
 
 
127
   /**
 
128
    * Adds a callback handler that will listen for callbacks from
 
129
    * the server invoker handler.
 
130
    *
 
131
    * @param callbackHandler
 
132
    */
 
133
   public void addListener(InvokerCallbackHandler callbackHandler)
 
134
   {
 
135
      //NOOP
 
136
   }
 
137
 
 
138
   /**
 
139
    * Removes the callback handler that was listening for callbacks
 
140
    * from the server invoker handler.
 
141
    *
 
142
    * @param callbackHandler
 
143
    */
 
144
   public void removeListener(InvokerCallbackHandler callbackHandler)
 
145
   {
 
146
      //NOOP
 
147
   }
 
148
 
 
149
   static private Method getMethod(final Class c, final String name, final Class[] parameterTypes)
 
150
   throws NoSuchMethodException
 
151
   {
 
152
      if (SecurityUtility.skipAccessControl())
 
153
      {
 
154
         return c.getMethod(name, parameterTypes);
 
155
      }
 
156
 
 
157
      try
 
158
      {
 
159
         return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
160
         {
 
161
            public Object run() throws NoSuchMethodException
 
162
            {
 
163
               return c.getMethod(name, parameterTypes);
 
164
            }
 
165
         });
 
166
      }
 
167
      catch (PrivilegedActionException e)
 
168
      {
 
169
         throw (NoSuchMethodException) e.getCause();
 
170
      }
 
171
   }
 
172
}
 
 
b'\\ No newline at end of file'