summaryrefslogtreecommitdiff
path: root/TAO/CIAO/tools/Assembly_Deployer/Assembly_Impl.cpp
blob: 8136296049461d945d1a99f4da97fdf861c3ba4b (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// $Id$

#include "Assembly_Impl.h"
//#include "ACtive_Objref_Map.h"
#include "Cookies.h"
#include "../XML_Helpers/Assembly_Spec.h"
#include "../XML_Helpers/XML_Utils.h"
#include "CIAO_EventsS.h"

#if !defined (__ACE_INLINE__)
# include "Assembly_Impl.inl"
#endif /* __ACE_INLINE__ */

CIAO::AssemblyFactory_Impl::~AssemblyFactory_Impl ()
{
  // @@ remove all Containers?
}

PortableServer::POA_ptr
CIAO::AssemblyFactory_Impl::_default_POA (void)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

int
CIAO::AssemblyFactory_Impl::init (const char *init_file
                                  ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  return this->deployment_config_.init (init_file);
}

::Components::Cookie *
CIAO::AssemblyFactory_Impl::create_assembly (const char * assembly_loc
                                        ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::Deployment::InvalidLocation,
                   Components::CreateFailure))
{
  CIAO::Assembly_Spec *assembly_spec;
  ACE_NEW_RETURN (assembly_spec,
                  CIAO::Assembly_Spec,
                  0);

  if (CIAO::XML_Utils::parse_componentassembly (assembly_loc,
                                                assembly_spec) != 0)
    ACE_THROW_RETURN (Components::Deployment::InvalidLocation (), 0);

  CIAO::Assembly_Impl *servant = 0;
  ACE_NEW_RETURN (servant,
                  CIAO::Assembly_Impl (this->orb_.in (),
                                       this->poa_.in (),
                                       assembly_spec,
                                       this->deployment_config_),
                  0);

  PortableServer::ServantBase_var save_servant (servant);

  // @@ TO-DO: acquire and parse a Component Assembly descriptor from
  // the assembly_loc and then the Assembly servant will use it to
  // initialize itself here.

  // Register with our POA.
  PortableServer::ObjectId_var oid
    = this->poa_->activate_object (servant
                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  CORBA::Object_var objref
    = this->poa_->id_to_reference (oid
                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  ::Components::Deployment::Assembly_var assembly =
      ::Components::Deployment::Assembly::_narrow (objref.in ()
                                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  ACE_Active_Map_Manager_Key key;
  this->assembly_map_.bind (assembly,
                            key);

  ::Components::Cookie_var retv = new CIAO::Map_Key_Cookie (key);

  return retv._retn ();
}

::Components::Deployment::Assembly_ptr
CIAO::AssemblyFactory_Impl::lookup (Components::Cookie * c
                                    ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::Deployment::InvalidAssembly))
{
  ::Components::Deployment::Assembly_var retv;
  if (c == 0)
    ACE_THROW_RETURN (::Components::Deployment::InvalidAssembly (), 0);

  ACE_Active_Map_Manager_Key key;
  if (CIAO::Map_Key_Cookie::extract (c, key) == -1)
    ACE_THROW_RETURN (::Components::Deployment::InvalidAssembly (), 0);

  if (this->assembly_map_.find (key,
                                retv) != 0)
    ACE_THROW_RETURN (::Components::Deployment::InvalidAssembly (), 0);

  return retv._retn ();
}

void
CIAO::AssemblyFactory_Impl::destroy (Components::Cookie * c
                                     ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::Deployment::InvalidAssembly,
                   Components::RemoveFailure))
{
  ::Components::Deployment::Assembly_var assembly;
  if (c == 0)
    ACE_THROW (::Components::Deployment::InvalidAssembly ());

  ACE_Active_Map_Manager_Key key;
  if (CIAO::Map_Key_Cookie::extract (c, key) == -1)
    ACE_THROW (::Components::Deployment::InvalidAssembly ());

  if (this->assembly_map_.unbind (key,
                                  assembly) != 0)
    ACE_THROW (::Components::Deployment::InvalidAssembly ());

  ::Components::Deployment::AssemblyState
      state = assembly->get_state (ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (state == ::Components::Deployment::INSERVICE)
    {
      assembly->tear_down (ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }

  PortableServer::ObjectId_var oid
    = this->poa_->reference_to_id (assembly.in ()
                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  this->poa_->deactivate_object (oid
                                 ACE_ENV_ARG_PARAMETER);
}


CORBA::ULong CIAO::Assembly_Impl::assembly_count_ = 0;

CIAO::Assembly_Impl::~Assembly_Impl ()
{
  // @@ tearing down everything?

  ACE_DEBUG ((LM_DEBUG,
              "CIAO::Assembly_Impl::~Assembly_Impl %d\n",
              this->serial_number_));

  delete this->assembly_spec_;
}

PortableServer::POA_ptr
CIAO::Assembly_Impl::_default_POA (void)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

int
CIAO::Assembly_Impl::init (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  // @@ Initialize Assembly and create the internal container
  // implementation that actually interacts with installed
  // homes/components.

  // We will probably need two ORBs in this process.  One for the
  // deployment framework, and one for the actual components.
  return 0;
}

void
CIAO::Assembly_Impl::build (ACE_ENV_SINGLE_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CreateFailure))
{
  ACE_DEBUG ((LM_DEBUG,
              "CIAO::Assembly_Impl::build %d\n",
              this->serial_number_));
  if (this->state_ == Components::Deployment::INSERVICE)
    return;                     // We are running already.

  ACE_DEBUG ((LM_DEBUG, "------------------------------------\n"));

  // Installing homes and instantiating components

  CIAO::Assembly_Builder_Visitor builder (this->orb_.in (),
                                          this->assembly_context_,
                                          this->assembly_spec_->componentfiles_,
                                          this->deployment_config_);
  int build_result = this->assembly_spec_->partitioning_.accept (builder
                                                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  ACE_DEBUG ((LM_DEBUG, "------------------------------------\n"));

  if (build_result != 0)
    {
      ACE_ERROR ((LM_ERROR, "Failed to install all homes or instantiate all components\n"));
      ACE_THROW (Components::CreateFailure ());
    }
  // Setting connections

  this->make_connections (ACE_ENV_SINGLE_ARG_PARAMETER);

  this->state_ = ::Components::Deployment::INSERVICE;

  ACE_DEBUG ((LM_DEBUG, "All connections established.\n"));
}

void
CIAO::Assembly_Impl::tear_down (ACE_ENV_SINGLE_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::RemoveFailure))
{
  if (this->state_ != Components::Deployment::INSERVICE)
    return;                     // Nothing to do here.

  // @@ At least we should remove home and kill the component server.
  // Remove all components:
  {
    CIAO::Assembly_Context::COMP_MAP::ITERATOR
      iter (this->assembly_context_.instantiated_components_);

    while (!iter.done ())
      {
        CIAO::Assembly_Context::COMP_MAP::ENTRY *entry;
        iter.next (entry);

        entry->int_id_->remove (ACE_ENV_SINGLE_ARG_PARAMETER);
        ACE_CHECK;

        iter.advance ();
      }
  }

  // Remove all server
  {
    CIAO::Assembly_Context::SERVER_QUEUE::ITERATOR
      iter (this->assembly_context_.component_servers_);

    while (!iter.done ())
      {
        Components::Deployment::ComponentServer_var *entry;
        iter.next (entry);

        (*entry)->remove (ACE_ENV_SINGLE_ARG_PARAMETER);
        ACE_CHECK;

        iter.advance ();
      }
  }

  ACE_DEBUG ((LM_DEBUG,
              "CIAO::Assembly_Impl::tear_down %d\n",
              this->serial_number_));
  this->state_ = ::Components::Deployment::INACTIVE;
}

::Components::Deployment::AssemblyState
CIAO::Assembly_Impl::get_state (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG,
              "CIAO::Assembly_Impl::get_state %d\n",
              this->serial_number_));
  return this->state_;
}

void
CIAO::Assembly_Impl::make_connections (ACE_ENV_SINGLE_ARG_DECL)
{

  CIAO::Assembly_Spec::CONNECTION_QUEUE::ITERATOR
    conn_iter (this->assembly_spec_->connections_);

  while (!conn_iter.done ())
    {
      CIAO::Assembly_Connection::Connect_Info *connection;
      conn_iter.next (connection);

      switch (connection->type_)
        {
        case CIAO::Assembly_Connection::INTERFACE:
          {
            CORBA::Object_var source
              = this->resolve_interface (connection->dest_iface_
                                         ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            Components::CCMObject_var comp
              = this->resolve_component (connection->src_comp_
                                         ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            comp->connect (connection->name_.c_str (),
                           source.in ()
                           ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            // @@ Register the connection?  How?
          }
          break;

        case CIAO::Assembly_Connection::EMITTER_CONSUMER:
          {
            Components::EventConsumerBase_var source
              = this->resolve_consumer (connection->dest_iface_
                                        ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            Components::CCMObject_var comp
              = this->resolve_component (connection->src_comp_
                                         ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            comp->connect_consumer (connection->name_.c_str (),
                                    source.in ()
                                    ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;
          }
          break;

        case CIAO::Assembly_Connection::PUBLISHER_CONSUMER:
          {
            Components::CCMObject_var sink
              = this->resolve_component (connection->dest_iface_->nested_resolver ()
                                         ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            if (CORBA::is_nil (sink.in ()))
              ACE_DEBUG ((LM_DEBUG, "Nil sink\n"));

            Components::EventConsumerBase_var consumer
              = sink->get_consumer (connection->dest_iface_->resolver_info ()
                                    ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            if (CORBA::is_nil (consumer.in ()))
              ACE_DEBUG ((LM_DEBUG, "Nil consumer\n"));

            Components::CCMObject_var source
              = this->resolve_component (connection->src_comp_
                                         ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            if (CORBA::is_nil (source.in ()))
              ACE_DEBUG ((LM_DEBUG, "Nil source\n"));

            Components::Deployment::Container_var container;
            this->assembly_context_.containers_.find (connection->src_comp_->resolver_info (),
                                                      container
                                                      ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            CIAO::ContainerEventService_var event_service =
              container->get_event_service (ACE_ENV_SINGLE_ARG_PARAMETER);
            ACE_CHECK;

            if (CORBA::is_nil (event_service.in ()))
              ACE_DEBUG ((LM_DEBUG, "Nil event_service\n"));

            CIAO::EventServiceType type = CIAO::RTEC;
            ACE_CString sid = source->component_UUID (ACE_ENV_SINGLE_ARG_DECL);
            ACE_CHECK;

            sid += "_";
            sid += connection->name_.c_str ();
            sid += "_publisher";

            ACE_CString cid = sink->component_UUID (ACE_ENV_SINGLE_ARG_DECL);
            ACE_CHECK;

            cid += "_";
            cid += connection->name_.c_str ();
            cid += "_consumer";

            ACE_DEBUG ((LM_DEBUG, "Publisher: %s\n", sid.c_str ()));
            ACE_DEBUG ((LM_DEBUG, "Subscriber: %s\n", cid.c_str ()));

            if (this->connected_publishers_.find (sid) == -1)
              {
                CIAO::Supplier_Config_var supplier_config =
                  event_service->create_supplier_config (type ACE_ENV_ARG_PARAMETER);
                ACE_CHECK;
            
                supplier_config->supplier_id (sid.c_str () ACE_ENV_ARG_PARAMETER);
                ACE_CHECK;

                event_service->connect_event_supplier (supplier_config.in () ACE_ENV_ARG_PARAMETER);
                ACE_CHECK;

                this->connected_publishers_.insert (sid);

                supplier_config->destroy (ACE_ENV_SINGLE_ARG_DECL);
                ACE_CHECK;
              }

            CIAO::Consumer_Config_var consumer_config =
              event_service->create_consumer_config (type ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            consumer_config->supplier_id (sid.c_str () ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;
            consumer_config->consumer_id (cid.c_str () ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;
            consumer_config->consumer (consumer.in ()
                                       ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            event_service->connect_event_consumer (consumer_config.in ()
                                                   ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            consumer_config->destroy (ACE_ENV_SINGLE_ARG_DECL);
            ACE_CHECK;

          }
          break;

        case CIAO::Assembly_Connection::HOME:
          ACE_THROW (CORBA::NO_IMPLEMENT ());

        default:
          ACE_THROW (CORBA::INTERNAL ());
        }

      conn_iter.advance ();
    }

}

CORBA::Object_ptr
CIAO::Assembly_Impl::resolve_interface (CIAO::Assembly_Connection::IF_Resolver_Info *info
                                        ACE_ENV_ARG_DECL_WITH_DEFAULTS)
{
  switch (info->resolver_type ())
    {
    case CIAO::Assembly_Connection::PROVIDER:
      {
        Components::CCMObject_var comp =
          this->resolve_component (info->nested_resolver ()
                                   ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (0);

        return comp->provide_facet (info->resolver_info ()
                                    ACE_ENV_ARG_PARAMETER);
      }

    case CIAO::Assembly_Connection::CONSUMER:
      return this->resolve_consumer (info
                                     ACE_ENV_ARG_PARAMETER);

    case CIAO::Assembly_Connection::COMP_IDREF:
      return this->resolve_component (info
                                      ACE_ENV_ARG_PARAMETER);

    case CIAO::Assembly_Connection::HOME_IDREF:
      return this->resolve_home (info
                                 ACE_ENV_ARG_PARAMETER);

    case CIAO::Assembly_Connection::NAMINGSERVICE:
      ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);

    case CIAO::Assembly_Connection::STRINGIFIEDOBJECTREF:
      return this->orb_->string_to_object (info->resolver_info ()
                                           ACE_ENV_ARG_PARAMETER);

    case CIAO::Assembly_Connection::HOMEFINDER:
      return this->resolve_home (info
                                 ACE_ENV_ARG_PARAMETER);

    case CIAO::Assembly_Connection::TRADERQUERY:
      ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);

    default:
      ACE_THROW_RETURN (CORBA::INTERNAL (), 0);
    }
}

Components::CCMObject_ptr
CIAO::Assembly_Impl::resolve_component (CIAO::Assembly_Connection::IF_Resolver_Info *info
                                        ACE_ENV_ARG_DECL_WITH_DEFAULTS)
{
  if (info->resolver_type () == CIAO::Assembly_Connection::COMP_IDREF)
    {
      CIAO::Assembly_Context::COMP_MAP::ENTRY *entry;

      if (this->assembly_context_.instantiated_components_.find (info->resolver_info (),
                                                                 entry) != 0)
        // This isn't really a good exception to throw unless we can
        // specify FailureReason somehow.
        ACE_THROW_RETURN (Components::CreateFailure (), 0);

      return Components::CCMObject::_duplicate (entry->int_id_.in ());
    }

  CORBA::Object_var obj = this->resolve_interface (info
                                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  Components::CCMObject_var comp
    = Components::CCMObject::_narrow (obj.in ()
                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  return comp._retn ();
}

Components::CCMHome_ptr
CIAO::Assembly_Impl::resolve_home (CIAO::Assembly_Connection::IF_Resolver_Info *info
                                   ACE_ENV_ARG_DECL_WITH_DEFAULTS)
{
  switch (info->resolver_type ())
    {
    case CIAO::Assembly_Connection::HOME_IDREF:
      {
        CIAO::Assembly_Context::HOME_MAP::ENTRY *entry;

        if (this->assembly_context_.installed_homes_.find (info->resolver_info (),
                                                           entry) != 0)
          // This isn't really a good exception to throw unless we can
          // specify FailureReason somehow.
          ACE_THROW_RETURN (Components::CreateFailure (), 0);

        return Components::CCMHome::_duplicate (entry->int_id_.in ());
      }

    case CIAO::Assembly_Connection::HOMEFINDER:
      ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);

    default:
      {
        CORBA::Object_var obj = this->resolve_interface (info
                                                   ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (0);

        Components::CCMHome_var home
          = Components::CCMHome::_narrow (obj.in ()
                                          ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (0);

        return home._retn ();
      }
    }
  ACE_THROW_RETURN (CORBA::INTERNAL (), 0);
}

Components::EventConsumerBase_ptr
CIAO::Assembly_Impl::resolve_consumer (CIAO::Assembly_Connection::IF_Resolver_Info *info
                                       ACE_ENV_ARG_DECL_WITH_DEFAULTS)
{
  if (info->resolver_type () == CIAO::Assembly_Connection::CONSUMER)
    {
      Components::CCMObject_var comp =
        this->resolve_component (info->nested_resolver ()
                                 ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      return comp->get_consumer (info->resolver_info ()
                                 ACE_ENV_ARG_PARAMETER);
    }

  CORBA::Object_var obj = this->resolve_interface (info
                                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  Components::EventConsumerBase_var consumer
    = Components::EventConsumerBase::_narrow (obj.in ()
                                              ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  return consumer._retn ();
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)  || \
    defined (ACE_HAS_GNU_REPO)
template class ACE_Active_Map_Manager<::Components::Deployment::Assembly_var>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Active_Map_Manager<::Components::Deployment::Assembly_var>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */