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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.remoting.transporter;

import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvocationHandler;
import org.jboss.remoting.detection.multicast.MulticastDetector;
import org.jboss.remoting.transport.Connector;
import org.jboss.remoting.util.SecurityUtility;
import org.w3c.dom.Element;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * The remoting server to expose the target POJO.  This should be called on as a factory via
 * static methods.
 *
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 */
public class TransporterServer
{
   private Connector connector = null;

   /**
    * Creates a remoting server using the provided locator and subsytem and creating a TransporterHandler which
    * takes the specified target object.
    *
    * @param locator
    * @param target
    * @param subsystem
    * @throws Exception
    */
   public TransporterServer(InvokerLocator locator, Object target, String subsystem) throws Exception
   {
      connector = getConnector(locator, null, null);
      ServerInvocationHandler handler = new TransporterHandler(target);
      if (subsystem != null)
      {
         connector.addInvocationHandler(subsystem.toUpperCase(), handler);
      }
      else
      {
         addInterfaceSubsystems(connector, handler, target);
      }
   }

   /**
    * Creates a remoting server using the provided locator and subsytem and creating a TransporterHandler which
    * takes the specified target object.
    *
    * @param xmlConfig
    * @param target
    * @param subsystem
    * @throws Exception
    */
   public TransporterServer(Element xmlConfig, Object target, String subsystem) throws Exception
   {
      connector = getConnector(null, null, xmlConfig);
      ServerInvocationHandler handler = new TransporterHandler(target);
      if (subsystem != null)
      {
         connector.addInvocationHandler(subsystem.toUpperCase(), handler);
      }
      else
      {
         addInterfaceSubsystems(connector, handler, target);
      }
   }

   /**
    * Creates a remoting server using the provided locator and subsytem and creating a TransporterHandler which
    * takes the specified target object.
    *
    * @param locator
    * @param target
    * @param subsystem
    * @param config    - configuration data for Connector
    * @throws Exception
    */
   public TransporterServer(InvokerLocator locator, Object target, String subsystem, Map config) throws Exception
   {
      connector = getConnector(locator, config, null);
      ServerInvocationHandler handler = new TransporterHandler(target);
      if (subsystem != null)
      {
         connector.addInvocationHandler(subsystem.toUpperCase(), handler);
      }
      else
      {
         addInterfaceSubsystems(connector, handler, target);
      }
   }

   private void addInterfaceSubsystems(Connector connector, ServerInvocationHandler handler, Object target) throws Exception
   {
      Class targetClass = target.getClass();

      //first have to build list of interface names
      List interfaceNames = new ArrayList();
      populateInterfaceNames(interfaceNames, targetClass);

      for (int i = 0; i < interfaceNames.size(); i++)
      {
         String interfaceClassName = (String) interfaceNames.get(i);
         connector.addInvocationHandler(interfaceClassName.toUpperCase(), handler);
      }
   }

   private void populateInterfaceNames(List interfaceNames, Class targetClass)
   {
      Class[] interfaces = targetClass.getInterfaces();
      if (interfaces != null)
      {
         for (int x = 0; x < interfaces.length; x++)
         {
            interfaceNames.add(interfaces[x].getName());
            populateInterfaceNames(interfaceNames, interfaces[x]);
         }
      }
   }

   /**
    * Returns the connector that this transporter server will use.  Subclasses are free to override this method in order
    * to create a more customized connector.
    *
    * @param locator
    * @param config    configuration data for connector
    * @param xmlConfig configuration data for connector (in xml form)
    * @return the connector to be used by this transporter server
    * @throws Exception
    */
   protected Connector getConnector(InvokerLocator locator, Map config, Element xmlConfig)
         throws Exception
   {
      Connector c = new Connector(locator, config);
      if (xmlConfig != null)
      {
         c.setConfiguration(xmlConfig);
      }
      c.create();

      return c;
   }

   /**
    * Adds a transporter handler to receive remote invocations on the target object passed.
    *
    * @param target         the target implementation to call on
    * @param proxyclassname the fully qualified classname of the interface that clients will use to call on
    */
   public void addHandler(Object target, String proxyclassname) throws Exception
   {
      if (connector != null)
      {
         connector.addInvocationHandler(proxyclassname, new TransporterHandler(target));
      }
      else
      {
         throw new Exception("Can not add handler to transporter server as has not be initialized yet.");
      }
   }

