1 package org.jacorb.naming; 2 3 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 44 45 46 public class NameServer 47 { 48 private static org.omg.CORBA.ORB orb = null; 49 private static org.jacorb.config.Configuration configuration = null; 50 51 52 private static Logger logger = null; 53 54 55 private static String fileName = null; 56 57 private static String filePrefix = "_nsdb"; 58 private static String commandSuffix = ""; 59 60 62 private static int time_out = 0; 63 64 static String 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 81 String 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 93 String command = 94 configuration.getAttribute("jacorb.java_exec", "") + commandSuffix; 95 96 ImRManager.autoRegisterServer( orb, 97 "StandardNS", 98 command, 99 ImRManager.getLocalHostName(), 100 true); } 102 } 103 104 105 108 109 static class NameServantActivatorImpl 110 extends _ServantActivatorLocalBase 111 { 112 private org.omg.CORBA.ORB orb = null; 113 private org.jacorb.config.Configuration configuration = null; 114 private Logger logger = null; 115 116 public NameServantActivatorImpl(org.omg.CORBA.ORB 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 132 133 public Servant incarnate( byte[] oid, POA adapter ) 134 throws ForwardRequest 135 { 136 String oidStr = new String (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 c ) 170 { 171 if (logger.isErrorEnabled()) 172 { 173 logger.error("Could not read object from file, class not found!"); 174 } 175 throw new RuntimeException ("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 199 200 public void etherealize(byte[] oid, POA adapter, 201 Servant servant, 202 boolean cleanup_in_progress, boolean remaining_activations) 203 { 204 String oidStr = new String (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 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 } 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 238 239 public static void main( String args[] ) 240 { 241 try 242 { 243 commandSuffix = " org.jacorb.naming.NameServer"; 245 246 java.util.Properties argProps = ObjectUtil.argsToProps( args ); 249 250 java.util.Properties props = new java.util.Properties (); 251 props.put("jacorb.implname", "StandardNS"); 252 253 259 260 props.put("jacorb.orb.objectKeyMap.NameService", 261 "StandardNS/NameServer-POA/_root"); 262 263 265 props.putAll( argProps ); 266 267 268 orb = org.omg.CORBA.ORB.init(args, props); 269 270 Configuration config = 271 ((org.jacorb.orb.ORB)orb).getConfiguration(); 272 273 274 configure(config); 275 276 org.omg.PortableServer.POA rootPOA = 277 org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 278 279 280 281 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [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 308 byte[] oid = ( new String ("_root").getBytes() ); 309 try 310 { 311 org.omg.CORBA.Object 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 e ) 324 { 325 e.printStackTrace(); 326 throw new RuntimeException (e.getMessage()); 327 } 328 329 if (logger.isInfoEnabled()) 330 { 331 logger.info("NS up"); 332 } 333 334 335 336 if( time_out == 0 ) 337 orb.run(); 338 else 339 Thread.sleep(time_out); 340 341 342 344 orb.shutdown( true ); 345 346 } 348 catch( ConfigurationException e ) 349 { 350 e.printStackTrace(); 351 usage(); 352 } 353 catch( Exception e ) 354 { 355 e.printStackTrace(); 356 System.exit(1); 357 } 358 } 359 360 361 362 } 363 | Popular Tags |