TransientContext.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* nContext.java -- implementation of NamingContext
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.NamingService;
  32. import org.omg.CORBA.Object;
  33. import org.omg.CosNaming.Binding;
  34. import org.omg.CosNaming.BindingIteratorHolder;
  35. import org.omg.CosNaming.BindingListHolder;
  36. import org.omg.CosNaming.BindingType;
  37. import org.omg.CosNaming.NameComponent;
  38. import org.omg.CosNaming.NamingContext;
  39. import org.omg.CosNaming.NamingContextOperations;
  40. import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
  41. import org.omg.CosNaming.NamingContextPackage.CannotProceed;
  42. import org.omg.CosNaming.NamingContextPackage.InvalidName;
  43. import org.omg.CosNaming.NamingContextPackage.NotEmpty;
  44. import org.omg.CosNaming.NamingContextPackage.NotFound;
  45. import org.omg.CosNaming.NamingContextPackage.NotFoundReason;
  46. import org.omg.CosNaming._NamingContextImplBase;
  47. import gnu.CORBA.SafeForDirectCalls;
  48. import java.util.Iterator;
  49. import java.util.Map;
  50. /**
  51. * This class implements the transient naming service, defined by
  52. * {@link NamingContext}. The 'transient' means that the service does
  53. * not store its state into the persistent memory. If the service is
  54. * restarted, the named objects must be re-registered again.
  55. *
  56. * TODO Write the persistent naming service.
  57. *
  58. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  59. */
  60. public class TransientContext
  61. extends _NamingContextImplBase
  62. implements NamingContext, NamingContextOperations, SafeForDirectCalls
  63. {
  64. /**
  65. * Use serial version UID for interoperability.
  66. */
  67. private static final long serialVersionUID = 2;
  68. /**
  69. * The already named contexts.
  70. */
  71. protected final NamingMap named_contexts;
  72. /**
  73. * The already named objects.
  74. */
  75. protected final NamingMap named_objects;
  76. /**
  77. * Create the naming conetxt with default (transient) naming maps.
  78. */
  79. public TransientContext()
  80. {
  81. this(new NamingMap(), new NamingMap());
  82. }
  83. /**
  84. * Create the naming conetxt with the two provided naming maps.
  85. *
  86. * @param context_map the map for contexts
  87. * @param object_map the map for objectss
  88. */
  89. public TransientContext(NamingMap context_map, NamingMap object_map)
  90. {
  91. named_contexts = context_map;
  92. named_objects = object_map;
  93. }
  94. /**
  95. * Gives the object a name, valid in this context.
  96. *
  97. * @param a_name the name, being given to the object.
  98. * @param an_object the object, being named.
  99. *
  100. * @throws AlreadyBound if the object is already named in this context.
  101. * @throws InvalidName if the name has zero length or otherwise invalid.
  102. */
  103. public void bind(NameComponent[] a_name, Object an_object)
  104. throws NotFound, CannotProceed, InvalidName, AlreadyBound
  105. {
  106. if (a_name.length == 1)
  107. named_objects.bind(a_name [ 0 ], an_object);
  108. else
  109. {
  110. NamingContext context =
  111. (NamingContext) named_contexts.get(a_name [ 0 ]);
  112. context.bind(getSuffix(a_name), an_object);
  113. }
  114. }
  115. /**
  116. * Gives a child context name, valid in this context.
  117. *
  118. * @param a_name the name, being given to the child context.
  119. * @param a_context the child context being named.
  120. *
  121. * @throws AlreadyBound if the child context is already named in
  122. * the current context.
  123. */
  124. public void bind_context(NameComponent[] a_name, NamingContext a_context)
  125. throws NotFound, CannotProceed, InvalidName, AlreadyBound
  126. {
  127. if (a_name.length == 1)
  128. named_contexts.bind(a_name [ 0 ], a_context);
  129. else
  130. {
  131. NamingContext context =
  132. (NamingContext) named_contexts.get(a_name [ 0 ]);
  133. context.bind_context(getSuffix(a_name), a_context);
  134. }
  135. }
  136. /**
  137. * Create a new context and give it a given name (bound it)
  138. * in the current context.
  139. *
  140. * The context being created is returned by calling
  141. * {@link #new_context()}.
  142. *
  143. * @param a_name the name being given to the new context.
  144. *
  145. * @return the newly created context.
  146. *
  147. * @throws AlreadyBound if the name is already in use.
  148. * @throws InvalidName if the name has zero length or otherwise invalid.
  149. */
  150. public NamingContext bind_new_context(NameComponent[] a_name)
  151. throws NotFound, AlreadyBound, CannotProceed,
  152. InvalidName
  153. {
  154. if (named_contexts.containsKey(a_name [ 0 ]) ||
  155. named_objects.containsKey(a_name [ 0 ])
  156. )
  157. throw new AlreadyBound();
  158. NamingContext child = new_context();
  159. bind_context(a_name, child);
  160. return child;
  161. }
  162. /**
  163. * Destroy this context (must be empty).
  164. * @throws NotEmpty if the context being destroyed is not empty.
  165. */
  166. public void destroy()
  167. throws NotEmpty
  168. {
  169. if (named_contexts.size() > 0 || named_objects.size() > 0)
  170. throw new NotEmpty();
  171. }
  172. /**
  173. * Iterate over all bindings, defined in this namind context.
  174. *
  175. * @param amount the maximal number of context to return in the
  176. * holder a_list. The remaining bindings are accessible via iterator
  177. * an_iter. If the parameter amount is zero, all bindings are accessed only
  178. * via this iterator.
  179. *
  180. * This implementation list contexts first, then objects.
  181. *
  182. * @param a_list the holder, where the returned bindigs are stored.
  183. * @param an_iter the iterator that can be used to access the remaining
  184. * bindings.
  185. */
  186. public void list(int amount, BindingListHolder a_list,
  187. BindingIteratorHolder an_iter
  188. )
  189. {
  190. int nb = named_contexts.size() + named_objects.size();
  191. int nl = nb;
  192. if (nl > amount)
  193. nl = amount;
  194. a_list.value = new Binding[ nl ];
  195. Iterator contexts = named_contexts.entries().iterator();
  196. Iterator objects = named_objects.entries().iterator();
  197. // Create a binding list.
  198. for (int i = 0; i < nl; i++)
  199. {
  200. if (contexts.hasNext())
  201. a_list.value [ i ] = mkBinding(contexts.next(), BindingType.ncontext);
  202. else if (objects.hasNext())
  203. a_list.value [ i ] = mkBinding(objects.next(), BindingType.nobject);
  204. else
  205. throw new InternalError();
  206. }
  207. // Create an iterator.
  208. Binding[] remainder = new Binding[ nb - nl ];
  209. int p = 0;
  210. while (contexts.hasNext())
  211. remainder [ p++ ] = mkBinding(contexts.next(), BindingType.ncontext);
  212. while (objects.hasNext())
  213. remainder [ p++ ] = mkBinding(objects.next(), BindingType.nobject);
  214. Binding_iterator_impl bit = new Binding_iterator_impl(remainder);
  215. _orb().connect(bit);
  216. an_iter.value = bit;
  217. }
  218. /**
  219. * Creates a new naming context, not bound to any name.
  220. */
  221. public NamingContext new_context()
  222. {
  223. Ext context = new Ext(new TransientContext());
  224. // Connect the context to the current ORB:
  225. _orb().connect(context);
  226. return context;
  227. }
  228. /**
  229. * Names or renames the object.
  230. *
  231. * @param a_name the new name, being given to the object
  232. * in the scope of the current context. If the object is already
  233. * named in this context, it is renamed.
  234. *
  235. * @param an_object the object, being named.
  236. *
  237. * @throws InvalidName if the name has zero length or otherwise invalid.
  238. */
  239. public void rebind(NameComponent[] a_name, Object an_object)
  240. throws NotFound, CannotProceed, InvalidName
  241. {
  242. if (a_name.length == 1)
  243. named_objects.rebind(a_name [ 0 ], an_object);
  244. else
  245. {
  246. NamingContext context =
  247. (NamingContext) named_contexts.get(a_name [ 0 ]);
  248. context.rebind(getSuffix(a_name), an_object);
  249. }
  250. }
  251. /**
  252. * Names or renames the child context.
  253. * If the child context is already named in
  254. * the current context, it is renamed. The the name being given is in
  255. * use, the old meaning of the name is discarded.
  256. *
  257. * @param a_name the name, being given to the child context in the scope
  258. * of the current context.
  259. *
  260. * @param a_context the child context being named.
  261. *
  262. * @throws InvalidName if the name has zero length or otherwise invalid.
  263. */
  264. public void rebind_context(NameComponent[] a_name, NamingContext a_context)
  265. throws NotFound, CannotProceed, InvalidName
  266. {
  267. if (a_name.length == 1)
  268. named_contexts.rebind(a_name [ 0 ], a_context);
  269. else
  270. {
  271. NamingContext context =
  272. (NamingContext) named_contexts.get(a_name [ 0 ]);
  273. context.rebind_context(getSuffix(a_name), a_context);
  274. }
  275. }
  276. /**
  277. * Get the object, bound to the specified name in this
  278. * context. The given object must match the bound
  279. * name.
  280. *
  281. * This implementation resolves the names as defined in specification
  282. * of the CORBA naming service. This means, if the beginning of the
  283. * name can be resolved to some naming context, the request is
  284. * forwarded to this context, passing the unresolved name part as a
  285. * parameter. In this way, it is possible to have a hierarchy of the
  286. * naming services. The central services resolve the the beginning
  287. * of the name. The local services resolve the remaining nodes of the
  288. * name that may be relevant to some local details. It can be three or
  289. * more ranks of the naming services.
  290. *
  291. * @param a_name the object name.
  292. *
  293. * @return the object, matching this name. The client
  294. * usually casts or narrows (using the helper) the returned value
  295. * to the more specific type.
  296. *
  297. * @throws NotFound if the name cannot be resolved.
  298. * @throws InvalidName if the name has zero length or otherwise invalid.
  299. */
  300. public Object resolve(NameComponent[] a_name)
  301. throws NotFound, CannotProceed, InvalidName
  302. {
  303. NameValidator.check(a_name);
  304. if (a_name.length > 1)
  305. return resolveSubContext(a_name);
  306. else
  307. {
  308. // A single node name.
  309. org.omg.CORBA.Object object;
  310. object = named_objects.get(a_name [ 0 ]);
  311. if (object != null)
  312. return object;
  313. object = named_contexts.get(a_name [ 0 ]);
  314. if (object != null)
  315. return object;
  316. }
  317. throw new NotFound(NotFoundReason.missing_node, a_name);
  318. }
  319. /**
  320. * Removes the name from the binding context.
  321. *
  322. * @param a_name a name to remove.
  323. *
  324. * @throws InvalidName if the name has zero length or otherwise invalid.
  325. */
  326. public void unbind(NameComponent[] a_name)
  327. throws NotFound, CannotProceed, InvalidName
  328. {
  329. NameValidator.check(a_name);
  330. // Single node name - handle it.
  331. if (a_name.length == 1)
  332. {
  333. if (named_objects.containsKey(a_name [ 0 ]))
  334. named_objects.remove(a_name [ 0 ]);
  335. else if (named_contexts.containsKey(a_name [ 0 ]))
  336. named_contexts.remove(a_name [ 0 ]);
  337. else
  338. throw new NotFound(NotFoundReason.missing_node, a_name);
  339. }
  340. else
  341. {
  342. // Handle the first node and forward the command.
  343. NamingContext subcontext =
  344. (NamingContext) named_contexts.get(a_name [ 0 ]);
  345. if (subcontext == null)
  346. throw new NotFound(NotFoundReason.missing_node, a_name);
  347. subcontext.unbind(getSuffix(a_name));
  348. }
  349. }
  350. /**
  351. * Get the name suffix, discarding the first member.
  352. */
  353. private NameComponent[] getSuffix(NameComponent[] a_name)
  354. {
  355. NameComponent[] suffix = new NameComponent[ a_name.length - 1 ];
  356. System.arraycopy(a_name, 1, suffix, 0, suffix.length);
  357. return suffix;
  358. }
  359. /**
  360. * Create a binding.
  361. *
  362. * @param an_entry the entry, defining the bound object.
  363. * @param type the binding type.
  364. * @return the created binding.
  365. */
  366. private Binding mkBinding(java.lang.Object an_entry, BindingType type)
  367. {
  368. Map.Entry entry = (Map.Entry) an_entry;
  369. Binding b = new Binding();
  370. // The name component has always only one node (the current context)
  371. b.binding_name = new NameComponent[] { (NameComponent) entry.getKey() };
  372. b.binding_type = type;
  373. return b;
  374. }
  375. /**
  376. * Find the context, bound to the first name of the given
  377. * name, and pass the remainder (without the first node)
  378. * of the name for that context to resolve.
  379. *
  380. * @param a_name the name to resolve.
  381. *
  382. * @return the resolved context
  383. */
  384. private Object resolveSubContext(NameComponent[] a_name)
  385. throws NotFound, CannotProceed, InvalidName
  386. {
  387. // A multiple node name.
  388. // This context resolves the first node only.
  389. NamingContext context = (NamingContext) named_contexts.get(a_name [ 0 ]);
  390. if (context == null)
  391. throw new NotFound(NotFoundReason.missing_node, a_name);
  392. NameComponent[] suffix = getSuffix(a_name);
  393. return context.resolve(suffix);
  394. }
  395. }