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