summaryrefslogtreecommitdiff
path: root/modules/CIAO/DAnCE/NodeApplication/ServerActivator_Impl.cpp
blob: 541457a9afe1c8a05952c15fd034216a95a17b6c (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
// $Id$

#include "ServerActivator_Impl.h"
#include "Cdmw/CDMW_IDL_ExtC.h"
#include "DAnCE/Logger/Log_Macros.h"

const char* COMPONENT_SERVER_NAME = "ComponentServer";

bool read_config_value (const ACE_CString & name,
                        const Components::ConfigValues & config,
                        CORBA::Any_out value)
{
  bool found = false;
  CORBA::ULong const len = config.length();
  for (CORBA::ULong count = 0; count < len; ++count)
    {
      if (name.compare (config[count]->name()) == 0)
        {
          value = new CORBA::Any (config[count]->value());
          found = true;
          break;
        }
    }

  return found;
}

using namespace DAnCE;

ServerActivator_Impl::ServerActivator_Impl (CORBA::ORB_ptr orb,
                                            PortableServer::POA_ptr poa,
                                            Components::Deployment::ComponentInstallation_ptr compInst)
    : orb_ (CORBA::ORB::_duplicate (orb))
    , poa_ (PortableServer::POA::_duplicate (poa))
    , compInst_ (Components::Deployment::ComponentInstallation::_duplicate (compInst))
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::ServerActivator_Impl - started\n"));

  CORBA::Object_var obj =
    this->orb_->resolve_initial_references ("ProcessDestinationNC");
  if (CORBA::is_nil(obj))
    {
      DANCE_ERROR((LM_ERROR, "[%M] ServerActivator_Impl::ServerActivator_Impl - Failed to retrieve the \"ProcessDestinationNC\" object.\n"));
      throw CORBA::ORB::InvalidName();
    }
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::ServerActivator_Impl name service reference received\n"));
  try
  {
    this->naming_ = CosNaming::NamingContext::_narrow (obj);
  }
  catch (...)
  {
    DANCE_ERROR((LM_ERROR, "[%M] ServerActivator_Impl::ServerActivator_Impl - failed to narrow the \"ProcessDestinationNC\" NC.\n"));
    throw;
  }
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::ServerActivator_Impl - finished\n"));
}

ServerActivator_Impl::~ServerActivator_Impl()
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::~ServerActivator_Impl - started\n"));
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::~ServerActivator_Impl - finished\n"));
}

::Components::Deployment::ComponentServer_ptr
ServerActivator_Impl::create_component_server (const ::Components::ConfigValues & config)
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::create_component_server - started\n"));

  // NOTA: These are the configuration values understood by the server activator
  // for component server creation
  //
  // +====================================+=============================+
  // | Config Name                        | Type                        |
  // +====================================+=============================+
  // | PROCESS_DESTINATION (*)            | string                      |
  // +------------------------------------+-----------------------------+
  //
  // (*) compulsory
  //
  //Extracting process destination
  ACE_CString processDest;
  {
    CORBA::Any_var processDestAny;

    if (read_config_value (Components::PROCESS_DESTINATION, config, processDestAny))
      {
        const char* szProcessDest = 0;
        processDestAny.in() >>= szProcessDest;
        processDest = szProcessDest;
      }
    else
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - read_config_value() function fails\n"));
        throw ::Components::Deployment::InvalidConfiguration();
      }
  }
  ::Components::Deployment::ComponentServer_var srv;
  if (0 == this->servers_.find (processDest, srv))
    {
      return srv._retn();//::Components::Deployment::ComponentServer::_duplicate (srv.in());
    }
  //Looking for ComponentServer
  ACE_CString processName = processDest + "." + COMPONENT_SERVER_NAME;
  CosNaming::Name name (1);
  name.length (1);
  name[0].id = CORBA::string_dup (processDest.c_str());
  name[0].kind = CORBA::string_dup (COMPONENT_SERVER_NAME);
  CORBA::Object_var obj;
  CosNaming::NamingContext_var naming;
  try
    {
      obj = this->naming_->resolve (name);
    }
  catch (const CosNaming::NamingContext::NotFound& )
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - NotFound exception rised."
                   "(Name : CDMW/SERVICES/ASSEMBLYANDDEPLOYMENT/%C)\n"
                   , processDest.c_str()));
      throw ::Components::CreateFailure();
    }
  catch (const CosNaming::NamingContext::CannotProceed& )
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - NotFound exception rised."
                   "(Name : CDMW/SERVICES/ASSEMBLYANDDEPLOYMENT/%C)\n"
                   , processDest.c_str()));
      throw ::Components::CreateFailure();
    }
  catch (const CosNaming::NamingContext::InvalidName& )
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - NotFound exception rised."
                   "(Name : CDMW/SERVICES/ASSEMBLYANDDEPLOYMENT/%C)\n"
                   , processDest.c_str()));
      throw ::Components::CreateFailure();
    }
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::create_component_server - ComponentServer object resolved\n"));
  //Casting founded object
  ::Components::Deployment::ComponentServer_var server
    = ::Components::Deployment::ComponentServer::_narrow (obj);
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::create_component_server - ComponentServer object narrowed\n"));
  if (CORBA::is_nil (server))
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - ComponentServer in process destination %s  could not be contacted\n", processDest.c_str()));
      throw ::Components::CreateFailure (::Components::COMPONENT_SERVER_NOT_FOUND);
    }

  //ComponentServer initialization by ServerActivator and ComponentInstallation references
  this->initializeComponentServer (server.in());

  //Saving server reference
  //TODO add checking on already present component server with exception CdmwDeployment::COMPONENT_SERVER_ALREADY_CREATED
  if (0 != this->servers_.bind (processDest, ::Components::Deployment::ComponentServer::_duplicate (server.in())))
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::create_component_server - ComponentServer in process destination %s already exists.\n", processDest.c_str()));
      throw ::Components::CreateFailure (::Components::COMPONENT_SERVER_ALREADY_CREATED);
    }

  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::create_component_server - finished\n"));
  return server._retn ();
}