   /**
    * Starts the remoting server.  This is called automatically upon any of the static createTransporterServer() methods.
    *
    * @throws Exception
    */
   public void start() throws Exception
   {
      connector.start();
   }

   /**
    * Stops the remoting server.  This must be called when no longer want to expose the target POJO for remote
    * method calls.
    */
   public void stop()
   {
      connector.stop();
   }

   /**
    * Creates a MBeanServer and MulticastDetector to start publishing detection messages so
    * other detectors will be aware this server is available.
    *
    * @throws Exception
    */
   private static void setupDetector() throws Exception
   {
      InternalTransporterServices services = InternalTransporterServices.getInstance();

      // if no one has setup our internal services yet, let's do it now
      if (!services.isSetup())
      {
         // we need an MBeanServer to store our network registry and multicast detector services
         MBeanServer server = createMBeanServer();

         // multicast detector will detect new network registries that come online
         MulticastDetector detector = new MulticastDetector();
         services.setup(server, detector, null, null, null, true, false);
         detector.start();
      }
      else if (services.getDetector() == null)
      {
         // the internal services singleton is already setup, make sure it has a detector because we need it
         MulticastDetector detector = new MulticastDetector();
         services.assignDetector(detector, null, true);
         detector.start();
      }

      return;
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator     - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem   - the name under which to register the handler within the remoting server.  <b>This must be
    *                    the fully qualified name of the interface for clients to use a the remote proxy to the target POJO.  Otherwise,
    *                    clustering will not work, as this is the value used to identifiy remote POJOs on the client side.</b>  If not clustered,
    *                    this is not as critical, and simply use the fully qualified class name of the POJO if desired.
    * @param isClustered - true indicates that would like this server to be considered available for
    *                    failover from clients calling on the same interface as exposed by the subsystem value.  False will only allow
    *                    those client that explicitly targeting this server to make calls on it.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(InvokerLocator locator, Object target,
                                                           String subsystem, boolean isClustered) throws Exception
   {
      return createTransporterServer(locator, target, subsystem, null, isClustered);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator     - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem   - the name under which to register the handler within the remoting server.  <b>This must be
    *                    the fully qualified name of the interface for clients to use a the remote proxy to the target POJO.  Otherwise,
    *                    clustering will not work, as this is the value used to identifiy remote POJOs on the client side.</b>  If not clustered,
    *                    this is not as critical, and simply use the fully qualified class name of the POJO if desired.
    * @param config      - the configuration to be used in setting up the Connector.
    * @param isClustered - true indicates that would like this server to be considered available for
    *                    failover from clients calling on the same interface as exposed by the subsystem value.  False will only allow
    *                    those client that explicitly targeting this server to make calls on it.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(InvokerLocator locator, Object target,
                                                           String subsystem, Map config, boolean isClustered) throws Exception
   {
      if (isClustered && (InternalTransporterServices.getInstance().getDetector() == null))
      {
         setupDetector();
      }

      TransporterServer server = new TransporterServer(locator, target, subsystem, config);
      server.start();
      return server;
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locatorURI  - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem   - the name under which to register the handler within the remoting server.  <b>This must be
    *                    the fully qualified name of the interface for clients to use a the remote proxy to the target POJO.  Otherwise,
    *                    clustering will not work, as this is the value used to identifiy remote POJOs on the client side.</b>  If not clustered,
    *                    this is not as critical, and simply use the fully qualified class name of the POJO if desired.
    * @param isClustered - true indicates that would like this server to be considered available for
    *                    failover from clients calling on the same interface as exposed by the subsystem value.  False will only allow
    *                    those client that explicitly targeting this server to make calls on it.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(String locatorURI, Object target,
                                                           String subsystem, boolean isClustered) throws Exception
   {
      return createTransporterServer(new InvokerLocator(locatorURI), target, subsystem, null, isClustered);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locatorURI  - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem   - the name under which to register the handler within the remoting server.  <b>This must be
    *                    the fully qualified name of the interface for clients to use a the remote proxy to the target POJO.  Otherwise,
    *                    clustering will not work, as this is the value used to identifiy remote POJOs on the client side.</b>  If not clustered,
    *                    this is not as critical, and simply use the fully qualified class name of the POJO if desired.
    * @param config      - the configuration data for the Connector.
    * @param isClustered - true indicates that would like this server to be considered available for
    *                    failover from clients calling on the same interface as exposed by the subsystem value.  False will only allow
    *                    those client that explicitly targeting this server to make calls on it.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(String locatorURI, Object target,
                                                           String subsystem, Map config, boolean isClustered) throws Exception
   {
      return createTransporterServer(new InvokerLocator(locatorURI), target, subsystem, config, isClustered);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.  Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param xmlconfig   - specifies config for Connector
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem   - the name under which to register the handler within the remoting server.  <b>This must be
    *                    the fully qualified name of the interface for clients to use a the remote proxy to the target POJO.  Otherwise,
    *                    clustering will not work, as this is the value used to identifiy remote POJOs on the client side.</b>  If not clustered,
    *                    this is not as critical, and simply use the fully qualified class name of the POJO if desired.
    * @param isClustered - true indicates that would like this server to be considered available for
    *                    failover from clients calling on the same interface as exposed by the subsystem value.  False will only allow
    *                    those client that explicitly targeting this server to make calls on it.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(Element xmlconfig, Object target,
                                                           String subsystem, boolean isClustered) throws Exception
   {
      if (isClustered && (InternalTransporterServices.getInstance().getDetector() == null))
      {
         setupDetector();
      }

      TransporterServer server = new TransporterServer(xmlconfig, target, subsystem);
      server.start();
      return server;
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator   - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target    - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem - the name under which to register the handler within the remoting server.  Can
    *                  simply use the fully qualified class name of the POJO if desired.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(InvokerLocator locator, Object target, String subsystem) throws Exception
   {
      return createTransporterServer(locator, target, subsystem, false);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target  - the target POJO to receive the method call upon getting remote invocation requests.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(InvokerLocator locator, Object target) throws Exception
   {
      return createTransporterServer(locator, target, false);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator     - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param isClustered - indicates if want automatic failover on calls to remote servers.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(InvokerLocator locator, Object target, boolean isClustered) throws Exception
   {
      if (isClustered && (InternalTransporterServices.getInstance().getDetector() == null))
      {
         setupDetector();
      }

      TransporterServer server = new TransporterServer(locator, target, null, null);
      server.start();
      return server;

   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target  - the target POJO to receive the method call upon getting remote invocation requests.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(String locator, Object target) throws Exception
   {
      return createTransporterServer(new InvokerLocator(locator), target, false);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locator     - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target      - the target POJO to receive the method call upon getting remote invocation requests.
    * @param isClustered - indicates if want automatic failover on calls to remote servers.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(String locator, Object target, boolean isClustered) throws Exception
   {
      return createTransporterServer(new InvokerLocator(locator), target, isClustered);
   }

   /**
    * Creates a remoting server based on given locator.  Will convert any remote invocation requests into
    * method calls on the given target object.Note: the TransporterServer instance returned will be a live (started)
    * instance.
    *
    * Once the TransporterServer has been returned, it will have already been started automatically, so is a live server
    * ready to receive requests.
    *
    * @param locatorURI - specifies what transport, host and port binding, etc. to use by the remoting server.
    * @param target     - the target POJO to receive the method call upon getting remote invocation requests.
    * @param subsystem  - the name under which to register the handler within the remoting server.  Can
    *                   simply use the fully qualified class name of the POJO if desired.
    * @return TransporterServer.  Note, it will already be started upon return.
    * @throws Exception
    */
   public static TransporterServer createTransporterServer(String locatorURI, Object target, String subsystem) throws Exception
   {
      return createTransporterServer(new InvokerLocator(locatorURI), target, subsystem, false);
   }

   static private MBeanServer createMBeanServer() throws Exception
   {
      if (SecurityUtility.skipAccessControl())
      {
         return MBeanServerFactory.createMBeanServer();
      }
      
      try
      {
         return (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return MBeanServerFactory.createMBeanServer();
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (Exception) e.getCause();
      }   
   }
}