001/* _NamingContextStub.java --
002   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package org.omg.CosNaming;
040
041import org.omg.CORBA.MARSHAL;
042import org.omg.CORBA.ObjectHelper;
043import org.omg.CORBA.portable.ApplicationException;
044import org.omg.CORBA.portable.Delegate;
045import org.omg.CORBA.portable.InputStream;
046import org.omg.CORBA.portable.ObjectImpl;
047import org.omg.CORBA.portable.OutputStream;
048import org.omg.CORBA.portable.RemarshalException;
049import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
050import org.omg.CosNaming.NamingContextPackage.AlreadyBoundHelper;
051import org.omg.CosNaming.NamingContextPackage.CannotProceed;
052import org.omg.CosNaming.NamingContextPackage.CannotProceedHelper;
053import org.omg.CosNaming.NamingContextPackage.InvalidName;
054import org.omg.CosNaming.NamingContextPackage.InvalidNameHelper;
055import org.omg.CosNaming.NamingContextPackage.NotEmpty;
056import org.omg.CosNaming.NamingContextPackage.NotEmptyHelper;
057import org.omg.CosNaming.NamingContextPackage.NotFound;
058import org.omg.CosNaming.NamingContextPackage.NotFoundHelper;
059
060/**
061 * The naming context stub (proxy), used on the client side.
062 * The {@link NamingContext} methods contain the code for remote
063 * invocaton.
064 *
065 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
066 */
067public class _NamingContextStub
068  extends ObjectImpl
069  implements NamingContext
070{
071  /**
072   * Use serialVersionUID (v1.4) for interoperability.
073   */
074  private static final long serialVersionUID = 6835430958405349379L;
075
076  /**
077   * Create the naming context stub.
078   */
079  public _NamingContextStub()
080  {
081    super();
082  }
083
084  /**
085   * Create the naming context stub with the given delegate.
086   */
087  _NamingContextStub(Delegate delegate)
088  {
089    super();
090    _set_delegate(delegate);
091  }
092
093  /**
094   * Return the array of repository ids for this object.
095   */
096  public String[] _ids()
097  {
098    return new String[] { NamingContextHelper.id() };
099  }
100
101  /** {@inheritDoc} */
102  public void bind(NameComponent[] a_name, org.omg.CORBA.Object an_object)
103            throws NotFound, CannotProceed, InvalidName, AlreadyBound
104  {
105    InputStream in = null;
106    try
107      {
108        OutputStream out = _request("bind", true);
109        NameHelper.write(out, a_name);
110        ObjectHelper.write(out, an_object);
111        in = _invoke(out);
112      }
113    catch (ApplicationException ex)
114      {
115        in = ex.getInputStream();
116
117        String id = ex.getId();
118        throw5(in, id);
119      }
120    catch (RemarshalException remarsh)
121      {
122        bind(a_name, an_object);
123      }
124    finally
125      {
126        _releaseReply(in);
127      }
128  }
129
130  /** {@inheritDoc} */
131  public void bind_context(NameComponent[] a_name, NamingContext a_context)
132                    throws NotFound, CannotProceed, InvalidName, AlreadyBound
133  {
134    InputStream in = null;
135    try
136      {
137        OutputStream out = _request("bind_context", true);
138        NameHelper.write(out, a_name);
139        NamingContextHelper.write(out, a_context);
140        in = _invoke(out);
141      }
142    catch (ApplicationException ex)
143      {
144        in = ex.getInputStream();
145
146        String id = ex.getId();
147        throw5(in, id);
148      }
149    catch (RemarshalException remarsh)
150      {
151        bind_context(a_name, a_context);
152      }
153    finally
154      {
155        _releaseReply(in);
156      }
157  }
158
159  /** {@inheritDoc} */
160  public NamingContext bind_new_context(NameComponent[] a_name)
161                                 throws NotFound, AlreadyBound, CannotProceed,
162                                        InvalidName
163  {
164    InputStream in = null;
165    try
166      {
167        OutputStream out = _request("bind_new_context", true);
168        NameHelper.write(out, a_name);
169        in = _invoke(out);
170
171        NamingContext __result = NamingContextHelper.read(in);
172        return __result;
173      }
174    catch (ApplicationException ex)
175      {
176        in = ex.getInputStream();
177
178        String id = ex.getId();
179        throw5(in, id);
180        throw new InternalError();
181      }
182    catch (RemarshalException remarsh)
183      {
184        return bind_new_context(a_name);
185      }
186    finally
187      {
188        _releaseReply(in);
189      }
190  }
191
192  /** {@inheritDoc} */
193  public void destroy()
194               throws NotEmpty
195  {
196    InputStream in = null;
197    try
198      {
199        OutputStream out = _request("destroy", true);
200        in = _invoke(out);
201      }
202    catch (ApplicationException ex)
203      {
204        in = ex.getInputStream();
205
206        String id = ex.getId();
207        if (id.equals(NotEmptyHelper.id()))
208          throw NotEmptyHelper.read(in);
209        else
210          throw new MARSHAL(id);
211      }
212    catch (RemarshalException remarsh)
213      {
214        destroy();
215      }
216    finally
217      {
218        _releaseReply(in);
219      }
220  }
221
222  /** {@inheritDoc} */
223  public void list(int amount, BindingListHolder a_list,
224                   BindingIteratorHolder an_iter
225                  )
226  {
227    InputStream in = null;
228    try
229      {
230        OutputStream out = _request("list", true);
231        out.write_ulong(amount);
232        in = _invoke(out);
233        a_list.value = BindingListHelper.read(in);
234        an_iter.value = BindingIteratorHelper.read(in);
235      }
236    catch (ApplicationException ex)
237      {
238        in = ex.getInputStream();
239        throw new MARSHAL(ex.getId());
240      }
241    catch (RemarshalException remarsh)
242      {
243        list(amount, a_list, an_iter);
244      }
245    finally
246      {
247        _releaseReply(in);
248      }
249  }
250
251  /** {@inheritDoc} */
252  public NamingContext new_context()
253  {
254    InputStream in = null;
255    try
256      {
257        OutputStream out = _request("new_context", true);
258        in = _invoke(out);
259
260        NamingContext __result = NamingContextHelper.read(in);
261        return __result;
262      }
263    catch (ApplicationException ex)
264      {
265        in = ex.getInputStream();
266        throw new MARSHAL(ex.getId());
267      }
268    catch (RemarshalException remarsh)
269      {
270        return new_context();
271      }
272    finally
273      {
274        _releaseReply(in);
275      }
276  }
277
278  /** {@inheritDoc} */
279  public void rebind(NameComponent[] a_name, org.omg.CORBA.Object an_object)
280              throws NotFound, CannotProceed, InvalidName
281  {
282    InputStream in = null;
283    try
284      {
285        OutputStream out = _request("rebind", true);
286        NameHelper.write(out, a_name);
287        ObjectHelper.write(out, an_object);
288        in = _invoke(out);
289      }
290    catch (ApplicationException ex)
291      {
292        in = ex.getInputStream();
293
294        String id = ex.getId();
295        throw4(in, id);
296      }
297    catch (RemarshalException remarsh)
298      {
299        rebind(a_name, an_object);
300      }
301    finally
302      {
303        _releaseReply(in);
304      }
305  }
306
307  /** {@inheritDoc} */
308  public void rebind_context(NameComponent[] a_name, NamingContext a_context)
309                      throws NotFound, CannotProceed, InvalidName
310  {
311    InputStream in = null;
312    try
313      {
314        OutputStream out = _request("rebind_context", true);
315        NameHelper.write(out, a_name);
316        NamingContextHelper.write(out, a_context);
317        in = _invoke(out);
318      }
319    catch (ApplicationException ex)
320      {
321        in = ex.getInputStream();
322
323        String id = ex.getId();
324        throw4(in, id);
325      }
326    catch (RemarshalException remarsh)
327      {
328        rebind_context(a_name, a_context);
329      }
330    finally
331      {
332        _releaseReply(in);
333      }
334  }
335
336  /** {@inheritDoc} */
337  public org.omg.CORBA.Object resolve(NameComponent[] a_name)
338                               throws NotFound, CannotProceed, InvalidName
339  {
340    InputStream in = null;
341    try
342      {
343        OutputStream out = _request("resolve", true);
344        NameHelper.write(out, a_name);
345        in = _invoke(out);
346
347        org.omg.CORBA.Object __result = ObjectHelper.read(in);
348        return __result;
349      }
350    catch (ApplicationException ex)
351      {
352        in = ex.getInputStream();
353
354        String id = ex.getId();
355        throw4(in, id);
356        throw new InternalError();
357      }
358    catch (RemarshalException remarsh)
359      {
360        return resolve(a_name);
361      }
362    finally
363      {
364        _releaseReply(in);
365      }
366  }
367
368  /** {@inheritDoc} */
369  public void unbind(NameComponent[] a_name)
370              throws NotFound, CannotProceed, InvalidName
371  {
372    InputStream in = null;
373    try
374      {
375        OutputStream out = _request("unbind", true);
376        NameHelper.write(out, a_name);
377        in = _invoke(out);
378      }
379    catch (ApplicationException ex)
380      {
381        in = ex.getInputStream();
382
383        String id = ex.getId();
384        if (id.equals(NotFoundHelper.id()))
385          throw NotFoundHelper.read(in);
386        else if (id.equals(CannotProceedHelper.id()))
387          throw CannotProceedHelper.read(in);
388        else if (id.equals(InvalidNameHelper.id()))
389          throw InvalidNameHelper.read(in);
390        else
391          throw new MARSHAL(id);
392      }
393    catch (RemarshalException remarsh)
394      {
395        unbind(a_name);
396      }
397    finally
398      {
399        _releaseReply(in);
400      }
401  }
402
403  /**
404   * Throw one of the three possible exceptions, as specified in
405   * the passed exception repository id.
406   *
407   * This method should never return normally.
408   *
409   * @param in the stream to read the exception from.
410   * @param id the exception id.
411   *
412   * @throws InvalidName if the id matches.
413   * @throws CannotProceed if the id matches.
414   * @throws NotFound if the id matches.
415   * @throws MARSHAL if the id does not match any of the previous 4 exceptions.
416   */
417  void throw4(InputStream in, String id)
418                 throws MARSHAL, InvalidName, CannotProceed, NotFound
419  {
420    if (id.equals(NotFoundHelper.id()))
421      throw NotFoundHelper.read(in);
422    else if (id.equals(CannotProceedHelper.id()))
423      throw CannotProceedHelper.read(in);
424    else if (id.equals(InvalidNameHelper.id()))
425      throw InvalidNameHelper.read(in);
426    else
427      throw new MARSHAL(id);
428  }
429
430  /**
431   * Throw one of the five possible exceptions, as specified in
432   * the passed exception repository id.
433   *
434   * This method should never return normally.
435   *
436   * @param in the stream to read the exception from.
437   * @param id the exception id.
438   *
439   * @throws AlreadyBound if the id matches.
440   * @throws InvalidName if the id matches.
441   * @throws CannotProceed if the id matches.
442   * @throws NotFound if the id matches.
443   * @throws MARSHAL if the id does not match any of the previous 4 exceptions.
444   */
445  void throw5(InputStream in, String id)
446                 throws MARSHAL, AlreadyBound, InvalidName, CannotProceed,
447                        NotFound
448  {
449    if (id.equals(AlreadyBoundHelper.id()))
450      throw AlreadyBoundHelper.read(in);
451    else
452      throw4(in, id);
453  }
454}