1 /*
   2  * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.naming.cosnaming;
  27 
  28 // Get CORBA type
  29 import org.omg.CORBA.INITIALIZE;
  30 import org.omg.CORBA.ORB;
  31 import org.omg.CORBA.CompletionStatus;
  32 
  33 import org.omg.CORBA.Policy;
  34 import org.omg.CORBA.INTERNAL;
  35 import org.omg.PortableServer.POA;
  36 import org.omg.PortableServer.LifespanPolicyValue;
  37 import org.omg.PortableServer.RequestProcessingPolicyValue;
  38 import org.omg.PortableServer.IdAssignmentPolicyValue;
  39 import org.omg.PortableServer.ServantRetentionPolicyValue;
  40 
  41 // Get org.omg.CosNaming types
  42 import org.omg.CosNaming.NamingContext;
  43 
  44 // Import transient naming context
  45 import com.sun.corba.se.impl.naming.cosnaming.TransientNamingContext;
  46 import com.sun.corba.se.impl.orbutil.ORBConstants;
  47 
  48 import com.sun.corba.se.spi.logging.CORBALogDomains;
  49 
  50 import com.sun.corba.se.impl.logging.NamingSystemException;
  51 
  52 /**
  53  * Class TransientNameService implements a transient name service
  54  * using TransientNamingContexts and TransientBindingIterators, which
  55  * implement the org.omg.CosNaming::NamingContext and org.omg.CosNaming::BindingIterator
  56  * interfaces specfied by the OMG Common Object Services Specification.
  57  * <p>
  58  * The TransientNameService creates the initial NamingContext object.
  59  * @see NamingContextImpl
  60  * @see BindingIteratorImpl
  61  * @see TransientNamingContext
  62  * @see TransientBindingIterator
  63  */
  64 public class TransientNameService
  65 {
  66     /**
  67      * Constructs a new TransientNameService, and creates an initial
  68      * NamingContext, whose object
  69      * reference can be obtained by the initialNamingContext method.
  70      * @param orb The ORB object
  71      * @exception org.omg.CORBA.INITIALIZE Thrown if
  72      * the TransientNameService cannot initialize.
  73      */
  74     public TransientNameService(com.sun.corba.se.spi.orb.ORB orb )
  75         throws org.omg.CORBA.INITIALIZE
  76     {
  77         // Default constructor uses "NameService" as the key for the Root Naming
  78         // Context. If default constructor is used then INS's object key for
  79         // Transient Name Service is "NameService"
  80         initialize( orb, "NameService" );
  81     }
  82 
  83     /**
  84      * Constructs a new TransientNameService, and creates an initial
  85      * NamingContext, whose object
  86      * reference can be obtained by the initialNamingContext method.
  87      * @param orb The ORB object
  88      * @param nameserviceName Stringified key used for INS Service registry
  89      * @exception org.omg.CORBA.INITIALIZE Thrown if
  90      * the TransientNameService cannot initialize.
  91      */
  92     public TransientNameService(com.sun.corba.se.spi.orb.ORB orb,
  93         String serviceName ) throws org.omg.CORBA.INITIALIZE
  94     {
  95         // This constructor gives the flexibility of providing the Object Key
  96         // for the Root Naming Context that is registered with INS.
  97         initialize( orb, serviceName );
  98     }
  99 
 100 
 101     /**
 102      * This method initializes Transient Name Service by associating Root
 103      * context with POA and registering the root context with INS Object Keymap.
 104      */
 105     private void initialize( com.sun.corba.se.spi.orb.ORB orb,
 106         String nameServiceName )
 107         throws org.omg.CORBA.INITIALIZE
 108     {
 109         NamingSystemException wrapper = NamingSystemException.get( orb,
 110             CORBALogDomains.NAMING ) ;
 111 
 112         try {
 113             POA rootPOA = (POA) orb.resolve_initial_references(
 114                 ORBConstants.ROOT_POA_NAME );
 115             rootPOA.the_POAManager().activate();
 116 
 117             int i = 0;
 118             Policy[] poaPolicy = new Policy[3];
 119             poaPolicy[i++] = rootPOA.create_lifespan_policy(
 120                 LifespanPolicyValue.TRANSIENT);
 121             poaPolicy[i++] = rootPOA.create_id_assignment_policy(
 122                 IdAssignmentPolicyValue.SYSTEM_ID);
 123             poaPolicy[i++] = rootPOA.create_servant_retention_policy(
 124                 ServantRetentionPolicyValue.RETAIN);
 125 
 126             POA nsPOA = rootPOA.create_POA( "TNameService", null, poaPolicy );
 127             nsPOA.the_POAManager().activate();
 128 
 129             // Create an initial context
 130             TransientNamingContext initialContext =
 131                 new TransientNamingContext(orb, null, nsPOA);
 132             byte[] rootContextId = nsPOA.activate_object( initialContext );
 133             initialContext.localRoot =
 134                 nsPOA.id_to_reference( rootContextId );
 135             theInitialNamingContext = initialContext.localRoot;
 136             orb.register_initial_reference( nameServiceName,
 137                 theInitialNamingContext );
 138         } catch (org.omg.CORBA.SystemException e) {
 139             throw wrapper.transNsCannotCreateInitialNcSys( e ) ;
 140         } catch (Exception e) {
 141             throw wrapper.transNsCannotCreateInitialNc( e ) ;
 142         }
 143     }
 144 
 145 
 146     /**
 147      * Return the initial NamingContext.
 148      * @return the object reference for the initial NamingContext.
 149      */
 150     public org.omg.CORBA.Object initialNamingContext()
 151     {
 152         return theInitialNamingContext;
 153     }
 154 
 155 
 156     // The initial naming context for this name service
 157     private org.omg.CORBA.Object theInitialNamingContext;
 158 }