corba/src/share/classes/com/sun/corba/se/impl/naming/pcosnaming/NameService.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) 1999, 2003, 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.naming.pcosnaming;
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.util.Properties;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import org.omg.CORBA.Policy;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import org.omg.PortableServer.POA;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import org.omg.PortableServer.LifespanPolicyValue;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import org.omg.PortableServer.RequestProcessingPolicyValue;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import org.omg.PortableServer.IdAssignmentPolicyValue;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import org.omg.PortableServer.ServantRetentionPolicyValue;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import org.omg.CosNaming.NamingContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import org.omg.CosNaming.NamingContextHelper;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import org.omg.PortableServer.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import com.sun.corba.se.spi.orb.ORB ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import com.sun.corba.se.impl.orbutil.ORBConstants ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
 * @author      Hemanth Puttaswamy
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
 * @since       JDK1.2
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
public class NameService
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
    private NamingContext rootContext = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
    private POA nsPOA = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    private ServantManagerImpl contextMgr;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
    private ORB theorb;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
     * Create NameService which starts the Root Naming Context in Persistent CosNaming
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
     * @param orb an ORB object.
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
     * @param logDir a File
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
     * @exception java.lang.Exception a Java exception.
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    public NameService(ORB orb, File logDir)
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        throws Exception
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
        theorb = orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
        // Moved this to the creation of the ORB that is passed into this
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
        // constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
        // This is required for creating Persistent Servants under this ORB
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
        // Right now the Persistent NameService and ORBD are launched together
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        // Find out a better way of doing this, Since ORBD is an important
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        // process which should not be killed because of some external process
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        // orb.setPersistentServerId( (int) 1000 );
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
        // get and activate the root naming POA
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
        POA rootPOA = (POA)orb.resolve_initial_references(
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
            ORBConstants.ROOT_POA_NAME ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
        rootPOA.the_POAManager().activate();
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        // create a new POA for persistent Naming Contexts
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        // With Non-Retain policy, So that every time Servant Manager
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
        // will be contacted when the reference is made for the context
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        // The id assignment is made by the NameServer, The Naming Context
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
        // id's will be in the format NC<Index>
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
        int i=0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
        Policy[] poaPolicy = new Policy[4];
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
        poaPolicy[i++] = rootPOA.create_lifespan_policy(
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
                         LifespanPolicyValue.PERSISTENT);
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        poaPolicy[i++] = rootPOA.create_request_processing_policy(
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
                         RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        poaPolicy[i++] = rootPOA.create_id_assignment_policy(
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
                         IdAssignmentPolicyValue.USER_ID);
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        poaPolicy[i++] = rootPOA.create_servant_retention_policy(
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
                         ServantRetentionPolicyValue.NON_RETAIN);
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        nsPOA = rootPOA.create_POA("NameService", null, poaPolicy);
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
        nsPOA.the_POAManager().activate( );
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
        // create and set the servant manager
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        contextMgr = new
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
            ServantManagerImpl(orb, logDir, this );
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        // The RootObject key will be NC0
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        String rootKey = contextMgr.getRootObjectKey( );
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        // initialize the root Naming Context
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
        NamingContextImpl nc =
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
                new NamingContextImpl( orb, rootKey, this, contextMgr );
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        nc = contextMgr.addContext( rootKey, nc );
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
        nc.setServantManagerImpl( contextMgr );
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        nc.setORB( orb );
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        nc.setRootNameService( this );
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
        nsPOA.set_servant_manager(contextMgr);
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
        rootContext = NamingContextHelper.narrow(
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
        nsPOA.create_reference_with_id( rootKey.getBytes( ),
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
        NamingContextHelper.id( ) ) );
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
     * This method returns the Root Naming Context
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
    public NamingContext initialNamingContext()
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
        return rootContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
     * This method returns nsPOA which is the only POA that we use for
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
     * Persistent Naming Contexts.
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    POA getNSPOA( ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
        return nsPOA;
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
     * This method  creates a NewContext, This will internally invoked from
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
     * NamingContextImpl. It is not a public API. NewContext is in this class
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
     * because a Persiten reference has to be created with Persistent NameService
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
     * POA.
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
    public NamingContext NewContext( ) throws org.omg.CORBA.SystemException
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
        try
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
                // Get the new Naming Context Key from
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
                // the ServantManager
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
                String newKey =
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
                contextMgr.getNewObjectKey( );
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
                // Create the new Naming context and create the Persistent
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
                // reference
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
                NamingContextImpl theContext =
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
                new NamingContextImpl( theorb, newKey,
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
                    this, contextMgr );
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
                NamingContextImpl tempContext = contextMgr.addContext( newKey,
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
                                                 theContext );
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
                if( tempContext != null )
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
                {
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
                        theContext = tempContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
                // If the context is read from the File, The following three entries
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
                // will be null. So a fresh setup may be required.
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
                theContext.setServantManagerImpl( contextMgr );
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
                theContext.setORB( theorb );
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
                theContext.setRootNameService( this );
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
                NamingContext theNewContext =
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
                NamingContextHelper.narrow(
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
                nsPOA.create_reference_with_id( newKey.getBytes( ),
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
                NamingContextHelper.id( )) );
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
                return theNewContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
        catch( org.omg.CORBA.SystemException e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
                throw e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        catch( java.lang.Exception e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
                //throw e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
     * getObjectReferenceFromKey returns the Object reference from the objectkey using POA.create_reference_with_id method
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
     * @param Object Key as String
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
     * @returns reference an CORBA.Object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
    org.omg.CORBA.Object getObjectReferenceFromKey( String key )
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        org.omg.CORBA.Object theObject = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        try
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
                theObject = nsPOA.create_reference_with_id( key.getBytes( ), NamingContextHelper.id( ) );
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
        catch (Exception e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
                theObject = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
        return theObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
     * getObjectKey gets the Object Key from the reference using POA.reference_to_id method
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
     * @param reference an CORBA.Object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
     * @returns Object Key as String
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
    String getObjectKey( org.omg.CORBA.Object reference )
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        byte theId[];
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
        try
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
                theId = nsPOA.reference_to_id( reference );
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
        catch( org.omg.PortableServer.POAPackage.WrongAdapter e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
                return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
        catch( org.omg.PortableServer.POAPackage.WrongPolicy e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
                return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        catch( Exception e )
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
                return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
        String theKey = new String( theId );
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
        return theKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
}