KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > naming > remote > JNDIServerHelper


1 package org.sapia.ubik.rmi.naming.remote;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.NamingException JavaDoc;
7
8 import org.sapia.ubik.mcast.EventChannel;
9 import org.sapia.ubik.net.ServerAddress;
10 import org.sapia.ubik.rmi.naming.remote.archie.UbikRemoteContext;
11
12
13 /**
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class JNDIServerHelper implements Consts {
22
23   /**
24    * Parses the given command-line arguments and returns their object representation, or
25    * null if the args could not be parsed. If the latter happens, this method displays
26    * help to stdout before returing.
27    *
28    * @return an <code>Args</code> instance holding command-line arguments.
29    */

30   public static Args parseArgs(String JavaDoc[] args) {
31     int port = DEFAULT_PORT;
32     String JavaDoc domain = DEFAULT_DOMAIN;
33     String JavaDoc mcastAddress = org.sapia.ubik.rmi.Consts.DEFAULT_MCAST_ADDR;
34     int mcastPort = org.sapia.ubik.rmi.Consts.DEFAULT_MCAST_PORT;
35
36     if (args.length > 0) {
37       if (args[0].equals("-h")) {
38         help();
39
40         return null;
41       }
42
43       try {
44         port = Integer.parseInt(args[0]);
45
46         if (args.length == 2) {
47           domain = args[1];
48         }
49       } catch (NumberFormatException JavaDoc e) {
50         domain = args[0];
51
52         if (args.length == 2) {
53           try {
54             port = Integer.parseInt(args[1]);
55           } catch (NumberFormatException JavaDoc e2) {
56             help();
57
58             return null;
59           }
60         }
61       }
62     }
63
64     try {
65       if (System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_PORT_KEY) != null) {
66         mcastPort = Integer.parseInt(System.getProperty(
67               org.sapia.ubik.rmi.Consts.MCAST_PORT_KEY));
68       }
69     } catch (NumberFormatException JavaDoc e) {
70       System.out.println("Invalid multicast port: " +
71         System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_PORT_KEY));
72       help();
73
74       return null;
75     }
76
77     if (System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_ADDR_KEY) != null) {
78       mcastAddress = System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_ADDR_KEY);
79     }
80
81     return new Args(port, domain, mcastAddress, mcastPort);
82   }
83
84   public static Context JavaDoc newRootContext(EventChannel ec)
85     throws NamingException JavaDoc {
86     return UbikRemoteContext.newInstance(ec);
87   }
88
89   public static ClientListener createClientListener(EventChannel ec,
90     ServerAddress addr) throws NamingException JavaDoc, IOException JavaDoc {
91     ClientListener listener = new ClientListener(ec, addr);
92     ec.registerAsyncListener(Consts.JNDI_CLIENT_PUBLISH, listener);
93
94     ec.dispatch(Consts.JNDI_SERVER_PUBLISH, addr);
95
96     return listener;
97   }
98
99   static final void help() {
100     System.out.println();
101     System.out.println("Syntax: jndi [<port>] [<domain>]");
102     System.out.println("where:");
103     System.out.println(
104       "<port> := port on which JNDI server should listen (defaults to 1099).");
105     System.out.println(
106       "<domain>:= domain name that JNDI server is part of (defaults to 'default').");
107     System.out.println();
108     System.out.println();
109     System.exit(1);
110   }
111
112   public static class Args {
113     int port;
114     int mcastPort;
115     String JavaDoc mcastAddress;
116     String JavaDoc domain;
117
118     Args(int port, String JavaDoc domain, String JavaDoc mcastAddress, int mcastPort) {
119       this.port = port;
120       this.domain = domain;
121       this.mcastAddress = mcastAddress;
122       this.mcastPort = mcastPort;
123     }
124   }
125 }
126
Popular Tags