summaryrefslogtreecommitdiff
path: root/DAnCE/dance/LocalityManager/Handler/Plugin_Conf.cpp
blob: 55d2e7a3def704dc78c7bf62e4eaecac8b509b4a (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
#include "Plugin_Conf.h"
#include "ace/Read_Buffer.h"
#include "ace/Tokenizer_T.h"
#include "dance/Logger/Log_Macros.h"
#include "dance/DAnCE_PropertiesC.h"

#include "Inst_Handler_Impl.h"
#include "Interceptor_Handler_Impl.h"
#include "Config_Handler_Impl.h"

#include <vector>
#include <iostream>

namespace DAnCE
{
  void
  Plugin_Configurator::load_from_text_file (const ACE_TCHAR *file)
  {
    DANCE_TRACE ("Plugin_Configurator::load_from_text_file");

    if (!file)
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Plugin_Configurator::load_from_text_file - ")
                      ACE_TEXT("Error: Provided with nil filename\n")));
        return;
      }

    FILE *inf = ACE_OS::fopen (file, ACE_TEXT("r"));

    if (!inf)
      {
        DANCE_ERROR (DANCE_LOG_ERROR,
                     (LM_ERROR, DLINFO ACE_TEXT("Plugin_Configurator::load_from_text_file - ")
                      ACE_TEXT("Fail to open plugin data file: <%s>\n"),
                      file));
        return;
      }

    ::Deployment::DeploymentPlan plan;

    ACE_Read_Buffer reader (inf, true);

    char * string = 0;
    CORBA::ULong pos (0);

    // Read from the file line by line
    while ((string = reader.read ('\n')) != 0)
      {
        try
          {
            size_t const len = ACE_OS::strlen (string);

            if (len != 0)
              {
                // If a line starts with # it is a comment line, we skip it,
                // but inform the user in case logging has been enabled
                if (string[0] == '#')
                {
                  DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                                (LM_DEBUG, DLINFO
                                ACE_TEXT ("Plugin_Configurator::load_from_text_file - ")
                                ACE_TEXT ("Skipping comment line <%C>\n"),
                                string));
                }
                else
                {
                  std::istringstream tokenizer (string);
                  std::vector< std::string > tokens;
                  std::copy (std::istream_iterator< std::string > (tokenizer),
                            std::istream_iterator <std::string > (),
                            std::back_inserter < std::vector < std::string > > (tokens));

                  if (tokens.size () < 3 || tokens.size () > 4)
                    {
                      DANCE_ERROR (DANCE_LOG_ERROR,
                                  (LM_ERROR, DLINFO
                                    ACE_TEXT ("Plugin_Configurator::load_from_text_file - ")
                                    ACE_TEXT ("Skipping line <%C>, wrong format.  Got %u tokens instead of 3 or 4\n"),
                                    string,
                                    tokens.size ()));
                    }
                  else
                    {
                      int open_mode = ACE_DEFAULT_SHLIB_MODE;

                      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                                  (LM_DEBUG, DLINFO
                                    ACE_TEXT ("Plugin_Configurator::load_from_text_file - ")
                                    ACE_TEXT ("Attempting to load plugin of type <%C>, artifact <%C>, entrypoint <%C>, open_mode <%C>\n"),
                                    tokens[0].c_str (),
                                    tokens[1].c_str (),
                                    tokens[2].c_str (),
                                    tokens.size () == 4 ? tokens[3].c_str() : 0));

                      if (tokens.size() == 4)
                        {
                          open_mode = ACE_OS::atoi (tokens[3].c_str());
                        }

                      if (ACE_OS::strcmp (tokens[0].c_str (),
                                          DAnCE::DANCE_INSTALLATIONHANDLER) == 0)
                        {
                          this->create_entry (tokens[1].c_str (), tokens[2].c_str (),
                                              DAnCE::DANCE_INSTALLATIONHANDLER,
                                              open_mode,
                                              plan, pos++);
                        }
                      else if (ACE_OS::strcmp (tokens[0].c_str (),
                                              DAnCE::DANCE_DEPLOYMENTINTERCEPTOR) == 0)
                        {
                          this->create_entry (tokens[1].c_str (), tokens[2].c_str (),
                                              DAnCE::DANCE_DEPLOYMENTINTERCEPTOR,
                                              open_mode,
                                              plan, pos++);
                        }
                      else if (ACE_OS::strcmp (tokens[0].c_str (),
                                              DAnCE::DANCE_CONFIGPLUGIN) == 0)
                        {
                          this->create_entry (tokens[1].c_str (), tokens[2].c_str (),
                                              DAnCE::DANCE_CONFIGPLUGIN,
                                              open_mode,
                                              plan, pos++);
                        }
                      else
                        {
                          DANCE_ERROR (DANCE_LOG_ERROR,
                                      (LM_ERROR, DLINFO
                                        ACE_TEXT ("Plugin_Configurator::load_from_text_file - ")
                                        ACE_TEXT ("Skipping line <%C>, bad type name\n"),
                                        string));
                        }
                    }
                }
              }
          }
        catch (...)
          {
            throw;
          }
        reader.alloc ()->free (string);
      }

    this->deploy_plan (plan);
  }

  void
  Plugin_Configurator::deploy_plan (::Deployment::DeploymentPlan &plan)
  {
    DANCE_TRACE ("Plugin_Configurator::deploy_plan");

    DAnCE::Inst_Handler_Impl inst_handler;
    DAnCE::Interceptor_Handler_Impl interceptor_handler;
    DAnCE::Config_Handler_Impl config_handler;

    for (CORBA::ULong i = 0; i < plan.instance.length (); ++i)
      {
        if (plan.instance[i].implementationRef >= plan.implementation.length ())
          {
            DANCE_ERROR (DANCE_LOG_ERROR,
                         (LM_ERROR, DLINFO
                          ACE_TEXT ("Plugin_Configurator::deploy_plan - ")
                          ACE_TEXT ("Invalid implementationRef for instance <%C>\n"),
                          plan.instance[i].name.in ()));
            continue;
          }

        ::Deployment::MonolithicDeploymentDescription &mdd =
            plan.implementation[plan.instance[i].implementationRef];

        CORBA::String_var inst_type =
          DAnCE::Utility::get_instance_type (mdd.execParameter);

        if (ACE_OS::strcmp (inst_type.in (),
                            DAnCE::DANCE_INSTALLATIONHANDLER) == 0)
          {
            CORBA::Any_var any;
            inst_handler.install_instance (plan, i, any.out ());
          }
        else if (ACE_OS::strcmp (inst_type.in (),
                                 DAnCE::DANCE_DEPLOYMENTINTERCEPTOR) == 0)
          {
            CORBA::Any_var any;
            interceptor_handler.install_instance (plan, i, any.out ());
          }
        else if (ACE_OS::strcmp (inst_type.in (),
                                 DAnCE::DANCE_CONFIGPLUGIN) == 0)
          {
            CORBA::Any_var any;
            config_handler.install_instance (plan, i, any.out ());
          }
      }
  }

  void
  Plugin_Configurator::create_entry (const char *artifact,
                                     const char *entrypoint,
                                     const char *type,
                                     int open_mode,
                                     ::Deployment::DeploymentPlan &plan,
                                     CORBA::ULong pos)
  {
    DANCE_TRACE ("Plugin_Configurator::create_entry");

    plan.implementation.length (pos + 1);
    plan.implementation[pos].name = artifact;
    plan.implementation[pos].execParameter.length (4);

    plan.implementation[pos].execParameter[0].name = DAnCE::DANCE_PLUGIN_ARTIFACT;
    plan.implementation[pos].execParameter[0].value <<=
      CORBA::Any::from_string (artifact, 0);

    plan.implementation[pos].execParameter[1].name = DAnCE::DANCE_PLUGIN_ENTRYPT;
    plan.implementation[pos].execParameter[1].value <<=
      CORBA::Any::from_string (entrypoint, 0);

    plan.implementation[pos].execParameter[2].name = DAnCE::DANCE_PLUGIN_OPENMODE;
    plan.implementation[pos].execParameter[2].value <<= open_mode;

    plan.implementation[pos].execParameter[3].name = DAnCE::IMPL_TYPE;
    plan.implementation[pos].execParameter[3].value <<=
      CORBA::Any::from_string (type, 0);

    plan.instance.length (pos + 1);
    plan.instance[pos].name = entrypoint;
    plan.instance[pos].implementationRef = pos;
  }
}