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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/mock/MockServerInvocationHandler.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
 
 
23
package org.jboss.test.remoting.transport.mock;
 
24
 
 
25
import java.rmi.MarshalledObject;
 
26
import java.util.ArrayList;
 
27
import java.util.Collections;
 
28
import java.util.HashMap;
 
29
import java.util.Iterator;
 
30
import java.util.List;
 
31
import java.util.Map;
 
32
import javax.management.MBeanServer;
 
33
import org.jboss.logging.Logger;
 
34
import org.jboss.remoting.Client;
 
35
import org.jboss.remoting.InvocationRequest;
 
36
import org.jboss.remoting.InvokerLocator;
 
37
import org.jboss.remoting.ServerInvocationHandler;
 
38
import org.jboss.remoting.ServerInvoker;
 
39
import org.jboss.remoting.callback.Callback;
 
40
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
41
import org.jboss.remoting.invocation.InternalInvocation;
 
42
import org.jboss.remoting.invocation.NameBasedInvocation;
 
43
import org.jboss.test.remoting.ComplexReturn;
 
44
import org.jboss.test.remoting.byvalue.ByValuePayload;
 
45
 
 
46
/**
 
47
 * MockServerInvocationHandler
 
48
 *
 
49
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 
50
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
51
 * @version $Revision: 1066 $
 
52
 */
 
53
public class MockServerInvocationHandler implements ServerInvocationHandler
 
54
{
 
55
   private ServerInvoker invoker;
 
56
   private List listeners = Collections.synchronizedList(new ArrayList());
 
57
   private Map clientListeners = new HashMap();
 
58
 
 
59
   private static final Logger log = Logger.getLogger(MockServerInvocationHandler.class);
 
60
 
 
61
 
 
62
   /**
 
63
    * set the invoker that owns this handler
 
64
    *
 
65
    * @param invoker
 
66
    */
 
67
   public void setInvoker(ServerInvoker invoker)
 
68
   {
 
69
      this.invoker = invoker;
 
70
   }
 
71
 
 
72
   /**
 
73
    * set the mbean server that the handler can reference
 
74
    *
 
75
    * @param server
 
76
    */
 
77
   public void setMBeanServer(MBeanServer server)
 
78
   {
 
79
   }
 
80
 
 
81
   public Object invoke(InvocationRequest invocation)
 
82
         throws Throwable
 
83
   {
 
84
      Object param = invocation.getParameter();
 
85
      String methodName = "";
 
86
      Object[] params = null;
 
87
      String[] sig = null;
 
88
 
 
89
      if(param instanceof NameBasedInvocation)
 
90
      {
 
91
         NameBasedInvocation nbi = (NameBasedInvocation) param;
 
92
         methodName = nbi.getMethodName();
 
93
         params = nbi.getParameters();
 
94
         sig = nbi.getSignature();
 
95
      }
 
96
      else if(param instanceof InternalInvocation)
 
97
      {
 
98
         InternalInvocation ii = (InternalInvocation) param;
 
99
         methodName = ii.getMethodName();
 
100
         params = ii.getParameters();
 
101
      }
 
102
      else
 
103
      {
 
104
         log.info("Don't recognize the parameter type, so just returning it.");
 
105
         return param;
 
106
      }
 
107
 
 
108
      String sessionId = invocation.getSessionId();
 
109
      String subsystem = invocation.getSubsystem();
 
110
 
 
111
      log.debug("invoke() called with method: " + methodName +
 
112
                "\tsessionId: " + sessionId + "\tsubsystem:" + subsystem);
 
113
      //deprecated since specific to JMX (old way of handling callbacks)
 
114
      if(methodName.equals("testComplexReturn"))
 
115
      {
 
116
         //Need to send back complex object containing array of complext objects.
 
117
         ComplexReturn ret = new ComplexReturn();
 
118
         return ret;
 
119
      }
 
120
      if(methodName.equals("testMarshalledObject"))
 
121
      {
 
122
         ComplexReturn ret = new ComplexReturn();
 
123
         MarshalledObject mObj = new MarshalledObject(ret);
 
124
         return mObj;
 
125
      }
 
126
      else if(methodName.equals("test"))
 
127
      {
 
128
         // will cause a callback on all the listeners
 
129
         log.debug("test called on server invocation handler, so should do callback.");
 
130
         CallbackDispatcher callbackDispatcher = new CallbackDispatcher(invocation.getSessionId(),
 
131
                                                                        invocation.getSubsystem(),
 
132
                                                                        new NameBasedInvocation("handleCallback",
 
133
                                                                                                params,
 
134
                                                                                                sig));
 
135
         Thread callbackThread = new Thread(callbackDispatcher);
 
136
         callbackThread.start();
 
137
      }
 
138
      else if(methodName.equals("addClientListener"))
 
139
      {
 
140
         Object obj = params[0];
 
141
         InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
 
142
 
 
143
         clientListeners.put(invocation.getSessionId(), clientHandler);
 
144
      }
 
145
      else if(methodName.equals("removeClientListener"))
 
146
      {
 
147
         Object obj = params[0];
 
148
         InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj;
 
149
 
 
150
         clientListeners.remove(invocation.getSessionId());
 
151
      }
 
152
      else if(methodName.equals("handleCallback"))
 
153
      {
 
154
         // got a callback from remote server
 
155
         InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) clientListeners.get(sessionId);
 
156
         clientHandler.handleCallback(new Callback(invocation.getParameter()));
 
157
      }
 
158
      else if(methodName.equals("testException") || methodName.equals("testThrowException"))
 
159
      {
 
160
         throw new Exception("Got call from client to throw exception.  This is expected.");
 
161
      }
 
162
      else if(methodName.equals("testByValue"))
 
163
      {
 
164
         // check to see if by value payload was serialized at some point
 
165
         Object arg = params[0];
 
166
         if(arg instanceof ByValuePayload)
 
167
         {
 
168
            ByValuePayload byValuePayload = (ByValuePayload) arg;
 
169
            return new Boolean(byValuePayload.wasMarshalled());
 
170
         }
 
171
         else
 
172
         {
 
173
            // Error in tests
 
174
            return Boolean.FALSE;
 
175
         }
 
176
      }
 
177
      Object ret = null;
 
178
      if(params != null)
 
179
      {
 
180
         ret = params[0];
 
181
         log.info("Found a parameter to return " + ret);
 
182
      } // end of if ()
 
183
      else
 
184
      {
 
185
         log.info("returning null");
 
186
      } // end of else
 
187
 
 
188
      return ret;
 
189
   }
 
