summaryrefslogtreecommitdiff
path: root/CIAO/ciao/Deployment/Interceptors/Deployment_Interceptors.cpp
blob: cbdc952a2cec206192a6de4bccd57fa892e6138f (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
// $Id$

#include "Deployment_Interceptors.h"
#include "tao/ORB_Core.h"
#include "Name_Utilities.h"
#include "dance/Deployment/Deployment_StartErrorC.h"
#include "dance/DAnCE_PropertiesC.h"
#include "dance/DAnCE_Utility.h"
#include "dance/LocalityManager/Scheduler/Plugin_Manager.h"
#include "ciao/Logger/Log_Macros.h"

namespace CIAO
{
  /// Implementation skeleton constructor
  CIAO_StoreReferences_i::CIAO_StoreReferences_i (void)
  {
    this->orb_ = DAnCE::PLUGIN_MANAGER::instance ()->get_orb ();

    if (CORBA::is_nil (this->orb_))
      {
        CIAO_ERROR (1, (LM_ERROR, CLINFO
                        "Container_Handler_i::configure -"
                        "Unable to locate ORB.\n"));
        throw ::Deployment::StartError ("CIAO Container Handler",
                                        "Unable to locate ORB");
      }
  }

  /// Implementation skeleton destructor
  CIAO_StoreReferences_i::~CIAO_StoreReferences_i (void)
  {
  }

  void CIAO_StoreReferences_i::pre_install (::Deployment::DeploymentPlan &,
                                                      ::CORBA::ULong)
  {
    // no-op
  }

  void CIAO_StoreReferences_i::post_install (const ::Deployment::DeploymentPlan &plan,
                                             ::CORBA::ULong index,
                                             const ::CORBA::Any &reference,
                                             const ::CORBA::Any &)
  {
    const ::Deployment::InstanceDeploymentDescription &inst =
      plan.instance[index];

    CIAO_DEBUG (9, (LM_TRACE, CLINFO
                    "CIAO_StoreReferences_i::post_install - "
                    "Interceptor post install for instance %C\n",
                     plan.instance[index].name.in ()));

    if (reference.type() == ::CORBA::_tc_null)
      {
        CIAO_ERROR (3, (LM_WARNING, CLINFO
                        "CIAO_StoreReferences_i::post_install - "
                        "Got a nil reference, unable to store reference "
                        "for instance <%C>\n",
                         inst.name.in ()));
        return;
      }

    for (CORBA::ULong i = 0;
         i < inst.configProperty.length ();
         ++i)
      {
        if (ACE_OS::strcmp (inst.configProperty[i].name.in (),
                            DAnCE::REGISTER_NAMING) == 0)
          {
            CORBA::Object_var obj;

            if (!(reference >>= CORBA::Any::to_object (obj)))
              {
                CIAO_ERROR (1, (LM_WARNING, CLINFO
                                "CIAO_StoreReferences_i::post_install - "
                                "Unable to extract instance reference from Any\n"));
              }

            const char * name = 0;
            inst.configProperty[i].value >>= CORBA::Any::to_string (name, 0);

            CIAO_DEBUG (9, (LM_TRACE, CLINFO
                            "CIAO_StoreReferences_i::post_install - "
                            "Registering name %C for instance %C\n",
                             name,
                             plan.instance[index].name.in ()));

            Name_Utilities::bind_object (name,
                                         obj.in (),
                                         this->ctx_.in ());
          }
        else if (ACE_OS::strcmp (inst.configProperty[i].name.in (),
                                 DAnCE::INSTANCE_IOR_FILE) == 0)
          {
            CORBA::Object_var obj;
            reference >>= CORBA::Any::to_object (obj);

            const char * name = 0;
            inst.configProperty[i].value >>= CORBA::Any::to_string (name, 0);

            CORBA::String_var ior = this->orb_->object_to_string (obj.in ());

            CIAO::Name_Utilities::write_ior (ACE_TEXT_CHAR_TO_TCHAR (name), ior.in ());
          }
      }
  }

  void
  CIAO_StoreReferences_i::configure (const ::Deployment::Properties &props )
  {
    for (CORBA::ULong i = 0; i < props.length (); ++i)
      {
        if (ACE_OS::strcmp (props[i].name.in (),
                            DAnCE::LOCALITY_NAMINGCONTEXT) == 0)
          {
            CORBA::Object_var obj;
            props[i].value >>= CORBA::Any::to_object (obj);

            this->ctx_ = CosNaming::NamingContext::_narrow (obj.in ());
          }
      }
  }

  /// Implementation skeleton constructor
  CIAO_ReferenceLookup_i::CIAO_ReferenceLookup_i (void)
  {
    this->orb_ = DAnCE::PLUGIN_MANAGER::instance ()->get_orb ();

    if (CORBA::is_nil (this->orb_))
      {
        CIAO_ERROR (1, (LM_ERROR, CLINFO
                        "Container_Handler_i::configure -"
                        "Unable to locate ORB.\n"));
        throw ::Deployment::StartError ("CIAO Container Handler",
                                        "Unable to locate ORB");
      }
  }

  /// Implementation skeleton destructor
  CIAO_ReferenceLookup_i::~CIAO_ReferenceLookup_i (void)
  {
  }

  void
  CIAO_ReferenceLookup_i::pre_connect (::Deployment::DeploymentPlan &plan,
                                       ::CORBA::ULong connectionref,
                                       ::CORBA::Any &ref)
  {
    ::Deployment::PlanConnectionDescription &conn = plan.connection[connectionref];

    if (conn.externalReference.length () >= 1)
      {
        try
          {
            CORBA::Object_ptr
              obj = this->orb_->string_to_object(conn.externalReference[0].location.in());

            if (!CORBA::is_nil (obj))
              {
                ref <<= obj;
              }
            else
              {
                DANCE_ERROR (DANCE_LOG_WARNING,
                             (LM_DEBUG, DLINFO
                              ACE_TEXT("CIAO_ReferenceLookup_i::pre_connect - ")
                              ACE_TEXT("can't create object for IOR %C\n"),
                              conn.externalReference[0].location.in()));
                throw Deployment::InvalidConnection (conn.name.in (),
                                                     ACE_TEXT ("Invalid ExternalReference\n"));
              }
          }
        catch (CORBA::Exception &ex)
            {
              DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT("CIAO_ReferenceLookup_i::pre_connect - ")
                            ACE_TEXT("Caught CORBA Exception while resolving endpoint for connection %C: %C\n"),
                            conn.name.in (),
                            ex._info ().c_str ()));
              throw Deployment::InvalidConnection (conn.name.in (),
                                                   ex._info ().c_str ());
            }
        catch (...)
          {
            DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                         (LM_ERROR, DLINFO
                          ACE_TEXT("CIAO_ReferenceLookup_i::pre_connect - ")
                          ACE_TEXT("Caught C++ Exception while resolving endpoint for connection\n")));
            throw Deployment::InvalidConnection (conn.name.in (),
                                                 ACE_TEXT ("C++ Exception while resolving external reference"));
          }
      }
  }

  void CIAO_ReferenceLookup_i::post_connect (const ::Deployment::DeploymentPlan &,
                                             ::CORBA::ULong,
                                             const ::CORBA::Any &)
  {
  }

  void
  CIAO_ReferenceLookup_i::configure (const ::Deployment::Properties & )
  {
  }
 }

extern "C"
{
  ::DAnCE::DeploymentInterceptor_ptr
  CIAO_Deployment_Interceptors_Export create_CIAO_StoreReferences (void)
  {
    return new CIAO::CIAO_StoreReferences_i ();
  }

  ::DAnCE::DeploymentInterceptor_ptr
  CIAO_Deployment_Interceptors_Export create_CIAO_ReferenceLookup (void)
  {
    return new CIAO::CIAO_ReferenceLookup_i ();
  }
}