summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/CosEC/Factory/CosEventChannelFactory_i.cpp
blob: 599367d40b19ee41ba307182c5f6d318af3538af (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// -*- C++ -*-
// $Id$

#include "CosEventChannelFactory_i.h"
#include "orbsvcs/CosEvent_Utilities.h"
#include "ace/Auto_Ptr.h"
#include "tao/POA.h"

TAO_CosEventChannelFactory_i::TAO_CosEventChannelFactory_i (void)
  :poa_ (PortableServer::POA::_nil ()),
   naming_ (CosNaming::NamingContext::_nil ())
{
  // No-Op.
}

TAO_CosEventChannelFactory_i::~TAO_CosEventChannelFactory_i (void)
{
#if 0
  ACE_DEBUG ((LM_DEBUG,
              "in TAO_CosEventChannelFactory_i dtor"));
#endif
  // No-Op.
}

int
TAO_CosEventChannelFactory_i::init (PortableServer::POA_ptr poa,
                                    const char* child_poa_name,
                                    CosNaming::NamingContext_ptr naming,
                                    CORBA::Environment &ACE_TRY_ENV)
{
  // Check if we have a parent poa.
  if (CORBA::is_nil (poa))
    return -1;

  this->naming_ = CosNaming::NamingContext::_duplicate (naming);
  // Save the naming context.

  // Create a UNIQUE_ID and USER_ID policy because we want the POA
  // to detect duplicates for us.
  PortableServer::IdUniquenessPolicy_var idpolicy =
    poa->create_id_uniqueness_policy (PortableServer::UNIQUE_ID,
                                      ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  PortableServer::IdAssignmentPolicy_var assignpolicy =
    poa->create_id_assignment_policy (PortableServer::USER_ID,
                                      ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  // Create a PolicyList
  CORBA::PolicyList policy_list;
  policy_list.length (2);
  policy_list [0] =
    PortableServer::IdUniquenessPolicy::_duplicate (idpolicy.in ());
  policy_list [1] =
    PortableServer::IdAssignmentPolicy::_duplicate (assignpolicy.in ());

  PortableServer::POAManager_ptr manager =
    poa->the_POAManager (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);
  // @@ Pradeep : TODO - find a way to destroy the policy_list if we return here.

  // Create the child POA.
  this->poa_ = poa->create_POA (child_poa_name,
                                manager,
                                policy_list,
                                ACE_TRY_ENV);

  ACE_CHECK_RETURN (-1);

  idpolicy->destroy (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  assignpolicy->destroy (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  //this->poa_ =  PortableServer::POA::_duplicate (poa);
  // uncomment this if we want to use the parent poa for some reason.
   return 0;
}

CosEventChannelAdmin::EventChannel_ptr
TAO_CosEventChannelFactory_i::create (const char * channel_id,
                                      CORBA::Boolean store_in_naming_service,
                                      CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((
        CORBA::SystemException,
        CosEventChannelFactory::DuplicateChannel,
        CosEventChannelFactory::BindFailed
      ))
{
  ACE_ASSERT (!CORBA::is_nil (this->poa_.in ()));

  CosEventChannelAdmin::EventChannel_var ec_return;

  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      CosEC_ServantBase *_ec = 0;

      ACE_NEW_THROW_EX (_ec,
                        CosEC_ServantBase (),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      auto_ptr <CosEC_ServantBase> ec (_ec);
      // @@ Pradeep: could we pass the POA used to activate the
      //    EC-generated objects as an argument?  The point is that
      //    the user must be aware that we require a POA with the
      //    SYSTEM_ID policy....  This is not urgent, but a "wishlist"

      // @@ Carlos: I'am passing the POA to activate the
      // generated objects as an argument in <init>.
      // Do you want the FactoryCosEventChannel_i constructor
      // to take that?

      // let all those contained in FactoryEC use the default POA.
      // We only need the FactoryEC's to be unique!
      PortableServer::POA_ptr defPOA = this->_default_POA (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // @@ Pradeep: I hate to bring this up, but what happens if the
      //    init() method raises ServantAlreadyActive or something
      //    similar?  Do we want to convert that into
      //    DuplicateChannel?  IMHO you should be more careful about
      //    the exception translation.

      // @@ Carlos: ServantAlreadyActive and ObjectAlreadyActive
      // mean that duplicates were detected, so i thought that
      // it made sense to translate them to DuplicateChannel.

      // @@ Pradeep: right, but you want to report those only if they
      //    are raised during the activation of the EC, the problem is
      //    that you are raising the same error if the EC makes a
      //    mistake and activates the same object twice.

      ec->init (this->poa_.in(),
                defPOA,
                0,0,0,
                ACE_TRY_ENV);
      ACE_TRY_CHECK;

      int retval = ec->activate (channel_id,
                                 ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (retval == -1)
        ACE_THROW_RETURN (CosEventChannelFactory::DuplicateChannel (),
                          ec_return._retn ());

      ec.release (); // release the ownership from the auto_ptr.

      CORBA::Object_var obj =
        this->poa_->servant_to_reference (_ec, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (store_in_naming_service &&
          !CORBA::is_nil (this->naming_.in ()))
        {
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (channel_id);

          this->naming_->rebind (name,
                                 obj.in (),
                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      ec_return = CosEventChannelAdmin::EventChannel::_narrow (obj.in ());
    }
  ACE_CATCH (PortableServer::POA::ServantAlreadyActive, sa_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::DuplicateChannel (),
                        ec_return._retn ());
    }
  ACE_CATCH (PortableServer::POA::ObjectAlreadyActive, oaa_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::DuplicateChannel (),
                        ec_return._retn ());
    }
  ACE_CATCH (PortableServer::POA::WrongPolicy, wp_ex)
    {
      ACE_THROW_RETURN (CORBA::UNKNOWN (),
                        ec_return._retn ());
    }
  ACE_CATCH (PortableServer::POA::ObjectNotActive, ona_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed (),
                        ec_return._retn ());
    }
  ACE_CATCH (CosNaming::NamingContext::NotFound, nf_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed (),
                        ec_return._retn ());
    }
  ACE_CATCH (CosNaming::NamingContext::CannotProceed, cp_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed (),
                        ec_return._retn ());
    }
  ACE_CATCH (CosNaming::NamingContext::InvalidName, in_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed (),
                        ec_return._retn ());
    }
  ACE_CATCH (CosNaming::NamingContext::AlreadyBound, ab)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed (),
                        ec_return._retn ());
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (ec_return._retn ());

  return ec_return._retn ();
}

void
TAO_CosEventChannelFactory_i::destroy
(
 const char * channel_id,
 CORBA::Boolean unbind_from_naming_service,
 CORBA::Environment &ACE_TRY_ENV
 )
 ACE_THROW_SPEC ((
        CORBA::SystemException,
        CosEventChannelFactory::NoSuchChannel
      ))
{
  ACE_ASSERT (!CORBA::is_nil (this->poa_.in ()));

  ACE_TRY
    {
      // Get hold of the objectid first.
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid.in (),
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CosEventChannelAdmin::EventChannel_var fact_ec =
        CosEventChannelAdmin::EventChannel::_narrow (obj.in (),
                                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      fact_ec->destroy (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Remove from the naming service.
      if (unbind_from_naming_service &&
          !CORBA::is_nil (this->naming_.in ()))
        {
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (channel_id);

          this->naming_->unbind (name,
                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCH (CosNaming::NamingContext::NotFound, nf_ex)
    {
      return; // don't bother the user with exceptions if unbind fails.
    }
  ACE_CATCH (CosNaming::NamingContext::CannotProceed, cp_ex)
    {
      return; // don't bother the user with exceptions if unbind fails.
    }
  ACE_CATCH (CosNaming::NamingContext::InvalidName, in_ex)
    {
      return; // don't bother the user with exceptions if unbind fails.
    }
  ACE_CATCH (CORBA::UserException, ue) // Translate any other user exception.
    {
      ACE_THROW (CosEventChannelFactory::NoSuchChannel ());
    }
  ACE_ENDTRY;
  ACE_CHECK;
}

CosEventChannelAdmin::EventChannel_ptr
TAO_CosEventChannelFactory_i::find
(
 const char * channel_id,
 CORBA::Environment &ACE_TRY_ENV
 )
  ACE_THROW_SPEC ((
        CORBA::SystemException,
        CosEventChannelFactory::NoSuchChannel
      ))
{
  ACE_ASSERT (!CORBA::is_nil (this->poa_.in ()));

  CosEventChannelAdmin::EventChannel_var ec_return;

  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid.in (),
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ec_return = CosEventChannelAdmin::EventChannel::_narrow (obj.in ());
    }
  ACE_CATCH (CORBA::UserException, ue) // Translate any user exception.
    {
      ACE_THROW_RETURN (CosEventChannelFactory::NoSuchChannel (),
                        ec_return._retn ());
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (ec_return._retn ());

  return ec_return._retn ();
}

char*
TAO_CosEventChannelFactory_i::find_channel_id
(
 CosEventChannelAdmin::EventChannel_ptr channel,
 CORBA::Environment &ACE_TRY_ENV
 )
  ACE_THROW_SPEC ((
        CORBA::SystemException,
        CosEventChannelFactory::NoSuchChannel
      ))
{
  ACE_ASSERT (!CORBA::is_nil (this->poa_.in ()));

  CORBA::String_var str_return;
  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        this->poa_->reference_to_id (channel,
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      str_return = TAO_POA::ObjectId_to_string (oid.in ());
    }
  ACE_CATCH (CORBA::UserException, ue) // Translate any user exception.
    {
      ACE_THROW_RETURN (CosEventChannelFactory::NoSuchChannel (),
                        str_return._retn ());
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (str_return._retn ());

  return str_return._retn ();
}


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class auto_ptr <CosEC_ServantBase>;
template class ACE_Auto_Basic_Ptr <CosEC_ServantBase>;

#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate auto_ptr <CosEC_ServantBase>
#pragma instantiate  ACE_Auto_Basic_Ptr <CosEC_ServantBase>

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */