summaryrefslogtreecommitdiff
path: root/TAO/tao/PI/ORBInitializer_Registry_Impl.cpp
blob: 919abb2706e85a20d4505a8c7b38720b8a8fd9f4 (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
#include "tao/PI/ORBInitializer_Registry_Impl.h"
#include "tao/PI/ORBInitInfo.h"
#include "tao/PI/PICurrent.h"

#include "tao/ORB_Core.h"
#include "tao/ORB_Constants.h"
#include "tao/SystemException.h"

#include "ace/Static_Object_Lock.h"
#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Log_Msg.h"

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


TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO::ORBInitializer_Registry::ORBInitializer_Registry (void)
  : lock_ (),
    initializers_ ()
{
}

int
TAO::ORBInitializer_Registry::fini (void)
{
  ACE_GUARD_RETURN (TAO_SYNCH_RECURSIVE_MUTEX,
                    guard,
                    this->lock_,
                    -1);

  // Release all initializers in the array
  size_t const initializer_count (this->initializers_.size ());
  for (size_t i = 0; i < initializer_count; ++i)
    {
      this->initializers_[i] = PortableInterceptor::ORBInitializer::_nil();
    }

  return 0;
}

void
TAO::ORBInitializer_Registry::register_orb_initializer (
  PortableInterceptor::ORBInitializer_ptr init
  ACE_ENV_ARG_DECL)
{
  if (!CORBA::is_nil (init))
    {
      ACE_GUARD (TAO_SYNCH_RECURSIVE_MUTEX,
                 guard,
                 this->lock_);

      // Increase the length of the ORBInitializer array by one.
      size_t const cur_len = this->initializers_.size ();
      size_t const new_len = cur_len + 1;
      if (this->initializers_.size (new_len) != 0)
        ACE_THROW (CORBA::INTERNAL ());

      // Add the given ORBInitializer to the sequence.
      this->initializers_[cur_len] =
        PortableInterceptor::ORBInitializer::_duplicate (init);
    }
  else
    ACE_THROW (CORBA::INV_OBJREF (
                 CORBA::SystemException::_tao_minor_code (
                   0,
                   EINVAL),
                 CORBA::COMPLETED_NO));
}

size_t
TAO::ORBInitializer_Registry::pre_init (
  TAO_ORB_Core *orb_core,
  int argc,
  char *argv[],
  PortableInterceptor::SlotId &slotid
  ACE_ENV_ARG_DECL)
{
  ACE_GUARD_RETURN (TAO_SYNCH_RECURSIVE_MUTEX,
                    guard,
                    this->lock_,
                    0);

  size_t const initializer_count (this->initializers_.size ());

  if (initializer_count > 0)
    {
      TAO_ORBInitInfo * orb_init_info_temp = 0;

      ACE_NEW_THROW_EX (orb_init_info_temp,
                        TAO_ORBInitInfo (orb_core,
                                         argc,
                                         argv,
                                         slotid),
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            0,
                            ENOMEM),
                          CORBA::COMPLETED_NO));
      ACE_CHECK_RETURN (0);

      TAO_ORBInitInfo_var orb_init_info_ = orb_init_info_temp;

      for (size_t i = 0; i < initializer_count; ++i)
        {
          this->initializers_[i]->pre_init (orb_init_info_.in ()
                                            ACE_ENV_ARG_PARAMETER);
          ACE_CHECK_RETURN (0);
        }

      slotid = orb_init_info_temp->slot_count ();

      // Invalidate the ORBInitInfo instance to prevent future
      // modifications to the ORB.  This behavior complies with the
      // PortableInterceptor specification.
      orb_init_info_temp->invalidate ();
    }

  return initializer_count;
}

void
TAO::ORBInitializer_Registry::post_init (
  size_t pre_init_count,
  TAO_ORB_Core *orb_core,
  int argc,
  char *argv[],
  PortableInterceptor::SlotId slotid
  ACE_ENV_ARG_DECL)
{
  if (pre_init_count > 0)
    {
      ACE_GUARD (TAO_SYNCH_RECURSIVE_MUTEX,
                 guard,
                 this->lock_);

      TAO_ORBInitInfo * orb_init_info_temp = 0;

      ACE_NEW_THROW_EX (orb_init_info_temp,
                        TAO_ORBInitInfo (orb_core,
                                         argc,
                                         argv,
                                         slotid),
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            0,
                            ENOMEM),
                          CORBA::COMPLETED_NO));
      ACE_CHECK;

      TAO_ORBInitInfo_var orb_init_info_ = orb_init_info_temp;

      for (size_t i = 0; i < pre_init_count; ++i)
        {
          this->initializers_[i]->post_init (orb_init_info_.in ()
                                             ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }

#if TAO_HAS_INTERCEPTORS == 1
      CORBA::Object_ptr picurrent_ptr = orb_core->pi_current ();
      PortableInterceptor::SlotId slot_count = orb_init_info_->slot_count ();

      if (CORBA::is_nil (picurrent_ptr) && slot_count != 0)
        {
          // Force instantiation of the PICurrent object. If we do not do it
          // now, the slot count will be lost.
          CORBA::Object_var tmp = orb_core->resolve_picurrent ();
          picurrent_ptr = orb_core->pi_current ();
        }

      if (!CORBA::is_nil (picurrent_ptr))
        {
          TAO::PICurrent *pi = dynamic_cast <TAO::PICurrent*> (picurrent_ptr);

          if (pi)
            {
              pi->initialize (slot_count ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }
        }
#endif /* TAO_HAS_INTERCEPTORS == 1 */

      // Invalidate the ORBInitInfo instance to prevent future
      // modifications to the ORB.  This behavior complies with the
      // PortableInterceptor specification.
      orb_init_info_temp->invalidate ();
    }
}

TAO_END_VERSIONED_NAMESPACE_DECL

ACE_STATIC_SVC_DEFINE (ORBInitializer_Registry,
                       ACE_TEXT ("ORBInitializer_Registry"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (ORBInitializer_Registry),
                       ACE_Service_Type::DELETE_THIS | ACE_Service_Type::DELETE_OBJ,
                       0)

ACE_FACTORY_NAMESPACE_DEFINE (TAO_PI, ORBInitializer_Registry, TAO::ORBInitializer_Registry)