KickJava   Java API By Example, From Geeks To Geeks.

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


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

7
8 package com.sun.corba.se.impl.activation;
9
10 /**
11  *
12  * @author Rohit Garg
13  * @author Ken Cavanaugh
14  * @author Hemanth Puttaswamy
15  * @since JDK1.2
16  */

17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
25 import org.omg.CORBA.SystemException JavaDoc;
26
27 import com.sun.corba.se.spi.activation.EndPointInfo;
28 import com.sun.corba.se.spi.activation.IIOP_CLEAR_TEXT;
29 import com.sun.corba.se.spi.activation.ORBPortInfo;
30 import com.sun.corba.se.spi.activation.Repository;
31 import com.sun.corba.se.spi.activation.LocatorPackage.ServerLocation;
32 import com.sun.corba.se.spi.activation.LocatorPackage.ServerLocationPerORB;
33 import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
34 import com.sun.corba.se.spi.activation._ServerManagerImplBase;
35 import com.sun.corba.se.spi.activation.ServerAlreadyActive;
36 import com.sun.corba.se.spi.activation.ServerAlreadyInstalled;
37 import com.sun.corba.se.spi.activation.ServerAlreadyUninstalled;
38 import com.sun.corba.se.spi.activation.ServerNotRegistered;
39 import com.sun.corba.se.spi.activation.ORBAlreadyRegistered;
40 import com.sun.corba.se.spi.activation.ServerHeldDown;
41 import com.sun.corba.se.spi.activation.ServerNotActive;
42 import com.sun.corba.se.spi.activation.NoSuchEndPoint;
43 import com.sun.corba.se.spi.activation.InvalidORBid;
44 import com.sun.corba.se.spi.activation.Server;
45 import com.sun.corba.se.spi.activation.IIOP_CLEAR_TEXT;
46 import com.sun.corba.se.spi.ior.IORTemplate ;
47 import com.sun.corba.se.spi.ior.IOR ;
48 import com.sun.corba.se.spi.ior.ObjectKey ;
49 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
50 import com.sun.corba.se.spi.ior.IORFactories ;
51 import com.sun.corba.se.spi.ior.iiop.GIOPVersion ;
52 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
53 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
54 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
55 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
56 import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
57 import com.sun.corba.se.spi.orb.ORB ;
58 import com.sun.corba.se.spi.protocol.ForwardException;
59 import com.sun.corba.se.spi.transport.CorbaTransportManager;
60
61 import com.sun.corba.se.spi.logging.CORBALogDomains ;
62 import com.sun.corba.se.impl.logging.ActivationSystemException ;
63
64 import com.sun.corba.se.impl.oa.poa.BadServerIdHandler;
65 import com.sun.corba.se.impl.orbutil.ORBConstants;
66 import com.sun.corba.se.impl.orbutil.ORBClassLoader;
67 import com.sun.corba.se.impl.orbutil.ORBUtility;
68 import com.sun.corba.se.impl.util.Utility;
69
70 public class ServerManagerImpl extends _ServerManagerImplBase
71     implements BadServerIdHandler
72 {
73     // Using HashMap, since synchronization should be done by the calling
74
// routines
75
HashMap JavaDoc serverTable;
76     Repository repository;
77
78     CorbaTransportManager transportManager;
79     int initialPort;
80     ORB orb;
81     ActivationSystemException wrapper;
82     String JavaDoc dbDirName;
83     boolean debug = false ;
84  
85     private int serverStartupDelay;
86
87     ServerManagerImpl(ORB orb, CorbaTransportManager transportManager,
88               Repository repository, String JavaDoc dbDirName, boolean debug)
89     {
90         this.orb = orb;
91     wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_ACTIVATOR ) ;
92
93         this.transportManager = transportManager; // REVISIT - NOT USED.
94
this.repository = repository;
95     this.dbDirName = dbDirName;
96     this.debug = debug ;
97
98     LegacyServerSocketEndPointInfo endpoint =
99         orb.getLegacyServerSocketManager()
100             .legacyGetEndpoint(LegacyServerSocketEndPointInfo.BOOT_NAMING);
101
102     initialPort = ((SocketOrChannelAcceptor)endpoint)
103         .getServerSocket().getLocalPort();
104         serverTable = new HashMap JavaDoc(256);
105
106         // The ServerStartupDelay is the delay added after the Server registers
107
// end point information. This is to allow the server to completely
108
// initialize after ORB is instantiated.
109
serverStartupDelay = ORBConstants.DEFAULT_SERVER_STARTUP_DELAY;
110         String JavaDoc delay = System.getProperty( ORBConstants.SERVER_STARTUP_DELAY);
111         if( delay != null ) {
112             try {
113                 serverStartupDelay = Integer.parseInt( delay );
114             } catch ( Exception JavaDoc e ) {
115                 // Just use the default 1000 milliseconds as the default
116
}
117         }
118
119         Class JavaDoc cls = orb.getORBData( ).getBadServerIdHandler();
120         if( cls == null ) {
121             orb.setBadServerIdHandler( this );
122         } else {
123         orb.initBadServerIdHandler() ;
124         }
125
126         orb.connect(this);
127         ProcessMonitorThread.start( serverTable );
128     }
129
130     public void activate(int serverId)
131         throws ServerAlreadyActive, ServerNotRegistered, ServerHeldDown
132     {
133
134         ServerLocation location;
135         ServerTableEntry entry;
136         Integer JavaDoc key = new Integer JavaDoc(serverId);
137
138     synchronized(serverTable) {
139             entry = (ServerTableEntry) serverTable.get(key);
140     }
141
142         if (entry != null && entry.isActive()) {
143         if (debug)
144         System.out.println( "ServerManagerImpl: activate for server Id " +
145                     serverId + " failed because server is already active. " +
146                     "entry = " + entry ) ;
147
148             throw new ServerAlreadyActive( serverId );
149     }
150
151         // locate the server
152
try {
153
154         // We call getEntry here so that state of the entry is
155
// checked for validity before we actually go and locate a server
156

157         entry = getEntry(serverId);
158
159             if (debug)
160                 System.out.println( "ServerManagerImpl: locateServer called with " +
161                                 " serverId=" + serverId + " endpointType="
162                                 + IIOP_CLEAR_TEXT.value + " block=false" ) ;
163
164         location = locateServer(entry, IIOP_CLEAR_TEXT.value, false);
165
166         if (debug)
167         System.out.println( "ServerManagerImpl: activate for server Id " +
168                     serverId + " found location " +
169                     location.hostname + " and activated it" ) ;
170         } catch (NoSuchEndPoint ex) {
171             if (debug)
172                 System.out.println( "ServerManagerImpl: activate for server Id " +
173                                     " threw NoSuchEndpoint exception, which was ignored" );
174     }
175     }
176
177     public void active(int serverId, Server server) throws ServerNotRegistered
178     {
179         ServerTableEntry entry;
180         Integer JavaDoc key = new Integer JavaDoc(serverId);
181
182         synchronized (serverTable) {
183             entry = (ServerTableEntry) serverTable.get(key);
184
185             if (entry == null) {
186         if (debug)
187             System.out.println( "ServerManagerImpl: active for server Id " +
188                     serverId + " called, but no such server is registered." ) ;
189
190         throw wrapper.serverNotExpectedToRegister() ;
191             } else {
192         if (debug)
193             System.out.println( "ServerManagerImpl: active for server Id " +
194                     serverId + " called. This server is now active." ) ;
195
196         entry.register(server);
197         }
198         }
199     }
200
201     public void registerEndpoints( int serverId, String JavaDoc orbId,
202     EndPointInfo [] endpointList ) throws NoSuchEndPoint, ServerNotRegistered,
203     ORBAlreadyRegistered
204     {
205     // orbId is ignored for now
206
ServerTableEntry entry;
207         Integer JavaDoc key = new Integer JavaDoc(serverId);
208
209         synchronized (serverTable) {
210             entry = (ServerTableEntry) serverTable.get(key);
211
212             if (entry == null) {
213         if (debug)
214             System.out.println(
215             "ServerManagerImpl: registerEndpoint for server Id " +
216             serverId + " called, but no such server is registered." ) ;
217
218         throw wrapper.serverNotExpectedToRegister() ;
219             } else {
220         if (debug)
221             System.out.println(
222             "ServerManagerImpl: registerEndpoints for server Id " +
223             serverId + " called. This server is now active." ) ;
224
225         entry.registerPorts( orbId, endpointList );
226                
227         }
228     }
229     }
230
231     public int[] getActiveServers()
232     {
233     ServerTableEntry entry;
234         int[] list = null;
235
236         synchronized (serverTable) {
237         // unlike vectors, list is not synchronized
238

239         ArrayList JavaDoc servers = new ArrayList JavaDoc(0);
240
241         Iterator JavaDoc serverList = serverTable.keySet().iterator();
242
243         try {
244             while (serverList.hasNext()) {
245                 Integer JavaDoc key = (Integer JavaDoc) serverList.next();
246                 // get an entry
247
entry = (ServerTableEntry) serverTable.get(key);
248
249             if (entry.isValid() && entry.isActive()) {
250                 servers.add(entry);
251                 }
252             }
253         } catch (NoSuchElementException JavaDoc e) {
254             // all done
255
}
256
257         // collect the active entries
258
list = new int[servers.size()];
259         for (int i = 0; i < servers.size(); i++) {
260             entry = (ServerTableEntry) servers.get(i);
261         list[i] = entry.getServerId();
262         }
263         }
264
265     if (debug) {
266         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
267         for (int ctr=0; ctr<list.length; ctr++) {
268         sb.append( ' ' ) ;
269         sb.append( list[ctr] ) ;
270         }
271
272         System.out.println( "ServerManagerImpl: getActiveServers returns" +
273                 sb.toString() ) ;
274     }
275
276         return list;
277     }
278
279     public void shutdown(int serverId) throws ServerNotActive
280     {
281         ServerTableEntry entry;
282         Integer JavaDoc key = new Integer JavaDoc(serverId);
283
284     synchronized(serverTable) {
285             entry = (ServerTableEntry) serverTable.remove(key);
286
287             if (entry == null) {
288             if (debug)
289             System.out.println( "ServerManagerImpl: shutdown for server Id " +
290                     serverId + " throws ServerNotActive." ) ;
291
292             throw new ServerNotActive( serverId );
293         }
294
295             try {
296                 entry.destroy();
297
298             if (debug)
299             System.out.println( "ServerManagerImpl: shutdown for server Id " +
300                     serverId + " completed." ) ;
301             } catch (Exception JavaDoc e) {
302             if (debug)
303             System.out.println( "ServerManagerImpl: shutdown for server Id " +
304                     serverId + " threw exception " + e ) ;
305         }
306     }
307     }
308
309     private ServerTableEntry getEntry( int serverId )
310     throws ServerNotRegistered
311     {
312         Integer JavaDoc key = new Integer JavaDoc(serverId);
313     ServerTableEntry entry = null ;
314
315     synchronized (serverTable) {
316             entry = (ServerTableEntry) serverTable.get(key);
317
318         if (debug)
319         if (entry == null) {
320             System.out.println( "ServerManagerImpl: getEntry: " +
321                     "no active server found." ) ;
322         } else {
323             System.out.println( "ServerManagerImpl: getEntry: " +
324                     " active server found " + entry + "." ) ;
325         }
326
327             if ((entry != null) && (!entry.isValid())) {
328                 serverTable.remove(key);
329                 entry = null;
330             }
331
332             if (entry == null) {
333         ServerDef serverDef = repository.getServer(serverId);
334
335                 entry = new ServerTableEntry( wrapper,
336             serverId, serverDef, initialPort, dbDirName, false, debug);
337         serverTable.put(key, entry);
338         entry.activate() ;
339             }
340         }
341
342     return entry ;
343     }
344
345     private ServerLocation locateServer (ServerTableEntry entry, String JavaDoc endpointType,
346                     boolean block)
347         throws NoSuchEndPoint, ServerNotRegistered, ServerHeldDown
348     {
349     ServerLocation location = new ServerLocation() ;
350
351     // if server location is desired, then wait for the server
352
// to register back, then return location
353

354     ORBPortInfo [] serverORBAndPortList;
355     if (block) {
356         try {
357                 serverORBAndPortList = entry.lookup(endpointType);
358             } catch (Exception JavaDoc ex) {
359         if (debug)
360             System.out.println( "ServerManagerImpl: locateServer: " +
361                     "server held down" ) ;
362
363             throw new ServerHeldDown( entry.getServerId() );
364         }
365
366             String JavaDoc host =
367         orb.getLegacyServerSocketManager()
368             .legacyGetEndpoint(LegacyServerSocketEndPointInfo.DEFAULT_ENDPOINT).getHostName();
369             location.hostname = host ;
370         int listLength;
371         if (serverORBAndPortList != null) {
372             listLength = serverORBAndPortList.length;
373         } else {
374             listLength = 0;
375         }
376         location.ports = new ORBPortInfo[listLength];
377         for (int i = 0; i < listLength; i++) {
378             location.ports[i] = new ORBPortInfo(serverORBAndPortList[i].orbId,
379             serverORBAndPortList[i].port) ;
380
381             if (debug)
382                 System.out.println( "ServerManagerImpl: locateServer: " +
383                     "server located at location " +
384                     location.hostname + " ORBid " +
385                                 serverORBAndPortList[i].orbId +
386                                 " Port " + serverORBAndPortList[i].port) ;
387         }
388     }
389
390     return location;
391     }
392
393     private ServerLocationPerORB locateServerForORB (ServerTableEntry entry, String JavaDoc orbId,
394                     boolean block)
395         throws InvalidORBid, ServerNotRegistered, ServerHeldDown
396     {
397     ServerLocationPerORB location = new ServerLocationPerORB() ;
398
399     // if server location is desired, then wait for the server
400
// to register back, then return location
401

402     EndPointInfo [] endpointInfoList;
403     if (block) {
404         try {
405             endpointInfoList = entry.lookupForORB(orbId);
406         } catch (InvalidORBid ex) {
407         throw ex;
408             } catch (Exception JavaDoc ex) {
409         if (debug)
410             System.out.println( "ServerManagerImpl: locateServerForORB: " +
411                     "server held down" ) ;
412
413             throw new ServerHeldDown( entry.getServerId() );
414         }
415
416             String JavaDoc host =
417         orb.getLegacyServerSocketManager()
418             .legacyGetEndpoint(LegacyServerSocketEndPointInfo.DEFAULT_ENDPOINT).getHostName();
419             location.hostname = host ;
420         int listLength;
421         if (endpointInfoList != null) {
422             listLength = endpointInfoList.length;
423         } else {
424             listLength = 0;
425         }
426         location.ports = new EndPointInfo[listLength];
427         for (int i = 0; i < listLength; i++) {
428             location.ports[i] = new EndPointInfo(endpointInfoList[i].endpointType,
429             endpointInfoList[i].port) ;
430
431             if (debug)
432                 System.out.println( "ServerManagerImpl: locateServer: " +
433                     "server located at location " +
434                     location.hostname + " endpointType " +
435                                 endpointInfoList[i].endpointType +
436                                 " Port " + endpointInfoList[i].port) ;
437         }
438     }
439
440     return location;
441     }
442
443     public String JavaDoc[] getORBNames(int serverId)
444     throws ServerNotRegistered
445     {
446     try {
447         ServerTableEntry entry = getEntry( serverId ) ;
448         return (entry.getORBList());
449     } catch (Exception JavaDoc ex) {
450         throw new ServerNotRegistered(serverId);
451     }
452     }
453
454     private ServerTableEntry getRunningEntry( int serverId )
455     throws ServerNotRegistered
456     {
457     ServerTableEntry entry = getEntry( serverId ) ;
458
459     try {
460         // this is to see if the server has any listeners
461
ORBPortInfo [] serverORBAndPortList = entry.lookup(IIOP_CLEAR_TEXT.value) ;
462     } catch (Exception JavaDoc exc) {
463         return null ;
464     }
465     return entry;
466
467     }
468
469     public void install( int serverId )
470     throws ServerNotRegistered, ServerHeldDown, ServerAlreadyInstalled
471     {
472     ServerTableEntry entry = getRunningEntry( serverId ) ;
473     if (entry != null) {
474         repository.install( serverId ) ;
475         entry.install() ;
476     }
477     }
478
479     public void uninstall( int serverId )
480     throws ServerNotRegistered, ServerHeldDown, ServerAlreadyUninstalled
481     {
482     ServerTableEntry entry =
483             (ServerTableEntry) serverTable.get( new Integer JavaDoc(serverId) );
484         
485     if (entry != null) {
486
487             entry =
488                 (ServerTableEntry) serverTable.remove(new Integer JavaDoc(serverId));
489
490             if (entry == null) {
491                 if (debug)
492                     System.out.println( "ServerManagerImpl: shutdown for server Id " +
493                                     serverId + " throws ServerNotActive." ) ;
494
495                 throw new ServerHeldDown( serverId );
496             }
497
498         entry.uninstall();
499     }
500     }
501
502     public ServerLocation locateServer (int serverId, String JavaDoc endpointType)
503         throws NoSuchEndPoint, ServerNotRegistered, ServerHeldDown
504     {
505     ServerTableEntry entry = getEntry( serverId ) ;
506         if (debug)
507             System.out.println( "ServerManagerImpl: locateServer called with " +
508                                 " serverId=" + serverId + " endpointType=" +
509                 endpointType + " block=true" ) ;
510
511     // passing in entry to eliminate multiple lookups for
512
// the same entry in some cases
513

514     return locateServer(entry, endpointType, true);
515     }
516
517     /** This method is used to obtain the registered ports for an ORB.
518     * This is useful for custom Bad server ID handlers in ORBD.
519     */

