1. /*
  2. * @(#)NamingContextImpl.java 1.67 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. package com.sun.corba.se.impl.naming.cosnaming;
  8. // Imports for Logging
  9. import java.util.logging.Logger;
  10. import java.util.logging.Level;
  11. import com.sun.corba.se.impl.orbutil.LogKeywords;
  12. // Import general CORBA classes
  13. import org.omg.CORBA.Object;
  14. import org.omg.CORBA.BAD_PARAM;
  15. import org.omg.CORBA.INTERNAL;
  16. import org.omg.CORBA.CompletionStatus;
  17. import org.omg.PortableServer.POA;
  18. import org.omg.PortableServer.Servant;
  19. // Import org.omg.CosNaming classes
  20. import org.omg.CosNaming.BindingType;
  21. import org.omg.CosNaming.BindingTypeHolder;
  22. import org.omg.CosNaming.BindingListHolder;
  23. import org.omg.CosNaming.BindingIteratorHolder;
  24. import org.omg.CosNaming.NameComponent;
  25. import org.omg.CosNaming.NamingContextHelper;
  26. import org.omg.CosNaming.NamingContext;
  27. import org.omg.CosNaming.NamingContextPackage.*;
  28. import org.omg.CosNaming._NamingContextImplBase;
  29. import org.omg.CosNaming.NamingContextExtHelper;
  30. import org.omg.CosNaming.NamingContextExt;
  31. import org.omg.CosNaming.NamingContextExtPOA;
  32. import org.omg.CosNaming.NamingContextExtPackage.*;
  33. import org.omg.CosNaming.NamingContextPackage.NotFound;
  34. import com.sun.corba.se.impl.naming.cosnaming.NamingContextDataStore;
  35. import com.sun.corba.se.impl.naming.namingutil.INSURLHandler;
  36. import com.sun.corba.se.spi.logging.CORBALogDomains;
  37. import com.sun.corba.se.impl.logging.NamingSystemException ;
  38. import com.sun.corba.se.spi.orb.ORB;
  39. /**
  40. * Class NamingContextImpl implements the org.omg.CosNaming::NamingContext
  41. * interface, but does not implement the methods associated with
  42. * maintaining the "table" of current bindings in a NamingContext.
  43. * Instead, this implementation assumes that the derived implementation
  44. * implements the NamingContextDataStore interface, which has the necessary
  45. * methods. This allows multiple
  46. * NamingContext implementations that differ in storage of the bindings,
  47. * as well as implementations of interfaces derived from
  48. * CosNaming::NamingContext that still reuses the implementation.
  49. * <p>
  50. * The operations bind(), rebind(), bind_context() and rebind_context()
  51. * are all really implemented by doBind(). resolve() is really implemented
  52. * by doResolve(), unbind() by doUnbind(). list(), new_context() and
  53. * destroy() uses the NamingContextDataStore interface directly. All the
  54. * doX() methods are public static.
  55. * They synchronize on the NamingContextDataStore object.
  56. * <p>
  57. * An implementation a NamingContext must extend this class and implement
  58. * the NamingContextDataStore interface with the operations:
  59. * Bind(), Resolve(),
  60. * Unbind(), List(), NewContext() and Destroy(). Calls
  61. * to these methods are synchronized; these methods should
  62. * therefore not be synchronized.
  63. */
  64. public abstract class NamingContextImpl
  65. extends NamingContextExtPOA
  66. implements NamingContextDataStore
  67. {
  68. protected POA nsPOA;
  69. private Logger readLogger, updateLogger, lifecycleLogger;
  70. private NamingSystemException wrapper ;
  71. private static NamingSystemException staticWrapper =
  72. NamingSystemException.get( CORBALogDomains.NAMING_UPDATE ) ;
  73. // The grammer for Parsing and Building Interoperable Stringified Names
  74. // are implemented in this class
  75. private InterOperableNamingImpl insImpl;
  76. /**
  77. * Create a naming context servant.
  78. * Runs the super constructor.
  79. * @param orb an ORB object.
  80. * @exception java.lang.Exception a Java exception.
  81. */
  82. public NamingContextImpl(ORB orb, POA poa) throws java.lang.Exception {
  83. super();
  84. this.orb = orb;
  85. wrapper = NamingSystemException.get( orb,
  86. CORBALogDomains.NAMING_UPDATE ) ;
  87. insImpl = new InterOperableNamingImpl( );
  88. this.nsPOA = poa;
  89. readLogger = orb.getLogger( CORBALogDomains.NAMING_READ);
  90. updateLogger = orb.getLogger( CORBALogDomains.NAMING_UPDATE);
  91. lifecycleLogger = orb.getLogger(
  92. CORBALogDomains.NAMING_LIFECYCLE);
  93. }
  94. public POA getNSPOA( ) {
  95. return nsPOA;
  96. }
  97. /**
  98. * Bind an object under a name in this NamingContext. If the name
  99. * contains multiple (n) components, n-1 will be resolved in this
  100. * NamingContext and the object bound in resulting NamingContext.
  101. * An exception is thrown if a binding with the supplied name already
  102. * exists. If the
  103. * object to be bound is a NamingContext it will not participate in
  104. * a recursive resolve.
  105. * @param n a sequence of NameComponents which is the name under which
  106. * the object will be bound.
  107. * @param obj the object reference to be bound.
  108. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  109. * multiple components was supplied, but the first component could not be
  110. * resolved.
  111. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could
  112. * not proceed in resolving the n-1 components of the supplied name.
  113. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  114. * supplied name is invalid (i.e., has length less than 1).
  115. * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
  116. * is already bound under the supplied name.
  117. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  118. * system exceptions.
  119. * @see doBind
  120. */
  121. public void bind(NameComponent[] n, org.omg.CORBA.Object obj)
  122. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  123. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  124. org.omg.CosNaming.NamingContextPackage.InvalidName,
  125. org.omg.CosNaming.NamingContextPackage.AlreadyBound
  126. {
  127. if( obj == null )
  128. {
  129. updateLogger.warning( LogKeywords.NAMING_BIND +
  130. " unsuccessful because NULL Object cannot be Bound " );
  131. throw wrapper.objectIsNull() ;
  132. }
  133. // doBind implements all four flavors of binding
  134. NamingContextDataStore impl = (NamingContextDataStore)this;
  135. doBind(impl,n,obj,false,BindingType.nobject);
  136. if( updateLogger.isLoggable( Level.FINE ) ) {
  137. // isLoggable call to make sure that we save some precious
  138. // processor cycles, if there is no need to log.
  139. updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS + " Name = " +
  140. NamingUtils.getDirectoryStructuredName( n ) );
  141. }
  142. }
  143. /**
  144. * Bind a NamingContext under a name in this NamingContext. If the name
  145. * contains multiple (n) components, n-1 will be resolved in this
  146. * NamingContext and the object bound in resulting NamingContext.
  147. * An exception is thrown if a binding with the supplied name already
  148. * exists. The NamingContext will participate in recursive resolving.
  149. * @param n a sequence of NameComponents which is the name under which
  150. * the object will be bound.
  151. * @param obj the NamingContect object reference to be bound.
  152. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  153. * multiple components was supplied, but the first component could not be
  154. * resolved.
  155. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could
  156. * not proceed in resolving the n-1 components of the supplied name.
  157. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  158. * supplied name is invalid (i.e., has length less than 1).
  159. * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
  160. * is already bound under the supplied name.
  161. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  162. * system exceptions.
  163. * @see doBind
  164. */
  165. public void bind_context(NameComponent[] n, NamingContext nc)
  166. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  167. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  168. org.omg.CosNaming.NamingContextPackage.InvalidName,
  169. org.omg.CosNaming.NamingContextPackage.AlreadyBound
  170. {
  171. if( nc == null ) {
  172. updateLogger.warning( LogKeywords.NAMING_BIND_FAILURE +
  173. " NULL Context cannot be Bound " );
  174. throw new BAD_PARAM( "Naming Context should not be null " );
  175. }
  176. // doBind implements all four flavors of binding
  177. NamingContextDataStore impl = (NamingContextDataStore)this;
  178. doBind(impl,n,nc,false,BindingType.ncontext);
  179. if( updateLogger.isLoggable( Level.FINE ) ) {
  180. // isLoggable call to make sure that we save some precious
  181. // processor cycles, if there is no need to log.
  182. updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS + " Name = " +
  183. NamingUtils.getDirectoryStructuredName( n ) );
  184. }
  185. }
  186. /**
  187. * Bind an object under a name in this NamingContext. If the name
  188. * contains multiple (n) components, n-1 will be resolved in this
  189. * NamingContext and the object bound in resulting NamingContext.
  190. * If a binding under the supplied name already exists it will be
  191. * unbound first. If the
  192. * object to be bound is a NamingContext it will not participate in
  193. * a recursive resolve.
  194. * @param n a sequence of NameComponents which is the name under which
  195. * the object will be bound.
  196. * @param obj the object reference to be bound.
  197. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  198. * multiple components was supplied, but the first component could not be
  199. * resolved.
  200. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  201. * proceed in resolving the n-1 components of the supplied name.
  202. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  203. * supplied name is invalid (i.e., has length less than 1).
  204. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  205. * system exceptions.
  206. * @see doBind
  207. */
  208. public void rebind(NameComponent[] n, org.omg.CORBA.Object obj)
  209. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  210. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  211. org.omg.CosNaming.NamingContextPackage.InvalidName
  212. {
  213. if( obj == null )
  214. {
  215. updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
  216. " NULL Object cannot be Bound " );
  217. throw wrapper.objectIsNull() ;
  218. }
  219. try {
  220. // doBind implements all four flavors of binding
  221. NamingContextDataStore impl = (NamingContextDataStore)this;
  222. doBind(impl,n,obj,true,BindingType.nobject);
  223. } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound ex) {
  224. updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
  225. NamingUtils.getDirectoryStructuredName( n ) +
  226. " is already bound to a Naming Context" );
  227. // This should not happen
  228. throw wrapper.namingCtxRebindAlreadyBound( ex ) ;
  229. }
  230. if( updateLogger.isLoggable( Level.FINE ) ) {
  231. // isLoggable call to make sure that we save some precious
  232. // processor cycles, if there is no need to log.
  233. updateLogger.fine( LogKeywords.NAMING_REBIND_SUCCESS + " Name = " +
  234. NamingUtils.getDirectoryStructuredName( n ) );
  235. }
  236. }
  237. /**
  238. * Bind a NamingContext under a name in this NamingContext. If the name
  239. * contains multiple (n) components, the first n-1 components will be
  240. * resolved in this NamingContext and the object bound in resulting
  241. * NamingContext. If a binding under the supplied name already exists it
  242. * will be unbound first. The NamingContext will participate in recursive
  243. * resolving.
  244. * @param n a sequence of NameComponents which is the name under which
  245. * the object will be bound.
  246. * @param obj the object reference to be bound.
  247. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  248. * multiple components was supplied, but the first component could not be
  249. * resolved.
  250. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  251. * proceed in resolving the n-1 components of the supplied name.
  252. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  253. * supplied name is invalid (i.e., has length less than 1).
  254. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  255. * system exceptions.
  256. * @see doBind
  257. */
  258. public void rebind_context(NameComponent[] n, NamingContext nc)
  259. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  260. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  261. org.omg.CosNaming.NamingContextPackage.InvalidName
  262. {
  263. if( nc == null )
  264. {
  265. updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
  266. " NULL Context cannot be Bound " );
  267. throw wrapper.objectIsNull() ;
  268. }
  269. try {
  270. // doBind implements all four flavors of binding
  271. NamingContextDataStore impl = (NamingContextDataStore)this;
  272. doBind(impl,n,nc,true,BindingType.ncontext);
  273. } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound ex) {
  274. // This should not happen
  275. updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
  276. NamingUtils.getDirectoryStructuredName( n ) +
  277. " is already bound to a CORBA Object" );
  278. throw wrapper.namingCtxRebindctxAlreadyBound( ex ) ;
  279. }
  280. if( updateLogger.isLoggable( Level.FINE ) ) {
  281. // isLoggable call to make sure that we save some precious
  282. // processor cycles, if there is no need to log.
  283. updateLogger.fine( LogKeywords.NAMING_REBIND_SUCCESS + " Name = " +
  284. NamingUtils.getDirectoryStructuredName( n ) );
  285. }
  286. }
  287. /**
  288. * Resolve a name in this NamingContext and return the object reference
  289. * bound to the name. If the name contains multiple (n) components,
  290. * the first component will be resolved in this NamingContext and the
  291. * remaining components resolved in the resulting NamingContext, provided
  292. * that the NamingContext bound to the first component of the name was
  293. * bound with bind_context().
  294. * @param n a sequence of NameComponents which is the name to be resolved.
  295. * @return the object reference bound under the supplied name.
  296. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  297. * multiple components was supplied, but the first component could not be
  298. * resolved.
  299. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  300. * proceed in resolving the n-1 components of the supplied name.
  301. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  302. * supplied name is invalid (i.e., has length less than 1).
  303. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  304. * system exceptions.
  305. * @see doResolve
  306. */
  307. public org.omg.CORBA.Object resolve(NameComponent[] n)
  308. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  309. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  310. org.omg.CosNaming.NamingContextPackage.InvalidName
  311. {
  312. // doResolve actually resolves
  313. NamingContextDataStore impl = (NamingContextDataStore)this;
  314. org.omg.CORBA.Object obj = doResolve(impl,n);
  315. if( obj != null ) {
  316. if( readLogger.isLoggable( Level.FINE ) ) {
  317. readLogger.fine( LogKeywords.NAMING_RESOLVE_SUCCESS +
  318. " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
  319. }
  320. } else {
  321. readLogger.warning( LogKeywords.NAMING_RESOLVE_FAILURE +
  322. " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
  323. }
  324. return obj;
  325. }
  326. /**
  327. * Remove a binding from this NamingContext. If the name contains
  328. * multiple (n) components, the first n-1 components will be resolved
  329. * from this NamingContext and the final component unbound in
  330. * the resulting NamingContext.
  331. * @param n a sequence of NameComponents which is the name to be unbound.
  332. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  333. * multiple components was supplied, but the first component could not be
  334. * resolved.
  335. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  336. * proceed in resolving the n-1 components of the supplied name.
  337. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  338. * supplied name is invalid (i.e., has length less than 1).
  339. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  340. * system exceptions.
  341. * @see doUnbind
  342. */
  343. public void unbind(NameComponent[] n)
  344. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  345. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  346. org.omg.CosNaming.NamingContextPackage.InvalidName
  347. {
  348. // doUnbind actually unbinds
  349. NamingContextDataStore impl = (NamingContextDataStore)this;
  350. doUnbind(impl,n);
  351. if( updateLogger.isLoggable( Level.FINE ) ) {
  352. // isLoggable call to make sure that we save some precious
  353. // processor cycles, if there is no need to log.
  354. updateLogger.fine( LogKeywords.NAMING_UNBIND_SUCCESS +
  355. " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
  356. }
  357. }
  358. /**
  359. * List the contents of this NamingContest. A sequence of bindings
  360. * is returned (a BindingList) containing up to the number of requested
  361. * bindings, and a BindingIterator object reference is returned for
  362. * iterating over the remaining bindings.
  363. * @param how_many The number of requested bindings in the BindingList.
  364. * @param bl The BindingList as an out parameter.
  365. * @param bi The BindingIterator as an out parameter.
  366. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  367. * system exceptions.
  368. * @see BindingListHolder
  369. * @see BindingIteratorImpl
  370. */
  371. public void list(int how_many, BindingListHolder bl,
  372. BindingIteratorHolder bi)
  373. {
  374. // List actually generates the list
  375. NamingContextDataStore impl = (NamingContextDataStore)this;
  376. synchronized (impl) {
  377. impl.List(how_many,bl,bi);
  378. }
  379. if( readLogger.isLoggable( Level.FINE ) && (bl.value != null )) {
  380. // isLoggable call to make sure that we save some precious
  381. // processor cycles, if there is no need to log.
  382. readLogger.fine ( LogKeywords.NAMING_LIST_SUCCESS +
  383. "list(" + how_many + ") -> bindings[" + bl.value.length +
  384. "] + iterator: " + bi.value);
  385. }
  386. }
  387. /**
  388. * Create a NamingContext object and return its object reference.
  389. * @return an object reference for a new NamingContext object implemented
  390. * by this Name Server.
  391. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  392. * system exceptions.
  393. */
  394. public synchronized NamingContext new_context()
  395. {
  396. // Create actually creates a new naming context
  397. lifecycleLogger.fine( "Creating New Naming Context " );
  398. NamingContextDataStore impl = (NamingContextDataStore)this;
  399. synchronized (impl) {
  400. NamingContext nctx = impl.NewContext();
  401. if( nctx != null ) {
  402. lifecycleLogger.fine( LogKeywords.LIFECYCLE_CREATE_SUCCESS );
  403. } else {
  404. // If naming context is null, then that must be a serious
  405. // error.
  406. lifecycleLogger.severe ( LogKeywords.LIFECYCLE_CREATE_FAILURE );
  407. }
  408. return nctx;
  409. }
  410. }
  411. /**
  412. * Create a new NamingContext, bind it in this Naming Context and return
  413. * its object reference. This is equivalent to using new_context() followed
  414. * by bind_context() with the supplied name and the object reference for
  415. * the newly created NamingContext.
  416. * @param n a sequence of NameComponents which is the name to be unbound.
  417. * @return an object reference for a new NamingContext object implemented
  418. * by this Name Server, bound to the supplied name.
  419. * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
  420. * is already bound under the supplied name.
  421. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  422. * multiple components was supplied, but the first component could not be
  423. * resolved.
  424. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  425. * proceed in resolving the n-1 components of the supplied name.
  426. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  427. * supplied name is invalid (i.e., has length less than 1).
  428. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  429. * system exceptions.
  430. * @see new_context
  431. * @see bind_context
  432. */
  433. public NamingContext bind_new_context(NameComponent[] n)
  434. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  435. org.omg.CosNaming.NamingContextPackage.AlreadyBound,
  436. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  437. org.omg.CosNaming.NamingContextPackage.InvalidName
  438. {
  439. NamingContext nc = null;
  440. NamingContext rnc = null;
  441. try {
  442. if (debug)
  443. dprint("bind_new_context " + nameToString(n));
  444. // The obvious solution:
  445. nc = this.new_context();
  446. this.bind_context(n,nc);
  447. rnc = nc;
  448. nc = null;
  449. } finally {
  450. try {
  451. if(nc != null)
  452. nc.destroy();
  453. } catch (org.omg.CosNaming.NamingContextPackage.NotEmpty e) {
  454. }
  455. }
  456. if( updateLogger.isLoggable( Level.FINE ) ) {
  457. // isLoggable call to make sure that we save some precious
  458. // processor cycles, if there is no need to log.
  459. updateLogger.fine ( LogKeywords.NAMING_BIND +
  460. "New Context Bound To " +
  461. NamingUtils.getDirectoryStructuredName( n ) );
  462. }
  463. return rnc;
  464. }
  465. /**
  466. * Destroy this NamingContext object. If this NamingContext contains
  467. * no bindings, the NamingContext is deleted.
  468. * @exception org.omg.CosNaming.NamingContextPackage.NotEmpty This
  469. * NamingContext is not empty (i.e., contains bindings).
  470. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  471. * system exceptions.
  472. */
  473. public void destroy()
  474. throws org.omg.CosNaming.NamingContextPackage.NotEmpty
  475. {
  476. lifecycleLogger.fine( "Destroying Naming Context " );
  477. NamingContextDataStore impl = (NamingContextDataStore)this;
  478. synchronized (impl) {
  479. if (impl.IsEmpty() == true) {
  480. // The context is empty so it can be destroyed
  481. impl.Destroy();
  482. lifecycleLogger.fine ( LogKeywords.LIFECYCLE_DESTROY_SUCCESS );
  483. }
  484. else {
  485. // This context is not empty!
  486. // Not a fatal error, warning should do.
  487. lifecycleLogger.warning( LogKeywords.LIFECYCLE_DESTROY_FAILURE +
  488. " NamingContext children are not destroyed still.." );
  489. throw new NotEmpty();
  490. }
  491. }
  492. }
  493. /**
  494. * Implements all four flavors of binding. It uses Resolve() to
  495. * check if a binding already exists (for bind and bind_context), and
  496. * unbind() to ensure that a binding does not already exist.
  497. * If the length of the name is 1, then Bind() is called with
  498. * the name and the object to bind. Otherwise, the first component
  499. * of the name is resolved in this NamingContext and the appropriate
  500. * form of bind passed to the resulting NamingContext.
  501. * This method is static for maximal reuse - even for extended naming
  502. * context implementations where the recursive semantics still apply.
  503. * @param impl an implementation of NamingContextDataStore
  504. * @param n a sequence of NameComponents which is the name under which
  505. * the object will be bound.
  506. * @param obj the object reference to be bound.
  507. * @param rebind Replace an existing binding or not.
  508. * @param bt Type of binding (as object or as context).
  509. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  510. * multiple components was supplied, but the first component could not be
  511. * resolved.
  512. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not * proceed
  513. * in resolving the first component of the supplied name.
  514. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
  515. * supplied name is invalid (i.e., has length less than 1).
  516. * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
  517. * is already bound under the supplied name.
  518. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  519. * system exceptions.
  520. * @see resolve
  521. * @see unbind
  522. * @see bind
  523. * @see bind_context
  524. * @see rebind
  525. * @see rebind_context
  526. */
  527. public static void doBind(NamingContextDataStore impl,
  528. NameComponent[] n,
  529. org.omg.CORBA.Object obj,
  530. boolean rebind,
  531. org.omg.CosNaming.BindingType bt)
  532. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  533. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  534. org.omg.CosNaming.NamingContextPackage.InvalidName,
  535. org.omg.CosNaming.NamingContextPackage.AlreadyBound
  536. {
  537. // Valid name?
  538. if (n.length < 1)
  539. throw new InvalidName();
  540. // At bottom level?
  541. if (n.length == 1) {
  542. // The identifier must be set
  543. if ( (n[0].id.length() == 0) && (n[0].kind.length() == 0 ) ) {
  544. throw new InvalidName();
  545. }
  546. // Ensure synchronization of backend
  547. synchronized (impl) {
  548. // Yes: bind object in this context under the name
  549. BindingTypeHolder bth = new BindingTypeHolder();
  550. if (rebind) {
  551. org.omg.CORBA.Object objRef = impl.Resolve( n[0], bth );
  552. if( objRef != null ) {
  553. // Refer Naming Service Doc:00-11-01 section 2.2.3.4
  554. // If there is an object already bound with the name
  555. // and the binding type is not ncontext a NotFound
  556. // Exception with a reason of not a context has to be
  557. // raised.
  558. // Fix for bug Id: 4384628
  559. if ( bth.value.value() == BindingType.nobject.value() ){
  560. if ( bt.value() == BindingType.ncontext.value() ) {
  561. throw new NotFound(
  562. NotFoundReason.not_context, n);
  563. }
  564. } else {
  565. // Previously a Context was bound and now trying to
  566. // bind Object. It is invalid.
  567. if ( bt.value() == BindingType.nobject.value() ) {
  568. throw new NotFound(
  569. NotFoundReason.not_object, n);
  570. }
  571. }
  572. impl.Unbind(n[0]);
  573. }
  574. } else {
  575. if (impl.Resolve(n[0],bth) != null)
  576. // "Resistence is futile." [Borg pickup line]
  577. throw new AlreadyBound();
  578. }
  579. // Now there are no other bindings under this name
  580. impl.Bind(n[0],obj,bt);
  581. }
  582. } else {
  583. // No: bind in a different context
  584. NamingContext context = resolveFirstAsContext(impl,n);
  585. // Compute tail
  586. NameComponent[] tail = new NameComponent[n.length - 1];
  587. System.arraycopy(n,1,tail,0,n.length-1);
  588. // How should we propagate the bind
  589. switch (bt.value()) {
  590. case BindingType._nobject:
  591. {
  592. // Bind as object
  593. if (rebind)
  594. context.rebind(tail,obj);
  595. else
  596. context.bind(tail,obj);
  597. }
  598. break;
  599. case BindingType._ncontext:
  600. {
  601. // Narrow to a naming context using Java casts. It must
  602. // work.
  603. NamingContext objContext = (NamingContext)obj;
  604. // Bind as context
  605. if (rebind)
  606. context.rebind_context(tail,objContext);
  607. else
  608. context.bind_context(tail,objContext);
  609. }
  610. break;
  611. default:
  612. // This should not happen
  613. throw staticWrapper.namingCtxBadBindingtype() ;
  614. }
  615. }
  616. }
  617. /**
  618. * Implements resolving names in this NamingContext. The first component
  619. * of the supplied name is resolved in this NamingContext by calling
  620. * Resolve(). If there are no more components in the name, the
  621. * resulting object reference is returned. Otherwise, the resulting object
  622. * reference must have been bound as a context and be narrowable to
  623. * a NamingContext. If this is the case, the remaining
  624. * components of the name is resolved in the resulting NamingContext.
  625. * This method is static for maximal reuse - even for extended naming
  626. * context implementations where the recursive semantics still apply.
  627. * @param impl an implementation of NamingContextDataStore
  628. * @param n a sequence of NameComponents which is the name to be resolved.
  629. * @return the object reference bound under the supplied name.
  630. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
  631. * multiple components was supplied, but the first component could not be
  632. * resolved.
  633. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
  634. * proceed
  635. * in resolving the first component of the supplied name.
  636. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The supplied
  637. * name is invalid (i.e., has length less than 1).
  638. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system
  639. * exceptions.
  640. * @see resolve
  641. */
  642. public static org.omg.CORBA.Object doResolve(NamingContextDataStore impl,
  643. NameComponent[] n)
  644. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  645. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  646. org.omg.CosNaming.NamingContextPackage.InvalidName
  647. {
  648. org.omg.CORBA.Object obj = null;
  649. BindingTypeHolder bth = new BindingTypeHolder();
  650. // Length must be greater than 0
  651. if (n.length < 1)
  652. throw new InvalidName();
  653. // The identifier must be set
  654. if (n.length == 1) {
  655. synchronized (impl) {
  656. // Resolve first level in this context
  657. obj = impl.Resolve(n[0],bth);
  658. }
  659. if (obj == null) {
  660. // Object was not found
  661. throw new NotFound(NotFoundReason.missing_node,n);
  662. }
  663. return obj;
  664. } else {
  665. // n.length > 1
  666. if ( (n[1].id.length() == 0) && (n[1].kind.length() == 0) ) {
  667. throw new InvalidName();
  668. }
  669. NamingContext context = resolveFirstAsContext(impl,n);
  670. // Compute restOfName = name[1..length]
  671. NameComponent[] tail = new NameComponent[n.length -1];
  672. System.arraycopy(n,1,tail,0,n.length-1);
  673. // Resolve rest of name in context
  674. try {
  675. // First try to resolve using the local call, this should work
  676. // most of the time unless there are federated naming contexts.
  677. Servant servant = impl.getNSPOA().reference_to_servant(
  678. context );
  679. return doResolve(((NamingContextDataStore)servant), tail) ;
  680. } catch( Exception e ) {
  681. return context.resolve(tail);
  682. }
  683. }
  684. }
  685. /**
  686. * Implements unbinding bound names in this NamingContext. If the
  687. * name contains only one component, the name is unbound in this
  688. * NamingContext using Unbind(). Otherwise, the first component
  689. * of the name is resolved in this NamingContext and
  690. * unbind passed to the resulting NamingContext.
  691. * This method is static for maximal reuse - even for extended naming
  692. * context implementations where the recursive semantics still apply.
  693. * @param impl an implementation of NamingContextDataStore
  694. * @param n a sequence of NameComponents which is the name to be unbound.
  695. * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with multiple
  696. * components was supplied, but the first component could not be
  697. * resolved.
  698. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not proceed
  699. * in resolving the n-1 components of the supplied name.
  700. * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The supplied name
  701. * is invalid (i.e., has length less than 1).
  702. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
  703. * @see resolve
  704. */
  705. public static void doUnbind(NamingContextDataStore impl,
  706. NameComponent[] n)
  707. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  708. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  709. org.omg.CosNaming.NamingContextPackage.InvalidName
  710. {
  711. // Name valid?
  712. if (n.length < 1)
  713. throw new InvalidName();
  714. // Unbind here?
  715. if (n.length == 1) {
  716. // The identifier must be set
  717. if ( (n[0].id.length() == 0) && (n[0].kind.length() == 0 ) ) {
  718. throw new InvalidName();
  719. }
  720. org.omg.CORBA.Object objRef = null;
  721. synchronized (impl) {
  722. // Yes: unbind in this context
  723. objRef = impl.Unbind(n[0]);
  724. }
  725. if (objRef == null)
  726. // It was not bound
  727. throw new NotFound(NotFoundReason.missing_node,n);
  728. // Done
  729. return;
  730. } else {
  731. // No: unbind in a different context
  732. // Resolve first - must be resolveable
  733. NamingContext context = resolveFirstAsContext(impl,n);
  734. // Compute tail
  735. NameComponent[] tail = new NameComponent[n.length - 1];
  736. System.arraycopy(n,1,tail,0,n.length-1);
  737. // Propagate unbind to this context
  738. context.unbind(tail);
  739. }
  740. }
  741. /**
  742. * Implements resolving a NameComponent in this context and
  743. * narrowing it to CosNaming::NamingContext. It will throw appropriate
  744. * exceptions if not found or not narrowable.
  745. * @param impl an implementation of NamingContextDataStore
  746. * @param n a NameComponents which is the name to be found.
  747. * @exception org.omg.CosNaming.NamingContextPackage.NotFound The
  748. * first component could not be resolved.
  749. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not proceed
  750. * in resolving the first component of the supplied name.
  751. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
  752. * @see resolve
  753. */
  754. protected static NamingContext resolveFirstAsContext(NamingContextDataStore impl,
  755. NameComponent[] n)
  756. throws org.omg.CosNaming.NamingContextPackage.NotFound {
  757. org.omg.CORBA.Object topRef = null;
  758. BindingTypeHolder bth = new BindingTypeHolder();
  759. NamingContext context = null;
  760. synchronized (impl) {
  761. // Resolve first - must be resolveable
  762. topRef = impl.Resolve(n[0],bth);
  763. if (topRef == null) {
  764. // It was not bound
  765. throw new NotFound(NotFoundReason.missing_node,n);
  766. }
  767. }
  768. // Was it bound as a context?
  769. if (bth.value != BindingType.ncontext) {
  770. // It was not a context
  771. throw new NotFound(NotFoundReason.not_context,n);
  772. }
  773. // Narrow to a naming context
  774. try {
  775. context = NamingContextHelper.narrow(topRef);
  776. } catch (org.omg.CORBA.BAD_PARAM ex) {
  777. // It was not a context
  778. throw new NotFound(NotFoundReason.not_context,n);
  779. }
  780. // Hmm. must be ok
  781. return context;
  782. }
  783. /**
  784. * This operation creates a stringified name from the array of Name
  785. * components.
  786. * @param n Name of the object <p>
  787. * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
  788. * Indicates the name does not identify a binding.<p>
  789. *
  790. */
  791. public String to_string(org.omg.CosNaming.NameComponent[] n)
  792. throws org.omg.CosNaming.NamingContextPackage.InvalidName
  793. {
  794. // Name valid?
  795. if ( (n == null ) || (n.length == 0) )
  796. {
  797. throw new InvalidName();
  798. }
  799. NamingContextDataStore impl = (NamingContextDataStore)this;
  800. String theStringifiedName = insImpl.convertToString( n );
  801. if( theStringifiedName == null )
  802. {
  803. throw new InvalidName();
  804. }
  805. return theStringifiedName;
  806. }
  807. /**
  808. * This operation converts a Stringified Name into an equivalent array
  809. * of Name Components.
  810. * @param sn Stringified Name of the object <p>
  811. * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
  812. * Indicates the name does not identify a binding.<p>
  813. *
  814. */
  815. public org.omg.CosNaming.NameComponent[] to_name(String sn)
  816. throws org.omg.CosNaming.NamingContextPackage.InvalidName
  817. {
  818. // Name valid?
  819. if ( (sn == null ) || (sn.length() == 0) )
  820. {
  821. throw new InvalidName();
  822. }
  823. NamingContextDataStore impl = (NamingContextDataStore)this;
  824. org.omg.CosNaming.NameComponent[] theNameComponents =
  825. insImpl.convertToNameComponent( sn );
  826. if( ( theNameComponents == null ) || (theNameComponents.length == 0 ) )
  827. {
  828. throw new InvalidName();
  829. }
  830. for( int i = 0; i < theNameComponents.length; i++ ) {
  831. // If there is a name component whose id and kind null or
  832. // zero length string, then an invalid name exception needs to be
  833. // raised.
  834. if ( ( ( theNameComponents[i].id == null )
  835. ||( theNameComponents[i].id.length() == 0 ) )
  836. &&( ( theNameComponents[i].kind == null )
  837. ||( theNameComponents[i].kind.length() == 0 ) ) ) {
  838. throw new InvalidName();
  839. }
  840. }
  841. return theNameComponents;
  842. }
  843. /**
  844. * This operation creates a URL based "iiopname://" format name
  845. * from the Stringified Name of the object.
  846. * @param addr internet based address of the host machine where
  847. * Name Service is running <p>
  848. * @param sn Stringified Name of the object <p>
  849. * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
  850. * Indicates the name does not identify a binding.<p>
  851. * @exception org.omg.CosNaming.NamingContextPackage.InvalidAddress
  852. * Indicates the internet based address of the host machine is
  853. * incorrect <p>
  854. *
  855. */
  856. public String to_url(String addr, String sn)
  857. throws org.omg.CosNaming.NamingContextExtPackage.InvalidAddress,
  858. org.omg.CosNaming.NamingContextPackage.InvalidName
  859. {
  860. // Name valid?
  861. if ( (sn == null ) || (sn.length() == 0) )
  862. {
  863. throw new InvalidName();
  864. }
  865. if( addr == null )
  866. {
  867. throw new
  868. org.omg.CosNaming.NamingContextExtPackage.InvalidAddress();
  869. }
  870. NamingContextDataStore impl = (NamingContextDataStore)this;
  871. String urlBasedAddress = null;
  872. urlBasedAddress = insImpl.createURLBasedAddress( addr, sn );
  873. // Extra check to see that corba name url created is valid as per
  874. // INS spec grammer.
  875. try {
  876. INSURLHandler.getINSURLHandler( ).parseURL( urlBasedAddress );
  877. } catch( BAD_PARAM e ) {
  878. throw new
  879. org.omg.CosNaming.NamingContextExtPackage.InvalidAddress();
  880. }
  881. return urlBasedAddress;
  882. }
  883. /**
  884. * This operation resolves the Stringified name into the object
  885. * reference.
  886. * @param sn Stringified Name of the object <p>
  887. * @exception org.omg.CosNaming.NamingContextPackage.NotFound
  888. * Indicates there is no object reference for the given name. <p>
  889. * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed
  890. * Indicates that the given compound name is incorrect <p>
  891. * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
  892. * Indicates the name does not identify a binding.<p>
  893. * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound
  894. * Indicates the name is already bound.<p>
  895. *
  896. */
  897. public org.omg.CORBA.Object resolve_str(String sn)
  898. throws org.omg.CosNaming.NamingContextPackage.NotFound,
  899. org.omg.CosNaming.NamingContextPackage.CannotProceed,
  900. org.omg.CosNaming.NamingContextPackage.InvalidName
  901. {
  902. org.omg.CORBA.Object theObject = null;
  903. // Name valid?
  904. if ( (sn == null ) || (sn.length() == 0) )
  905. {
  906. throw new InvalidName();
  907. }
  908. NamingContextDataStore impl = (NamingContextDataStore)this;
  909. org.omg.CosNaming.NameComponent[] theNameComponents =
  910. insImpl.convertToNameComponent( sn );
  911. if( ( theNameComponents == null ) || (theNameComponents.length == 0 ) )
  912. {
  913. throw new InvalidName();
  914. }
  915. theObject = resolve( theNameComponents );
  916. return theObject;
  917. }
  918. transient protected ORB orb;
  919. public static String nameToString(NameComponent[] name)
  920. {
  921. StringBuffer s = new StringBuffer("{");
  922. if (name != null || name.length > 0) {
  923. for (int i=0;i<name.length;i++) {
  924. if (i>0)
  925. s.append(",");
  926. s.append("[").
  927. append(name[i].id).
  928. append(",").
  929. append(name[i].kind).
  930. append("]");
  931. }
  932. }
  933. s.append("}");
  934. return s.toString();
  935. }
  936. // Debugging aids.
  937. public static final boolean debug = false;
  938. private static void dprint(String msg) {
  939. NamingUtils.dprint("NamingContextImpl(" +
  940. Thread.currentThread().getName() + " at " +
  941. System.currentTimeMillis() +
  942. " ems): " + msg);
  943. }
  944. }