/[classpath]/classpath/gnu/CORBA/Poa/LocalRequest.java
ViewVC logotype

Diff of /classpath/gnu/CORBA/Poa/LocalRequest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.2.1 by gnu_andrew, Tue Aug 2 20:12:08 2005 UTC revision 1.1.2.2 by gnu_andrew, Sat Sep 10 15:31:36 2005 UTC
# Line 40  package gnu.CORBA.Poa; Line 40  package gnu.CORBA.Poa;
40    
41  import gnu.CORBA.CDR.cdrBufOutput;  import gnu.CORBA.CDR.cdrBufOutput;
42  import gnu.CORBA.GIOP.MessageHeader;  import gnu.CORBA.GIOP.MessageHeader;
43    import gnu.CORBA.GIOP.v1_2.ReplyHeader;
44    import gnu.CORBA.GIOP.v1_2.RequestHeader;
45    import gnu.CORBA.Interceptor.gnuClientRequestInfo;
46    import gnu.CORBA.Interceptor.gnuServerRequestInfo;
47    import gnu.CORBA.ObjectCreator;
48  import gnu.CORBA.Unexpected;  import gnu.CORBA.Unexpected;
49  import gnu.CORBA.gnuAny;  import gnu.CORBA.gnuAny;
50  import gnu.CORBA.gnuRequest;  import gnu.CORBA.gnuRequest;
51    import gnu.CORBA.recordTypeCode;
52  import gnu.CORBA.streamReadyHolder;  import gnu.CORBA.streamReadyHolder;
53  import gnu.CORBA.streamRequest;  import gnu.CORBA.streamRequest;
54    
55  import org.omg.CORBA.ARG_OUT;  import org.omg.CORBA.ARG_OUT;
56    import org.omg.CORBA.Any;
57  import org.omg.CORBA.BAD_INV_ORDER;  import org.omg.CORBA.BAD_INV_ORDER;
58  import org.omg.CORBA.BAD_OPERATION;  import org.omg.CORBA.BAD_OPERATION;
59  import org.omg.CORBA.Bounds;  import org.omg.CORBA.Bounds;
60  import org.omg.CORBA.NamedValue;  import org.omg.CORBA.NamedValue;
61  import org.omg.CORBA.ORB;  import org.omg.CORBA.ORB;
62    import org.omg.CORBA.SystemException;
63    import org.omg.CORBA.TCKind;
64  import org.omg.CORBA.UnknownUserException;  import org.omg.CORBA.UnknownUserException;
65    import org.omg.CORBA.UserException;
66  import org.omg.CORBA.portable.ApplicationException;  import org.omg.CORBA.portable.ApplicationException;
67    import org.omg.CORBA.portable.InputStream;
68  import org.omg.CORBA.portable.InvokeHandler;  import org.omg.CORBA.portable.InvokeHandler;
69    import org.omg.CORBA.portable.ObjectImpl;
70  import org.omg.CORBA.portable.OutputStream;  import org.omg.CORBA.portable.OutputStream;
71  import org.omg.CORBA.portable.ResponseHandler;  import org.omg.CORBA.portable.ResponseHandler;
72    import org.omg.PortableInterceptor.ClientRequestInterceptorOperations;
73    import org.omg.PortableInterceptor.ForwardRequest;
74    import org.omg.PortableInterceptor.ServerRequestInterceptorOperations;
75  import org.omg.PortableServer.CurrentOperations;  import org.omg.PortableServer.CurrentOperations;
76  import org.omg.PortableServer.CurrentPackage.NoContext;  import org.omg.PortableServer.CurrentPackage.NoContext;
77  import org.omg.PortableServer.DynamicImplementation;  import org.omg.PortableServer.DynamicImplementation;
# Line 68  import org.omg.PortableServer.portable.D Line 83  import org.omg.PortableServer.portable.D
83  import java.io.IOException;  import java.io.IOException;
84    
85  /**  /**
86   * Directs the invocation to the locally available servant.   * Directs the invocation to the locally available servant. The POA servant does
87   * The POA servant does not longer implement the CORBA object and   * not longer implement the CORBA object and cannot be substituted directly.
  * cannot be substituted directly.  
88   *   *
89   * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)   * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
90   */   */
91  public class LocalRequest  public class LocalRequest extends gnuRequest implements ResponseHandler,
92    extends gnuRequest    CurrentOperations
   implements ResponseHandler, CurrentOperations  