void
ServerActivator_Impl::remove_component_server (::Components::Deployment::ComponentServer_ptr server)
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::remove_component_server - started\n"));

  // Checking input parameters
  if (CORBA::is_nil (server))
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::remove_component_server - Wrong input parameter\n"));
      throw CORBA::BAD_PARAM();
    }

  // find equivalent server in servers list
  bool bFound = false;
  for (TCompServers::iterator iter = this->servers_.begin();
       iter != this->servers_.end();
       ++iter)
    {
      if (server->_is_equivalent ( (*iter).int_id_.in()))
        {
          // remove ComponentServer from list
          this->servers_.unbind ( (*iter).ext_id_);
          bFound = true;
          break;
        }
    }

  // if no equivalent found, throw exception
  if (!bFound)
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::remove_component_server - equivalent server cannot be found in servers list\n"));
      throw ::Components::RemoveFailure (::Components::UNKNOWN_COMPONENT_SERVER);
    }

  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::remove_component_server - finished\n"));
}

::Components::Deployment::ComponentServers *
ServerActivator_Impl::get_component_servers ()
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::get_component_servers - started\n"));

  ::Components::Deployment::ComponentServers* pServers = 0;
  ACE_NEW_THROW_EX (pServers,
                    ::Components::Deployment::ComponentServers (this->servers_.current_size()),
                    CORBA::NO_MEMORY());
  int i = 0;
  for (TCompServers::iterator iter = this->servers_.begin();
       iter != this->servers_.end();
       ++iter)
    {
      (*pServers) [i] = ::Components::Deployment::ComponentServer::_duplicate ( (*iter).int_id_.in());
      i++;
    }

  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::get_component_servers - finished\n"));
  return pServers;
}

void
ServerActivator_Impl::initializeComponentServer (::Components::Deployment::ComponentServer_ptr server)
{
  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::initializeComponentServer - started\n"));

  // the ComponentServer should be a Cdmw ComponentServer
  Components::Deployment::ComponentServer_var cdmwServer =
    Components::Deployment::ComponentServer::_narrow (server);

  if (CORBA::is_nil (cdmwServer.in ())
    {
      // Component server is not Cdmw ComponentServer
      DANCE_ERROR ( (LM_ERROR, "[%M] ServerActivator_Impl::initializeComponentServer - ComponentServer is not CdmwComponentServer\n"));
      throw ::Components::CreateFailure (::Components::COMPONENT_SERVER_NOT_FOUND);
    }
  if (CORBA::is_nil (this->compInst_.in()))
    {
      DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::initializeComponentServer - Reference on ComponentInstallation is nil!!!!\n"));
    }
  CdmwCcmComponentServer::ComponentServer_var proprietary_svr =
    CdmwCcmComponentServer::ComponentServer::_narrow (cdmwServer);

  // give it the ComponentInstallation
  proprietary_svr->set_component_installation (this->compInst_.in());
  // give it the ServerActivator
  proprietary_svr->set_server_activator (Components::Deployment::ServerActivator::_narrow (this->poa_->servant_to_reference (this)));

  DANCE_DEBUG ( (LM_DEBUG, "[%M] ServerActivator_Impl::initializeComponentServer - finished\n"));
}