KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > pcosnaming > NameService


1 /*
2  * @(#)NameService.java 1.10 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * @(#)NameService.java 1.3 00/04/06
9  *
10  * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
11  * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
12  *
13  * This software is the confidential and proprietary information of Sun
14  * Microsystems, Inc. ("Confidential Information"). You shall not
15  * disclose such Confidential Information and shall use it only in
16  * accordance with the terms of the license agreement you entered into
17  * with Sun.
18  *
19  * CopyrightVersion 1.2
20  *
21  */

22
23 package com.sun.corba.se.impl.naming.pcosnaming;
24
25 import java.io.File JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import org.omg.CORBA.Policy JavaDoc;
29 import org.omg.PortableServer.POA JavaDoc;
30 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
31 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
32 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
33 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
34 import org.omg.CosNaming.NamingContext JavaDoc;
35 import org.omg.CosNaming.NamingContextHelper JavaDoc;
36 import org.omg.PortableServer.*;
37
38 import com.sun.corba.se.spi.orb.ORB ;
39
40 import com.sun.corba.se.impl.orbutil.ORBConstants ;
41
42 /**
43  * @version 1.3, 00/04/06
44  * @author Hemanth Puttaswamy
45  * @since JDK1.2
46  */

47
48 public class NameService
49 {
50     private NamingContext JavaDoc rootContext = null;
51     private POA JavaDoc nsPOA = null;
52     private ServantManagerImpl contextMgr;
53     private ORB theorb;
54
55     /**
56      * Create NameService which starts the Root Naming Context in Persistent CosNaming
57      * @param orb an ORB object.
58      * @param logDir a File
59      * @exception java.lang.Exception a Java exception.
60      */

61     public NameService(ORB orb, File JavaDoc logDir)
62     throws Exception JavaDoc
63     {
64     theorb = orb;
65
66     // Moved this to the creation of the ORB that is passed into this
67
// constructor.
68
//
69
// This is required for creating Persistent Servants under this ORB
70
// Right now the Persistent NameService and ORBD are launched together
71
// Find out a better way of doing this, Since ORBD is an important
72
// process which should not be killed because of some external process
73
// orb.setPersistentServerId( (int) 1000 );
74

75     // get and activate the root naming POA
76
POA JavaDoc rootPOA = (POA JavaDoc)orb.resolve_initial_references(
77         ORBConstants.ROOT_POA_NAME ) ;
78     rootPOA.the_POAManager().activate();
79
80     // create a new POA for persistent Naming Contexts
81
// With Non-Retain policy, So that every time Servant Manager
82
// will be contacted when the reference is made for the context
83
// The id assignment is made by the NameServer, The Naming Context
84
// id's will be in the format NC<Index>
85
int i=0;
86     Policy JavaDoc[] poaPolicy = new Policy JavaDoc[4];
87     poaPolicy[i++] = rootPOA.create_lifespan_policy(
88              LifespanPolicyValue.PERSISTENT);
89     poaPolicy[i++] = rootPOA.create_request_processing_policy(
90              RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
91     poaPolicy[i++] = rootPOA.create_id_assignment_policy(
92                  IdAssignmentPolicyValue.USER_ID);
93         poaPolicy[i++] = rootPOA.create_servant_retention_policy(
94              ServantRetentionPolicyValue.NON_RETAIN);
95
96
97     nsPOA = rootPOA.create_POA("NameService", null, poaPolicy);
98     nsPOA.the_POAManager().activate( );
99
100     // create and set the servant manager
101
contextMgr = new
102         ServantManagerImpl(orb, logDir, this );
103
104     // The RootObject key will be NC0
105
String JavaDoc rootKey = contextMgr.getRootObjectKey( );
106     // initialize the root Naming Context
107
NamingContextImpl nc =
108         new NamingContextImpl( orb, rootKey, this, contextMgr );
109     nc = contextMgr.addContext( rootKey, nc );
110     nc.setServantManagerImpl( contextMgr );
111     nc.setORB( orb );
112     nc.setRootNameService( this );
113
114     nsPOA.set_servant_manager(contextMgr);
115     rootContext = NamingContextHelper.narrow(
116     nsPOA.create_reference_with_id( rootKey.getBytes( ),
117     NamingContextHelper.id( ) ) );
118     }
119
120     /**
121      * This method returns the Root Naming Context
122      */

123     public NamingContext JavaDoc initialNamingContext()
124     {
125     return rootContext;
126     }
127
128     /**
129      * This method returns nsPOA which is the only POA that we use for
130      * Persistent Naming Contexts.
131      */

132     POA JavaDoc getNSPOA( ) {
133         return nsPOA;
134     }
135
136
137     /**
138      * This method creates a NewContext, This will internally invoked from
139      * NamingContextImpl. It is not a public API. NewContext is in this class
140      * because a Persiten reference has to be created with Persistent NameService
141      * POA.
142      */

143     public NamingContext JavaDoc NewContext( ) throws org.omg.CORBA.SystemException JavaDoc
144     {
145     try
146     {
147         // Get the new Naming Context Key from
148
// the ServantManager
149
String JavaDoc newKey =
150         contextMgr.getNewObjectKey( );
151         // Create the new Naming context and create the Persistent
152
// reference
153
NamingContextImpl theContext =
154         new NamingContextImpl( theorb, newKey,
155             this, contextMgr );
156         NamingContextImpl tempContext = contextMgr.addContext( newKey,
157                          theContext );
158         if( tempContext != null )
159         {
160             theContext = tempContext;
161         }
162         // If the context is read from the File, The following three entries
163
// will be null. So a fresh setup may be required.
164
theContext.setServantManagerImpl( contextMgr );
165         theContext.setORB( theorb );
166         theContext.setRootNameService( this );
167             NamingContext JavaDoc theNewContext =
168         NamingContextHelper.narrow(
169         nsPOA.create_reference_with_id( newKey.getBytes( ),
170             NamingContextHelper.id( )) );
171         return theNewContext;
172     }
173     catch( org.omg.CORBA.SystemException JavaDoc e )
174     {
175         throw e;
176     }
177     catch( java.lang.Exception JavaDoc e )
178     {
179         //throw e;
180
}
181     return null;
182     }
183
184     /**
185      * getObjectReferenceFromKey returns the Object reference from the objectkey using POA.create_reference_with_id method
186      * @param Object Key as String
187      * @returns reference an CORBA.Object.
188      */

189     org.omg.CORBA.Object JavaDoc getObjectReferenceFromKey( String JavaDoc key )
190     {
191     org.omg.CORBA.Object JavaDoc theObject = null;
192     try
193     {
194         theObject = nsPOA.create_reference_with_id( key.getBytes( ), NamingContextHelper.id( ) );
195     }
196     catch (Exception JavaDoc e )
197     {
198         theObject = null;
199     }
200         return theObject;
201     }
202
203     /**
204      * getObjectKey gets the Object Key from the reference using POA.reference_to_id method
205      * @param reference an CORBA.Object.
206      * @returns Object Key as String
207      */

208     String JavaDoc getObjectKey( org.omg.CORBA.Object JavaDoc reference )
209     {
210     byte theId[];
211     try
212     {
213         theId = nsPOA.reference_to_id( reference );
214     }
215     catch( org.omg.PortableServer.POAPackage.WrongAdapter JavaDoc e )
216     {
217         return null;
218     }
219     catch( org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc e )
220     {
221         return null;
222     }
223     catch( Exception JavaDoc e )
224     {
225         return null;
226     }
227     String JavaDoc theKey = new String JavaDoc( theId );
228     return theKey;
229     }
230
231  
232 }
233
Popular Tags