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

« back to all changes in this revision

Viewing changes to examples/org/jboss/remoting/samples/detection/jndi/SimpleJNDIServer.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.util.SecurityUtility;
4
 
import org.jnp.server.Main;
5
 
 
6
 
import java.lang.reflect.Method;
7
 
import java.net.InetAddress;
8
 
import java.security.AccessController;
9
 
import java.security.PrivilegedActionException;
10
 
import java.security.PrivilegedExceptionAction;
11
 
 
12
 
/**
13
 
 * A JNDI server that should be run before running the simple detector client/server.
14
 
 * Leave running while shutting down different server instances.
15
 
 *
16
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
17
 
 */
18
 
public class SimpleJNDIServer
19
 
{
20
 
   // Default locator values - command line args can override transport and port
21
 
   private static String transport = "socket";
22
 
   private static String host = "localhost";
23
 
   private static int port = 5400;
24
 
   private int detectorPort = 1099;
25
 
 
26
 
 
27
 
   /**
28
 
    * Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.
29
 
    *
30
 
    * @param args transport and port number
31
 
    */
32
 
   public static void main(String[] args)
33
 
   {
34
 
      // get system property -Dargs that is in format "transport:port"
35
 
      String prop = System.getProperty("args");
36
 
      if(prop != null)
37
 
      {
38
 
         try
39
 
         {
40
 
            SimpleJNDIServer.transport = prop.substring(0, prop.indexOf("-"));
41
 
            SimpleJNDIServer.port = Integer.parseInt(prop.substring(prop.indexOf("-") + 1));
42
 
         }
43
 
         catch(NumberFormatException nfe)
44
 
         {
45
 
            SimpleJNDIServer.println("INVALID ARGUMENTS: Bad port from property args: " + prop);
46
 
            System.exit(1);
47
 
         }
48
 
         catch(Exception e)
49
 
         {
50
 
            SimpleJNDIServer.println("INVALID ARGUMENTS: -Dargs property must be in the form '{socket|rmi}-{port#}': " + prop);
51
 
            System.exit(1);
52
 
         }
53
 
      }
54
 
 
55
 
      // command line args override defaults and system property
56
 
      if((args != null) && (args.length != 0))
57
 
      {
58
 
         if(args.length == 2)
59
 
         {
60
 
            SimpleJNDIServer.transport = args[0];
61
 
            SimpleJNDIServer.port = Integer.parseInt(args[1]);
62
 
         }
63
 
         else
64
 
         {
65
 
            SimpleJNDIServer.println("INVALID ARGUMENTS: Usage: " + SimpleJNDIServer.class.getName()
66
 
                                         + " [rmi|socket <port>]");
67
 
            System.exit(1);
68
 
         }
69
 
      }
70
 
 
71
 
      SimpleJNDIServer.println("Starting JNDI server... to stop this server, kill it manually via Control-C");
72
 
 
73
 
      SimpleJNDIServer server = new SimpleJNDIServer();
74
 
      try
75
 
      {
76
 
         server.setupJNDIServer();
77
 
 
78
 
         // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
79
 
         while(true)
80
 
         {
81
 
            Thread.sleep(1000);
82
 
         }
83
 
      }
84
 
      catch(Exception e)
85
 
      {
86
 
         e.printStackTrace();
87
 
      }
88
 
 
89
 
      SimpleJNDIServer.println("Stopping JBoss/Remoting server");
90
 
   }
91
 
 
92
 
   private void setupJNDIServer() throws Exception
93
 
   {
94
 
      Object namingBean = null;
95
 
      Class namingBeanImplClass = null;
96
 
      try
97
 
      {
98
 
         namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
99
 
         namingBean = namingBeanImplClass.newInstance();
100
 
         Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
101
 
         setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
102
 
         startMethod.invoke(namingBean, new Object[] {});
103
 
      }
104
 
      catch (Exception e)
105
 
      {
106
 
         SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
107
 
      }
108
 
      
109
 
      // start JNDI server
110
 
      String detectorHost = InetAddress.getLocalHost().getHostName();
111
 
 
112
 
      Main JNDIServer = new Main();
113
 
      if (namingBean != null)
114
 
      {
115
 
         Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
116
 
         Method setNamingInfoMethod = JNDIServer.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
117
 
         setNamingInfoMethod.invoke(JNDIServer, new Object[] {namingBean});
118
 
      }
119
 
      JNDIServer.setPort(detectorPort);
120
 
      JNDIServer.setBindAddress(detectorHost);
121
 
      JNDIServer.start();
122
 
      System.out.println("Started JNDI server on " + detectorHost + ":" + detectorPort);
123
 
 
124
 
   }
125
 
 
126
 
   /**
127
 
    * Outputs a message to stdout.
128
 
    *
129
 
    * @param msg the message to output
130
 
    */
131
 
   public static void println(String msg)
132
 
   {
133
 
      System.out.println(new java.util.Date() + ": [SERVER]: " + msg);
134
 
   }
135
 
   
136
 
   static private void setSystemProperty(final String name, final String value)
137
 
   {
138
 
      if (SecurityUtility.skipAccessControl())
139
 
      {
140
 
         System.setProperty(name, value);
141
 
         return;
142
 
      }
143
 
      
144
 
      try
145
 
      {
146
 
         AccessController.doPrivileged( new PrivilegedExceptionAction()
147
 
         {
148
 
            public Object run() throws Exception
149
 
            {
150
 
               return System.setProperty(name, value);
151
 
            }
152
 
         });
153
 
      }
154
 
      catch (PrivilegedActionException e)
155
 
      {
156
 
         throw (RuntimeException) e.getCause();
157
 
      }
158
 
   }
159
 
}