KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > activation > ServerTableEntry


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

7 package com.sun.corba.se.impl.activation;
8
9 /**
10  *
11  * @version 1.28
12  * @author Anita Jindal
13  * @since JDK1.2
14  */

15
16 import org.omg.CORBA.CompletionStatus JavaDoc;
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 JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
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 JavaDoc printState()
47     {
48     String JavaDoc 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     // state of each entry
66
private int state;
67     private int serverId;
68     private HashMap JavaDoc orbAndPortInfo;
69     private Server serverObj;
70     private ServerDef serverDef;
71     private Process JavaDoc process;
72     private int activateRetryCount=0;
73     private String JavaDoc activationCmd;
74     private ActivationSystemException wrapper ;
75     public String JavaDoc toString()
76     {
77     return "ServerTableEntry[" + "state=" + printState() +
78         " serverId=" + serverId +
79         " activateRetryCount=" + activateRetryCount + "]" ;
80     }
81
82     // get the string needed to make the activation command
83
private static String JavaDoc 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 JavaDoc dbDirName, boolean verify, boolean debug )
95     {
96     this.wrapper = wrapper ;
97         this.serverId = serverId;
98         this.serverDef = serverDef;
99     this.debug = debug ;
100     // create a HashMap with capacity 255
101
// Since all methods are synchronized, we don't need any
102
// additional synchronization mechanisms
103
orbAndPortInfo = new HashMap JavaDoc(255);
104
105     activateRetryCount = 0;
106         state = ACTIVATING;
107
108     // compute the activation command
109
activationCmd =
110
111         // add path to the java vm
112
javaHome + fileSep + "bin" + fileSep + "java " +
113
114         // add any arguments to the server Java VM
115
serverDef.serverVmArgs + " " +
116
117         // add ORB properties
118
"-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         // we need to pass in the verify flag, so that the server is not
125
// launched, when we try to validate its definition during registration
126
// into the RepositoryImpl
127

128         (verify ? "-D" + ORBConstants.SERVER_DEF_VERIFY_PROPERTY + "=true ": "") +
129
130         // add classpath to the server
131
"-classpath " + classPath +
132         (serverDef.serverClassPath.equals("") == true ? "" : pathSep) +
133         serverDef.serverClassPath +
134
135         // add server class name and arguments
136
" com.sun.corba.se.impl.activation.ServerMain " + serverDef.serverArgs
137
138         // Add the debug flag, if any
139
+ (debug ? " -debug" : "") ;
140
141     if (debug) System.out.println(
142                       "ServerTableEntry constructed with activation command " +
143                       activationCmd);
144     }
145
146     /**
147      * Verify whether the server definition is valid.
148      */

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 JavaDoc 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 JavaDoc method, String JavaDoc 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 JavaDoc
179     {
180         state = ACTIVATED;
181
182         try {
183             if (debug)
184         printDebug("activate", "activating server");
185             process = Runtime.getRuntime().exec(activationCmd);
186         } catch (Exception JavaDoc 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             //state = RUNNING;
201
//notifyAll();
202

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 JavaDoc orbId, EndPointInfo [] endpointList)
215     throws ORBAlreadyRegistered
216     {
217
218     // find if the ORB is already registered, then throw an exception
219
if (orbAndPortInfo.containsKey(orbId)) {
220         throw new ORBAlreadyRegistered(orbId);
221     }
222
223     // store all listener ports and their types
224
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     // put this set of listener ports in the HashMap associated
234
// with the orbId
235
orbAndPortInfo.put(orbId, serverListenerPorts);
236         if (state == ACTIVATED) {
237             state = RUNNING;
238             notifyAll();
239         }
240         // _REVISIT_, If the state is not equal to ACTIVATED then it is a bug
241
// need to log that error, once the Logging framework is in place
242
// for rip-int.
243
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 JavaDoc 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(); // shutdown the server
282
localServerObj.uninstall() ; // call the uninstall
283
}
284
285             if (localProcess != null) {
286                 localProcess.destroy();
287             }
288         } catch (Exception JavaDoc ex) {
289             // what kind of exception should be thrown
290
}
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 the State in the ServerTableEntry is RUNNING and the
315
// Process was shut down abnormally, The method will change the
316
// server state as De-Activated.
317
if( state == RUNNING ) {
318             try {
319                 int exitVal = process.exitValue();
320             } catch (IllegalThreadStateException JavaDoc e1) {
321                 return;
322             }
323             synchronized ( this ) {
324                 // Clear the PortInformation as it is old
325
orbAndPortInfo.clear();
326                 // Move the state to De-Activated, So that the next
327
// call to this server will re-activate.
328
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 JavaDoc 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 JavaDoc endpointType) throws ServerHeldDown
369     {
370         while ((state == ACTIVATING) || (state == ACTIVATED)) {
371             try {
372                 wait(waitTime);
373                 if (!isValid()) break;
374             } catch(Exception JavaDoc e) {}
375         }
376     ORBPortInfo[] orbAndPortList = null;
377
378         if (state == RUNNING) {
379         orbAndPortList = new ORBPortInfo[orbAndPortInfo.size()];
380         Iterator JavaDoc setORBids = orbAndPortInfo.keySet().iterator();
381
382         try {
383         int numElements = 0;
384         int i;
385             int port;
386             while (setORBids.hasNext()) {
387             String JavaDoc orbId = (String JavaDoc) setORBids.next();
388             // get an entry corresponding to orbId
389
EndPointInfo [] serverListenerPorts = (EndPointInfo []) orbAndPortInfo.get(orbId);
390                 port = -1;
391                 // return the port corresponding to the endpointType
392
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 JavaDoc e) {
406             // have everything in the table
407
}
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 JavaDoc orbId)
418     throws ServerHeldDown, InvalidORBid
419     {
420         while ((state == ACTIVATING) || (state == ACTIVATED)) {
421             try {
422                 wait(waitTime);
423                 if (!isValid()) break;
424             } catch(Exception JavaDoc e) {}
425         }
426     EndPointInfo[] portList = null;
427
428         if (state == RUNNING) {
429
430         try {
431
432         // get an entry corresponding to orbId
433
EndPointInfo [] serverListenerPorts = (EndPointInfo []) orbAndPortInfo.get(orbId);
434
435             portList = new EndPointInfo[serverListenerPorts.length];
436             // return the port corresponding to the endpointType
437
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 JavaDoc e) {
445             // no element in HashMap corresponding to ORBid found
446
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 JavaDoc[] getORBList()
458     {
459     String JavaDoc [] orbList = new String JavaDoc[orbAndPortInfo.size()];
460     Iterator JavaDoc setORBids = orbAndPortInfo.keySet().iterator();
461
462     try {
463         int numElements = 0;
464         while (setORBids.hasNext()) {
465         String JavaDoc orbId = (String JavaDoc) setORBids.next();
466         orbList[numElements++] = orbId ;
467         }
468     } catch (NoSuchElementException JavaDoc e) {
469         // have everything in the table
470
}
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 JavaDoc 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 JavaDoc ex) {
504             if (debug)
505                 printDebug( "destroy",
506                             "server shutdown threw exception" + ex ) ;
507             // ex.printStackTrace();
508
}
509
510         try {
511             if (localProcess != null)
512                 localProcess.destroy();
513
514             if (debug)
515                 printDebug( "destroy", "process destroyed successfully" ) ;
516         } catch (Exception JavaDoc ex) {
517             if (debug)
518                 printDebug( "destroy",
519                             "process destroy threw exception" + ex ) ;
520
521             // ex.printStackTrace();
522
}
523     }
524
525     private boolean debug = false;
526 }
527
Popular Tags