KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RepositoryImpl.java 1.45 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  * @(#)RepositoryImpl.java 1.1 97/10/17
9  *
10  * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
11  * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
12  *
13  * This software is the confidential and proprietary information of Sun
14  * Microsystems, Inc. ("Confidential Information"). You shall not
15  * disclose such Confidential Information and shall use it only in
16  * accordance with the terms of the license agreement you entered into
17  * with Sun.
18  *
19  * CopyrightVersion 1.2
20  *
21  */

22
23 package com.sun.corba.se.impl.activation;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import org.omg.CORBA.CompletionStatus JavaDoc;
37 import org.omg.CORBA.INITIALIZE JavaDoc;
38 import org.omg.CORBA.INTERNAL JavaDoc;
39 import org.omg.CORBA.SystemException JavaDoc;
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 /**
57  *
58  * @version 1.1, 97/10/17
59  * @author Rohit Garg
60  * @since JDK1.2
61  */

62 public class RepositoryImpl extends _RepositoryImplBase
63     implements Serializable JavaDoc
64 {
65
66     // added serialver computed by the tool
67
private static final long serialVersionUID = 8458417785209341858L;
68
69     RepositoryImpl(ORB orb, File JavaDoc dbDir, boolean debug)
70     {
71     this.debug = debug ;
72     this.orb = orb;
73     wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_REPOSITORY ) ;
74
75     // if databse does not exist, create it otherwise read it in
76
File JavaDoc dbFile = new File JavaDoc(dbDir, "servers.db");
77     if (!dbFile.exists()) {
78         db = new RepositoryDB(dbFile);
79         db.flush();
80     } else {
81             try {
82                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(dbFile);
83                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
84                 db = (RepositoryDB) ois.readObject();
85                 ois.close();
86             } catch (Exception JavaDoc e) {
87         throw wrapper.cannotReadRepositoryDb( e ) ;
88             }
89     }
90
91     // export the repository
92
orb.connect(this);
93     }
94
95     private String JavaDoc 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         // check if server already registered
114
Enumeration JavaDoc 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         // generate a new server id
132
if (theServerId == illegalServerId)
133             serverId = db.incrementServerIdCounter();
134         else
135         serverId = theServerId;
136      
137         // add server def to the database
138
server = new DBServerDef(serverDef, serverId);
139         db.serverTable.put(new Integer JavaDoc(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     // verify that the entry is valid
160
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 JavaDoc id = new Integer JavaDoc(serverId);
188
189         synchronized (db) {
190
191         // check to see if the server is registered
192
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         // remove server from the database
203
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 JavaDoc id = new Integer JavaDoc(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 JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc() ;
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 JavaDoc applicationName) throws ServerNotRegistered {
300     synchronized (db) {
301         int result = -1 ;
302
303         for (Enumeration JavaDoc serverIds = db.serverTable.keys();
304          serverIds.hasMoreElements();)
305         {
306             Integer JavaDoc nextServerId = (Integer JavaDoc) 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 JavaDoc[] getApplicationNames() {
329     synchronized (db) {
330         Vector JavaDoc v = new Vector JavaDoc();
331         for (Enumeration JavaDoc serverIds = db.serverTable.keys();
332          serverIds.hasMoreElements();)
333         {
334             Integer JavaDoc nextServerId = (Integer JavaDoc) 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 JavaDoc[] apps = new String JavaDoc[v.size()];
344         for (int i = 0; i < v.size(); i++) {
345         apps[i] = (String JavaDoc)v.elementAt(i);
346         }
347
348         if (debug) {
349         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
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     /**
363      * Typically the Repositoy is created within the ORBd VM but it can
364      * be independently started as well.
365      */

366     public static void main(String JavaDoc 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         // See Bug 4396928 for more information about why we are
374
// initializing the ORBClass to PIORB (now ORBImpl, but see the bug).
375
Properties JavaDoc props = new Properties JavaDoc();
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         // create the repository object
381
String JavaDoc db = System.getProperty( ORBConstants.DB_PROPERTY,
382             ORBConstants.DEFAULT_DB_NAME );
383         RepositoryImpl repository = new RepositoryImpl(orb, new File JavaDoc(db),
384                                debug);
385
386         // wait for shutdown
387
orb.run();
388     } catch (Exception JavaDoc 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 JavaDoc
404     {
405     File JavaDoc db;
406     Hashtable JavaDoc serverTable;
407     Integer JavaDoc serverIdCounter;
408
409     RepositoryDB(File JavaDoc dbFile) {
410         
411         db = dbFile;
412
413         // initialize the Server Id counter and hashtable.
414
// the lower id range is reserved for system servers
415
serverTable = new Hashtable JavaDoc(255);
416         serverIdCounter = new Integer JavaDoc(256);
417     }
418
419     int incrementServerIdCounter()
420     {
421         int value = serverIdCounter.intValue();
422         serverIdCounter = new Integer JavaDoc(++value);
423  
424         return value;
425     }
426
427     void flush()
428     {
429         try {
430         db.delete();
431         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(db);
432         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
433         oos.writeObject(this);
434         oos.flush();
435         oos.close();
436         } catch (Exception JavaDoc ex) {
437         throw wrapper.cannotWriteRepositoryDb( ex ) ;
438             }
439     }
440     }
441
442     class DBServerDef implements Serializable JavaDoc
443     {
444     public String JavaDoc 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 JavaDoc applicationName;
465     String JavaDoc name;
466     String JavaDoc classPath;
467     String JavaDoc args;
468     String JavaDoc vmArgs;
469     boolean isInstalled ;
470     int id;
471     }
472 }
473
Popular Tags