summaryrefslogtreecommitdiff
path: root/TAO/tao/Policy_Set.cpp
blob: e0e1b2cc11588a5d68baf3c15c3d7c017f979b69 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// $Id$

#include "tao/Policy_Set.h"
#include "tao/debug.h"

#if !defined (__ACE_INLINE__)
# include "tao/Policy_Set.i"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID(tao, Policy_Set, "$Id$")

TAO_Policy_Set::~TAO_Policy_Set (void)
{
  TAO_ENV_DECLARE_NEW_ENV;
  ACE_TRY
    {
      this->cleanup_i (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      // Ignore exceptions...
    }
  ACE_ENDTRY;
}

TAO_Policy_Set::TAO_Policy_Set (const TAO_Policy_Set &rhs)
  :
    scope_ (rhs.scope_)
{
  // Initialize the cache.
  for (int i = 0; i < TAO_CACHED_POLICY_MAX_CACHED; i++)
    this->cached_policies_[i] = 0;

  // Copy over the policy list.
  this->policy_list_.length (rhs.policy_list_.length ());

  ACE_TRY_NEW_ENV
    {
      for (CORBA::ULong i = 0;
           i < rhs.policy_list_.length ();
           ++i)
        {
          CORBA::Policy_ptr policy = rhs.policy_list_[i];
          if (CORBA::is_nil (policy))
            continue;

          CORBA::Policy_var copy = policy->copy (TAO_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK;

          // Add the "cacheable" policies into the cache.
          if (copy->_tao_cached_type () != TAO_CACHED_POLICY_UNCACHED)
            this->cached_policies_[copy->_tao_cached_type ()] = copy.ptr ();

          this->policy_list_[i] = copy._retn ();
        }
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      if (TAO_debug_level > 4)
        ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                             "TAO_Policy_Set::TAO_Policy_Set");

      // "Try" to make this recoverable as we must have run out of memory.
      this->policy_list_.length (0);
    }
  ACE_ENDTRY;
}

void
TAO_Policy_Set::copy_from (TAO_Policy_Set *source
                                    TAO_ENV_ARG_DECL)
{
  if (source == 0)
    return;

  this->cleanup_i (TAO_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  for (CORBA::ULong i = 0;
       i < source->policy_list_.length ();
       ++i)
    {
      CORBA::Policy_ptr policy = source->policy_list_[i];
      if (CORBA::is_nil (policy))
        continue;

      if (! this->compatible_scope (policy->_tao_scope()))
        ACE_THROW (CORBA::NO_PERMISSION ());

      CORBA::Policy_var copy = policy->copy (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      CORBA::ULong length = this->policy_list_.length ();
      this->policy_list_.length (length + 1);

      // Add the "cacheable" policies into the cache.
      if (copy->_tao_cached_type () != TAO_CACHED_POLICY_UNCACHED)
        this->cached_policies_[copy->_tao_cached_type ()] = copy.ptr ();

      this->policy_list_[length] = copy._retn ();
    }
}

void
TAO_Policy_Set::cleanup_i (TAO_ENV_SINGLE_ARG_DECL)
{
  CORBA::ULong i;

  // Cleanup the policy list.
  for (i = 0;
       i < this->policy_list_.length ();
       ++i)
    {
      this->policy_list_[i]->destroy (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      this->policy_list_[i] = CORBA::Policy::_nil ();
    }
  this->policy_list_.length (0);

  // Cleanup the cache.
  for (i = 0; i < TAO_CACHED_POLICY_MAX_CACHED; i++)
    this->cached_policies_[i] = 0;
}

  // @@ !!! Add comments regarding Policy lifetimes, etc.
void
TAO_Policy_Set::set_policy_overrides (
    const CORBA::PolicyList &policies,
    CORBA::SetOverrideType set_add
    TAO_ENV_ARG_DECL)
{
  // @@ The spec does not say what to do on this case.
  if (set_add != CORBA::SET_OVERRIDE
      && set_add != CORBA::ADD_OVERRIDE)
    ACE_THROW (CORBA::BAD_PARAM ());

  if (set_add == CORBA::SET_OVERRIDE)
    {
      this->cleanup_i (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
    }

  // Flag, indicating whether we have already overridden
  // RTCORBA::ServerProtocolPolicy during this call.
  int server_protocol_set = 0;

  for (CORBA::ULong i = 0; i < policies.length ();  ++i)
    {
      // Because MSVC 5 has a bug, we had to replace
      // a simple CORBA::Policy_var policy = policies[i];
      // with the following.
      CORBA::Policy_ptr temp = policies[i];
      CORBA::Policy_var policy = CORBA::Policy::_duplicate (temp);

      if (CORBA::is_nil (policy.in ()))
        continue;

      CORBA::PolicyType policy_type = policy->policy_type (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      if (policy_type == TAO_RT_SERVER_PROTOCOL_POLICY_TYPE)
        {
          // Only one ServerProtocolPolicy should be included in a
          // given PolicyList (section 4.15.2 of RTCORBA 1.0, i.e.,
          // ptc/99-05-03).
          // User-caused exceptional conditions can leave the Policy
          // Manager in an inconsistent state.  It is the
          // responsibility of the user to return it to consistent state.
          if (server_protocol_set != 0)
            ACE_THROW (CORBA::INV_POLICY ());
          server_protocol_set = 1;
        }

      this->set_policy (policy.in () TAO_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }
}

void
TAO_Policy_Set::set_policy (const CORBA::Policy_ptr policy
                                     TAO_ENV_ARG_DECL)
{
  if (! this->compatible_scope (policy->_tao_scope()))
    ACE_THROW (CORBA::NO_PERMISSION ());

  CORBA::PolicyType policy_type = policy->policy_type (TAO_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::Policy_var copy = policy->copy (TAO_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;


  CORBA::ULong j = 0;
  CORBA::ULong length = this->policy_list_.length ();
  while (j != length)
    {
      CORBA::ULong current =
        this->policy_list_[j]->policy_type (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      if (current == policy_type)
        {
          this->policy_list_[j]->destroy (TAO_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK;
          this->policy_list_[j] = copy.ptr ();
          break;
        }
      ++j;
    }
  if (j == length)
    {
      this->policy_list_.length (length + 1);
      this->policy_list_[j] = copy.ptr ();
    }

  // If this is a policy that gets accessed on the critical path,
  // save a pointer to it in the cache.
  TAO_Cached_Policy_Type cached_policy_type = policy->_tao_cached_type ();
  if (cached_policy_type != TAO_CACHED_POLICY_UNCACHED)
    this->cached_policies_[cached_policy_type] = copy.ptr ();

  // Transfer ownership to the policy list.
  (void) copy._retn ();
}

CORBA::PolicyList *
TAO_Policy_Set::get_policy_overrides (
    const CORBA::PolicyTypeSeq &types
    TAO_ENV_ARG_DECL)
{
  CORBA::ULong types_length = types.length ();

  CORBA::ULong slots = types_length;
  CORBA::PolicyList *policy_list_ptr;

  if (slots == 0)
    {
      slots = this->policy_list_.length ();
      ACE_NEW_THROW_EX (policy_list_ptr,
                        CORBA::PolicyList (slots),
                        CORBA::NO_MEMORY ());
      ACE_CHECK_RETURN (0);
      *policy_list_ptr = this->policy_list_;
      return policy_list_ptr;
    }

  ACE_NEW_THROW_EX (policy_list_ptr,
                    CORBA::PolicyList (slots),
                    CORBA::NO_MEMORY ());
  ACE_CHECK_RETURN (0);

  CORBA::PolicyList_var policy_list (policy_list_ptr);

  policy_list->length (slots);

  CORBA::ULong n = 0;

  for (CORBA::ULong j = 0; j < types.length ();  ++j)
    {
      CORBA::ULong slot = types[j];

      CORBA::ULong length = this->policy_list_.length ();
      for (CORBA::ULong i = 0;
           i != length;
           ++i)
        {
          CORBA::ULong current =
            this->policy_list_[i]->policy_type (TAO_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK_RETURN (0);

          if (current != slot)
            continue;

          policy_list[n++] =
            CORBA::Policy::_duplicate (this->policy_list_[i].in ());
          break;
        }
    }
  policy_list->length (n);
  return policy_list._retn ();
}

CORBA::Policy_ptr
TAO_Policy_Set::get_policy (CORBA::PolicyType type
                                     TAO_ENV_ARG_DECL)
{
  CORBA::ULong length = this->policy_list_.length ();
  for (CORBA::ULong i = 0;
       i != length;
       ++i)
    {
      CORBA::ULong current =
        this->policy_list_[i]->policy_type (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      if (current != type)
        continue;

      return CORBA::Policy::_duplicate (this->policy_list_[i].in ());
    }

  return CORBA::Policy::_nil ();
}

CORBA::Policy_ptr
TAO_Policy_Set::get_cached_const_policy (TAO_Cached_Policy_Type type) const
{
  if (type != TAO_CACHED_POLICY_UNCACHED && type < TAO_CACHED_POLICY_MAX_CACHED)
    return this->cached_policies_[type];

  return CORBA::Policy::_nil ();
}

CORBA::Policy_ptr
TAO_Policy_Set::get_cached_policy (TAO_Cached_Policy_Type type)
{
  if (type != TAO_CACHED_POLICY_UNCACHED && type < TAO_CACHED_POLICY_MAX_CACHED)
    return CORBA::Policy::_duplicate (this->cached_policies_[type]);

  return CORBA::Policy::_nil ();
}