vtkPVPluginTracker.cxx 14.7 KB
Newer Older
1
2
3
/*=========================================================================

  Program:   ParaView
Sebastien Jourdain's avatar
Sebastien Jourdain committed
4
  Module:    vtkPVPluginTracker.cxx
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

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkPVPluginTracker.h"

#include "vtkClientServerInterpreterInitializer.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkPVConfig.h"
#include "vtkPVOptions.h"
#include "vtkPVPlugin.h"
#include "vtkPVPluginLoader.h"
#include "vtkPVPythonModule.h"
#include "vtkPVPythonPluginInterface.h"
#include "vtkPVServerManagerPluginInterface.h"
#include "vtkPVXMLElement.h"
#include "vtkPVXMLParser.h"

#include <assert.h>
32
33
#include <string>
#include <vector>
34
35
#include <vtksys/ios/sstream>
#include <vtksys/SystemTools.hxx>
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
36
37
38
39
40
41
42
43
44
45
46
#include <vtksys/String.hxx>


#if defined(WIN32) && !defined(__CYGWIN__)
/* String comparison routine. */
# define VTKSTRNCASECMP _strnicmp
#else
# include "strings.h"
# define VTKSTRNCASECMP strncasecmp
#endif

47
48
49
50

#define vtkPVPluginTrackerDebugMacro(x)\
{ if (debug_plugin) {\
  vtksys_ios::ostringstream vtkerror;\
51
  vtkerror << x << endl;\
52
53
54
55
56
57
58
  vtkOutputWindowDisplayText(vtkerror.str().c_str());} }

namespace
{
  class vtkItem
    {
  public:
59
60
    std::string FileName;
    std::string PluginName;
61
    vtkPVPlugin* Plugin;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
62
    bool AutoLoad;
63
64
65
    vtkItem()
      {
      this->Plugin = NULL;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
66
      this->AutoLoad = false;
67
      }
68
69
    };

70
  std::string vtkLocatePlugin(const char* plugin, bool add_extensions)
71
    {
Sebastien Jourdain's avatar
Sebastien Jourdain committed
72
73
74
    // Make sure we can get the options before going further
    if(vtkProcessModule::GetProcessModule() == NULL)
      {
75
      return std::string();
Sebastien Jourdain's avatar
Sebastien Jourdain committed
76
77
      }

78
79
    bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
    vtkPVOptions* options = vtkProcessModule::GetProcessModule()->GetOptions();
80
    std::string app_dir = options->GetApplicationPath();
81
82
    app_dir = vtksys::SystemTools::GetProgramPath(app_dir.c_str());

83
    std::vector<std::string> paths_to_search;
84
    paths_to_search.push_back(app_dir);
85
    paths_to_search.push_back(app_dir + "/../lib/");
86
87
88
89
90
    paths_to_search.push_back(app_dir + "/plugins/" + plugin);
#if defined(__APPLE__)
    paths_to_search.push_back(app_dir + "/../Plugins");
    paths_to_search.push_back(app_dir + "/../../..");
#endif
91
92
    // On windows configuration files are in the parent directory
    paths_to_search.push_back(app_dir + "/../");
93

94
95
    std::string name = plugin;
    std::string filename = name;
96
97
    if (add_extensions)
      {
98
#if defined(_WIN32) && !defined(__CYGWIN__)
99
      filename = name + ".dll";
100
#elif defined(__APPLE__)
101
      filename = "lib" + name + ".dylib";
102
#else
103
      filename = "lib" + name + ".so";
104
#endif
105
      }
106
107
    for (size_t cc=0; cc < paths_to_search.size(); cc++)
      {
108
      std::string path = paths_to_search[cc];
109
110
111
112
113
114
115
116
      if (vtksys::SystemTools::FileExists(
          (path + "/" + filename).c_str(), true))
        {
        return (path + "/" + filename);
        }
      vtkPVPluginTrackerDebugMacro(
        (path + "/" + filename).c_str() << "-- not found");
      }
117
    return std::string();
118
    }
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
119

120
  std::string vtkGetPluginNameFromFileName(const std::string& filename)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
121
    {
122
    std::string defaultname =
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
123
124
      vtksys::SystemTools::GetFilenameWithoutExtension(filename);
    if (defaultname.size() > 3 &&
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
125
      VTKSTRNCASECMP(defaultname.c_str(), "lib", 3) == 0)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
126
127
128
129
130
      {
      defaultname.erase(0, 3);
      }
    return defaultname;
    }
131
132
133
}

