1 7 22 23 package com.sun.corba.se.impl.activation; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.Serializable ; 31 import java.util.Properties ; 32 import java.util.Hashtable ; 33 import java.util.Enumeration ; 34 import java.util.Vector ; 35 36 import org.omg.CORBA.CompletionStatus ; 37 import org.omg.CORBA.INITIALIZE ; 38 import org.omg.CORBA.INTERNAL ; 39 import org.omg.CORBA.SystemException ; 40 41 import com.sun.corba.se.spi.activation.BadServerDefinition; 42 import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef; 43 import com.sun.corba.se.spi.activation._RepositoryImplBase; 44 import com.sun.corba.se.spi.activation.ServerAlreadyRegistered; 45 import com.sun.corba.se.spi.activation.ServerAlreadyInstalled; 46 import com.sun.corba.se.spi.activation.ServerAlreadyUninstalled; 47 import com.sun.corba.se.spi.activation.ServerNotRegistered; 48 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo; 49 import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor; 50 import com.sun.corba.se.spi.orb.ORB; 51 import com.sun.corba.se.impl.orbutil.ORBConstants; 52 53 import com.sun.corba.se.spi.logging.CORBALogDomains; 54 import com.sun.corba.se.impl.logging.ActivationSystemException; 55 56 62 public class RepositoryImpl extends _RepositoryImplBase 63 implements Serializable 64 { 65 66 private static final long serialVersionUID = 8458417785209341858L; 68 69 RepositoryImpl(ORB orb, File dbDir, boolean debug) 70 { 71 this.debug = debug ; 72 this.orb = orb; 73 wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_REPOSITORY ) ; 74 75 File dbFile = new File (dbDir, "servers.db"); 77 if (!dbFile.exists()) { 78 db = new RepositoryDB(dbFile); 79 db.flush(); 80 } else { 81 try { 82 FileInputStream fis = new FileInputStream (dbFile); 83 ObjectInputStream ois = new ObjectInputStream (fis); 84 db = (RepositoryDB) ois.readObject(); 85 ois.close(); 86 } catch (Exception e) { 87 throw wrapper.cannotReadRepositoryDb( e ) ; 88 } 89 } 90 91 orb.connect(this); 93 } 94 95 private String printServerDef( ServerDef sd ) 96 { 97 return "ServerDef[applicationName=" + sd.applicationName + 98 " serverName=" + sd.serverName + 99 " serverClassPath=" + sd.serverClassPath + 100 " serverArgs=" + sd. serverArgs + 101 " serverVmArgs=" + sd.serverVmArgs + 102 "]" ; 103 } 104 105 public int registerServer(ServerDef serverDef, int theServerId) 106 throws ServerAlreadyRegistered 107 { 108 int serverId; 109 DBServerDef server = null; 110 111 synchronized (db) { 112 113 Enumeration enumeration = db.serverTable.elements(); 115 while (enumeration.hasMoreElements()) { 116 server = (DBServerDef) enumeration.nextElement(); 117 if (serverDef.applicationName.equals(server.applicationName)) { 118 if (debug) 119 System.out.println( 120 "RepositoryImpl: registerServer called " + 121 "to register ServerDef " + 122 printServerDef( serverDef ) + 123 " with " + ((theServerId==illegalServerId) ? 124 "a new server Id" : ("server Id " + theServerId)) + 125 " FAILED because it is already registered." ) ; 126 127 throw (new ServerAlreadyRegistered(server.id)); 128 } 129 } 130 131 if (theServerId == illegalServerId) 133 serverId = db.incrementServerIdCounter(); 134 else 135 serverId = theServerId; 136 137 server = new DBServerDef(serverDef, serverId); 139 db.serverTable.put(new Integer (serverId), server); 140 db.flush(); 141 142 if (debug) 143 if (theServerId==illegalServerId) 144 System.out.println( "RepositoryImpl: registerServer called " + 145 "to register ServerDef " + printServerDef( serverDef ) + 146 " with new serverId " + serverId ) ; 147 else 148 System.out.println( "RepositoryImpl: registerServer called " + 149 "to register ServerDef " + printServerDef( serverDef ) + 150 " with assigned serverId " + serverId ) ; 151 152 return serverId; 153 } 154 } 155 156 public int registerServer(ServerDef serverDef) 157 throws ServerAlreadyRegistered, BadServerDefinition 158 { 159 LegacyServerSocketEndPointInfo endpoint = 161 orb.getLegacyServerSocketManager() 162 .legacyGetEndpoint(LegacyServerSocketEndPointInfo.BOOT_NAMING); 163 int initSvcPort = ((SocketOrChannelAcceptor)endpoint) 164 .getServerSocket().getLocalPort(); 165 ServerTableEntry entry = new ServerTableEntry( wrapper, 166 illegalServerId, serverDef, (int) initSvcPort, "", true, debug ); 167 168 switch (entry.verify()) { 169 case ServerMain.OK: 170 break; 171 case ServerMain.MAIN_CLASS_NOT_FOUND: 172 throw new BadServerDefinition("main class not found."); 173 case ServerMain.NO_MAIN_METHOD: 174 throw new BadServerDefinition("no main method found."); 175 case ServerMain.APPLICATION_ERROR: 176 throw new BadServerDefinition("server application error."); 177 default: 178 throw new BadServerDefinition("unknown Exception."); 179 } 180 181 return registerServer(serverDef, illegalServerId); 182 } 183 184 public void unregisterServer(int serverId) throws ServerNotRegistered { 185 186 DBServerDef server = null; 187 Integer id = new Integer (serverId); 188 189 synchronized (db) { 190 191 server = (DBServerDef) db.serverTable.get(id); 193 if (server == null) { 194 if (debug) 195 System.out.println( 196 "RepositoryImpl: unregisterServer for serverId " + 197 serverId + " called: server not registered" ) ; 198 199 throw (new ServerNotRegistered()); 200 } 201 202 db.serverTable.remove(id); 204 db.flush(); 205 } 206 207 if (debug) 208 System.out.println( 209 "RepositoryImpl: unregisterServer for serverId " + serverId + 210 " called" ) ; 211 } 212 213 private DBServerDef getDBServerDef(int serverId) throws ServerNotRegistered 214 { 215 Integer id = new Integer (serverId); 216 DBServerDef server = (DBServerDef) db.serverTable.get(id); 217 218 if (server == null) 219 throw new ServerNotRegistered( serverId ); 220 221 return server ; 222 } 223 224 public ServerDef getServer(int serverId) throws ServerNotRegistered 225 { 226 DBServerDef server = getDBServerDef( serverId ) ; 227 228 ServerDef serverDef = new ServerDef(server.applicationName, server.name, 229 server.classPath, server.args, server.vmArgs); 230 231 if (debug) 232 System.out.println( 233 "RepositoryImpl: getServer for serverId " + serverId + 234 " returns " + printServerDef( serverDef ) ) ; 235 236 return serverDef; 237 } 238 239 public boolean isInstalled(int serverId) throws ServerNotRegistered { 240 DBServerDef server = getDBServerDef( serverId ) ; 241 return server.isInstalled ; 242 } 243 244 public void install( int serverId ) 245 throws ServerNotRegistered, ServerAlreadyInstalled 246 { 247 DBServerDef server = getDBServerDef( serverId ) ; 248 249 if (server.isInstalled) 250 throw new ServerAlreadyInstalled( serverId ) ; 251 else { 252 server.isInstalled = true ; 253 db.flush() ; 254 } 255 } 256 257 public void uninstall( int serverId ) 258 throws ServerNotRegistered, ServerAlreadyUninstalled 259 { 260 DBServerDef server = getDBServerDef( serverId ) ; 261 262 if (!server.isInstalled) 263 throw new ServerAlreadyUninstalled( serverId ) ; 264 else { 265 server.isInstalled = false ; 266 db.flush() ; 267 } 268 } 269 270 public int[] listRegisteredServers() { 271 synchronized (db) { 272 int i=0; 273 274 int servers[] = new int[db.serverTable.size()]; 275 276 Enumeration enumeration = db.serverTable.elements(); 277 278 while (enumeration.hasMoreElements()) { 279 DBServerDef server = (DBServerDef) enumeration.nextElement(); 280 servers[i++] = server.id; 281 } 282 283 if (debug) { 284 StringBuffer sb = new StringBuffer () ; 285 for (int ctr=0; ctr<servers.length; ctr++) { 286 sb.append( ' ' ) ; 287 sb.append( servers[ctr] ) ; 288 } 289 290 System.out.println( 291 "RepositoryImpl: listRegisteredServers returns" + 292 sb.toString() ) ; 293 } 294 295 return servers; 296 } 297 } 298 299 public int getServerID(String applicationName) throws ServerNotRegistered { 300 synchronized (db) { 301 int result = -1 ; 302 303 for (Enumeration serverIds = db.serverTable.keys(); 304 serverIds.hasMoreElements();) 305 { 306 Integer nextServerId = (Integer ) serverIds.nextElement(); 307 DBServerDef dbServerDef = 308 (DBServerDef) db.serverTable.get(nextServerId); 309 310 if (dbServerDef.applicationName.equals(applicationName)) { 311 result = nextServerId.intValue(); 312 break ; 313 } 314 } 315 316 if (debug) 317 System.out.println("RepositoryImpl: getServerID for " + 318 applicationName + " is " + result ) ; 319 320 if (result == -1) { 321 throw (new ServerNotRegistered()); 322 } else { 323 return result ; 324 } 325 } 326 } 327 328 public String [] getApplicationNames() { 329 synchronized (db) { 330 Vector v = new Vector (); 331 for (Enumeration serverIds = db.serverTable.keys(); 332 serverIds.hasMoreElements();) 333 { 334 Integer nextServerId = (Integer ) serverIds.nextElement(); 335 336 DBServerDef dbServerDef = (DBServerDef)db.serverTable.get( 337 nextServerId); 338 339 if (!dbServerDef.applicationName.equals("")) 340 v.addElement( dbServerDef.applicationName ) ; 341 } 342 343 String [] apps = new String [v.size()]; 344 for (int i = 0; i < v.size(); i++) { 345 apps[i] = (String )v.elementAt(i); 346 } 347 348 if (debug) { 349 StringBuffer sb = new StringBuffer () ; 350 for (int ctr=0; ctr<apps.length; ctr++) { 351 sb.append( ' ' ) ; 352 sb.append( apps[ctr] ) ; 353 } 354 355 System.out.println( "RepositoryImpl: getApplicationNames returns " + 356 sb.toString() ) ; 357 } 358 359 return apps; 360 } 361 } 362 366 public static void main(String args[]) { 367 boolean debug = false ; 368 for (int ctr=0; ctr<args.length; ctr++) 369 if (args[ctr].equals("-debug")) 370 debug = true ; 371 372 try { 373 Properties props = new Properties (); 376 props.put("org.omg.CORBA.ORBClass", 377 "com.sun.corba.se.impl.orb.ORBImpl"); 378 ORB orb = (ORB) ORB.init(args, props); 379 380 String db = System.getProperty( ORBConstants.DB_PROPERTY, 382 ORBConstants.DEFAULT_DB_NAME ); 383 RepositoryImpl repository = new RepositoryImpl(orb, new File (db), 384 debug); 385 386 orb.run(); 388 } catch (Exception ex) { 389 ex.printStackTrace(); 390 } 391 } 392 393 transient private boolean debug = false; 394 395 final static int illegalServerId = -1; 396 397 transient private RepositoryDB db = null; 398 399 transient ORB orb = null; 400 401 transient ActivationSystemException wrapper ; 402 403 class RepositoryDB implements Serializable 404 { 405 File db; 406 Hashtable serverTable; 407 Integer serverIdCounter; 408 409 RepositoryDB(File dbFile) { 410 411 db = dbFile; 412 413 serverTable = new Hashtable (255); 416 serverIdCounter = new Integer (256); 417 } 418 419 int incrementServerIdCounter() 420 { 421 int value = serverIdCounter.intValue(); 422 serverIdCounter = new Integer (++value); 423 424 return value; 425 } 426 427 void flush() 428 { 429 try { 430 db.delete(); 431 FileOutputStream fos = new FileOutputStream (db); 432 ObjectOutputStream oos = new ObjectOutputStream (fos); 433 oos.writeObject(this); 434 oos.flush(); 435 oos.close(); 436 } catch (Exception ex) { 437 throw wrapper.cannotWriteRepositoryDb( ex ) ; 438 } 439 } 440 } 441 442 class DBServerDef implements Serializable 443 { 444 public String toString() { 445 return "DBServerDef(applicationName=" + applicationName + 446 ", name=" + name + 447 ", classPath=" + classPath + 448 ", args=" + args + 449 ", vmArgs=" + vmArgs + 450 ", id=" + id + 451 ", isInstalled=" + isInstalled + ")" ; 452 } 453 454 DBServerDef(ServerDef server, int server_id) { 455 applicationName = server.applicationName ; 456 name = server.serverName; 457 classPath = server.serverClassPath; 458 args = server.serverArgs; 459 vmArgs = server.serverVmArgs; 460 id = server_id; 461 isInstalled = false ; 462 } 463 464 String applicationName; 465 String name; 466 String classPath; 467 String args; 468 String vmArgs; 469 boolean isInstalled ; 470 int id; 471 } 472 } 473 | Popular Tags |