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

« back to all changes in this revision

Viewing changes to examples/org/jboss/remoting/samples/bisocket/BisocketSampleClient.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.samples.bisocket;
23
 
 
24
 
import java.lang.reflect.Field;
25
 
import java.net.InetAddress;
26
 
import java.util.HashMap;
27
 
import java.util.List;
28
 
import java.util.Set;
29
 
 
30
 
import org.apache.log4j.ConsoleAppender;
31
 
import org.apache.log4j.Level;
32
 
import org.apache.log4j.Logger;
33
 
import org.apache.log4j.PatternLayout;
34
 
import org.jboss.logging.XLevel;
35
 
import org.jboss.remoting.Client;
36
 
import org.jboss.remoting.InvokerLocator;
37
 
import org.jboss.remoting.callback.Callback;
38
 
import org.jboss.remoting.callback.HandleCallbackException;
39
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
40
 
import org.jboss.remoting.transport.Connector;
41
 
import org.jboss.remoting.transport.bisocket.Bisocket;
42
 
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
43
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
44
 
 
45
 
 
46
 
/**
47
 
 * This class and org.jboss.remoting.samples.bisocket.BisocketSampleServer
48
 
 * demonstrate how to how to make an invocation and how to set up push callbacks
49
 
 * over the bisocket transport.
50
 
 * 
51
 
 * The reason for the existance of the bisocket transport, which is derived from the
52
 
 * socket transport, is to make it possible to do push callbacks to a client which 
53
 
 * is unable to create a ServerSocket, either due to security restrictions or due
54
 
 * to the fact that it is behind a firewall. 
55
 
 * 
56
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
57
 
 * @version $Revision: 1.1 $
58
 
 * <p>
59
 
 * Copyright May 1, 2008
60
 
 * </p>
61
 
 */
62
 
public class BisocketSampleClient
63
 
{
64
 
   private static Logger log = Logger.getLogger(BisocketSampleClient.class);
65
 
 
66
 
   
67
 
   public void makeInvocation(String locatorURI) throws Throwable
68
 
   {
69
 
      // Create client.
70
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
71
 
      Client client = new Client(clientLocator);
72
 
      client.connect();
73
 
      log.info("client is connected");
74
 
      
75
 
      // Test connection.
76
 
      if ("abc".equals(client.invoke("abc")))
77
 
         log.info("connection is good");
78
 
      else
79
 
         log.info("Should have gotten \"abc\" in reply");
80
 
      
81
 
      // Set up push callbacks.  Tell the Connector created by Client.addListener()
82
 
      // that it is a callback Connector, which tells the BisocketServerInvoker
83
 
      // not to create a ServerSocket.
84
 
      HashMap metadata = new HashMap();
85
 
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
86
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
87
 
      client.addListener(callbackHandler, metadata);
88
 
      
89
 
      // Use reflection to verify that the callback BisocketServerInvoker did not
90
 
      // create a ServerSocket.
91
 
      Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
92
 
      if (callbackConnectors.size() != 1)
93
 
      {
94
 
         log.info("There should be one callback Connector");
95
 
      }
96
 
      else
97
 
      {
98
 
         Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
99
 
         BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
100
 
         Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
101
 
         field.setAccessible(true);
102
 
         List serverSockets = (List) field.get(serverInvoker);
103
 
         log.info("number of ServerSockets held by callback BisocketServerInvoker: " + serverSockets.size());
104
 
      }
105
 
 
106
 
      // Request callback.
107
 
      client.invoke("CALLBACK");
108
 
      if (callbackHandler.getCounter() == 1)
109
 
         log.info("received callback");
110
 
      else
111
 
         log.info("didn't receive callback");
112
 
      
113
 
      client.removeListener(callbackHandler);
114
 
      client.disconnect();
115
 
   }
116
 
   
117
 
   
118
 
   public static void main(String[] args)
119
 
   {
120
 
      // Configure logging.
121
 
      Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
122
 
      Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
123
 
      String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
124
 
      PatternLayout layout = new PatternLayout(pattern);
125
 
      ConsoleAppender consoleAppender = new ConsoleAppender(layout);
126
 
      Logger.getRootLogger().addAppender(consoleAppender);  
127
 
      
128
 
      try
129
 
      {
130
 
         String host = InetAddress.getLocalHost().getHostName();
131
 
         int port = BisocketSampleServer.port;
132
 
         String locatorURI = "bisocket://" + host + ":" + port;
133
 
         BisocketSampleClient client = new BisocketSampleClient();
134
 
         client.makeInvocation(locatorURI);
135
 
      }
136
 
      catch(Throwable e)
137
 
      {
138
 
         e.printStackTrace();
139
 
      }
140
 
   }
141
 
   
142
 
   /**
143
 
    * An InvokerCallbackHandler is registered with the callback Connector, which
144
 
    * passes push callbacks to it by way of the handleCallback() method.
145
 
    * </p>
146
 
    */
147
 
   static class TestCallbackHandler implements InvokerCallbackHandler
148
 
   {
149
 
      private int counter;
150
 
      
151
 
      public void handleCallback(Callback callback) throws HandleCallbackException
152
 
      {
153
 
         counter++;
154
 
      }
155
 
      
156
 
      public int getCounter()
157
 
      {
158
 
         return counter;
159
 
      }
160
 
   }
161
 
}
 
 
b'\\ No newline at end of file'