class vtkPVPluginTracker::vtkPluginsList :
134
  public std::vector<vtkItem>
135
136
{
public:
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
137
  iterator LocateUsingPluginName(const char* pluginname)
138
139
140
141
142
143
144
145
146
147
    {
    for (iterator iter = this->begin(); iter != this->end(); ++iter)
      {
      if (iter->PluginName == pluginname)
        {
        return iter;
        }
      }
    return this->end();
    }
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
148
149
150
151
152
153
154
155
156
157
158
159

  iterator LocateUsingFileName(const char* filename)
    {
    for (iterator iter = this->begin(); iter != this->end(); ++iter)
      {
      if (iter->FileName == filename)
        {
        return iter;
        }
      }
    return this->end();
    }
160
161
};

162
vtkStandardNewMacro(vtkPVPluginTracker);
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//----------------------------------------------------------------------------
vtkPVPluginTracker::vtkPVPluginTracker()
{
  this->PluginsList = new vtkPluginsList();
}

//----------------------------------------------------------------------------
vtkPVPluginTracker::~vtkPVPluginTracker()
{
  delete this->PluginsList;
  this->PluginsList = NULL;
}

//----------------------------------------------------------------------------
vtkPVPluginTracker* vtkPVPluginTracker::GetInstance()
{
179
180
  static vtkSmartPointer<vtkPVPluginTracker> Instance;
  if (Instance.GetPointer() == NULL)
181
    {
182
    vtkPVPluginTracker* mgr = vtkPVPluginTracker::New();
183
    Instance = mgr;
184
    mgr->FastDelete();
185
186
187
188

    bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
    vtkPVPluginTrackerDebugMacro("Locate and load distributed plugin list.");

189
190
191
    // Locate ".plugins" file and process it.
    // This will setup the distributed-list of plugins. Also it will load any
    // auto-load plugins.
192
    std::string _plugins = vtkLocatePlugin(".plugins", false);
193
194
195
196
197
198
199
200
201
    if (!_plugins.empty())
      {
      mgr->LoadPluginConfigurationXML(_plugins.c_str());
      }
    else
      {
      vtkPVPluginTrackerDebugMacro(
        "Could not find .plugins file for distributed plugins");
      }
202
203
204
205
206
207

    // Now load any plugins located in the PV_PLUGIN_PATH environment variable.
    // These are always loaded (not merely located).
    vtkPVPluginLoader* loader = vtkPVPluginLoader::New();
    loader->LoadPluginsFromPluginSearchPath();
    loader->Delete();
208
209
    }

210
  return Instance;
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
}

//----------------------------------------------------------------------------
void vtkPVPluginTracker::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXML(const char* filename)
{
  bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
  vtkPVPluginTrackerDebugMacro("Loading plugin configuration xml: " << filename);
  if (!vtksys::SystemTools::FileExists(filename, true))
    {
    vtkPVPluginTrackerDebugMacro("Failed to located configuration xml. "
      "Could not populate the list of plugins distributed with application.");
    return;
    }

  vtkSmartPointer<vtkPVXMLParser> parser = vtkSmartPointer<vtkPVXMLParser>::New();
  parser->SetFileName(filename);
  parser->SuppressErrorMessagesOn();
  if (!parser->Parse())
    {
    vtkPVPluginTrackerDebugMacro("Configuration file not a valid xml.");
    return;
    }

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
  this->LoadPluginConfigurationXML(parser->GetRootElement());
}

//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXMLFromString(const char* xmlcontents)
{
  bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
  vtkSmartPointer<vtkPVXMLParser> parser = vtkSmartPointer<vtkPVXMLParser>::New();
  parser->SuppressErrorMessagesOn();
  if (!parser->Parse(xmlcontents))
    {
    vtkPVPluginTrackerDebugMacro("Configuration file not a valid xml.");
    return;
    }

  this->LoadPluginConfigurationXML(parser->GetRootElement());
}

