summaryrefslogtreecommitdiff
path: root/trunk/TAO/tao/PI/ClientRequestInterceptor_Adapter_Impl.cpp
blob: 3575003f79b715c311977c0ec3cfea0998636901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "tao/PI/ClientRequestInterceptor_Adapter_Impl.h"

#if TAO_HAS_INTERCEPTORS == 1

#if !defined (__ACE_INLINE__)
#include "tao/PI/ClientRequestInterceptor_Adapter_Impl.inl"
#endif /* defined INLINE */

#include "tao/PI/ClientRequestInfo.h"

#include "tao/Invocation_Base.h"
#include "tao/ORB_Core.h"
#include "tao/ORB_Core_TSS_Resources.h"
#include "tao/PortableInterceptorC.h"

ACE_RCSID (PI,
           ClientRequestInterceptorAdapter_Impl,
           "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  void
  ClientRequestInterceptor_Adapter_Impl::send_request (
      Invocation_Base &invocation
      )
  {
    // This method implements one of the "starting" client side
    // interception point.

    bool const is_remote_request = invocation.is_remote_request();

    try
      {
        TAO_ClientRequestInfo ri (&invocation);

        for (size_t i = 0 ; i < this->interceptor_list_.size (); ++i)
          {
            ClientRequestInterceptor_List::RegisteredInterceptor& registered =
              this->interceptor_list_.registered_interceptor (i);

            if (registered.details_.should_be_processed (is_remote_request))
              {
                registered.interceptor_->send_request (&ri);
              }

            // The starting interception point completed successfully.
            // Push  the interceptor on to the flow stack.
            ++invocation.stack_size ();
          }
      }
    catch ( ::PortableInterceptor::ForwardRequest& exc)
      {
        this->process_forward_request (invocation, exc);
      }
  }

  void
  ClientRequestInterceptor_Adapter_Impl::receive_reply (
    Invocation_Base &invocation)
  {
    // This is an "ending" interception point so we only process the
    // interceptors pushed on to the flow stack.

    bool const is_remote_request = invocation.is_remote_request();

    // Notice that the interceptors are processed in the opposite order
    // they were pushed onto the stack since this is an "ending"
    // interception point.

    TAO_ClientRequestInfo ri (&invocation);

    // Unwind the stack.
    size_t const len = invocation.stack_size ();
    for (size_t i = 0; i < len; ++i)
      {
        // Pop the interceptor off of the flow stack before it is
        // invoked.  This is necessary to prevent an interceptor already
        // invoked in this "ending" interception point from being
        // invoked in another "ending" interception point.
        --invocation.stack_size ();

        ClientRequestInterceptor_List::RegisteredInterceptor& registered =
          this->interceptor_list_.registered_interceptor (
            invocation.stack_size ());

        if (registered.details_.should_be_processed (is_remote_request))
          {
            registered.interceptor_->
              receive_reply (
                &ri
               );
          }
      }

    // The receive_reply() interception point does not raise a
    // PortableInterceptor::ForwardRequest exception so there is no need
    // to attempt to catch it here.
  }

  void
  ClientRequestInterceptor_Adapter_Impl::receive_exception (
      Invocation_Base &invocation)
  {
    // This is an "ending" interception point so we only process the
    // interceptors pushed on to the flow stack.

    bool const is_remote_request = invocation.is_remote_request();

    // Notice that the interceptors are processed in the opposite order
    // they were pushed onto the stack since this is an "ending"
    // interception point.
    try
      {
        TAO_ClientRequestInfo ri (&invocation);

        // Unwind the flow stack.
        size_t const len = invocation.stack_size ();
        for (size_t i = 0; i < len; ++i)
          {
            // Pop the interceptor off of the flow stack before it is
            // invoked.  This is necessary to prevent an interceptor
            // already invoked in this "ending" interception point from
            // being invoked in another "ending" interception point.
            --invocation.stack_size ();

            ClientRequestInterceptor_List::RegisteredInterceptor& registered =
              this->interceptor_list_.registered_interceptor (
                invocation.stack_size ());

            if (registered.details_.should_be_processed (is_remote_request))
              {
                registered.interceptor_->receive_exception (&ri);
              }
          }
      }
    catch ( ::PortableInterceptor::ForwardRequest& exc)
      {
        this->process_forward_request (invocation, exc);
      }
    catch ( ::CORBA::Exception& ex)
      {
        // The receive_exception() interception point in the remaining
        // interceptors must be called so call this method (not the
        // interceptor's corresponding method) recursively.  The call is
        // made recursively since the caught exception must survive
        // until the remaining interceptors have been called.

        // Note that the recursion will stop once the flow stack size
        // drops to zero, i.e., once each interceptor has been invoked.
        // This prevents infinite recursion from occuring.

        invocation.exception (&ex);

        this->receive_exception (invocation);

        PortableInterceptor::ReplyStatus status =
          this->reply_status (invocation);

        // Only re-throw the exception if it hasn't been transformed by
        // the receive_exception() interception point (e.g. to a
        // LOCATION_FORWARD).
        if (status == PortableInterceptor::SYSTEM_EXCEPTION
            || status == PortableInterceptor::USER_EXCEPTION)
          throw;
      }
  }

  void
  ClientRequestInterceptor_Adapter_Impl::receive_other (
      Invocation_Base &invocation
      )
  {
    // This is an "ending" interception point so we only process the
    // interceptors pushed on to the flow stack.

    bool const is_remote_request = invocation.is_remote_request();

    // Notice that the interceptors are processed in the opposite order
    // they were pushed onto the stack since this is an "ending"
    // interception point.

    try
      {
        TAO_ClientRequestInfo ri (&invocation);

        // Unwind the stack.
        size_t const len = invocation.stack_size ();
        for (size_t i = 0; i < len; ++i)
        {
          // Pop the interceptor off of the flow stack before it is
          // invoked.  This is necessary to prevent an interceptor
          // already invoked in this "ending" interception point from
          // being invoked in another "ending" interception point.
          --invocation.stack_size ();

          ClientRequestInterceptor_List::RegisteredInterceptor& registered =
            this->interceptor_list_.registered_interceptor (
              invocation.stack_size ());

          if (registered.details_.should_be_processed (is_remote_request))
            {
              registered.interceptor_->receive_other (&ri);
            }
        }
      }
    catch ( ::PortableInterceptor::ForwardRequest& exc)
      {
        this->process_forward_request (invocation, exc);
      }
    catch ( ::CORBA::Exception& ex)
      {
        // The receive_exception() interception point in the remaining
        // interceptors must be called so call this method (not the
        // interceptor's corresponding method) recursively.  The call is
        // made recursively since the caught exception must survive
        // until the remaining interceptors have been called.

        // Note that the recursion will stop once the flow stack size
        // drops to zero, i.e., once each interceptor has been invoked.
        // This prevents infinite recursion from occuring.

        invocation.exception (&ex);

        this->receive_exception (invocation);

        PortableInterceptor::ReplyStatus status =
          this->reply_status (invocation);

        // Only re-throw the exception if it hasn't been transformed by
        // the receive_exception() interception point (e.g. to a
        // LOCATION_FORWARD).
        if (status == PortableInterceptor::SYSTEM_EXCEPTION
            || status == PortableInterceptor::USER_EXCEPTION)
          throw;
      }
  }

  void
  ClientRequestInterceptor_Adapter_Impl::process_forward_request (
      Invocation_Base &invocation,
      PortableInterceptor::ForwardRequest &exc)
  {
    invocation.forwarded_reference (exc.forward.in ());

    // receive_other() is potentially invoked recursively.
    this->receive_other (invocation);
  }

  void
  ClientRequestInterceptor_Adapter_Impl::add_interceptor (
    PortableInterceptor::ClientRequestInterceptor_ptr interceptor)
  {
    this->interceptor_list_.add_interceptor (interceptor);
  }

  void
  ClientRequestInterceptor_Adapter_Impl::add_interceptor (
    PortableInterceptor::ClientRequestInterceptor_ptr interceptor,
    const CORBA::PolicyList& policies)
  {
    this->interceptor_list_.add_interceptor (interceptor, policies);
  }

  void
  ClientRequestInterceptor_Adapter_Impl::destroy_interceptors (void)
  {
    this->interceptor_list_.destroy_interceptors ();
  }

  PortableInterceptor::ReplyStatus
  ClientRequestInterceptor_Adapter_Impl::reply_status (
    TAO::Invocation_Base const &invocation_base)
  {
    PortableInterceptor::ReplyStatus reply_status;

    switch (invocation_base.invoke_status ())
      {
      case TAO::TAO_INVOKE_SUCCESS:
        reply_status = PortableInterceptor::SUCCESSFUL;
        break;
      case TAO::TAO_INVOKE_RESTART:
        if (invocation_base.is_forwarded ())
          reply_status = PortableInterceptor::LOCATION_FORWARD;
        else
          reply_status = PortableInterceptor::TRANSPORT_RETRY;
        break;
      case TAO::TAO_INVOKE_USER_EXCEPTION:
        reply_status = PortableInterceptor::USER_EXCEPTION;
        break;
      case TAO::TAO_INVOKE_SYSTEM_EXCEPTION:
        reply_status = PortableInterceptor::SYSTEM_EXCEPTION;
        break;
      default:
        reply_status = PortableInterceptor::UNKNOWN;
        break;
      }

    return reply_status;
  }
}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif  /* TAO_HAS_INTERCEPTORS == 1 */