93  {  {
94    /**    /**
95     * Used by servant locator, if involved.     * Used by servant locator, if involved.
# Line 94  public class LocalRequest Line 107  public class LocalRequest
107    private static final MessageHeader header = new MessageHeader();    private static final MessageHeader header = new MessageHeader();
108    
109    /**    /**
110     * True if the stream was obtained by invoking {@link #createExceptionReply()},         * True if the stream was obtained by invoking {@link #createExceptionReply()},
111     * false otherwise.     * false otherwise.
112     */     */
113    boolean exceptionReply;    boolean exceptionReply;
# Line 129  public class LocalRequest Line 142  public class LocalRequest
142    
143      // Instantiate the cookie holder only if required.      // Instantiate the cookie holder only if required.
144      if (poa.servant_locator != null)      if (poa.servant_locator != null)
145        cookie = new CookieHolder();        {
146            cookie = new CookieHolder();
147          }
148      object = local_object;      object = local_object;
149      prepareStream();      prepareStream();
150    }    }
151    
152    /**    /**
153     * Make an invocation and return a stream from where the results     * Make an invocation and return a stream from where the results can be read
154     * can be read and throw ApplicationException, where applicable.     * and throw ApplicationException, where applicable.
155     */     */
156    org.omg.CORBA.portable.InputStream s_invoke(InvokeHandler handler)    org.omg.CORBA.portable.InputStream s_invoke(InvokeHandler handler)
157                                         throws ApplicationException      throws ApplicationException
158    {    {
159      try      try
160        {        {
# Line 148  public class LocalRequest Line 163  public class LocalRequest
163          org.omg.CORBA.portable.InputStream input = v_invoke(handler);          org.omg.CORBA.portable.InputStream input = v_invoke(handler);
164    
165          if (!exceptionReply)          if (!exceptionReply)
166            return input;            {
167                return input;
168              }
169          else          else
170            {            {
171              input.mark(500);              input.mark(500);
# Line 174  public class LocalRequest Line 191  public class LocalRequest
191    }    }
192    
193    /**    /**
194     * Make an invocation and return a stream from where the results     * Make an invocation and return a stream from where the results can be read.
    * can be read.  
195     *     *
196     * @param the invoke handler (can be null, then it is obtained self     * @param the invoke handler (can be null, then it is obtained self
197     * dependently).     * dependently).
198     */     */
199    public org.omg.CORBA.portable.InputStream v_invoke(InvokeHandler handler)    public org.omg.CORBA.portable.InputStream v_invoke(InvokeHandler handler)
200    {    {
201      if (handler == null)      // Local request must be intercepted both by server and request
202        handler = object.getHandler(operation(), cookie, false);      // interceptors.
203        boolean s_intercept = false;
204        ServerRequestInterceptorOperations s_interceptor = null;
205        gnuServerRequestInfo s_info = null;
206    
207        boolean c_intercept = false;
208        ClientRequestInterceptorOperations c_interceptor = null;
209        gnuClientRequestInfo c_info = null;
210    
211      cdrBufOutput request_part = new cdrBufOutput();      try
212          {
213            if (poa.m_orb.iServer != null || poa.m_orb.iClient != null)
214              {
215                setORB(poa.m_orb);
216    
217      request_part.setOrb(orb());              // These two are only needed with interceptors.
218                m_rqh = new RequestHeader();
219                m_rqh.operation = m_operation;
220                m_rph = new ReplyHeader();
221    
222      if (m_args != null && m_args.count() > 0)              m_rqh.object_key = object.Id;
223        {              m_rph.request_id = m_rqh.request_id;
224          write_parameters(header, request_part);            }
225    
226            if (poa.m_orb.iClient != null)
227              {
228                c_interceptor = poa.m_orb.iClient;
229    
230                c_info = new gnuClientRequestInfo(this);
231                c_intercept = true;
232    
233                c_interceptor.send_request(c_info);
234    
235                m_target = object;
236              }
237    
238            if (poa.m_orb.iServer != null)
239              {
240                s_interceptor = poa.m_orb.iServer;
241    
242                s_info = new gnuServerRequestInfo(object, m_rqh, m_rph);
243                s_info.m_request = this;
244    
245                s_intercept = true;
246    
247                s_interceptor.receive_request_service_contexts(s_info);
248              }
249    
250            if (handler == null)
251              {
252                handler = object.getHandler(operation(), cookie, false);
253              }
254    
255            cdrBufOutput request_part = new cdrBufOutput();
256    
257            request_part.setOrb(orb());
258    
259            if (m_args != null && m_args.count() > 0)
260              {
261                write_parameters(header, request_part);
262    
263                if (m_parameter_buffer != null)
264                  {
265                    throw new BAD_INV_ORDER("Please either add parameters or " +
266                      "write them into stream, but not both " + "at once."
267                    );
268                  }
269              }
270    
271          if (m_parameter_buffer != null)          if (m_parameter_buffer != null)
272            throw new BAD_INV_ORDER("Please either add parameters or " +            {
273                                    "write them into stream, but not both " +              write_parameter_buffer(header, request_part);
274                                    "at once."            }
                                  );  
       }  
275    
276      if (m_parameter_buffer != null)          Servant servant;
       {  
         write_parameter_buffer(header, request_part);  
       }  
277    
278      Servant servant;          if (handler instanceof Servant)
279              {
280                servant = (Servant) handler;
281              }
282            else
283              {
284                throw new BAD_OPERATION("Unexpected handler type " + handler);
285              }
286    
287      if (handler instanceof Servant)          org.omg.CORBA.portable.InputStream input =
288        servant = (Servant) handler;            request_part.create_input_stream();
     else  
       throw new BAD_OPERATION("Unexpected handler type " + handler);  
289    
290      org.omg.CORBA.portable.InputStream input =          // Ensure the servant (handler) has a delegate set.
291        request_part.create_input_stream();          servantDelegate sd = null;
292    
293      // Ensure the servant (handler) has a delegate set.          Delegate d = null;
     servantDelegate sd = null;  
294    
295      Delegate d = null;          try
296              {
297                d = servant._get_delegate();
298              }
299            catch (Exception ex)
300              {
301                // In some cases exception is thrown if the delegate is not set.
302              }
303            if (d instanceof servantDelegate)
304              {
305                // If the delegate is already set, try to reuse the existing
306                // instance.
307                sd = (servantDelegate) d;
308                if (sd.object != object)
309                  {
310                    sd = new servantDelegate(servant, poa, Id);
311                  }
312              }
313            else
314              {
315                sd = new servantDelegate(servant, poa, Id);
316              }
317            servant._set_delegate(sd);
318    
319      try          try
320        {            {
321          d = servant._get_delegate();              ORB o = orb();
322        }              if (o instanceof ORB_1_4)
323      catch (Exception ex)                {
324        {                  ((ORB_1_4) o).currents.put(Thread.currentThread(), this);
325          // In some cases exception is thrown if the delegate is not set.                }
326        }  
327      if (d instanceof servantDelegate)              try
328        {                {
329          // If the delegate is already set, try to reuse the existing                  if (s_intercept)
330          // instance.                    {
331          sd = (servantDelegate) d;                      s_interceptor.receive_request(s_info);
332          if (sd.object != object)                    }
333            sd = new servantDelegate(servant, poa, Id);                  handler._invoke(m_operation, input, this);
334    
335                    // Handler is casted into i_handler.
336                    if ((s_intercept || c_intercept) && isExceptionReply())
337                      {
338                        s_info.m_reply_header.reply_status =
339                          ReplyHeader.USER_EXCEPTION;
340                        m_rph.reply_status = ReplyHeader.USER_EXCEPTION;
341    
342                        // Make Any, holding the user exception.
343                        Any a = new gnuAny();
344                        OutputStream buf = getBuffer();
345                        InputStream in = buf.create_input_stream();
346                        String uex_idl = "unknown";
347                        try
348                          {
349                            in.mark(Integer.MAX_VALUE);
350                            uex_idl = in.read_string();
351                            m_exception_id = uex_idl;
352                            in.reset();
353                          }
354                        catch (IOException e)
355                          {
356                            throw new Unexpected(e);
357                          }
358    
359                        try
360                          {
361                            UserException exception =
362                              ObjectCreator.readUserException(uex_idl, in);
363    
364                            m_environment.exception(exception);
365                            ObjectCreator.insertWithHelper(a, exception);
366                          }
367                        catch (Exception e)
368                          {
369                            // Failed due any reason, insert without
370                            // helper.
371                            a.insert_Streamable(new streamReadyHolder(
372                                buf.create_input_stream()
373                              )
374                            );
375    
376                            recordTypeCode r =
377                              new recordTypeCode(TCKind.tk_except);
378                            r.setId(uex_idl);
379                            r.setName(ObjectCreator.getDefaultName(uex_idl));
380                          }
381    
382                        s_info.m_usr_exception = a;
383                        c_info.m_wrapped_exception = a;
384                        s_interceptor.send_exception(s_info);
385                        c_interceptor.receive_exception(c_info);
386                      }
387                    else
388                      {
389                        if (s_intercept)
390                          {
391                            s_info.m_reply_header.reply_status =
392                              ReplyHeader.NO_EXCEPTION;
393                            s_interceptor.send_reply(s_info);
394                          }
395                        if (c_intercept)
396                          {
397                            m_rph.reply_status = ReplyHeader.NO_EXCEPTION;
398                            c_interceptor.receive_reply(c_info);
399                          }
400                      }
401                  }
402                catch (SystemException sys_ex)
403                  {
404                    if (s_intercept)
405                      {
406                        s_info.m_reply_header.reply_status =
407                          ReplyHeader.SYSTEM_EXCEPTION;
408                        s_info.m_sys_exception = sys_ex;
409                        s_interceptor.send_exception(s_info);
410                      }
411    
412                    if (c_intercept)
413                      {
414                        m_rph.reply_status = ReplyHeader.SYSTEM_EXCEPTION;
415    
416                        Any a = new gnuAny();
417                        if (ObjectCreator.insertSysException(a, sys_ex))
418                          {
419                            c_info.m_wrapped_exception = a;
420                          }
421                        c_interceptor.receive_exception(c_info);
422                      }
423    
424                    throw sys_ex;
425                  }
426              }
427            finally
428              {
429                ORB o = orb();
430                if (o instanceof ORB_1_4)
431                  {
432                    ((ORB_1_4) o).currents.remove(Thread.currentThread());
433                  }
434              }
435    
436            if (poa.servant_locator != null)
437              {
438                poa.servant_locator.postinvoke(object.Id, poa, operation(),
439                  cookie.value, object.getServant()
440                );
441              }
442            return buffer.create_input_stream();
443        }        }
     else  
       sd = new servantDelegate(servant, poa, Id);  
     servant._set_delegate(sd);  
444    
445      try      catch (ForwardRequest fex)
446        {        {
447          ORB o = orb();          // May be thrown by interceptor.
448          if (o instanceof ORB_1_4)          if (s_intercept)
449            {            {
450              ((ORB_1_4) o).currents.put(Thread.currentThread(), this);              Forwarding:
451                while (true)
452                  {
453                    s_info.m_reply_header.reply_status =
454                      ReplyHeader.LOCATION_FORWARD;
455                    s_info.m_forward_reference = fex.forward;
456                    try
457                      {
458                        s_interceptor.send_other(s_info);
459                        break Forwarding;
460                      }
461                    catch (ForwardRequest fex2)
462                      {
463                        s_info.m_forward_reference = fex2.forward;
464                        fex.forward = s_info.m_forward_reference;
465                      }
466                  }
467            }            }
468    
469          handler._invoke(m_operation, input, this);          if (c_intercept)
470              {
471                this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
472                this.m_forwarding_target = fex.forward;
473                try
474                  {
475                    c_interceptor.receive_other(c_info);
476                  }
477                catch (ForwardRequest fex2)
478                  {
479                    fex.forward = fex2.forward;
480                  }
481              }
482            throw new gnuForwardRequest(fex.forward);
483        }        }
484      finally      catch (gnuForwardRequest fex)
485        {        {
486          ORB o = orb();          // May be thrown during activation.
487          if (o instanceof ORB_1_4)          // May be thrown during activation.
488            ((ORB_1_4) o).currents.remove(Thread.currentThread());          if (s_intercept)
489        }            {
490                Forwarding:
491      if (poa.servant_locator != null)              while (true)
492        poa.servant_locator.postinvoke(object.Id, poa, operation(), cookie.value,                {
493                                       object.getServant()                  s_info.m_reply_header.reply_status =
494                                      );                    ReplyHeader.LOCATION_FORWARD;
495                    s_info.m_forward_reference = fex.forward_reference;
496                    try
497                      {
498                        s_interceptor.send_other(s_info);
499                        break Forwarding;
500                      }
501                    catch (ForwardRequest fex2)
502                      {
503                        s_info.m_forward_reference = fex2.forward;
504                        fex.forward_reference = (ObjectImpl) fex2.forward;
505                      }
506                  }
507              }
508    
509      return buffer.create_input_stream();          if (c_intercept)
510              {
511                this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
512                this.m_forwarding_target = fex.forward_reference;
513                try
514                  {
515                    c_interceptor.receive_other(c_info);
516                  }
517                catch (ForwardRequest fex2)
518                  {
519                    fex.forward_reference = (ObjectImpl) fex2.forward;
520                  }
521              }
522            throw fex;
523          }
524    }    }
525    
526    /**    /**
527     * Make an invocation and store the result in the fields of this     * Make an invocation and store the result in the fields of this Request. Used
528     * Request. Used with DII only.     * with DII only.
529     */     */
530    public void invoke()    public void invoke()
531    {    {
# Line 277  public class LocalRequest Line 535  public class LocalRequest
535        {        {
536          DynamicImplementation dyn = ((dynImpHandler) handler).servant;          DynamicImplementation dyn = ((dynImpHandler) handler).servant;
537          if (serverRequest == null)          if (serverRequest == null)
538            serverRequest = new LocalServerRequest(this);            {
539                serverRequest = new LocalServerRequest(this);
540              }
541          try          try
542            {            {
543              poa.m_orb.currents.put(Thread.currentThread(), this);              poa.m_orb.currents.put(Thread.currentThread(), this);
# Line 304  public class LocalRequest Line 564  public class LocalRequest
564    
565              // Read returned parameters, if set.              // Read returned parameters, if set.
566              if (m_args != null)              if (m_args != null)
567                for (int i = 0; i < m_args.count(); i++)                {
568                  {                  for (int i = 0; i < m_args.count(); i++)
569                    try                    {
570                      {                      try
571                        arg = m_args.item(i);                        {
572                            arg = m_args.item(i);
573                        // Both ARG_INOUT and ARG_OUT have this binary flag set.  
574                        if ((arg.flags() & ARG_OUT.value) != 0)                          // Both ARG_INOUT and ARG_OUT have this binary flag set.
575                          {                          if ((arg.flags() & ARG_OUT.value) != 0)
576                            arg.value().read_value(input, arg.value().type());                            {
577                          }                              arg.value().read_value(input, arg.value().type());
578                      }                            }
579                    catch (Bounds ex)                        }
580                      {                      catch (Bounds ex)
581                        Unexpected.error(ex);                        {
582                      }                          Unexpected.error(ex);
583                  }                        }
584                      }
585                  }
586            }            }
587          else // User exception reply          else// User exception reply
588            {            {
589              // Prepare an Any that will hold the exception.              // Prepare an Any that will hold the exception.
590              gnuAny exc = new gnuAny();              gnuAny exc = new gnuAny();
# Line 336  public class LocalRequest Line 598  public class LocalRequest
598    }    }
599    
600    /**    /**
601     * Get an output stream for providing details about the exception.     * Get an output stream for providing details about the exception. Before
602     * Before returning the stream, the handler automatically writes     * returning the stream, the handler automatically writes the message header
603     * the message header and the reply about exception header,     * and the reply about exception header, but not the message header.
    * but not the message header.  
604     *     *
605     * @return the stream to write exception details into.     * @return the stream to write exception details into.
606     */     */
# Line 353  public class LocalRequest Line 614  public class LocalRequest
614    /**    /**
615     * Get an output stream for writing a regular reply (not an exception).     * Get an output stream for writing a regular reply (not an exception).
616     *     *
617     * Before returning the stream, the handler automatically writes     * Before returning the stream, the handler automatically writes the regular
618     * the regular reply header, but not the message header.     * reply header, but not the message header.
619     *     *
620     * @return the output stream for writing a regular reply.     * @return the output stream for writing a regular reply.
621     */     */
# Line 366  public class LocalRequest Line 627  public class LocalRequest
627    }    }
628    
629    /**    /**
630     * Get the buffer, normally containing the written reply.     * Get the buffer, normally containing the written reply. The reply includes
631     * The reply includes the reply header (or the exception header)     * the reply header (or the exception header) but does not include the message
632     * but does not include the message header.     * header.
633     *     *
634     * The stream buffer can also be empty if no data have been written     * The stream buffer can also be empty if no data have been written into
635     * into streams, returned by {@link #createReply()} or     * streams, returned by {@link #createReply()} or
636     * {@link #createExceptionReply()}.     * {@link #createExceptionReply()}.
637     *     *
638     * @return the CDR output stream, containing the written output.     * @return the CDR output stream, containing the written output.
# Line 382  public class LocalRequest Line 643  public class LocalRequest
643    }    }
644    
645    /**    /**
646     * True if the stream was obtained by invoking     * True if the stream was obtained by invoking {@link #createExceptionReply()},
647     * {@link #createExceptionReply()}, false otherwise     * false otherwise (usually no-exception reply).
    * (usually no-exception reply).  
648     */     */
649    boolean isExceptionReply()    boolean isExceptionReply()
650    {    {
# Line 401  public class LocalRequest Line 661  public class LocalRequest
661    }    }
662    
663    /**    /**
664     * Get the parameter stream, where the invocation arguments should     * Get the parameter stream, where the invocation arguments should be written
665     * be written if they are written into the stream directly.     * if they are written into the stream directly.
666     */     */
667    public streamRequest getParameterStream()    public streamRequest getParameterStream()
668    {    {
# Line 412  public class LocalRequest Line 672  public class LocalRequest
672      return m_parameter_buffer;      return m_parameter_buffer;
673    }    }
674    
675    public byte[] get_object_id()    public byte[] get_object_id() throws NoContext
                        throws NoContext  
676    {    {
677      return Id;      return Id;
678    }    }
679    
680    public POA get_POA()    public POA get_POA() throws NoContext
               throws NoContext  
681    {    {
682      return poa;      return poa;
683    }    }

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26