//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXML(vtkPVXMLElement* root)
{
  if (root == NULL)
    {
    return;
    }

  bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
267
268
269
270
271
272
273
274
275
276
277
278
  if (strcmp(root->GetName(), "Plugins") != 0)
    {
    vtkPVPluginTrackerDebugMacro("Root element in the xml must be <Plugins/>. "
      "Got " << root->GetName());
    return;
    }

  for (unsigned int cc=0; cc < root->GetNumberOfNestedElements(); cc++)
    {
    vtkPVXMLElement* child = root->GetNestedElement(cc);
    if (child && child->GetName() && strcmp(child->GetName(), "Plugin") == 0)
      {
279
      std::string name = child->GetAttributeOrEmpty("name");
280
      int auto_load;
281
      if (name.empty() || !child->GetScalarAttribute("auto_load", &auto_load))
282
283
284
285
286
287
        {
        vtkPVPluginTrackerDebugMacro(
          "Missing required attribute name or auto_load. Skipping element.");
        continue;
        }
      vtkPVPluginTrackerDebugMacro("Trying to locate plugin with name: "
288
        << name.c_str());
289
      std::string plugin_filename;
290
291
      if (child->GetAttribute("filename") &&
        vtksys::SystemTools::FileExists(child->GetAttribute("filename"), true))
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
292
293
294
295
296
        {
        plugin_filename = child->GetAttribute("filename");
        }
      else
        {
297
        plugin_filename = vtkLocatePlugin(name.c_str(), true);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
298
299
        }
      if (plugin_filename.empty())
300
301
302
303
304
305
        {
        int required = 0;
        child->GetScalarAttribute("required", &required);
        if (required)
          {
          vtkErrorMacro(
306
            "Failed to locate required plugin: " << name.c_str() << "\n"
307
308
309
            "Application may not work exactly as expected.");
          }
        vtkPVPluginTrackerDebugMacro("Failed to locate file plugin: "
310
          << name.c_str());
311
312
313
        continue;
        }
      vtkPVPluginTrackerDebugMacro("--- Found " << plugin_filename);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
314
315
      unsigned int index = this->RegisterAvailablePlugin(plugin_filename.c_str());
      if (auto_load && !this->GetPluginLoaded(index))
316
317
318
319
320
321
        {
        // load the plugin.
        vtkPVPluginLoader* loader = vtkPVPluginLoader::New();
        loader->LoadPlugin(plugin_filename.c_str());
        loader->Delete();
        }
Sebastien Jourdain's avatar
Sebastien Jourdain committed
322
      (*this->PluginsList)[index].AutoLoad = (auto_load != 0);
323
324
325
326
327
328
329
330
331
332
      }
    }
}

//----------------------------------------------------------------------------
unsigned int vtkPVPluginTracker::GetNumberOfPlugins()
{
  return static_cast<unsigned int>(this->PluginsList->size());
}

333
//----------------------------------------------------------------------------
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
334
unsigned int vtkPVPluginTracker::RegisterAvailablePlugin(const char* filename)
335
{
336
  std::string defaultname = vtkGetPluginNameFromFileName(filename);
337
  vtkPluginsList::iterator iter =
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
338
339
340
341
342
    this->PluginsList->LocateUsingFileName(filename);
  if (iter == this->PluginsList->end())
    {
    iter = this->PluginsList->LocateUsingPluginName(defaultname.c_str());
    }
343
344
345
346
347
348
  if (iter == this->PluginsList->end())
    {
    vtkItem item;
    item.FileName = filename;
    item.PluginName = defaultname;
    this->PluginsList->push_back(item);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
349
    return static_cast<unsigned int>(this->PluginsList->size()-1);
350
351
352
353
    }
  else
    {
    iter->FileName = filename;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
354
    return static_cast<unsigned int>(iter - this->PluginsList->begin());
355
356
357
    }
}

