001    /* _NamingContextImplBase.java --
002       Copyright (C) 2005 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package org.omg.CosNaming;
040    
041    import gnu.CORBA.Minor;
042    
043    import org.omg.CORBA.BAD_OPERATION;
044    import org.omg.CORBA.CompletionStatus;
045    import org.omg.CORBA.DynamicImplementation;
046    import org.omg.CORBA.ObjectHelper;
047    import org.omg.CORBA.ObjectHolder;
048    import org.omg.CORBA.ServerRequest;
049    import org.omg.CORBA.portable.InputStream;
050    import org.omg.CORBA.portable.InvokeHandler;
051    import org.omg.CORBA.portable.OutputStream;
052    import org.omg.CORBA.portable.ResponseHandler;
053    import org.omg.CORBA.portable.Streamable;
054    import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
055    import org.omg.CosNaming.NamingContextPackage.AlreadyBoundHelper;
056    import org.omg.CosNaming.NamingContextPackage.CannotProceed;
057    import org.omg.CosNaming.NamingContextPackage.CannotProceedHelper;
058    import org.omg.CosNaming.NamingContextPackage.InvalidName;
059    import org.omg.CosNaming.NamingContextPackage.InvalidNameHelper;
060    import org.omg.CosNaming.NamingContextPackage.NotEmpty;
061    import org.omg.CosNaming.NamingContextPackage.NotEmptyHelper;
062    import org.omg.CosNaming.NamingContextPackage.NotFound;
063    import org.omg.CosNaming.NamingContextPackage.NotFoundHelper;
064    
065    import java.util.Hashtable;
066    
067    /**
068     * The naming context implementation base.
069     *
070     * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
071     */
072    public abstract class _NamingContextImplBase
073      extends DynamicImplementation
074      implements NamingContext, InvokeHandler
075    {
076      /**
077       * Use serialVersionUID (v1.4) for interoperability.
078       */
079      private static final long serialVersionUID = -114280294134561035L;
080    
081      /**
082       * As there are quite many methods, it may be sensible to use the hashtable.
083       * This field is also reused in NamingContextPOA.
084       */
085      static Hashtable<String,Integer> methods = new Hashtable<String,Integer>();
086    
087      /**
088       * Put all methods into the table.
089       */
090      static
091      {
092        methods.put("bind", new Integer(0));
093        methods.put("rebind", new Integer(1));
094        methods.put("bind_context", new Integer(2));
095        methods.put("rebind_context", new Integer(3));
096        methods.put("resolve", new Integer(4));
097        methods.put("unbind", new Integer(5));
098        methods.put("new_context", new Integer(6));
099        methods.put("bind_new_context", new Integer(7));
100        methods.put("destroy", new Integer(8));
101        methods.put("list", new Integer(9));
102      }
103    
104      /**
105       * Return the array of repository ids.
106       */
107      public String[] _ids()
108      {
109        return new String[] { NamingContextHelper.id() };
110      }
111    
112      /**
113       * The server calls this method after receiving the request message
114       * from client. The implementation base calls one of its abstract
115       * methods to perform the requested operation.
116       *
117       * @param method the method being invoked.
118       * @param in the stream to read parameters from.
119       * @param rh the handler to get a stream for writing a response.
120       *
121       * @return the stream, returned by the handler.
122       */
123      public OutputStream _invoke(String method, InputStream in, ResponseHandler rh)
124      {
125        OutputStream out = null;
126        Integer call_method = (Integer) methods.get(method);
127        if (call_method == null)
128          throw new BAD_OPERATION(Minor.Method, CompletionStatus.COMPLETED_MAYBE);
129    
130        switch (call_method.intValue())
131          {
132            case 0 : // bind
133            {
134              try
135                {
136                  NameComponent[] a_name = NameHelper.read(in);
137                  org.omg.CORBA.Object an_object = ObjectHelper.read(in);
138                  bind(a_name, an_object);
139                  out = rh.createReply();
140                }
141              catch (NotFound ex)
142                {
143                  out = rh.createExceptionReply();
144                  NotFoundHelper.write(out, ex);
145                }
146              catch (CannotProceed ex)
147                {
148                  out = rh.createExceptionReply();
149                  CannotProceedHelper.write(out, ex);
150                }
151              catch (InvalidName ex)
152                {
153                  out = rh.createExceptionReply();
154                  InvalidNameHelper.write(out, ex);
155                }
156              catch (AlreadyBound ex)
157                {
158                  out = rh.createExceptionReply();
159                  AlreadyBoundHelper.write(out, ex);
160                }
161              break;
162            }
163    
164            case 1 : // rebind
165            {
166              try
167                {
168                  NameComponent[] a_name = NameHelper.read(in);
169                  org.omg.CORBA.Object an_object = ObjectHelper.read(in);
170                  rebind(a_name, an_object);
171                  out = rh.createReply();
172                }
173              catch (NotFound ex)
174                {
175                  out = rh.createExceptionReply();
176                  NotFoundHelper.write(out, ex);
177                }
178              catch (CannotProceed ex)
179                {
180                  out = rh.createExceptionReply();
181                  CannotProceedHelper.write(out, ex);
182                }
183              catch (InvalidName ex)
184                {
185                  out = rh.createExceptionReply();
186                  InvalidNameHelper.write(out, ex);
187                }
188              break;
189            }
190    
191            case 2 : // bind_context
192            {
193              try
194                {
195                  NameComponent[] a_name = NameHelper.read(in);
196                  NamingContext a_context = NamingContextHelper.read(in);
197                  bind_context(a_name, a_context);
198                  out = rh.createReply();
199                }
200              catch (NotFound ex)
201                {
202                  out = rh.createExceptionReply();
203                  NotFoundHelper.write(out, ex);
204                }
205              catch (CannotProceed ex)
206                {
207                  out = rh.createExceptionReply();
208                  CannotProceedHelper.write(out, ex);
209                }
210              catch (InvalidName ex)
211                {
212                  out = rh.createExceptionReply();
213                  InvalidNameHelper.write(out, ex);
214                }
215              catch (AlreadyBound ex)
216                {
217                  out = rh.createExceptionReply();
218                  AlreadyBoundHelper.write(out, ex);
219                }
220              break;
221            }
222    
223            case 3 : // rebind_context
224            {
225              try
226                {
227                  NameComponent[] a_name = NameHelper.read(in);
228                  NamingContext a_context = NamingContextHelper.read(in);
229                  rebind_context(a_name, a_context);
230                  out = rh.createReply();
231                }
232              catch (NotFound ex)
233                {
234                  out = rh.createExceptionReply();
235                  NotFoundHelper.write(out, ex);
236                }
237              catch (CannotProceed ex)
238                {
239                  out = rh.createExceptionReply();
240                  CannotProceedHelper.write(out, ex);
241                }
242              catch (InvalidName ex)
243                {
244                  out = rh.createExceptionReply();
245                  InvalidNameHelper.write(out, ex);
246                }
247              break;
248            }
249    
250            case 4 : // resolve
251            {
252              try
253                {
254                  NameComponent[] a_name = NameHelper.read(in);
255                  org.omg.CORBA.Object __result = null;
256                  __result = resolve(a_name);
257                  out = rh.createReply();
258                  ObjectHelper.write(out, __result);
259                }
260              catch (NotFound ex)
261                {
262                  out = rh.createExceptionReply();
263                  NotFoundHelper.write(out, ex);
264                }
265              catch (CannotProceed ex)
266                {
267                  out = rh.createExceptionReply();
268                  CannotProceedHelper.write(out, ex);
269                }
270              catch (InvalidName ex)
271                {
272                  out = rh.createExceptionReply();
273                  InvalidNameHelper.write(out, ex);
274                }
275              break;
276            }
277    
278            case 5 : // unbind
279            {
280              try
281                {
282                  NameComponent[] a_name = NameHelper.read(in);
283                  unbind(a_name);
284                  out = rh.createReply();
285                }
286              catch (NotFound ex)
287                {
288                  out = rh.createExceptionReply();
289                  NotFoundHelper.write(out, ex);
290                }
291              catch (CannotProceed ex)
292                {
293                  out = rh.createExceptionReply();
294                  CannotProceedHelper.write(out, ex);
295                }
296              catch (InvalidName ex)
297                {
298                  out = rh.createExceptionReply();
299                  InvalidNameHelper.write(out, ex);
300                }
301              break;
302            }
303    
304            case 6 : // new_context
305            {
306              NamingContext __result = null;
307              __result = new_context();
308              out = rh.createReply();
309              NamingContextHelper.write(out, __result);
310              break;
311            }
312    
313            case 7 : // bind_new_context
314            {
315              try
316                {
317                  NameComponent[] a_name = NameHelper.read(in);
318                  NamingContext __result = null;
319                  __result = bind_new_context(a_name);
320                  out = rh.createReply();
321                  NamingContextHelper.write(out, __result);
322                }
323              catch (NotFound ex)
324                {
325                  out = rh.createExceptionReply();
326                  NotFoundHelper.write(out, ex);
327                }
328              catch (AlreadyBound ex)
329                {
330                  out = rh.createExceptionReply();
331                  AlreadyBoundHelper.write(out, ex);
332                }
333              catch (CannotProceed ex)
334                {
335                  out = rh.createExceptionReply();
336                  CannotProceedHelper.write(out, ex);
337                }
338              catch (InvalidName ex)
339                {
340                  out = rh.createExceptionReply();
341                  InvalidNameHelper.write(out, ex);
342                }
343              break;
344            }
345    
346            case 8 : // destroy
347            {
348              try
349                {
350                  destroy();
351                  out = rh.createReply();
352                }
353              catch (NotEmpty ex)
354                {
355                  out = rh.createExceptionReply();
356                  NotEmptyHelper.write(out, ex);
357                }
358              break;
359            }
360    
361            case 9 : // list
362            {
363              int amount = in.read_ulong();
364              BindingListHolder a_list = new BindingListHolder();
365              BindingIteratorHolder an_iter = new BindingIteratorHolder();
366              list(amount, a_list, an_iter);
367              out = rh.createReply();
368              BindingListHelper.write(out, a_list.value);
369              BindingIteratorHelper.write(out, an_iter.value);
370              break;
371            }
372    
373            default :
374              throw new BAD_OPERATION(0, CompletionStatus.COMPLETED_MAYBE);
375          }
376    
377        return out;
378      }
379    
380      /**
381       * The obsolete invocation using server request. Implemented for
382       * compatibility reasons, but is it more effectinve to use
383       * {@link #_invoke}.
384       *
385       * @param request a server request.
386       */
387      public void invoke(ServerRequest request)
388      {
389        Streamable result = null;
390    
391        // The server request contains no required result type.
392        Integer call_method = (Integer) methods.get(request.operation());
393        if (call_method == null)
394          throw new BAD_OPERATION(Minor.Method, CompletionStatus.COMPLETED_MAYBE);
395    
396        switch (call_method.intValue())
397          {
398            case 4 : // resolve, object
399              result = new ObjectHolder();
400              break;
401    
402            case 6 : // new_context, NamingContext
403            case 7 : // bind_new_context, NamingContext
404            {
405              result = new NamingContextHolder();
406              break;
407            }
408    
409            default : // void for others.
410              result = null;
411          }
412    
413        gnu.CORBA.ServiceRequestAdapter.invoke(request, this, result);
414      }
415    }