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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 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 412 413 414 415 416 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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
/*=========================================================================
Program: ParaView
Module: vtkPVPluginTracker.cxx
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 "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkPSystemTools.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>
#include <string>
#include <vector>
#include <sstream>
#include <vtksys/SystemTools.hxx>
#include <vtksys/String.hxx>
#if defined(_WIN32) && !defined(__CYGWIN__)
/* String comparison routine. */
# define VTKSTRNCASECMP _strnicmp
#else
# include "strings.h"
# define VTKSTRNCASECMP strncasecmp
#endif
#define vtkPVPluginTrackerDebugMacro(x)\
{ if (debug_plugin) {\
std::ostringstream vtkerror;\
vtkerror << x << endl;\
vtkOutputWindowDisplayText(vtkerror.str().c_str());} }
namespace
{
class vtkItem
{
public:
std::string FileName;
std::string PluginName;
vtkPVPlugin* Plugin;
bool AutoLoad;
vtkItem()
{
this->Plugin = NULL;
this->AutoLoad = false;
}
};
std::string vtkLocatePluginSerial(const char* plugin, bool add_extensions, vtkPluginSearchFunction searchFunction)
{
(void)searchFunction;
// Make sure we can get the options before going further
if(vtkProcessModule::GetProcessModule() == NULL)
{
return std::string();
}
bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
#ifndef BUILD_SHARED_LIBS
vtkPVPluginTrackerDebugMacro("Looking for static plugin \'" << plugin << "\'");
if (searchFunction && searchFunction(plugin))
{
vtkPVPluginTrackerDebugMacro("Found static plugin \'" << plugin << "\'");
return plugin;
}
#endif
vtkPVOptions* options = vtkProcessModule::GetProcessModule()->GetOptions();
std::string app_dir = options->GetApplicationPath();
app_dir = vtksys::SystemTools::GetProgramPath(app_dir.c_str());
std::vector<std::string> paths_to_search;
paths_to_search.push_back(app_dir);
paths_to_search.push_back(app_dir + "/../lib/");
paths_to_search.push_back(app_dir + "/plugins/" + plugin);
#if defined(__APPLE__)
// paths for app
paths_to_search.push_back(app_dir + "/../Plugins");
paths_to_search.push_back(app_dir + "/../../..");
paths_to_search.push_back(app_dir + "/../../../../lib");
#endif
// paths when doing an unix style install on OsX and static builds on Linux.
paths_to_search.push_back(app_dir +"/../lib/paraview-" PARAVIEW_VERSION);
// On windows configuration files are in the parent directory
paths_to_search.push_back(app_dir + "/../");
// Add test plugin path into the search path.
std::string test_plugin_dir ( options->GetTestPluginPath());
if ( !test_plugin_dir.empty() )
{
paths_to_search.push_back( test_plugin_dir );
}
std::string name = plugin;
std::string filename = name;
if (add_extensions)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
filename = name + ".dll";
#elif defined(__APPLE__)
filename = "lib" + name + ".dylib";
#else
filename = "lib" + name + ".so";
#endif
}
for (size_t cc=0; cc < paths_to_search.size(); cc++)
{
std::string path = paths_to_search[cc];
if (vtksys::SystemTools::FileExists(
(path + "/" + filename).c_str(), true))
{
return (path + "/" + filename);
}
vtkPVPluginTrackerDebugMacro(
(path + "/" + filename).c_str() << "-- not found");
}
return std::string();
}
std::string vtkLocatePlugin(const char* plugin, bool add_extensions, vtkPluginSearchFunction searchFunction)
{
if(vtkMultiProcessController* controller = vtkMultiProcessController::GetGlobalController())
{
std::string pluginLocation;
if(controller->GetLocalProcessId() == 0)
{
pluginLocation = vtkLocatePluginSerial(plugin, add_extensions, searchFunction);
}
vtkPSystemTools::BroadcastString(pluginLocation, 0);
return pluginLocation;
}
return vtkLocatePluginSerial(plugin, add_extensions, searchFunction);
}
std::string vtkGetPluginNameFromFileName(const std::string& filename)
{
std::string defaultname =
vtksys::SystemTools::GetFilenameWithoutExtension(filename);
if (defaultname.size() > 3 &&
VTKSTRNCASECMP(defaultname.c_str(), "lib", 3) == 0)
{
defaultname.erase(0, 3);
}
return defaultname;
}
}
class vtkPVPluginTracker::vtkPluginsList :
public std::vector<vtkItem>
{
public:
iterator LocateUsingPluginName(const char* pluginname)
{
for (iterator iter = this->begin(); iter != this->end(); ++iter)
{
if (iter->PluginName == pluginname)
{
return iter;
}
}
return this->end();
}
iterator LocateUsingFileName(const char* filename)
{
for (iterator iter = this->begin(); iter != this->end(); ++iter)
{
if (iter->FileName == filename)
{
return iter;
}
}
return this->end();
}
};
vtkPluginSearchFunction vtkPVPluginTracker::StaticPluginSearchFunction = 0;
vtkStandardNewMacro(vtkPVPluginTracker);
//----------------------------------------------------------------------------
vtkPVPluginTracker::vtkPVPluginTracker()
{
this->PluginsList = new vtkPluginsList();
}
//----------------------------------------------------------------------------
vtkPVPluginTracker::~vtkPVPluginTracker()
{
delete this->PluginsList;
this->PluginsList = NULL;
}
//----------------------------------------------------------------------------
vtkPVPluginTracker* vtkPVPluginTracker::GetInstance()
{
static vtkSmartPointer<vtkPVPluginTracker> Instance;
if (Instance.GetPointer() == NULL)
{
vtkPVPluginTracker* mgr = vtkPVPluginTracker::New();
Instance = mgr;
mgr->FastDelete();
bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
vtkPVPluginTrackerDebugMacro("Locate and load distributed plugin list.");
// Locate ".plugins" file and process it.
// This will setup the distributed-list of plugins. Also it will load any
// auto-load plugins.
std::string _plugins = vtkLocatePlugin(".plugins", false, StaticPluginSearchFunction);
if (!_plugins.empty())
{
mgr->LoadPluginConfigurationXML(_plugins.c_str());
}
else
{
vtkPVPluginTrackerDebugMacro(
"Could not find .plugins file for distributed plugins");
}
}
return Instance;
}
//----------------------------------------------------------------------------
void vtkPVPluginTracker::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXML(const char* filename, bool forceLoad)
{
bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
vtkPVPluginTrackerDebugMacro("Loading plugin configuration xml: " << filename);
if (!vtkPSystemTools::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;
}
this->LoadPluginConfigurationXML(parser->GetRootElement(), forceLoad);
}
//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXMLFromString(const char* xmlcontents, bool forceLoad)
{
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(), forceLoad);
}
//----------------------------------------------------------------------------
void vtkPVPluginTracker::LoadPluginConfigurationXML(vtkPVXMLElement* root, bool forceLoad)
{
if (root == NULL)
{
return;
}
bool debug_plugin = vtksys::SystemTools::GetEnv("PV_PLUGIN_DEBUG") != NULL;
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)
{
std::string name = child->GetAttributeOrEmpty("name");
int auto_load = 0;
child->GetScalarAttribute("auto_load", &auto_load);
if (name.empty())
{
vtkPVPluginTrackerDebugMacro(
"Missing required attribute name. Skipping element.");
continue;
}
vtkPVPluginTrackerDebugMacro("Trying to locate plugin with name: " << name.c_str());
std::string plugin_filename;
if (child->GetAttribute("filename") &&
vtkPSystemTools::FileExists(child->GetAttribute("filename"), true))
{
plugin_filename = child->GetAttribute("filename");
}
else
{
plugin_filename = vtkLocatePlugin(name.c_str(), true, StaticPluginSearchFunction);
}
if (plugin_filename.empty())
{
int required = 0;
child->GetScalarAttribute("required", &required);
if (required)
{
vtkErrorMacro(
"Failed to locate required plugin: " << name.c_str() << "\n"
"Application may not work exactly as expected.");
}
vtkPVPluginTrackerDebugMacro("Failed to locate file plugin: "
<< name.c_str());
continue;
}
vtkPVPluginTrackerDebugMacro("--- Found " << plugin_filename);
unsigned int index = this->RegisterAvailablePlugin(plugin_filename.c_str());
if ((auto_load || forceLoad) && !this->GetPluginLoaded(index))
{
// load the plugin.
vtkPVPluginLoader* loader = vtkPVPluginLoader::New();
loader->LoadPlugin(plugin_filename.c_str());
loader->Delete();
}
(*this->PluginsList)[index].AutoLoad = (auto_load != 0);
}
}
}
//----------------------------------------------------------------------------
unsigned int vtkPVPluginTracker::GetNumberOfPlugins()
{
return static_cast<unsigned int>(this->PluginsList->size());
}
//----------------------------------------------------------------------------
unsigned int vtkPVPluginTracker::RegisterAvailablePlugin(const char* filename)
{
std::string defaultname = vtkGetPluginNameFromFileName(filename);
vtkPluginsList::iterator iter =
this->PluginsList->LocateUsingFileName(filename);
if (iter == this->PluginsList->end())
{
iter = this->PluginsList->LocateUsingPluginName(defaultname.c_str());
}
if (iter == this->PluginsList->end())
{
vtkItem item;
item.FileName = filename;
item.PluginName = defaultname;
this->PluginsList->push_back(item);
return static_cast<unsigned int>(this->PluginsList->size()-1);
}
else
{
// don't update the filename here. This avoids cloberring of paths for
// distributed plugins between servers that are named the same (as far as
// the client goes).
// iter->FileName = filename;
return static_cast<unsigned int>(iter - this->PluginsList->begin());
}
}
//----------------------------------------------------------------------------
void vtkPVPluginTracker::RegisterPlugin(vtkPVPlugin* plugin)
{
assert(plugin != NULL);
vtkPluginsList::iterator iter = this->PluginsList->LocateUsingPluginName(
plugin->GetPluginName());
// use filename for matching is present, that's a better test.
if (plugin->GetFileName())
{
iter = this->PluginsList->LocateUsingFileName(plugin->GetFileName());
}
if (iter == this->PluginsList->end())
{
vtkItem item;
item.FileName = plugin->GetFileName()? plugin->GetFileName() :
"linked-in";
item.PluginName = plugin->GetPluginName();
item.Plugin = plugin;
this->PluginsList->push_back(item);
}
else
{
iter->Plugin = plugin;
if (plugin->GetFileName())
{
iter->FileName = plugin->GetFileName();
}
}
// 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)
{
std::vector<std::string> modules, sources;
std::vector<int> package_flags;
pythonplugin->GetPythonSourceList(modules, sources, package_flags);
assert(modules.size() == sources.size() &&
sources.size() == package_flags.size());
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);
return false;
}
return (*this->PluginsList)[index].Plugin != NULL;
}
//----------------------------------------------------------------------------
bool vtkPVPluginTracker::GetPluginAutoLoad(unsigned int index)
{
if (index >= this->GetNumberOfPlugins())
{
vtkWarningMacro("Invalid index: " << index);
return false;
}
return (*this->PluginsList)[index].AutoLoad;
}
//-----------------------------------------------------------------------------
void vtkPVPluginTracker::SetStaticPluginSearchFunction(vtkPluginSearchFunction function)
{
if (!StaticPluginSearchFunction)
{
StaticPluginSearchFunction = function;
}
}
|