1 7 package com.sun.corba.se.impl.activation; 8 9 15 16 import org.omg.CORBA.CompletionStatus ; 17 18 import com.sun.corba.se.spi.activation.Server; 19 import com.sun.corba.se.spi.activation.EndPointInfo; 20 import com.sun.corba.se.spi.activation.ORBAlreadyRegistered; 21 import com.sun.corba.se.spi.activation.ORBPortInfo; 22 import com.sun.corba.se.spi.activation.InvalidORBid; 23 import com.sun.corba.se.spi.activation.ServerHeldDown; 24 import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef; 25 import com.sun.corba.se.spi.activation.IIOP_CLEAR_TEXT; 26 import com.sun.corba.se.spi.orb.ORB ; 27 28 import com.sun.corba.se.impl.orbutil.ORBConstants; 29 import com.sun.corba.se.impl.logging.ActivationSystemException ; 30 31 import java.io.File ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.NoSuchElementException ; 35 36 public class ServerTableEntry 37 { 38 39 private final static int DE_ACTIVATED = 0; 40 private final static int ACTIVATING = 1; 41 private final static int ACTIVATED = 2; 42 private final static int RUNNING = 3; 43 private final static int HELD_DOWN = 4; 44 45 46 private String printState() 47 { 48 String str = "UNKNOWN"; 49 50 switch (state) { 51 case (DE_ACTIVATED) : str = "DE_ACTIVATED"; break; 52 case (ACTIVATING ) : str = "ACTIVATING "; break; 53 case (ACTIVATED ) : str = "ACTIVATED "; break; 54 case (RUNNING ) : str = "RUNNING "; break; 55 case (HELD_DOWN ) : str = "HELD_DOWN "; break; 56 default: break; 57 } 58 59 return str; 60 } 61 62 private final static long waitTime = 2000; 63 private static final int ActivationRetryMax = 5; 64 65 private int state; 67 private int serverId; 68 private HashMap orbAndPortInfo; 69 private Server serverObj; 70 private ServerDef serverDef; 71 private Process process; 72 private int activateRetryCount=0; 73 private String activationCmd; 74 private ActivationSystemException wrapper ; 75 public String toString() 76 { 77 return "ServerTableEntry[" + "state=" + printState() + 78 " serverId=" + serverId + 79 " activateRetryCount=" + activateRetryCount + "]" ; 80 } 81 82 private static String javaHome, classPath, fileSep, pathSep; 84 85 static { 86 javaHome = System.getProperty("java.home"); 87 classPath = System.getProperty("java.class.path"); 88 fileSep = System.getProperty("file.separator"); 89 pathSep = System.getProperty("path.separator"); 90 } 91 92 ServerTableEntry( ActivationSystemException wrapper, 93 int serverId, ServerDef serverDef, int initialPort, 94 String dbDirName, boolean verify, boolean debug ) 95 { 96 this.wrapper = wrapper ; 97 this.serverId = serverId; 98 this.serverDef = serverDef; 99 this.debug = debug ; 100 orbAndPortInfo = new HashMap (255); 104 105 activateRetryCount = 0; 106 state = ACTIVATING; 107 108 activationCmd = 110 111 javaHome + fileSep + "bin" + fileSep + "java " + 113 114 serverDef.serverVmArgs + " " + 116 117 "-Dioser=" + System.getProperty( "ioser" ) + " " + 119 "-D" + ORBConstants.INITIAL_PORT_PROPERTY + "=" + initialPort + " " + 120 "-D" + ORBConstants.DB_DIR_PROPERTY + "=" + dbDirName + " " + 121 "-D" + ORBConstants.ACTIVATED_PROPERTY + "=true " + 122 "-D" + ORBConstants.SERVER_ID_PROPERTY + "=" + serverId + " " + 123 "-D" + ORBConstants.SERVER_NAME_PROPERTY + "=" + serverDef.serverName + " " + 124 128 (verify ? "-D" + ORBConstants.SERVER_DEF_VERIFY_PROPERTY + "=true ": "") + 129 130 "-classpath " + classPath + 132 (serverDef.serverClassPath.equals("") == true ? "" : pathSep) + 133 serverDef.serverClassPath + 134 135 " com.sun.corba.se.impl.activation.ServerMain " + serverDef.serverArgs 137 138 + (debug ? " -debug" : "") ; 140 141 if (debug) System.out.println( 142 "ServerTableEntry constructed with activation command " + 143 activationCmd); 144 } 145 146 149 public int verify() 150 { 151 try { 152 153 if (debug) 154 System.out.println("Server being verified w/" + activationCmd); 155 156 process = Runtime.getRuntime().exec(activationCmd); 157 int result = process.waitFor(); 158 if (debug) 159 printDebug( "verify", "returns " + ServerMain.printResult( result ) ) ; 160 return result ; 161 } catch (Exception e) { 162 if (debug) 163 printDebug( "verify", "returns unknown error because of exception " + 164 e ) ; 165 return ServerMain.UNKNOWN_ERROR; 166 } 167 } 168 169 private void printDebug(String method, String msg) 170 { 171 System.out.println("ServerTableEntry: method =" + method); 172 System.out.println("ServerTableEntry: server =" + serverId); 173 System.out.println("ServerTableEntry: state =" + printState()); 174 System.out.println("ServerTableEntry: message =" + msg); 175 System.out.println(); 176 } 177 178 synchronized void activate() throws org.omg.CORBA.SystemException 179 { 180 state = ACTIVATED; 181 182 try { 183 if (debug) 184 printDebug("activate", "activating server"); 185 process = Runtime.getRuntime().exec(activationCmd); 186 } catch (Exception e) { 187 deActivate(); 188 if (debug) 189 printDebug("activate", "throwing premature process exit"); 190 throw wrapper.unableToStartProcess() ; 191 } 192 } 193 194 synchronized void register(Server server) 195 { 196 if (state == ACTIVATED) { 197 198 serverObj = server; 199 200 203 if (debug) 204 printDebug("register", "process registered back"); 205 206 } else { 207 208 if (debug) 209 printDebug("register", "throwing premature process exit"); 210 throw wrapper.serverNotExpectedToRegister() ; 211 } 212 } 213 214 synchronized void registerPorts( String orbId, EndPointInfo [] endpointList) 215 throws ORBAlreadyRegistered 216 { 217 218 if (orbAndPortInfo.containsKey(orbId)) { 220 throw new ORBAlreadyRegistered(orbId); 221 } 222 223 int numListenerPorts = endpointList.length; 225 EndPointInfo [] serverListenerPorts = new EndPointInfo[numListenerPorts]; 226 227 for (int i = 0; i < numListenerPorts; i++) { 228 serverListenerPorts[i] = new EndPointInfo (endpointList[i].endpointType, endpointList[i].port); 229 if (debug) 230 System.out.println("registering type: " + serverListenerPorts[i].endpointType + " port " + serverListenerPorts[i].port); 231 } 232 233 orbAndPortInfo.put(orbId, serverListenerPorts); 236 if (state == ACTIVATED) { 237 state = RUNNING; 238 notifyAll(); 239 } 240 if (debug) 244 printDebug("registerPorts", "process registered Ports"); 245 } 246 247 void install() 248 { 249 Server localServerObj = null; 250 synchronized ( this ) { 251 if (state == RUNNING) 252 localServerObj = serverObj; 253 else 254 throw wrapper.serverNotRunning() ; 255 } 256 if (localServerObj != null) { 257 localServerObj.install() ; 258 } 259 260 } 261 262 void uninstall() 263 { 264 Server localServerObj = null; 265 Process localProcess = null; 266 267 synchronized (this) { 268 localServerObj = serverObj; 269 localProcess = process; 270 271 if (state == RUNNING) { 272 273 deActivate(); 274 275 } else { 276 throw wrapper.serverNotRunning() ; 277 } 278 } 279 try { 280 if (localServerObj != null) { 281 localServerObj.shutdown(); localServerObj.uninstall() ; } 284 285 if (localProcess != null) { 286 localProcess.destroy(); 287 } 288 } catch (Exception ex) { 289 } 291 } 292 293 synchronized void holdDown() 294 { 295 state = HELD_DOWN; 296 297 if (debug) 298 printDebug( "holdDown", "server held down" ) ; 299 300 notifyAll(); 301 } 302 303 synchronized void deActivate() 304 { 305 state = DE_ACTIVATED; 306 307 if (debug) 308 printDebug( "deActivate", "server deactivated" ) ; 309 310 notifyAll(); 311 } 312 313 synchronized void checkProcessHealth( ) { 314 if( state == RUNNING ) { 318 try { 319 int exitVal = process.exitValue(); 320 } catch (IllegalThreadStateException e1) { 321 return; 322 } 323 synchronized ( this ) { 324 orbAndPortInfo.clear(); 326 deActivate(); 329 } 330 } 331 } 332 333 synchronized boolean isValid() 334 { 335 if ((state == ACTIVATING) || (state == HELD_DOWN)) { 336 if (debug) 337 printDebug( "isValid", "returns true" ) ; 338 339 return true; 340 } 341 342 try { 343 int exitVal = process.exitValue(); 344 } catch (IllegalThreadStateException e1) { 345 return true; 346 } 347 348 if (state == ACTIVATED) { 349 if (activateRetryCount < ActivationRetryMax) { 350 if (debug) 351 printDebug("isValid", "reactivating server"); 352 activateRetryCount++; 353 activate(); 354 return true; 355 } 356 357 if (debug) 358 printDebug("isValid", "holding server down"); 359 360 holdDown(); 361 return true; 362 } 363 364 deActivate(); 365 return false; 366 } 367 368 synchronized ORBPortInfo[] lookup(String endpointType) throws ServerHeldDown 369 { 370 while ((state == ACTIVATING) || (state == ACTIVATED)) { 371 try { 372 wait(waitTime); 373 if (!isValid()) break; 374 } catch(Exception e) {} 375 } 376 ORBPortInfo[] orbAndPortList = null; 377 378 if (state == RUNNING) { 379 orbAndPortList = new ORBPortInfo[orbAndPortInfo.size()]; 380 Iterator setORBids = orbAndPortInfo.keySet().iterator(); 381 382 try { 383 int numElements = 0; 384 int i; 385 int port; 386 while (setORBids.hasNext()) { 387 String orbId = (String ) setORBids.next(); 388 EndPointInfo [] serverListenerPorts = (EndPointInfo []) orbAndPortInfo.get(orbId); 390 port = -1; 391 for (i = 0; i < serverListenerPorts.length; i++) { 393 if (debug) 394 System.out.println("lookup num-ports " + serverListenerPorts.length + " " + 395 serverListenerPorts[i].endpointType + " " + 396 serverListenerPorts[i].port ); 397 if ((serverListenerPorts[i].endpointType).equals(endpointType)) { 398 port = serverListenerPorts[i].port; 399 break; 400 } 401 } 402 orbAndPortList[numElements] = new ORBPortInfo(orbId, port); 403 numElements++; 404 } 405 } catch (NoSuchElementException e) { 406 } 408 return orbAndPortList; 409 } 410 411 if (debug) 412 printDebug("lookup", "throwing server held down error"); 413 414 throw new ServerHeldDown( serverId ) ; 415 } 416 417 synchronized EndPointInfo[] lookupForORB(String orbId) 418 throws ServerHeldDown, InvalidORBid 419 { 420 while ((state == ACTIVATING) || (state == ACTIVATED)) { 421 try { 422 wait(waitTime); 423 if (!isValid()) break; 424 } catch(Exception e) {} 425 } 426 EndPointInfo[] portList = null; 427 428 if (state == RUNNING) { 429 430 try { 431 432 EndPointInfo [] serverListenerPorts = (EndPointInfo []) orbAndPortInfo.get(orbId); 434 435 portList = new EndPointInfo[serverListenerPorts.length]; 436 for (int i = 0; i < serverListenerPorts.length; i++) { 438 if (debug) 439 System.out.println("lookup num-ports " + serverListenerPorts.length + " " 440 + serverListenerPorts[i].endpointType + " " + 441 serverListenerPorts[i].port ); 442 portList[i] = new EndPointInfo(serverListenerPorts[i].endpointType, serverListenerPorts[i].port); 443 } 444 } catch (NoSuchElementException e) { 445 throw new InvalidORBid(); 447 } 448 return portList; 449 } 450 451 if (debug) 452 printDebug("lookup", "throwing server held down error"); 453 454 throw new ServerHeldDown( serverId ) ; 455 } 456 457 synchronized String [] getORBList() 458 { 459 String [] orbList = new String [orbAndPortInfo.size()]; 460 Iterator setORBids = orbAndPortInfo.keySet().iterator(); 461 462 try { 463 int numElements = 0; 464 while (setORBids.hasNext()) { 465 String orbId = (String ) setORBids.next(); 466 orbList[numElements++] = orbId ; 467 } 468 } catch (NoSuchElementException e) { 469 } 471 return orbList; 472 } 473 474 int getServerId() 475 { 476 return serverId; 477 } 478 479 boolean isActive() 480 { 481 return (state == RUNNING) || (state == ACTIVATED); 482 } 483 484 synchronized void destroy() 485 { 486 487 Server localServerObj = null; 488 Process localProcess = null; 489 490 synchronized (this) { 491 localServerObj = serverObj; 492 localProcess = process; 493 494 deActivate(); 495 } 496 497 try { 498 if (localServerObj != null) 499 localServerObj.shutdown(); 500 501 if (debug) 502 printDebug( "destroy", "server shutdown successfully" ) ; 503 } catch (Exception ex) { 504 if (debug) 505 printDebug( "destroy", 506 "server shutdown threw exception" + ex ) ; 507 } 509 510 try { 511 if (localProcess != null) 512 localProcess.destroy(); 513 514 if (debug) 515 printDebug( "destroy", "process destroyed successfully" ) ; 516 } catch (Exception ex) { 517 if (debug) 518 printDebug( "destroy", 519 "process destroy threw exception" + ex ) ; 520 521 } 523 } 524 525 private boolean debug = false; 526 } 527 | Popular Tags |