corba/src/share/classes/com/sun/corba/se/impl/activation/RepositoryImpl.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.activation;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.io.File;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import java.io.FileInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.io.FileOutputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.util.Properties;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.util.Enumeration;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import org.omg.CORBA.CompletionStatus;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import org.omg.CORBA.INITIALIZE;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import org.omg.CORBA.INTERNAL;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import org.omg.CORBA.SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import com.sun.corba.se.spi.activation.BadServerDefinition;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import com.sun.corba.se.spi.activation._RepositoryImplBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import com.sun.corba.se.spi.activation.ServerAlreadyRegistered;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import com.sun.corba.se.spi.activation.ServerAlreadyInstalled;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import com.sun.corba.se.spi.activation.ServerAlreadyUninstalled;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import com.sun.corba.se.spi.activation.ServerNotRegistered;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import com.sun.corba.se.spi.orb.ORB;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
import com.sun.corba.se.impl.orbutil.ORBConstants;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
import com.sun.corba.se.spi.logging.CORBALogDomains;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
import com.sun.corba.se.impl.logging.ActivationSystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
 * @author      Rohit Garg
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
 * @since       JDK1.2
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
public class RepositoryImpl extends _RepositoryImplBase
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
    implements Serializable
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    // added serialver computed by the tool
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    private static final long serialVersionUID = 8458417785209341858L;
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    RepositoryImpl(ORB orb, File dbDir, boolean debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        this.debug = debug ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        this.orb = orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        wrapper = ActivationSystemException.get( orb, CORBALogDomains.ORBD_REPOSITORY ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
        // if databse does not exist, create it otherwise read it in
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
        File dbFile = new File(dbDir, "servers.db");
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
        if (!dbFile.exists()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
            db = new RepositoryDB(dbFile);
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
            db.flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
                FileInputStream fis = new FileInputStream(dbFile);
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
                ObjectInputStream ois = new ObjectInputStream(fis);
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
                db = (RepositoryDB) ois.readObject();
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
                ois.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
            } catch (Exception e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
                throw wrapper.cannotReadRepositoryDb( e ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        // export the repository
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        orb.connect(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
    private String printServerDef( ServerDef sd )
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        return "ServerDef[applicationName=" + sd.applicationName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
            " serverName=" + sd.serverName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
            " serverClassPath=" + sd.serverClassPath +
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
            " serverArgs=" + sd. serverArgs +
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
            " serverVmArgs=" + sd.serverVmArgs +
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
            "]" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
    public int registerServer(ServerDef serverDef, int theServerId)
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        throws ServerAlreadyRegistered
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
        int         serverId;
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        DBServerDef server = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        synchronized (db) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            // check if server already registered
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
            Enumeration enumeration = db.serverTable.elements();
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            while (enumeration.hasMoreElements()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
                server = (DBServerDef) enumeration.nextElement();
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
                if (serverDef.applicationName.equals(server.applicationName)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
                    if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
                        System.out.println(
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
                            "RepositoryImpl: registerServer called " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
                            "to register ServerDef " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
                            printServerDef( serverDef ) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
                            " with " + ((theServerId==illegalServerId) ?
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
                        "a new server Id" : ("server Id " + theServerId)) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
                                           " FAILED because it is already registered." ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
                    throw (new ServerAlreadyRegistered(server.id));
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
            // generate a new server id
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
            if (theServerId == illegalServerId)
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
                serverId = db.incrementServerIdCounter();
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
            else
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
                serverId = theServerId;
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
            // add server def to the database
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
            server = new DBServerDef(serverDef, serverId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
            db.serverTable.put(new Integer(serverId), server);
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
            db.flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
            if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
                if (theServerId==illegalServerId)
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
                    System.out.println( "RepositoryImpl: registerServer called " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
                                        "to register ServerDef " + printServerDef( serverDef ) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
                                        " with new serverId " + serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
                else
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
                    System.out.println( "RepositoryImpl: registerServer called " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
                                        "to register ServerDef " + printServerDef( serverDef ) +
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
                                        " with assigned serverId " + serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
            return serverId;
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
    public int registerServer(ServerDef serverDef)
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
        throws ServerAlreadyRegistered, BadServerDefinition
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        // verify that the entry is valid
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
        LegacyServerSocketEndPointInfo endpoint =
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
            orb.getLegacyServerSocketManager()
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
                .legacyGetEndpoint(LegacyServerSocketEndPointInfo.BOOT_NAMING);
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
        int initSvcPort = ((SocketOrChannelAcceptor)endpoint)
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
            .getServerSocket().getLocalPort();
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
        ServerTableEntry entry = new ServerTableEntry( wrapper,
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
            illegalServerId, serverDef, (int) initSvcPort, "", true, debug );
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
        switch (entry.verify()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
        case ServerMain.OK:
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
            break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
        case ServerMain.MAIN_CLASS_NOT_FOUND:
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
            throw new BadServerDefinition("main class not found.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
        case ServerMain.NO_MAIN_METHOD:
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
            throw new BadServerDefinition("no main method found.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
        case ServerMain.APPLICATION_ERROR:
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
            throw new BadServerDefinition("server application error.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        default:
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
            throw new BadServerDefinition("unknown Exception.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        return registerServer(serverDef, illegalServerId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    public void unregisterServer(int serverId) throws ServerNotRegistered {
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        DBServerDef server = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
        Integer id = new Integer(serverId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        synchronized (db) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
            // check to see if the server is registered
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
            server = (DBServerDef) db.serverTable.get(id);
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
            if (server == null)  {
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
                if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
                    System.out.println(
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
                                       "RepositoryImpl: unregisterServer for serverId " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
                                       serverId + " called: server not registered" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
                throw (new ServerNotRegistered());
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
            // remove server from the database
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
            db.serverTable.remove(id);
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
            db.flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
        if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
            System.out.println(
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
                               "RepositoryImpl: unregisterServer for serverId " + serverId +
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
                               " called" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
    private DBServerDef getDBServerDef(int serverId) throws ServerNotRegistered
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
        Integer id = new Integer(serverId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        DBServerDef server = (DBServerDef) db.serverTable.get(id);
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
        if (server == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
            throw new ServerNotRegistered( serverId );
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
        return server ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
    public ServerDef getServer(int serverId) throws ServerNotRegistered
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
        DBServerDef server = getDBServerDef( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
        ServerDef serverDef = new ServerDef(server.applicationName, server.name,
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
                                            server.classPath, server.args, server.vmArgs);
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
        if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
            System.out.println(
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
                               "RepositoryImpl: getServer for serverId " + serverId +
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
                               " returns " + printServerDef( serverDef ) ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
        return serverDef;
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
    public boolean isInstalled(int serverId) throws ServerNotRegistered {
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
        DBServerDef server = getDBServerDef( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
        return server.isInstalled ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
    public void install( int serverId )
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
        throws ServerNotRegistered, ServerAlreadyInstalled
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        DBServerDef server = getDBServerDef( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
        if (server.isInstalled)
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
            throw new ServerAlreadyInstalled( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
            server.isInstalled = true ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
            db.flush() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
    public void uninstall( int serverId )
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
        throws ServerNotRegistered, ServerAlreadyUninstalled
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
        DBServerDef server = getDBServerDef( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        if (!server.isInstalled)
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
            throw new ServerAlreadyUninstalled( serverId ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
        else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
            server.isInstalled = false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
            db.flush() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
    public int[] listRegisteredServers() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
        synchronized (db) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
            int i=0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
            int servers[] = new int[db.serverTable.size()];
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
            Enumeration enumeration = db.serverTable.elements();
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
            while (enumeration.hasMoreElements()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
                DBServerDef server = (DBServerDef) enumeration.nextElement();
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
                servers[i++] = server.id;
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
            if (debug) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
                StringBuffer sb = new StringBuffer() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
                for (int ctr=0; ctr<servers.length; ctr++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
                    sb.append( ' ' ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
                    sb.append( servers[ctr] ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
                System.out.println(
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
                                   "RepositoryImpl: listRegisteredServers returns" +
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
                                   sb.toString() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
            return servers;
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
    public int getServerID(String applicationName) throws ServerNotRegistered {
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
        synchronized (db) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
            int result = -1 ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
            for (Enumeration serverIds = db.serverTable.keys();
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
                 serverIds.hasMoreElements();)
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
                {
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
                    Integer nextServerId = (Integer) serverIds.nextElement();
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
                    DBServerDef dbServerDef =
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
                        (DBServerDef) db.serverTable.get(nextServerId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
                    if (dbServerDef.applicationName.equals(applicationName)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
                        result = nextServerId.intValue();
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
                        break ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
            if (debug)
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
                System.out.println("RepositoryImpl: getServerID for " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
                                   applicationName + " is " + result ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
            if (result == -1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
                throw (new ServerNotRegistered());
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
                return result ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
    public String[] getApplicationNames() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
        synchronized (db) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
            Vector v = new Vector();
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
            for (Enumeration serverIds = db.serverTable.keys();
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
                 serverIds.hasMoreElements();)
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
                {
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
                    Integer nextServerId = (Integer) serverIds.nextElement();
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                    DBServerDef dbServerDef = (DBServerDef)db.serverTable.get(
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
                                                                              nextServerId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
                    if (!dbServerDef.applicationName.equals(""))
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
                        v.addElement( dbServerDef.applicationName ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
            String[] apps = new String[v.size()];
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
            for (int i = 0; i < v.size(); i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
                apps[i] = (String)v.elementAt(i);
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
            if (debug) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
                StringBuffer sb = new StringBuffer() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
                for (int ctr=0; ctr<apps.length; ctr++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
                    sb.append( ' ' ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
                    sb.append( apps[ctr] ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
                System.out.println( "RepositoryImpl: getApplicationNames returns " +
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
                                    sb.toString() ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
            return apps;
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
     * Typically the Repositoy is created within the ORBd VM but it can
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
     * be independently started as well.
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
    public static void main(String args[]) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
        boolean debug = false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
        for (int ctr=0; ctr<args.length; ctr++)
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
            if (args[ctr].equals("-debug"))
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
                debug = true ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
            // See Bug 4396928 for more information about why we are
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
            // initializing the ORBClass to PIORB (now ORBImpl, but see the bug).
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
            Properties props = new Properties();
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
            props.put("org.omg.CORBA.ORBClass",
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
                "com.sun.corba.se.impl.orb.ORBImpl");
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
            ORB orb = (ORB) ORB.init(args, props);
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
            // create the repository object
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
            String db = System.getProperty( ORBConstants.DB_PROPERTY,
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
                    ORBConstants.DEFAULT_DB_NAME );
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
            RepositoryImpl repository = new RepositoryImpl(orb, new File(db),
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
                                                           debug);
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
            // wait for shutdown
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
            orb.run();
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
        } catch (Exception ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
            ex.printStackTrace();
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
    transient private boolean debug = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
    final static int illegalServerId = -1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
    transient private RepositoryDB db = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
    transient ORB orb = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
    transient ActivationSystemException wrapper ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
    class RepositoryDB implements Serializable
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
        File            db;
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
        Hashtable       serverTable;
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
        Integer         serverIdCounter;
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
        RepositoryDB(File dbFile) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
            db = dbFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
            // initialize the Server Id counter and hashtable.
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
            // the lower id range is reserved for system servers
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
            serverTable     = new Hashtable(255);
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
            serverIdCounter = new Integer(256);
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
        int incrementServerIdCounter()
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
            int value = serverIdCounter.intValue();
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
            serverIdCounter = new Integer(++value);
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
            return value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
        void flush()
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
                db.delete();
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
                FileOutputStream fos = new FileOutputStream(db);
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
                ObjectOutputStream oos = new ObjectOutputStream(fos);
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
                oos.writeObject(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
                oos.flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
   437
                oos.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   438
            } catch (Exception ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   439
                throw wrapper.cannotWriteRepositoryDb( ex ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   440
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   441
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   442
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   443
02bb8761fcce Initial load
duke
parents:
diff changeset
   444
    class DBServerDef implements Serializable
02bb8761fcce Initial load
duke
parents:
diff changeset
   445
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   446
        public String toString() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   447
            return "DBServerDef(applicationName=" + applicationName +
02bb8761fcce Initial load
duke
parents:
diff changeset
   448
                ", name=" + name +
02bb8761fcce Initial load
duke
parents:
diff changeset
   449
                ", classPath=" + classPath +
02bb8761fcce Initial load
duke
parents:
diff changeset
   450
                ", args=" + args +
02bb8761fcce Initial load
duke
parents:
diff changeset
   451
                ", vmArgs=" + vmArgs +
02bb8761fcce Initial load
duke
parents:
diff changeset
   452
                ", id=" + id +
02bb8761fcce Initial load
duke
parents:
diff changeset
   453
                ", isInstalled=" + isInstalled + ")" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   454
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   455
02bb8761fcce Initial load
duke
parents:
diff changeset
   456
        DBServerDef(ServerDef server, int server_id) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   457
            applicationName     = server.applicationName ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   458
            name        = server.serverName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   459
            classPath   = server.serverClassPath;
02bb8761fcce Initial load
duke
parents:
diff changeset
   460
            args        = server.serverArgs;
02bb8761fcce Initial load
duke
parents:
diff changeset
   461
            vmArgs      = server.serverVmArgs;
02bb8761fcce Initial load
duke
parents:
diff changeset
   462
            id          = server_id;
02bb8761fcce Initial load
duke
parents:
diff changeset
   463
            isInstalled = false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   464
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   465
02bb8761fcce Initial load
duke
parents:
diff changeset
   466
        String applicationName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   467
        String name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   468
        String classPath;
02bb8761fcce Initial load
duke
parents:
diff changeset
   469
        String args;
02bb8761fcce Initial load
duke
parents:
diff changeset
   470
        String vmArgs;
02bb8761fcce Initial load
duke
parents:
diff changeset
   471
        boolean isInstalled ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   472
        int    id;
02bb8761fcce Initial load
duke
parents:
diff changeset
   473
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   474
}