KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > cosnaming > TransientNameServer


1 /*
2  * @(#)TransientNameServer.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.naming.cosnaming;
9
10 import java.util.Properties JavaDoc;
11 import java.net.InetAddress JavaDoc;
12
13 import org.omg.CORBA.ORB JavaDoc;
14
15 import org.omg.CosNaming.NamingContext JavaDoc;
16
17 import com.sun.corba.se.spi.logging.CORBALogDomains;
18
19 import com.sun.corba.se.impl.naming.cosnaming.TransientNameService;
20
21 import com.sun.corba.se.impl.orbutil.ORBConstants;
22 import com.sun.corba.se.impl.orbutil.CorbaResourceUtil;
23 import com.sun.corba.se.impl.logging.NamingSystemException;
24
25 /**
26  * Class TransientNameServer is a standalone application which
27  * implements a transient name service. It uses the TransientNameService
28  * class for the name service implementation, and the BootstrapServer
29  * for implementing bootstrapping, i.e., to get the initial NamingContext.
30  * <p>
31  * The BootstrapServer uses a Properties object specify the initial service
32  * object references supported; such as Properties object is created containing
33  * only a "NameService" entry together with the stringified object reference
34  * for the initial NamingContext. The BootstrapServer's listening port
35  * is set by first checking the supplied arguments to the name server
36  * (-ORBInitialPort), and if not set, defaults to the standard port number.
37  * The BootstrapServer is created supplying the Properties object, using no
38  * external File object for storage, and the derived initial port number.
39  * @see TransientNameService
40  * @see BootstrapServer
41  */