358
359
360
361
362
//----------------------------------------------------------------------------
void vtkPVPluginTracker::RegisterPlugin(vtkPVPlugin* plugin)
{
  assert(plugin != NULL);

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
363
  vtkPluginsList::iterator iter = this->PluginsList->LocateUsingPluginName(
364
    plugin->GetPluginName());
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
365
366
367
368
369
  // use filename for matching is present, that's a better test.
  if (plugin->GetFileName())
    {
    iter = this->PluginsList->LocateUsingFileName(plugin->GetFileName());
    }
370
371
372
  if (iter == this->PluginsList->end())
    {
    vtkItem item;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
373
374
    item.FileName = plugin->GetFileName()? plugin->GetFileName() :
      "linked-in";
375
376
377
378
379
380
381
    item.PluginName = plugin->GetPluginName();
    item.Plugin = plugin;
    this->PluginsList->push_back(item);
    }
  else
    {
    iter->Plugin = plugin;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
382
383
384
385
    if (plugin->GetFileName())
      {
      iter->FileName = plugin->GetFileName();
      }
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
    }

  // Do some basic processing of the plugin here itself.

  // If this plugin has functions for initializing the interpreter, we set them
  // up right now.
  vtkPVServerManagerPluginInterface* smplugin =
    dynamic_cast<vtkPVServerManagerPluginInterface*>(plugin);
  if (smplugin)
    {
    if (smplugin->GetInitializeInterpreterCallback())
      {
      // This also initializes any existing instances of
      // vtkClientServerInterpreter. Refer to
      // vtkClientServerInterpreterInitializer::RegisterCallback implementation
      // for details.
      vtkClientServerInterpreterInitializer::GetInitializer()->RegisterCallback(
        smplugin->GetInitializeInterpreterCallback());
      }
    }

  // If this plugin has Python modules, process those.
  vtkPVPythonPluginInterface* pythonplugin =
    dynamic_cast<vtkPVPythonPluginInterface*>(plugin);
  if (pythonplugin)
    {
412
413
    std::vector<std::string> modules, sources;
    std::vector<int> package_flags;
414
    pythonplugin->GetPythonSourceList(modules, sources, package_flags);
415
416
    assert(modules.size() == sources.size() &&
      sources.size() == package_flags.size());
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
    for (size_t cc=0; cc < modules.size(); cc++)
      {
      vtkPVPythonModule* module = vtkPVPythonModule::New();
      module->SetFullName(modules[cc].c_str());
      module->SetSource(sources[cc].c_str());
      module->SetIsPackage(package_flags[cc]);
      vtkPVPythonModule::RegisterModule(module);
      module->Delete();
      }
    }

  this->InvokeEvent(vtkCommand::RegisterEvent, plugin);
}

//----------------------------------------------------------------------------
vtkPVPlugin* vtkPVPluginTracker::GetPlugin(unsigned int index)
{
  if (index >= this->GetNumberOfPlugins())
    {
    vtkWarningMacro("Invalid index: " << index);
    return NULL;
    }
  return (*this->PluginsList)[index].Plugin;
}

//----------------------------------------------------------------------------
const char* vtkPVPluginTracker::GetPluginName(unsigned int index)
{
if (index >= this->GetNumberOfPlugins())
    {
    vtkWarningMacro("Invalid index: " << index);
    return NULL;
    }
  return (*this->PluginsList)[index].PluginName.c_str();
}

//----------------------------------------------------------------------------
const char* vtkPVPluginTracker::GetPluginFileName(unsigned int index)
{
if (index >= this->GetNumberOfPlugins())
    {
    vtkWarningMacro("Invalid index: " << index);
    return NULL;
    }
  return (*this->PluginsList)[index].FileName.c_str();
}

//----------------------------------------------------------------------------
bool vtkPVPluginTracker::GetPluginLoaded(unsigned int index)
{
if (index >= this->GetNumberOfPlugins())
    {
    vtkWarningMacro("Invalid index: " << index);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
470
    return false;
471
472
473
    }
  return (*this->PluginsList)[index].Plugin != NULL;
}
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
474
475
476
477
478
479
480

//----------------------------------------------------------------------------
bool vtkPVPluginTracker::GetPluginAutoLoad(unsigned int index)
{
  if (index >= this->GetNumberOfPlugins())
    {
    vtkWarningMacro("Invalid index: " << index);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
481
    return false;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
482
483
484
    }
  return (*this->PluginsList)[index].AutoLoad;
}