520     public ServerLocationPerORB locateServerForORB (int serverId, String JavaDoc orbId)
521         throws InvalidORBid, ServerNotRegistered, ServerHeldDown
522     {
523     ServerTableEntry entry = getEntry( serverId ) ;
524
525     // passing in entry to eliminate multiple lookups for
526
// the same entry in some cases
527

528     if (debug)
529         System.out.println( "ServerManagerImpl: locateServerForORB called with " +
530                 " serverId=" + serverId + " orbId=" + orbId +
531                 " block=true" ) ;
532     return locateServerForORB(entry, orbId, true);
533     }
534
535
536     public void handle(ObjectKey okey)
537     {
538     IOR newIOR = null;
539     ServerLocationPerORB location;
540
541     // we need to get the serverid and the orbid from the object key
542
ObjectKeyTemplate oktemp = okey.getTemplate();
543     int serverId = oktemp.getServerId() ;
544     String JavaDoc orbId = oktemp.getORBId() ;
545
546     try {
547         // get the ORBName corresponding to the orbMapid, that was
548
// first registered by the server
549
ServerTableEntry entry = getEntry( serverId ) ;
550         location = locateServerForORB(entry, orbId, true);
551              
552         if (debug)
553             System.out.println( "ServerManagerImpl: handle called for server id" +
554             serverId + " orbid " + orbId) ;
555
556         // we received a list of ports corresponding to an ORB in a
557
// particular server, now retrieve the one corresponding
558
// to IIOP_CLEAR_TEXT, and for other created the tagged
559
// components to be added to the IOR
560

561         int clearPort = 0;
562         EndPointInfo[] listenerPorts = location.ports;
563         for (int i = 0; i < listenerPorts.length; i++) {
564             if ((listenerPorts[i].endpointType).equals(IIOP_CLEAR_TEXT.value)) {
565                 clearPort = listenerPorts[i].port;
566                 break;
567                 }
568         }
569
570         // create a new IOR with the correct port and correct tagged
571
// components
572
IIOPAddress addr = IIOPFactories.makeIIOPAddress( orb,
573         location.hostname, clearPort ) ;
574         IIOPProfileTemplate iptemp =
575         IIOPFactories.makeIIOPProfileTemplate(
576                     orb, GIOPVersion.V1_2, addr ) ;
577         if (GIOPVersion.V1_2.supportsIORIIOPProfileComponents()) {
578         iptemp.add(IIOPFactories.makeCodeSetsComponent(orb));
579         iptemp.add(IIOPFactories.makeMaxStreamFormatVersionComponent());
580         }
581         IORTemplate iortemp = IORFactories.makeIORTemplate(oktemp) ;
582         iortemp.add( iptemp ) ;
583
584         newIOR = iortemp.makeIOR(orb, "IDL:org/omg/CORBA/Object:1.0",
585         okey.getId() );
586     } catch (Exception JavaDoc e) {
587         throw wrapper.errorInBadServerIdHandler( e ) ;
588     }
589
590     if (debug)
591         System.out.println( "ServerManagerImpl: handle " +
592                 "throws ForwardException" ) ;
593
594         
595         try {
596             // This delay is required in case of Server is activated or
597
// re-activated the first time. Server needs some time before
598
// handling all the requests.
599
// (Talk to Ken to see whether there is a better way of doing this).
600
Thread.sleep( serverStartupDelay );
601         } catch ( Exception JavaDoc e ) {
602             System.out.println( "Exception = " + e );
603             e.printStackTrace();
604         }
605
606     throw new ForwardException(orb, newIOR);
607     }
608
609     public int getEndpoint(String JavaDoc endpointType) throws NoSuchEndPoint
610     {
611         return orb.getLegacyServerSocketManager()
612         .legacyGetTransientServerPort(endpointType);
613     }
614
615     public int getServerPortForType(ServerLocationPerORB location,
616                     String JavaDoc endPointType)
617     throws NoSuchEndPoint
618     {
619     EndPointInfo[] listenerPorts = location.ports;
620     for (int i = 0; i < listenerPorts.length; i++) {
621         if ((listenerPorts[i].endpointType).equals(endPointType)) {
622         return listenerPorts[i].port;
623         }
624     }
625     throw new NoSuchEndPoint();
626     }
627
628 }
629
Popular Tags