RepositoryImpl.java revision 608:7e06bf1dcb09
1184610Salfred/*
2184610Salfred * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
3184610Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4184610Salfred *
5230132Suqs * This code is free software; you can redistribute it and/or modify it
6184610Salfred * under the terms of the GNU General Public License version 2 only, as
7184610Salfred * published by the Free Software Foundation.  Oracle designates this
8184610Salfred * particular file as subject to the "Classpath" exception as provided
9184610Salfred * by Oracle in the LICENSE file that accompanied this code.
10184610Salfred *
11184610Salfred * This code is distributed in the hope that it will be useful, but WITHOUT
12184610Salfred * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13184610Salfred * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14184610Salfred * version 2 for more details (a copy is included in the LICENSE file that
15184610Salfred * accompanied this code).
16184610Salfred *
17184610Salfred * You should have received a copy of the GNU General Public License version
18184610Salfred * 2 along with this work; if not, write to the Free Software Foundation,
19184610Salfred * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20184610Salfred *
21184610Salfred * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22184610Salfred * or visit www.oracle.com if you need additional information or have any
23184610Salfred * questions.
24184610Salfred */
25184610Salfred
26184610Salfredpackage com.sun.corba.se.impl.activation;
27184610Salfred
28184610Salfredimport java.io.File;
29184610Salfredimport java.io.FileInputStream;
30184610Salfredimport java.io.FileOutputStream;
31184610Salfredimport java.io.ObjectInputStream;
32184610Salfredimport java.io.ObjectOutputStream;
33184610Salfredimport java.io.Serializable;
34184610Salfredimport java.util.Properties;
35184610Salfredimport java.util.Hashtable;
36184610Salfredimport java.util.Enumeration;
37194677Sthompsaimport java.util.Vector;
38194677Sthompsa
39194677Sthompsaimport org.omg.CORBA.CompletionStatus;
40194677Sthompsaimport org.omg.CORBA.INITIALIZE;
41194677Sthompsaimport org.omg.CORBA.INTERNAL;
42194677Sthompsaimport org.omg.CORBA.SystemException;
43194677Sthompsa
44194677Sthompsaimport com.sun.corba.se.spi.activation.BadServerDefinition;
45194677Sthompsaimport com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
46194677Sthompsaimport com.sun.corba.se.spi.activation._RepositoryImplBase;
47194677Sthompsaimport com.sun.corba.se.spi.activation.ServerAlreadyRegistered;
48194677Sthompsaimport com.sun.corba.se.spi.activation.ServerAlreadyInstalled;
49194677Sthompsaimport com.sun.corba.se.spi.activation.ServerAlreadyUninstalled;
50194677Sthompsaimport com.sun.corba.se.spi.activation.ServerNotRegistered;
51194677Sthompsaimport com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
52194677Sthompsaimport com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
53194677Sthompsaimport com.sun.corba.se.spi.orb.ORB;
54194677Sthompsaimport com.sun.corba.se.impl.orbutil.ORBConstants;
55194677Sthompsa
56188942Sthompsaimport com.sun.corba.se.spi.logging.CORBALogDomains;
57194677Sthompsaimport com.sun.corba.se.impl.logging.ActivationSystemException;
58194677Sthompsa
59188942Sthompsa/**
60194677Sthompsa *
61184610Salfred * @author      Rohit Garg
62194228Sthompsa * @since       JDK1.2
63188942Sthompsa */
64188942Sthompsapublic class RepositoryImpl extends _RepositoryImplBase
65184610Salfred    implements Serializable
66188942Sthompsa{
67184610Salfred
68184610Salfred    // added serialver computed by the tool
69184610Salfred    private static final long serialVersionUID = 8458417785209341858L;
70184610Salfred
71184610Salfred    RepositoryImpl(ORB orb, File dbDir, boolean debug)
72187259Sthompsa    {
73187259Sthompsa        this.debug = debug ;
74187259Sthompsa        this.orb = orb;
75188413Sthompsa        wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_REPOSITORY ) ;
76187259Sthompsa
77187259Sthompsa        // if databse does not exist, create it otherwise read it in
78184610Salfred        File dbFile = new File(dbDir, "servers.db");
79192984Sthompsa        if (!dbFile.exists()) {
80192984Sthompsa            db = new RepositoryDB(dbFile);
81184610Salfred            db.flush();
82192984Sthompsa        } else {
83192984Sthompsa            try {
84189265Sthompsa                FileInputStream fis = new FileInputStream(dbFile);
85184610Salfred                ObjectInputStream ois = new ObjectInputStream(fis);
86184610Salfred                db = (RepositoryDB) ois.readObject();
87184610Salfred                ois.close();
88184610Salfred            } catch (Exception e) {
89184610Salfred                throw wrapper.cannotReadRepositoryDb( e ) ;
90184610Salfred            }
91184610Salfred        }
92184610Salfred
93184610Salfred        // export the repository
94184610Salfred        orb.connect(this);
95184610Salfred    }
96184610Salfred
97184610Salfred    private String printServerDef( ServerDef sd )
98184610Salfred    {
99184610Salfred        return "ServerDef[applicationName=" + sd.applicationName +
100184610Salfred            " serverName=" + sd.serverName +
101184610Salfred            " serverClassPath=" + sd.serverClassPath +
102184610Salfred            " serverArgs=" + sd. serverArgs +
103184610Salfred            " serverVmArgs=" + sd.serverVmArgs +
104184610Salfred            "]" ;
105184610Salfred    }
106184610Salfred
107184610Salfred    public int registerServer(ServerDef serverDef, int theServerId)
108184610Salfred        throws ServerAlreadyRegistered
109184610Salfred    {
110184610Salfred        int         serverId;
111184610Salfred        DBServerDef server = null;
112184610Salfred
113239299Shselasky        synchronized (db) {
114184610Salfred
115193045Sthompsa            // check if server already registered
116193045Sthompsa            Enumeration enumeration = db.serverTable.elements();
117184610Salfred            while (enumeration.hasMoreElements()) {
118239180Shselasky                server = (DBServerDef) enumeration.nextElement();
119192984Sthompsa                if (serverDef.applicationName.equals(server.applicationName)) {
120192984Sthompsa                    if (debug)
121192984Sthompsa                        System.out.println(
122192984Sthompsa                            "RepositoryImpl: registerServer called " +
123192984Sthompsa                            "to register ServerDef " +
124185948Sthompsa                            printServerDef( serverDef ) +
125192984Sthompsa                            " with " + ((theServerId==illegalServerId) ?
126192984Sthompsa                        "a new server Id" : ("server Id " + theServerId)) +
127197570Sthompsa                                           " FAILED because it is already registered." ) ;
128184610Salfred
129192984Sthompsa                    throw (new ServerAlreadyRegistered(server.id));
130184610Salfred                }
131187259Sthompsa            }
132184610Salfred
133184610Salfred            // generate a new server id
134184610Salfred            if (theServerId == illegalServerId)
135192984Sthompsa                serverId = db.incrementServerIdCounter();
136190734Sthompsa            else
137190734Sthompsa                serverId = theServerId;
138184610Salfred
139184610Salfred            // add server def to the database
140187259Sthompsa            server = new DBServerDef(serverDef, serverId);
141184610Salfred            db.serverTable.put(new Integer(serverId), server);
142184610Salfred            db.flush();
143184610Salfred
144190734Sthompsa            if (debug)
145190734Sthompsa                if (theServerId==illegalServerId)
146190734Sthompsa                    System.out.println( "RepositoryImpl: registerServer called " +
147184610Salfred                                        "to register ServerDef " + printServerDef( serverDef ) +
148184610Salfred                                        " with new serverId " + serverId ) ;
149184610Salfred                else
150192984Sthompsa                    System.out.println( "RepositoryImpl: registerServer called " +
151194228Sthompsa                                        "to register ServerDef " + printServerDef( serverDef ) +
152194228Sthompsa                                        " with assigned serverId " + serverId ) ;
153194228Sthompsa
154194228Sthompsa            return serverId;
155194228Sthompsa        }
156194228Sthompsa    }
157194228Sthompsa
158197570Sthompsa    public int registerServer(ServerDef serverDef)
159239180Shselasky        throws ServerAlreadyRegistered, BadServerDefinition
160184610Salfred    {
161184610Salfred        // verify that the entry is valid
162184610Salfred        LegacyServerSocketEndPointInfo endpoint =
163184610Salfred            orb.getLegacyServerSocketManager()
164184610Salfred                .legacyGetEndpoint(LegacyServerSocketEndPointInfo.BOOT_NAMING);
165184610Salfred        int initSvcPort = ((SocketOrChannelAcceptor)endpoint)
166239180Shselasky            .getServerSocket().getLocalPort();
167184610Salfred        ServerTableEntry entry = new ServerTableEntry( wrapper,
168184610Salfred            illegalServerId, serverDef, (int) initSvcPort, "", true, debug );
169184610Salfred
170184610Salfred        switch (entry.verify()) {
171184610Salfred        case ServerMain.OK:
172184610Salfred            break;
173184610Salfred        case ServerMain.MAIN_CLASS_NOT_FOUND:
174184610Salfred            throw new BadServerDefinition("main class not found.");
175184610Salfred        case ServerMain.NO_MAIN_METHOD:
176184610Salfred            throw new BadServerDefinition("no main method found.");
177189275Sthompsa        case ServerMain.APPLICATION_ERROR:
178188942Sthompsa            throw new BadServerDefinition("server application error.");
179188942Sthompsa        default:
180212122Sthompsa            throw new BadServerDefinition("unknown Exception.");
181184610Salfred        }
182184610Salfred
183184610Salfred        return registerServer(serverDef, illegalServerId);
184184610Salfred    }
185223486Shselasky
186184610Salfred    public void unregisterServer(int serverId) throws ServerNotRegistered {
187184610Salfred
188184610Salfred        DBServerDef server = null;
189184610Salfred        Integer id = new Integer(serverId);
190184610Salfred
191184610Salfred        synchronized (db) {
192184610Salfred
193184610Salfred            // check to see if the server is registered
194184610Salfred            server = (DBServerDef) db.serverTable.get(id);
195192984Sthompsa            if (server == null)  {
196184610Salfred                if (debug)
197192499Sthompsa                    System.out.println(
198184610Salfred                                       "RepositoryImpl: unregisterServer for serverId " +
199184610Salfred                                       serverId + " called: server not registered" ) ;
200184610Salfred
201184610Salfred                throw (new ServerNotRegistered());
202184610Salfred            }
203184610Salfred
204184610Salfred            // remove server from the database
205184610Salfred            db.serverTable.remove(id);
206194228Sthompsa            db.flush();
207184610Salfred        }
208184610Salfred
209184610Salfred        if (debug)
210184610Salfred            System.out.println(
211184610Salfred                               "RepositoryImpl: unregisterServer for serverId " + serverId +
212192984Sthompsa                               " called" ) ;
213184610Salfred    }
214184610Salfred
215184610Salfred    private DBServerDef getDBServerDef(int serverId) throws ServerNotRegistered
216184610Salfred    {
217184610Salfred        Integer id = new Integer(serverId);
218184610Salfred        DBServerDef server = (DBServerDef) db.serverTable.get(id);
219184610Salfred
220184610Salfred        if (server == null)
221194228Sthompsa            throw new ServerNotRegistered( serverId );
222189265Sthompsa
223239180Shselasky        return server ;
224184610Salfred    }
225184610Salfred
226184610Salfred    public ServerDef getServer(int serverId) throws ServerNotRegistered
227184610Salfred    {
228184610Salfred        DBServerDef server = getDBServerDef( serverId ) ;
229184610Salfred
230184610Salfred        ServerDef serverDef = new ServerDef(server.applicationName, server.name,
231184610Salfred                                            server.classPath, server.args, server.vmArgs);
232184610Salfred
233184610Salfred        if (debug)
234184610Salfred            System.out.println(
235184610Salfred                               "RepositoryImpl: getServer for serverId " + serverId +
236184610Salfred                               " returns " + printServerDef( serverDef ) ) ;
237194228Sthompsa
238184610Salfred        return serverDef;
239184610Salfred    }
240184610Salfred
241184610Salfred    public boolean isInstalled(int serverId) throws ServerNotRegistered {
242184610Salfred        DBServerDef server = getDBServerDef( serverId ) ;
243184610Salfred        return server.isInstalled ;
244194228Sthompsa    }
245184610Salfred
246184610Salfred    public void install( int serverId )
247184610Salfred        throws ServerNotRegistered, ServerAlreadyInstalled
248184610Salfred    {
249184610Salfred        DBServerDef server = getDBServerDef( serverId ) ;
250184610Salfred
251184610Salfred        if (server.isInstalled)
252184610Salfred            throw new ServerAlreadyInstalled( serverId ) ;
253184610Salfred        else {
254184610Salfred            server.isInstalled = true ;
255184610Salfred            db.flush() ;
256184610Salfred        }
257184610Salfred    }
258184610Salfred
259184610Salfred    public void uninstall( int serverId )
260184610Salfred        throws ServerNotRegistered, ServerAlreadyUninstalled
261184610Salfred    {
262184610Salfred        DBServerDef server = getDBServerDef( serverId ) ;
263184610Salfred
264194228Sthompsa        if (!server.isInstalled)
265187259Sthompsa            throw new ServerAlreadyUninstalled( serverId ) ;
266189265Sthompsa        else {
267184610Salfred            server.isInstalled = false ;
268184610Salfred            db.flush() ;
269199816Sthompsa        }
270184610Salfred    }
271184610Salfred
272194228Sthompsa    public int[] listRegisteredServers() {
273189265Sthompsa        synchronized (db) {
274184610Salfred            int i=0;
275184610Salfred
276184610Salfred            int servers[] = new int[db.serverTable.size()];
277214843Sn_hibma
278214843Sn_hibma            Enumeration enumeration = db.serverTable.elements();
279184610Salfred
280184610Salfred            while (enumeration.hasMoreElements()) {
281184610Salfred                DBServerDef server = (DBServerDef) enumeration.nextElement();
282214843Sn_hibma                servers[i++] = server.id;
283184610Salfred            }
284184610Salfred
285184610Salfred            if (debug) {
286184610Salfred                StringBuffer sb = new StringBuffer() ;
287184610Salfred                for (int ctr=0; ctr<servers.length; ctr++) {
288184610Salfred                    sb.append( ' ' ) ;
289184610Salfred                    sb.append( servers[ctr] ) ;
290184610Salfred                }
291184610Salfred
292184610Salfred                System.out.println(
293184610Salfred                                   "RepositoryImpl: listRegisteredServers returns" +
294184610Salfred                                   sb.toString() ) ;
295184610Salfred            }
296184610Salfred
297184610Salfred            return servers;
298214761Sn_hibma        }
299194228Sthompsa    }
300184610Salfred
301239299Shselasky    public int getServerID(String applicationName) throws ServerNotRegistered {
302239299Shselasky        synchronized (db) {
303239299Shselasky            int result = -1 ;
304239299Shselasky
305184610Salfred            for (Enumeration serverIds = db.serverTable.keys();
306184610Salfred                 serverIds.hasMoreElements();)
307184610Salfred                {
308239180Shselasky                    Integer nextServerId = (Integer) serverIds.nextElement();
309239180Shselasky                    DBServerDef dbServerDef =
310184610Salfred                        (DBServerDef) db.serverTable.get(nextServerId);
311239299Shselasky
312239180Shselasky                    if (dbServerDef.applicationName.equals(applicationName)) {
313239180Shselasky                        result = nextServerId.intValue();
314239299Shselasky                        break ;
315239299Shselasky                    }
316239180Shselasky                }
317239180Shselasky
318239180Shselasky            if (debug)
319239180Shselasky                System.out.println("RepositoryImpl: getServerID for " +
320239180Shselasky                                   applicationName + " is " + result ) ;
321239180Shselasky
322239299Shselasky            if (result == -1) {
323239180Shselasky                throw (new ServerNotRegistered());
324239180Shselasky            } else {
325239180Shselasky                return result ;
326192984Sthompsa            }
327184610Salfred        }
328184610Salfred    }
329184610Salfred
330184610Salfred    public String[] getApplicationNames() {
331184610Salfred        synchronized (db) {
332184610Salfred            Vector v = new Vector();
333184610Salfred            for (Enumeration serverIds = db.serverTable.keys();
334184610Salfred                 serverIds.hasMoreElements();)
335192984Sthompsa                {
336184610Salfred                    Integer nextServerId = (Integer) serverIds.nextElement();
337184610Salfred
338184610Salfred                    DBServerDef dbServerDef = (DBServerDef)db.serverTable.get(
339194228Sthompsa                                                                              nextServerId);
340184610Salfred
341184610Salfred                    if (!dbServerDef.applicationName.equals(""))
342184610Salfred                        v.addElement( dbServerDef.applicationName ) ;
343192984Sthompsa                }
344184610Salfred
345184610Salfred            String[] apps = new String[v.size()];
346184610Salfred            for (int i = 0; i < v.size(); i++) {
347194228Sthompsa                apps[i] = (String)v.elementAt(i);
348184610Salfred            }
349184610Salfred
350184610Salfred            if (debug) {
351192984Sthompsa                StringBuffer sb = new StringBuffer() ;
352184610Salfred                for (int ctr=0; ctr<apps.length; ctr++) {
353184610Salfred                    sb.append( ' ' ) ;
354184610Salfred                    sb.append( apps[ctr] ) ;
355194228Sthompsa                }
356184610Salfred
357184610Salfred                System.out.println( "RepositoryImpl: getApplicationNames returns " +
358184610Salfred                                    sb.toString() ) ;
359192984Sthompsa            }
360184610Salfred
361184610Salfred            return apps;
362184610Salfred        }
363194228Sthompsa    }
364184610Salfred    /**
365184610Salfred     * Typically the Repositoy is created within the ORBd VM but it can
366184610Salfred     * be independently started as well.
367194677Sthompsa     */
368184610Salfred    public static void main(String args[]) {
369194677Sthompsa        boolean debug = false ;
370192984Sthompsa        for (int ctr=0; ctr<args.length; ctr++)
371194677Sthompsa            if (args[ctr].equals("-debug"))
372184610Salfred                debug = true ;
373184610Salfred
374184610Salfred        try {
375184610Salfred            // See Bug 4396928 for more information about why we are
376194677Sthompsa            // initializing the ORBClass to PIORB (now ORBImpl, but see the bug).
377194677Sthompsa            Properties props = new Properties();
378194677Sthompsa            props.put("org.omg.CORBA.ORBClass",
379184610Salfred                "com.sun.corba.se.impl.orb.ORBImpl");
380184610Salfred            ORB orb = (ORB) ORB.init(args, props);
381184610Salfred
382184610Salfred            // create the repository object
383184610Salfred            String db = System.getProperty( ORBConstants.DB_PROPERTY,
384184610Salfred                    ORBConstants.DEFAULT_DB_NAME );
385184610Salfred            RepositoryImpl repository = new RepositoryImpl(orb, new File(db),
386184610Salfred                                                           debug);
387184610Salfred
388184610Salfred            // wait for shutdown
389184610Salfred            orb.run();
390184610Salfred        } catch (Exception ex) {
391184610Salfred            ex.printStackTrace();
392184610Salfred        }
393184610Salfred    }
394184610Salfred
395184610Salfred    transient private boolean debug = false;
396194677Sthompsa
397184610Salfred    final static int illegalServerId = -1;
398184610Salfred
399184610Salfred    transient private RepositoryDB db = null;
400184610Salfred
401184610Salfred    transient ORB orb = null;
402184610Salfred
403184610Salfred    transient ActivationSystemException wrapper ;
404184610Salfred
405184610Salfred    class RepositoryDB implements Serializable
406184610Salfred    {
407184610Salfred        File            db;
408184610Salfred        Hashtable       serverTable;
409184610Salfred        Integer         serverIdCounter;
410184610Salfred
411184610Salfred        RepositoryDB(File dbFile) {
412184610Salfred
413184610Salfred            db = dbFile;
414184610Salfred
415184610Salfred            // initialize the Server Id counter and hashtable.
416184610Salfred            // the lower id range is reserved for system servers
417184610Salfred            serverTable     = new Hashtable(255);
418194677Sthompsa            serverIdCounter = new Integer(256);
419194677Sthompsa        }
420184610Salfred
421194677Sthompsa        int incrementServerIdCounter()
422194677Sthompsa        {
423194677Sthompsa            int value = serverIdCounter.intValue();
424194228Sthompsa            serverIdCounter = new Integer(++value);
425184610Salfred
426184610Salfred            return value;
427184610Salfred        }
428184610Salfred
429194677Sthompsa        void flush()
430184610Salfred        {
431184610Salfred            try {
432184610Salfred                db.delete();
433194677Sthompsa                FileOutputStream fos = new FileOutputStream(db);
434184610Salfred                ObjectOutputStream oos = new ObjectOutputStream(fos);
435184610Salfred                oos.writeObject(this);
436184610Salfred                oos.flush();
437184610Salfred                oos.close();
438184610Salfred            } catch (Exception ex) {
439184610Salfred                throw wrapper.cannotWriteRepositoryDb( ex ) ;
440184610Salfred            }
441192984Sthompsa        }
442184610Salfred    }
443193045Sthompsa
444184610Salfred    class DBServerDef implements Serializable
445184610Salfred    {
446184610Salfred        public String toString() {
447184610Salfred            return "DBServerDef(applicationName=" + applicationName +
448184610Salfred                ", name=" + name +
449184610Salfred                ", classPath=" + classPath +
450184610Salfred                ", args=" + args +
451184610Salfred                ", vmArgs=" + vmArgs +
452184610Salfred                ", id=" + id +
453184610Salfred                ", isInstalled=" + isInstalled + ")" ;
454184610Salfred        }
455184610Salfred
456184610Salfred        DBServerDef(ServerDef server, int server_id) {
457184610Salfred            applicationName     = server.applicationName ;
458184610Salfred            name        = server.serverName;
459184610Salfred            classPath   = server.serverClassPath;
460184610Salfred            args        = server.serverArgs;
461184610Salfred            vmArgs      = server.serverVmArgs;
462184610Salfred            id          = server_id;
463184610Salfred            isInstalled = false ;
464194228Sthompsa        }
465188413Sthompsa
466184610Salfred        String applicationName;
467184610Salfred        String name;
468194228Sthompsa        String classPath;
469184610Salfred        String args;
470184610Salfred        String vmArgs;
471184610Salfred        boolean isInstalled ;
472184610Salfred        int    id;
473192984Sthompsa    }
474184610Salfred}
475184610Salfred