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