KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > NameServer


1 package org.jacorb.naming;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.net.*;
24 import java.io.*;
25
26 import org.omg.PortableServer.*;
27 import org.omg.CosNaming.*;
28 import org.omg.CosNaming.NamingContextPackage.*;
29
30 import org.jacorb.orb.*;
31 import org.jacorb.util.*;
32
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.avalon.framework.configuration.*;
35
36 import org.jacorb.imr.util.ImRManager;
37
38 /**
39  * The name server application
40  *
41  * @author Gerald Brose, FU Berlin
42  * @version $Id: NameServer.java,v 1.32 2005/06/01 10:04:23 andre.spiegel Exp $
43  */

44
45
46 public class NameServer
47 {
48     private static org.omg.CORBA.ORB JavaDoc orb = null;
49     private static org.jacorb.config.Configuration configuration = null;
50
51     /** the specific logger for this component */
52     private static Logger logger = null;
53
54     /** the file name int which the IOR will be stored */
55     private static String JavaDoc fileName = null;
56
57     private static String JavaDoc filePrefix = "_nsdb";
58     private static String JavaDoc commandSuffix = "";
59
60     /** if this value is != 0, the name server will automatically shut
61         down after the given time */

62     private static int time_out = 0;
63
64     static String JavaDoc name_delimiter = "/";
65
66
67     public static void configure(Configuration myConfiguration)
68         throws ConfigurationException
69     {
70         configuration = (org.jacorb.config.Configuration)myConfiguration;
71         logger =
72             configuration.getNamedLogger("jacorb.naming");
73
74         time_out =
75             configuration.getAttributeAsInteger("jacorb.naming.time_out",0);
76
77         fileName =
78             configuration.getAttribute("jacorb.naming.ior_filename", "");
79
80         /* which directory to store/load in? */
81         String JavaDoc directory =
82             configuration.getAttribute("jacorb.naming.db_dir", "");
83         
84         if( !directory.equals("") )
85             filePrefix = directory + File.separatorChar + filePrefix;
86
87         if ( configuration.getAttribute("jacorb.use_imr","off").equals("on") )
88         {
89
90             // don't supply "imr_register", so a ns started by an imr_ssd
91
// won't try to register himself again.
92

93             String JavaDoc command =
94                 configuration.getAttribute("jacorb.java_exec", "") + commandSuffix;
95             
96             ImRManager.autoRegisterServer( orb,
97                                            "StandardNS",
98                                            command,
99                                            ImRManager.getLocalHostName(),
100                                            true); //edit existing
101
}
102     }
103
104
105     /**
106      * The servant manager (servant activator) for the name server POA
107      */

108
109     static class NameServantActivatorImpl
110         extends _ServantActivatorLocalBase
111     {
112         private org.omg.CORBA.ORB JavaDoc orb = null;
113         private org.jacorb.config.Configuration configuration = null;
114         private Logger logger = null;
115
116         public NameServantActivatorImpl(org.omg.CORBA.ORB JavaDoc orb)
117         {
118             this.orb = orb;
119         }
120
121         public void configure(Configuration myConfiguration)
122             throws ConfigurationException
123         {
124             this.configuration = (org.jacorb.config.Configuration)myConfiguration;
125             this.logger = configuration.getNamedLogger("jacorb.naming.activator");
126         }
127
128
129         /**
130          * @return - a servant initialized from a file
131          */

132
133         public Servant incarnate( byte[] oid, POA adapter )
134             throws ForwardRequest
135         {
136             String JavaDoc oidStr = new String JavaDoc(oid);
137
138             NamingContextImpl n = null;
139             try
140             {
141                 File f = new File( filePrefix + oidStr );
142                 if( f.exists() )
143                 {
144                     if( logger.isDebugEnabled())
145                         logger.debug("Reading in context state from file");
146
147                     FileInputStream f_in = new FileInputStream(f);
148
149                     if( f_in.available() > 0 )
150                     {
151                         ObjectInputStream in = new ObjectInputStream(f_in);
152                         n = (NamingContextImpl)in.readObject();
153                         in.close();
154                     }
155                     f_in.close();
156                 }
157                 else
158                 {
159                     if( logger.isDebugEnabled())
160                         logger.debug("No naming context state, starting empty");
161                 }
162
163             }
164             catch( IOException io )
165             {
166                 if( logger.isDebugEnabled())
167                     logger.debug("File seems corrupt, starting empty");
168             }
169             catch( java.lang.ClassNotFoundException JavaDoc c )
170             {
171                 if (logger.isErrorEnabled())
172                 {
173                     logger.error("Could not read object from file, class not found!");
174                 }
175                 throw new RuntimeException JavaDoc ("Could not read object from file, class not found!");
176             }
177
178             if( n == null )
179             {
180                 n = new NamingContextImpl();
181             }
182
183             n.init(adapter);
184             try
185             {
186                 n.configure(configuration);
187             }
188             catch( ConfigurationException ce )
189             {
190                 if (logger.isErrorEnabled())
191                     logger.error("ConfigurationException: " + ce.getMessage());
192             }
193             return n;
194         }
195
196         /**
197          * Saves the servant's state in a file
198          */

199
200         public void etherealize(byte[] oid, POA adapter,
201                                 Servant servant,
202                                 boolean cleanup_in_progress, boolean remaining_activations)
203         {
204             String JavaDoc oidStr = new String JavaDoc(oid);
205
206             try
207             {
208                 File f = new File(filePrefix + oidStr);
209                 FileOutputStream fout = new FileOutputStream(f);
210
211                 ObjectOutputStream out =
212                 new ObjectOutputStream(fout);
213
214                 /* save state */
215                 out.writeObject((NamingContextImpl)servant);
216                 if (logger.isDebugEnabled())
217                 {
218                     logger.debug("Saved state for servant " + oidStr);
219                 }
220             }
221             catch( IOException io )
222             {
223                 io.printStackTrace();
224                 logger.error("Error opening output file " + filePrefix + oidStr );
225                 // System.exit(1);
226
}
227         }
228     }
229
230
231     private static void usage()
232     {
233         System.err.println("Usage: java org.jacorb.naming.NameServer [-Djacorb.naming.ior_filename=fname] [-Djacorb.naming.time_out=x][-Djacorb.use_imr=on/off][-Djacorb.naming.purge=on/off ]");
234         System.exit(1);
235     }
236
237     /** Main */
238
239     public static void main( String JavaDoc args[] )
240     {
241         try
242         {
243             // TODO: is this correct? needs testing
244
commandSuffix = " org.jacorb.naming.NameServer";
245
246             // translate any properties set on the commandline but after the
247
// class name to a properties
248
java.util.Properties JavaDoc argProps = ObjectUtil.argsToProps( args );
249
250             java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
251             props.put("jacorb.implname", "StandardNS");
252
253             /*
254              * by setting the following property, the ORB will
255              * accept client requests targeted at the object with
256              * key "NameService", so more readablee corbaloc URLs
257              * can be used
258              */

259
260             props.put("jacorb.orb.objectKeyMap.NameService",
261                       "StandardNS/NameServer-POA/_root");
262
263             /* any command line properties set _after_ the class name will also
264                be considered */

265             props.putAll( argProps );
266
267             /* intialize the ORB and Root POA */
268             orb = org.omg.CORBA.ORB.init(args, props);
269
270             Configuration config =
271                 ((org.jacorb.orb.ORB)orb).getConfiguration();
272
273             /* configure the name service using the ORB configuration */
274             configure(config);
275
276             org.omg.PortableServer.POA JavaDoc rootPOA =
277             org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
278
279             /* create a user defined poa for the naming contexts */
280
281             org.omg.CORBA.Policy JavaDoc [] policies = new org.omg.CORBA.Policy JavaDoc[3];
282
283             policies[0] =
284             rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
285             policies[1] =
286             rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
287
288             policies[2] =
289             rootPOA.create_request_processing_policy(
290                 RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
291
292             POA nsPOA = rootPOA.create_POA("NameServer-POA",
293                                            rootPOA.the_POAManager(),
294                                            policies);
295
296             NamingContextImpl.init(orb, rootPOA);
297             NameServer.NameServantActivatorImpl servantActivator =
298                 new NameServer.NameServantActivatorImpl( orb );
299             servantActivator.configure(config);
300
301             nsPOA.set_servant_manager( servantActivator );
302             nsPOA.the_POAManager().activate();
303
304             for (int i = 0; i < policies.length; i++)
305                 policies[i].destroy();
306
307             /* export the root context's reference to a file */
308             byte[] oid = ( new String JavaDoc("_root").getBytes() );
309             try
310             {
311                 org.omg.CORBA.Object JavaDoc obj =
312                 nsPOA.create_reference_with_id( oid, "IDL:omg.org/CosNaming/NamingContextExt:1.0");
313
314                 if( fileName != null && fileName.length() > 0 )
315                 {
316                     PrintWriter out =
317                     new PrintWriter( new FileOutputStream( fileName ), true );
318
319                     out.println( orb.object_to_string(obj) );
320                     out.close();
321                 }
322             }
323             catch ( Exception JavaDoc e )
324             {
325                 e.printStackTrace();
326                 throw new RuntimeException JavaDoc(e.getMessage());
327             }
328
329             if (logger.isInfoEnabled())
330             {
331                 logger.info("NS up");
332             }
333
334             /* either block indefinitely or time out */
335
336             if( time_out == 0 )
337                 orb.run();
338             else
339                 Thread.sleep(time_out);
340
341
342             /* shutdown. This will etherealize all servants, thus
343                saving their state */

344             orb.shutdown( true );
345
346             // System.exit(0);
347
}
348         catch( ConfigurationException e )
349         {
350             e.printStackTrace();
351             usage();
352         }
353         catch( Exception JavaDoc e )
354         {
355             e.printStackTrace();
356             System.exit(1);
357         }
358     }
359
360
361
362 }
363
Popular Tags