190
 
 
191
   /**
 
192
    * @param sessionId
 
193
    * @deprecated
 
194
    */
 
195
   private void handleRemoveNotificationListener(String sessionId)
 
196
   {
 
197
      listeners.remove(sessionId);
 
198
   }
 
199
 
 
200
   /**
 
201
    * @param clientLocator
 
202
    * @param subsystem
 
203
    * @param sessionId
 
204
    * @throws Exception
 
205
    * @deprecated
 
206
    */
 
207
   private void handleAddNotificationListener(InvokerLocator clientLocator,
 
208
                                              String subsystem,
 
209
                                              String sessionId) throws Exception
 
210
   {
 
211
      Client callBackClient = new Client(clientLocator, subsystem);
 
212
      callBackClient.connect();
 
213
      listeners.add(callBackClient);
 
214
   }
 
215
 
 
216
   public void addListener(InvokerCallbackHandler callbackHandler)
 
217
   {
 
218
      listeners.add(callbackHandler);
 
219
      log.debug("added listener " + callbackHandler);
 
220
   }
 
221
 
 
222
   public void removeListener(InvokerCallbackHandler callbackHandler)
 
223
   {
 
224
      listeners.remove(callbackHandler);
 
225
      log.debug("removed listener " + callbackHandler);
 
226
   }
 
227
 
 
228
   private class CallbackDispatcher implements Runnable
 
229
   {
 
230
      private String sessionId;
 
231
      private String subsystem;
 
232
      private Object param;
 
233
 
 
234
      public CallbackDispatcher(String sessionId, String subsystem, Object param)
 
235
      {
 
236
         this.sessionId = sessionId;
 
237
         this.subsystem = subsystem;
 
238
         this.param = param;
 
239
      }
 
240
 
 
241
      public void run()
 
242
      {
 
243
         List tempList = null;
 
244
         synchronized(listeners)
 
245
         {
 
246
            tempList = new ArrayList(listeners);
 
247
         }
 
248
         
 
249
         Iterator itr = tempList.iterator();
 
250
         while(itr.hasNext())
 
251
         {
 
252
            try
 
253
            {
 
254
               InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next();
 
255
               Callback invocation = new Callback(param);
 
256
               handler.handleCallback(invocation);
 
257
            }
 
258
            catch(Throwable e)
 
259
            {
 
260
               e.printStackTrace();
 
261
            }
 
262
 
 
263
         }
 
264
      }
 
265
   }
 
266
}