42 public class TransientNameServer
43 {
44     static private boolean debug = false ;
45     static NamingSystemException wrapper = NamingSystemException.get(
46     CORBALogDomains.NAMING ) ;
47
48     static public void trace( String JavaDoc msg ) {
49     if (debug)
50         System.out.println( msg ) ;
51     }
52
53     static public void initDebug( String JavaDoc[] args ) {
54     // If debug was compiled to be true for testing purposes,
55
// don't change it.
56
if (debug)
57         return ;
58         
59     for (int ctr=0; ctr<args.length; ctr++)
60         if (args[ctr].equalsIgnoreCase( "-debug" )) {
61         debug = true ;
62         return ;
63         }
64     debug = false ;
65     }
66
67     private static org.omg.CORBA.Object JavaDoc initializeRootNamingContext( ORB JavaDoc orb ) {
68         org.omg.CORBA.Object JavaDoc rootContext = null;
69         try {
70         com.sun.corba.se.spi.orb.ORB coreORB =
71         (com.sun.corba.se.spi.orb.ORB)orb ;
72         
73             TransientNameService tns = new TransientNameService(coreORB );
74             return tns.initialNamingContext();
75         } catch (org.omg.CORBA.SystemException JavaDoc e) {
76         throw wrapper.transNsCannotCreateInitialNcSys( e ) ;
77         } catch (Exception JavaDoc e) {
78         throw wrapper.transNsCannotCreateInitialNc( e ) ;
79         }
80     }
81
82     /**
83      * Main startup routine. It instantiates a TransientNameService
84      * object and a BootstrapServer object, and then allows invocations to
85      * happen.
86      * @param args an array of strings representing the startup arguments.
87      */

88     public static void main(String JavaDoc args[]) {
89     initDebug( args ) ;
90
91         boolean invalidHostOption = false;
92         boolean orbInitialPort0 = false;
93
94     // Determine the initial bootstrap port to use
95
int initialPort = 0;
96     try {
97         trace( "Transient name server started with args " + args ) ;
98
99         // Create an ORB object
100
Properties JavaDoc props = System.getProperties() ;
101
102         props.put( ORBConstants.SERVER_ID_PROPERTY, ORBConstants.NAME_SERVICE_SERVER_ID ) ;
103             props.put( "org.omg.CORBA.ORBClass",
104                 "com.sun.corba.se.impl.orb.ORBImpl" );
105
106         try {
107         // Try environment
108
String JavaDoc ips = System.getProperty( ORBConstants.INITIAL_PORT_PROPERTY ) ;
109         if (ips != null && ips.length() > 0 ) {
110             initialPort = java.lang.Integer.parseInt(ips);
111                     // -Dorg.omg.CORBA.ORBInitialPort=0 is invalid
112
if( initialPort == 0 ) {
113                         orbInitialPort0 = true;
114             throw wrapper.transientNameServerBadPort() ;
115                     }
116                 }
117         String JavaDoc hostName =
118                     System.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ;
119                 if( hostName != null ) {
120                     invalidHostOption = true;
121             throw wrapper.transientNameServerBadHost() ;
122                 }
123         } catch (java.lang.NumberFormatException JavaDoc e) {
124         // do nothing
125
}
126
127         // Let arguments override
128
for (int i=0;i<args.length;i++) {
129         // Was the initial port specified?
130
if (args[i].equals("-ORBInitialPort") &&
131             i < args.length-1) {
132             initialPort = java.lang.Integer.parseInt(args[i+1]);
133                     // -ORBInitialPort 0 is invalid
134
if( initialPort == 0 ) {
135                         orbInitialPort0 = true;
136             throw wrapper.transientNameServerBadPort() ;
137                     }
138         }
139                 if (args[i].equals("-ORBInitialHost" ) ) {
140                     invalidHostOption = true;
141             throw wrapper.transientNameServerBadHost() ;
142                 }
143         }
144
145             // If initialPort is not set, then we need to set the Default
146
// Initial Port Property for the ORB
147
if( initialPort == 0 ) {
148                 initialPort = ORBConstants.DEFAULT_INITIAL_PORT;
149                 props.put( ORBConstants.INITIAL_PORT_PROPERTY,
150                     java.lang.Integer.toString(initialPort) );
151             }
152
153             // Set -ORBInitialPort = Persistent Server Port so that ORBImpl
154
// will start Boot Strap.
155
props.put( ORBConstants.PERSISTENT_SERVER_PORT_PROPERTY,
156                java.lang.Integer.toString(initialPort) );
157
158         org.omg.CORBA.ORB JavaDoc corb = ORB.init( args, props ) ;
159         trace( "ORB object returned from init: " + corb ) ;
160   
161             org.omg.CORBA.Object JavaDoc ns = initializeRootNamingContext( corb ) ;
162         ((com.sun.corba.se.org.omg.CORBA.ORB)corb).register_initial_reference(
163         "NamingService", ns ) ;
164
165         String JavaDoc stringifiedIOR = null;
166  
167             if( ns != null ) {
168             stringifiedIOR = corb.object_to_string(ns) ;
169             } else {
170              NamingUtils.errprint(CorbaResourceUtil.getText(
171                      "tnameserv.exception", initialPort));
172                  NamingUtils.errprint(CorbaResourceUtil.getText(
173                      "tnameserv.usage"));
174                 System.exit( 1 );
175             }
176
177         trace( "name service created" ) ;
178
179         // This is used for handshaking by the IBM test framework!
180
// Do not modify, unless another synchronization protocol is
181
// used to replace this hack!
182

183         System.out.println(CorbaResourceUtil.getText(
184                 "tnameserv.hs1", stringifiedIOR));
185             System.out.println(CorbaResourceUtil.getText(
186                 "tnameserv.hs2", initialPort));
187             System.out.println(CorbaResourceUtil.getText("tnameserv.hs3"));
188
189         // Serve objects.
190
java.lang.Object JavaDoc sync = new java.lang.Object JavaDoc();
191         synchronized (sync) {sync.wait();}
192     } catch (Exception JavaDoc e) {
193         if( invalidHostOption ) {
194                 // Let the User Know that -ORBInitialHost is not valid for
195
// tnameserver
196
NamingUtils.errprint( CorbaResourceUtil.getText(
197                     "tnameserv.invalidhostoption" ) );
198             } else if( orbInitialPort0 ) {
199                 // Let the User Know that -ORBInitialPort 0 is not valid for
200
// tnameserver
201
NamingUtils.errprint( CorbaResourceUtil.getText(
202                     "tnameserv.orbinitialport0" ));
203             } else {
204             NamingUtils.errprint(CorbaResourceUtil.getText(
205                     "tnameserv.exception", initialPort));
206                 NamingUtils.errprint(CorbaResourceUtil.getText(
207                     "tnameserv.usage"));
208             }
209
210         e.printStackTrace() ;
211     }
212     }
213
214     /**
215      * Private constructor since no object of this type should be instantiated.
216      */

217     private TransientNameServer() {}
218 }
219
Popular Tags