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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/detection/jndi/SimpleDetectorServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.remoting.samples.detection.jndi;
 
2
 
 
3
import org.jboss.remoting.InvocationRequest;
 
4
import org.jboss.remoting.InvokerLocator;
 
5
import org.jboss.remoting.ServerInvocationHandler;
 
6
import org.jboss.remoting.ServerInvoker;
 
7
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
8
import org.jboss.remoting.detection.jndi.JNDIDetector;
 
9
import org.jboss.remoting.transport.Connector;
 
10
 
 
11
import javax.management.MBeanServer;
 
12
import javax.management.MBeanServerFactory;
 
13
import javax.management.ObjectName;
 
14
import java.net.InetAddress;
 
15
import java.util.HashMap;
 
16
import java.util.Map;
 
17
 
 
18
/**
 
19
 * A simple JBoss/Remoting remoting server.  This uses an inner class SampleInvocationHandler as the invocation
 
20
 * target handler class.
 
21
 *
 
22
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
23
 * @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
 
24
 */
 
25
public class SimpleDetectorServer
 
26
{
 
27
   // Default locator values - command line args can override transport and port
 
28
   protected static String transport = "socket";
 
29
   protected static String host = "localhost";
 
30
   protected static int port = 5400;
 
31
 
 
32
   private int detectorPort = 1099;
 
33
   private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
 
34
   private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
 
35
 
 
36
 
 
37
   /**
 
38
    * Sets up NetworkRegistry and JNDItDetector so will can register ourselves on the network.  We could also
 
39
    * use these to listen ourselves for any additions or removals of remoting servers on the network.
 
40
    *
 
41
    * @throws Exception
 
42
    */
 
43
   public void setupDetector()
 
44
         throws Exception
 
45
   {
 
46
      // we need an MBeanServer to store our network registry and multicast detector services
 
47
      MBeanServer server = MBeanServerFactory.createMBeanServer();
 
48
 
 
49
      String detectorHost = InetAddress.getLocalHost().getHostName();
 
50
 
 
51
      // multicast detector will detect new network registries that come online
 
52
      JNDIDetector detector = new JNDIDetector(getConfiguration());
 
53
      // set config info for detector and start it.
 
54
      detector.setPort(detectorPort);
 
55
      detector.setHost(detectorHost);
 
56
      detector.setContextFactory(contextFactory);
 
57
      detector.setURLPackage(urlPackage);
 
58
 
 
59
      server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
60
      detector.start();
 
61
      println("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
 
62
 
 
63
   }
 
64
 
 
65
   /**
 
66
    * Sets up our JBoss/Remoting server by creating our Connector on the given locator URI and installing our
 
67
    * invocation handler that will handle incoming messages.
 
68
    *
 
69
    * @param locatorURI defines our server endpoing
 
70
    * @throws Exception
 
71
    */
 
72
   public void setupServer(String locatorURI)
 
73
         throws Exception
 
74
   {
 
75
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
76
      println("Starting remoting server with locator uri of: " + locatorURI);
 
77
      Connector connector = new Connector(getConfiguration());
 
78
      connector.setInvokerLocator(locator.getLocatorURI());
 
79
      connector.create();
 
80
 
 
81
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
82
 
 
83
      // first parameter is sub-system name.  can be any String value.
 
84
      connector.addInvocationHandler("sample", invocationHandler);
 
85
 
 
86
      println("Added our invocation handler; we are now ready to begin accepting messages from clients");
 
87
 
 
88
      connector.start();
 
89
 
 
90
   }
 
91
 
 
92
   /**
 
93
    * Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.
 
94
    *
 
95
    * @param args transport and port number
 
96
    */
 
97
   public static void main(String[] args)
 
98
   {
 
99
      println("Starting JBoss/Remoting server... to stop this server, kill it manually via Control-C");
 
100
      String locatorURI = getLocatorURI(args);
 
101
      println("This server's endpoint will be: " + locatorURI);
 
102
 
 
103
      SimpleDetectorServer server = new SimpleDetectorServer();
 
104
      try
 
105
      {
 
106
         server.setupDetector();
 
107
         server.setupServer(locatorURI);
 
108
 
 
109
         // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
 
110
         while(true)
 
111
         {
 
112
            Thread.sleep(1000);
 
113
         }
 
114
      }
 
115
      catch(Exception e)
 
116
      {
 
117
         e.printStackTrace();
 
118
      }
 
119
 
 
120
      println("Stopping JBoss/Remoting server");
 
121
   }
 
122
 
 
123
   /**
 
124
    * Outputs a message to stdout.
 
125
    *
 
126
    * @param msg the message to output
 
127
    */
 
128
   public static void println(String msg)
 
129
   {
 
130
      System.out.println(new java.util.Date() + ": [SERVER]: " + msg);
 
131
   }
 
132
   
 
133
   protected static String getLocatorURI(String[] args)
 
134
   {
 
135
      // get system property -Dargs that is in format "transport:port"
 
136
      String prop = System.getProperty("args");
 
137
      if(prop != null)
 
138
      {
 
139
         try
 
140
         {
 
141
            transport = prop.substring(0, prop.indexOf("-"));
 
142
            port = Integer.parseInt(prop.substring(prop.indexOf("-") + 1));
 
143
         }
 
144
         catch(NumberFormatException nfe)
 
145
         {
 
146
            println("INVALID ARGUMENTS: Bad port from property args: " + prop);
 
147
            System.exit(1);
 
148
         }
 
149
         catch(Exception e)
 
150
         {
 
151
            println("INVALID ARGUMENTS: -Dargs property must be in the form '{socket|rmi}-{port#}': " + prop);
 
152
            System.exit(1);
 
153
         }
 
154
      }
 
155
 
 
156
      // command line args override defaults and system property
 
157
      if((args != null) && (args.length != 0))
 
158
      {
 
159
         if(args.length == 2)
 
160
         {
 
161
            transport = args[0];
 
162
            port = Integer.parseInt(args[1]);
 
163
         }
 
164
         else
 
165
         {
 
166
            println("INVALID ARGUMENTS: Usage: " + SimpleDetectorServer.class.getName()
 
167
                    + " [rmi|socket <port>]");
 
168
            System.exit(1);
 
169
         }
 
170
      }
 
171
 
 
172
      return transport + "://" + host + ":" + port;
 
173
   }
 
174
   
 
175
   /**
 
176
    * @return configuration map for Connector and JNDIDetector
 
177
    */
 
178
   protected Map getConfiguration()
 
179
   {
 
180
      return new HashMap();
 
181
   }
 
182
   
 
183
   protected SimpleDetectorServer getDetectorServer()
 
184
   {
 
185
      return new SimpleDetectorServer();
 
186
   }
 
187
 
 
188
   /**
 
189
    * Simple invocation handler implementation.  This is the handler that processes incoming messages from clients.
 
190
    */
 
191
   public static class SampleInvocationHandler
 
192
         implements ServerInvocationHandler
 
193
   {
 
194
      /**
 
195
       * This is the method that is called when a new message comes in from a client.
 
196
       *
 
197
       * @param invocation the incoming client invocation, encapsulates the message object
 
198
       * @return the response object we send back to the client.
 
199
       * @throws Throwable
 
200
       */
 
201
      public Object invoke(InvocationRequest invocation)
 
202
            throws Throwable
 
203
      {
 
204
         // Print out the invocation request
 
205
         String msg = invocation.getParameter().toString();
 
206
 
 
207
         println("RECEIVED A CLIENT MESSAGE: " + msg);
 
208
 
 
209
         String response = "Server received your message that said [" + msg + "]";
 
210
 
 
211
         if(msg.indexOf("Welcome") > -1)
 
212
         {
 
213
            response = "Received your welcome message.  Thank you!";
 
214
         }
 
215
 
 
216
         println("Returning the following message back to the client: " + response);
 
217
 
 
218
         return response;
 
219
      }
 
220
 
 
221
      /**
 
222
       * Adds a callback handler that will listen for callbacks from the server invoker handler.
 
223
       *
 
224
       * @param callbackHandler
 
225
       */
 
226
      public void addListener(InvokerCallbackHandler callbackHandler)
 
227
      {
 
228
         // NO OP as we do not handle callback listeners in this example
 
229
      }
 
230
 
 
231
      /**
 
232
       * Removes the callback handler that was listening for callbacks from the server invoker handler.
 
233
       *
 
234
       * @param callbackHandler
 
235
       */
 
236
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
237
      {
 
238
         // NO OP as we do not handle callback listeners in this example
 
239
      }
 
240
 
 
241
      /**
 
242
       * set the mbean server that the handler can reference
 
243
       *
 
244
       * @param server
 
245
       */
 
246
      public void setMBeanServer(MBeanServer server)
 
247
      {
 
248
         // NO OP as we do not need a reference to the MBeanServer for this handler
 
249
      }
 
250
 
 
251
      /**
 
252
       * set the invoker that owns this handler
 
253
       *
 
254
       * @param invoker
 
255
       */
 
256
      public void setInvoker(ServerInvoker invoker)
 
257
      {
 
258
         // NO OP as we do not need a reference back to the server invoker
 
259
      }
 
260
   }
 
261
}