ctkPluginFrameworkLauncher.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginFrameworkLauncher.h"
  16. #include "ctkPluginFrameworkFactory.h"
  17. #include "ctkPluginFrameworkProperties_p.h"
  18. #include "ctkPluginFramework.h"
  19. #include "ctkPluginContext.h"
  20. #include "ctkPluginException.h"
  21. #include "ctkDefaultApplicationLauncher_p.h"
  22. #include "ctkLocationManager_p.h"
  23. #include "ctkBasicLocation_p.h"
  24. #include <ctkConfig.h>
  25. #include <QStringList>
  26. #include <QDirIterator>
  27. #include <QFileInfo>
  28. #include <QDebug>
  29. #include <QRunnable>
  30. #include <QSettings>
  31. #include <QProcessEnvironment>
  32. #ifdef _WIN32
  33. #include <windows.h>
  34. #include <cstdlib>
  35. #endif // _WIN32
  36. const QString ctkPluginFrameworkLauncher::PROP_USER_HOME = "user.home";
  37. const QString ctkPluginFrameworkLauncher::PROP_USER_DIR = "user.dir";
  38. // Framework properties
  39. const QString ctkPluginFrameworkLauncher::PROP_PLUGINS = "ctk.plugins";
  40. const QString ctkPluginFrameworkLauncher::PROP_PLUGINS_START_OPTIONS = "ctk.plugins.startOptions";
  41. const QString ctkPluginFrameworkLauncher::PROP_DEBUG = "ctk.debug";
  42. const QString ctkPluginFrameworkLauncher::PROP_DEV = "ctk.dev";
  43. const QString ctkPluginFrameworkLauncher::PROP_CONSOLE = "ctk.console";
  44. const QString ctkPluginFrameworkLauncher::PROP_OS = "ctk.os";
  45. const QString ctkPluginFrameworkLauncher::PROP_ARCH = "ctk.arch";
  46. const QString ctkPluginFrameworkLauncher::PROP_NOSHUTDOWN = "ctk.noShutdown";
  47. const QString ctkPluginFrameworkLauncher::PROP_IGNOREAPP = "ctk.ignoreApp";
  48. const QString ctkPluginFrameworkLauncher::PROP_INSTALL_AREA = "ctk.install.area";
  49. const QString ctkPluginFrameworkLauncher::PROP_CONFIG_AREA = "ctk.configuration.area";
  50. const QString ctkPluginFrameworkLauncher::PROP_SHARED_CONFIG_AREA = "ctk.sharedConfiguration.area";
  51. const QString ctkPluginFrameworkLauncher::PROP_INSTANCE_AREA = "ctk.instance.area";
  52. const QString ctkPluginFrameworkLauncher::PROP_USER_AREA = "ctk.user.area";
  53. const QString ctkPluginFrameworkLauncher::PROP_HOME_LOCATION_AREA = "ctk.home.location";
  54. const QString ctkPluginFrameworkLauncher::PROP_CONFIG_AREA_DEFAULT = "ctk.configuration.area.default";
  55. const QString ctkPluginFrameworkLauncher::PROP_INSTANCE_AREA_DEFAULT = "ctk.instance.area.default";
  56. const QString ctkPluginFrameworkLauncher::PROP_USER_AREA_DEFAULT = "ctk.user.area.default";
  57. const QString ctkPluginFrameworkLauncher::PROP_EXITCODE = "ctk.exitcode";
  58. const QString ctkPluginFrameworkLauncher::PROP_EXITDATA = "ctk.exitdata";
  59. const QString ctkPluginFrameworkLauncher::PROP_CONSOLE_LOG = "ctk.consoleLog";
  60. const QString ctkPluginFrameworkLauncher::PROP_ALLOW_APPRELAUNCH = "ctk.allowAppRelaunch";
  61. const QString ctkPluginFrameworkLauncher::PROP_APPLICATION_LAUNCHDEFAULT = "ctk.application.launchDefault";
  62. const QString ctkPluginFrameworkLauncher::PROP_OSGI_RELAUNCH = "ctk.pluginfw.relaunch";
  63. static const QString PROP_FORCED_RESTART = "ctk.forcedRestart";
  64. class ctkPluginFrameworkLauncherPrivate
  65. {
  66. public:
  67. //----------------------------------------------------------------------------
  68. ctkPluginFrameworkLauncherPrivate()
  69. : fwFactory(0)
  70. , running(false)
  71. , endSplashHandler(NULL)
  72. , processEnv(QProcessEnvironment::systemEnvironment())
  73. {
  74. #ifdef CMAKE_INTDIR
  75. QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
  76. #else
  77. QString pluginPath = CTK_PLUGIN_DIR;
  78. #endif
  79. pluginSearchPaths.append(pluginPath);
  80. pluginLibFilter << "*.dll" << "*.so" << "*.dylib";
  81. }
  82. //----------------------------------------------------------------------------
  83. bool isForcedRestart() const
  84. {
  85. return ctkPluginFrameworkProperties::getProperty(PROP_FORCED_RESTART).toBool();
  86. }
  87. //----------------------------------------------------------------------------
  88. void loadConfigurationInfo()
  89. {
  90. ctkBasicLocation* configArea = ctkLocationManager::getConfigurationLocation();
  91. if (configArea == NULL)
  92. {
  93. return;
  94. }
  95. QUrl location(configArea->getUrl().toString() + ctkLocationManager::CONFIG_FILE);
  96. mergeProperties(ctkPluginFrameworkProperties::getProperties(), loadProperties(location));
  97. }
  98. //----------------------------------------------------------------------------
  99. void mergeProperties(ctkProperties& destination, const ctkProperties& source)
  100. {
  101. for (ctkProperties::const_iterator iter = source.begin(); iter != source.end(); ++iter)
  102. {
  103. if (!destination.contains(iter.key()))
  104. {
  105. destination.insert(iter.key(), iter.value());
  106. }
  107. }
  108. }
  109. //----------------------------------------------------------------------------
  110. ctkProperties loadProperties(const QUrl& location)
  111. {
  112. ctkProperties result;
  113. if (!location.isValid() || !QFileInfo(location.toLocalFile()).exists())
  114. {
  115. return result;
  116. }
  117. QSettings iniProps(location.toLocalFile(), QSettings::IniFormat);
  118. foreach (const QString& key, iniProps.allKeys())
  119. {
  120. result.insert(key, iniProps.value(key));
  121. }
  122. return substituteVars(result);
  123. }
  124. //----------------------------------------------------------------------------
  125. ctkProperties& substituteVars(ctkProperties& result)
  126. {
  127. for (ctkProperties::iterator iter = result.begin(); iter != result.end(); ++iter)
  128. {
  129. if (iter.value().type() == QVariant::String)
  130. {
  131. iter.value() = substituteVars(iter.value().toString());
  132. }
  133. }
  134. return result;
  135. }
  136. //----------------------------------------------------------------------------
  137. QString substituteVars(const QString& path)
  138. {
  139. QString buf;
  140. bool varStarted = false; // indicates we are processing a var subtitute
  141. QString var; // the current var key
  142. for (QString::const_iterator iter = path.begin(); iter != path.end(); ++iter)
  143. {
  144. QChar tok = *iter;
  145. if (tok == '$')
  146. {
  147. if (!varStarted)
  148. {
  149. varStarted = true; // we found the start of a var
  150. var.clear();
  151. }
  152. else
  153. {
  154. // we have found the end of a var
  155. QVariant prop;
  156. // get the value of the var from system properties
  157. if (!var.isEmpty())
  158. {
  159. prop = ctkPluginFrameworkProperties::getProperty(var);
  160. }
  161. if (prop.isNull() && processEnv.contains(var))
  162. {
  163. prop = processEnv.value(var);
  164. }
  165. if (!prop.isNull())
  166. {
  167. // found a value; use it
  168. buf.append(prop.toString());
  169. }
  170. else
  171. {
  172. // could not find a value append the var name w/o delims
  173. buf.append(var);
  174. }
  175. varStarted = false;
  176. var.clear();
  177. }
  178. }
  179. else
  180. {
  181. if (!varStarted)
  182. {
  183. buf.append(tok); // the token is not part of a var
  184. }
  185. else
  186. {
  187. var.append(tok); // the token is part of the var key; save the key to process when we find the end token
  188. }
  189. }
  190. }
  191. if (!var.isEmpty())
  192. {
  193. // found a case of $var at the end of the path with no trailing $; just append it as is.
  194. buf.append('$').append(var);
  195. }
  196. return buf;
  197. }
  198. //----------------------------------------------------------------------------
  199. QSharedPointer<ctkPlugin> install(const QUrl& pluginPath, ctkPluginContext* context)
  200. {
  201. try
  202. {
  203. return context->installPlugin(pluginPath);
  204. }
  205. catch (const ctkPluginException& exc)
  206. {
  207. qWarning() << "Failed to install plugin " << pluginPath << ":" << exc.printStackTrace();
  208. return QSharedPointer<ctkPlugin>();
  209. }
  210. }
  211. //----------------------------------------------------------------------------
  212. QSharedPointer<ctkPlugin> install(const QString& symbolicName, ctkPluginContext* context)
  213. {
  214. QString pluginPath = ctkPluginFrameworkLauncher::getPluginPath(symbolicName);
  215. if (pluginPath.isEmpty()) return QSharedPointer<ctkPlugin>();
  216. ctkPluginContext* pc = context;
  217. if (pc == 0 && fwFactory == 0)
  218. {
  219. fwFactory.reset(new ctkPluginFrameworkFactory(fwProps));
  220. try
  221. {
  222. fwFactory->getFramework()->init();
  223. pc = fwFactory->getFramework()->getPluginContext();
  224. }
  225. catch (const ctkPluginException& exc)
  226. {
  227. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  228. fwFactory.reset();
  229. return QSharedPointer<ctkPlugin>();
  230. }
  231. }
  232. return install(QUrl::fromLocalFile(pluginPath), pc);
  233. }
  234. /*
  235. * Ensure all basic plugins are installed, resolved and scheduled to start. Returns a list containing
  236. * all basic bundles that are marked to start.
  237. */
  238. //----------------------------------------------------------------------------
  239. void loadBasicPlugins()
  240. {
  241. QVariant pluginsProp = ctkPluginFrameworkProperties::getProperty(ctkPluginFrameworkLauncher::PROP_PLUGINS);
  242. QVariant startOptionsProp = ctkPluginFrameworkProperties::getProperty(ctkPluginFrameworkLauncher::PROP_PLUGINS_START_OPTIONS);
  243. ctkPlugin::StartOptions startOptions = ctkPlugin::START_ACTIVATION_POLICY;
  244. if (startOptionsProp.isValid())
  245. {
  246. bool okay = false;
  247. int startOptionsInt = startOptionsProp.toInt(&okay);
  248. if (okay)
  249. {
  250. startOptions = ctkPlugin::StartOptions(startOptionsInt);
  251. }
  252. }
  253. QStringList installEntries;
  254. if (pluginsProp.type() == QVariant::StringList)
  255. {
  256. installEntries = pluginsProp.toStringList();
  257. }
  258. else
  259. {
  260. installEntries = pluginsProp.toString().split(',');
  261. }
  262. QList<QSharedPointer<ctkPlugin> > startEntries;
  263. ctkPluginContext* context = fwFactory->getFramework()->getPluginContext();
  264. foreach(const QString& installEntry, installEntries)
  265. {
  266. QUrl pluginUrl(installEntry);
  267. if (pluginUrl.isValid() && pluginUrl.scheme().isEmpty())
  268. {
  269. // try a local file path
  270. QFileInfo installFileInfo(installEntry);
  271. if (installFileInfo.exists())
  272. {
  273. pluginUrl = QUrl::fromLocalFile(installFileInfo.absoluteFilePath());
  274. }
  275. else
  276. {
  277. pluginUrl.clear();
  278. }
  279. }
  280. if (pluginUrl.isValid())
  281. {
  282. QSharedPointer<ctkPlugin> plugin = install(pluginUrl, context);
  283. if (plugin)
  284. {
  285. startEntries.push_back(plugin);
  286. }
  287. }
  288. else
  289. {
  290. QSharedPointer<ctkPlugin> plugin = install(installEntry, context);
  291. if (plugin)
  292. {
  293. // schedule all basic bundles to be started
  294. startEntries.push_back(plugin);
  295. }
  296. }
  297. }
  298. foreach(QSharedPointer<ctkPlugin> plugin, startEntries)
  299. {
  300. plugin->start(startOptions);
  301. }
  302. }
  303. QStringList pluginSearchPaths;
  304. QStringList pluginLibFilter;
  305. ctkProperties fwProps;
  306. QScopedPointer<ctkPluginFrameworkFactory> fwFactory;
  307. bool running;
  308. QRunnable* endSplashHandler;
  309. QScopedPointer<ctkDefaultApplicationLauncher> appLauncher;
  310. ctkServiceRegistration appLauncherRegistration;
  311. QProcessEnvironment processEnv;
  312. };
  313. const QScopedPointer<ctkPluginFrameworkLauncherPrivate> ctkPluginFrameworkLauncher::d(
  314. new ctkPluginFrameworkLauncherPrivate());
  315. //----------------------------------------------------------------------------
  316. void ctkPluginFrameworkLauncher::setFrameworkProperties(const ctkProperties& props)
  317. {
  318. ctkPluginFrameworkProperties::setProperties(props);
  319. }
  320. //----------------------------------------------------------------------------
  321. QVariant ctkPluginFrameworkLauncher::run(QRunnable* endSplashHandler)
  322. {
  323. if (d->running)
  324. {
  325. throw ctkIllegalStateException("Framework already running");
  326. }
  327. {
  328. struct Finalize {
  329. ~Finalize()
  330. {
  331. try
  332. {
  333. // The application typically sets the exit code however the framework can request that
  334. // it be re-started. We need to check for this and potentially override the exit code.
  335. if (d->isForcedRestart())
  336. {
  337. ctkPluginFrameworkProperties::setProperty(PROP_EXITCODE, "23");
  338. }
  339. if (!ctkPluginFrameworkProperties::getProperty(PROP_NOSHUTDOWN).toBool())
  340. {
  341. shutdown();
  342. }
  343. }
  344. catch (const std::exception& e)
  345. {
  346. qWarning() << "Shutdown error:" << e.what();
  347. }
  348. }
  349. };
  350. Finalize finalizer;
  351. Q_UNUSED(finalizer)
  352. try
  353. {
  354. startup(d->endSplashHandler);
  355. if (ctkPluginFrameworkProperties::getProperty(PROP_IGNOREAPP).toBool() || d->isForcedRestart())
  356. {
  357. return QVariant();
  358. }
  359. return run(QVariant());
  360. }
  361. catch (const std::exception& e)
  362. {
  363. // ensure the splash screen is down
  364. if (endSplashHandler != NULL)
  365. {
  366. endSplashHandler->run();
  367. }
  368. // may use startupFailed to understand where the error happened
  369. if (const ctkException* ce = dynamic_cast<const ctkException*>(&e))
  370. {
  371. qWarning() << "Startup error:" << ce->printStackTrace();
  372. }
  373. else
  374. {
  375. qWarning() << "Startup error:" << e.what();
  376. }
  377. }
  378. }
  379. // we only get here if an error happened
  380. if (ctkPluginFrameworkProperties::getProperty(PROP_EXITCODE).isNull())
  381. {
  382. ctkPluginFrameworkProperties::setProperty(PROP_EXITCODE, "13");
  383. ctkPluginFrameworkProperties::setProperty(PROP_EXITDATA, QString("An error has occured. See the console output and log file for details."));
  384. }
  385. return QVariant();
  386. }
  387. //----------------------------------------------------------------------------
  388. QVariant ctkPluginFrameworkLauncher::run(const QVariant& argument)
  389. {
  390. if (!d->running)
  391. {
  392. throw ctkIllegalStateException("Framework not running.");
  393. }
  394. // if we are just initializing, do not run the application just return.
  395. /*
  396. if (d->initialize)
  397. {
  398. return 0;
  399. }
  400. */
  401. try
  402. {
  403. if (!d->appLauncher)
  404. {
  405. bool launchDefault = ctkPluginFrameworkProperties::getProperty(PROP_APPLICATION_LAUNCHDEFAULT, true).toBool();
  406. // create the ApplicationLauncher and register it as a service
  407. d->appLauncher.reset(new ctkDefaultApplicationLauncher(d->fwFactory->getFramework()->getPluginContext(),
  408. ctkPluginFrameworkProperties::getProperty(PROP_ALLOW_APPRELAUNCH).toBool(),
  409. launchDefault));
  410. d->appLauncherRegistration = d->fwFactory->getFramework()->getPluginContext()->
  411. registerService<ctkApplicationLauncher>(d->appLauncher.data());
  412. // must start the launcher AFTER service registration because this method
  413. // blocks and runs the application on the current thread. This method
  414. // will return only after the application has stopped.
  415. return d->appLauncher->start(argument);
  416. }
  417. return d->appLauncher->reStart(argument);
  418. }
  419. catch (const ctkException& e)
  420. {
  421. qWarning() << "Application launch failed:" << e.printStackTrace();
  422. throw;
  423. }
  424. catch (const std::exception& e)
  425. {
  426. qWarning() << "Application launch failed:" << e.what();
  427. throw;
  428. }
  429. }
  430. //----------------------------------------------------------------------------
  431. ctkPluginContext* ctkPluginFrameworkLauncher::startup(QRunnable* /*endSplashHandler*/)
  432. {
  433. if (d->running)
  434. {
  435. throw ctkIllegalStateException("Framework already running.");
  436. }
  437. ctkPluginFrameworkProperties::initializeProperties();
  438. //processCommandLine(args);
  439. ctkLocationManager::initializeLocations();
  440. d->loadConfigurationInfo();
  441. //finalizeProperties();
  442. d->fwFactory.reset(new ctkPluginFrameworkFactory(ctkPluginFrameworkProperties::getProperties()));
  443. //d->context = framework.getBundle(0).getBundleContext();
  444. //registerFrameworkShutdownHandlers();
  445. //publishSplashScreen(endSplashHandler);
  446. //consoleMgr = ConsoleManager.startConsole(framework);
  447. d->fwFactory->getFramework()->start();
  448. d->loadBasicPlugins();
  449. d->running = true;
  450. return d->fwFactory->getFramework()->getPluginContext();
  451. }
  452. void ctkPluginFrameworkLauncher::shutdown()
  453. {
  454. if (!d->running || d->fwFactory == NULL)
  455. return;
  456. //if (appLauncherRegistration != null)
  457. // appLauncherRegistration.unregister();
  458. //if (splashStreamRegistration != null)
  459. // splashStreamRegistration.unregister();
  460. //if (defaultMonitorRegistration != null)
  461. // defaultMonitorRegistration.unregister();
  462. if (d->appLauncher)
  463. {
  464. d->appLauncher->shutdown();
  465. }
  466. //appLauncherRegistration = null;
  467. //appLauncher = null;
  468. //splashStreamRegistration = null;
  469. //defaultMonitorRegistration = null;
  470. //d->fwFactory.reset();
  471. stop();
  472. d->running = false;
  473. }
  474. //----------------------------------------------------------------------------
  475. long ctkPluginFrameworkLauncher::install(const QString& symbolicName, ctkPluginContext* context)
  476. {
  477. QSharedPointer<ctkPlugin> plugin = d->install(symbolicName, context);
  478. if (plugin) return plugin->getPluginId();
  479. return -1;
  480. }
  481. //----------------------------------------------------------------------------
  482. bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::StartOptions options,
  483. ctkPluginContext* context)
  484. {
  485. // instantiate and start the framework
  486. if (context == 0 && d->fwFactory == 0) {
  487. d->fwFactory.reset(new ctkPluginFrameworkFactory(d->fwProps));
  488. try
  489. {
  490. d->fwFactory->getFramework()->start();
  491. }
  492. catch (const ctkPluginException& exc)
  493. {
  494. qCritical() << "Failed to start the plug-in framework:" << exc;
  495. d->fwFactory.reset();
  496. return false;
  497. }
  498. }
  499. else if (context == 0 && d->fwFactory->getFramework()->getState() != ctkPlugin::ACTIVE)
  500. {
  501. try
  502. {
  503. d->fwFactory->getFramework()->start(options);
  504. }
  505. catch (const ctkPluginException& exc)
  506. {
  507. qCritical() << "Failed to start the plug-in framework:" << exc;
  508. d->fwFactory.reset();
  509. return false;
  510. }
  511. }
  512. if(!symbolicName.isEmpty())
  513. {
  514. QString pluginPath = getPluginPath(symbolicName);
  515. if (pluginPath.isEmpty()) return false;
  516. ctkPluginContext* pc = context ? context : getPluginContext();
  517. try
  518. {
  519. pc->installPlugin(QUrl::fromLocalFile(pluginPath))->start(options);
  520. }
  521. catch (const ctkPluginException& exc)
  522. {
  523. qWarning() << "Failed to install plugin:" << exc;
  524. return false;
  525. }
  526. }
  527. return true;
  528. }
  529. //----------------------------------------------------------------------------
  530. bool ctkPluginFrameworkLauncher::stop(const QString& symbolicName,
  531. ctkPlugin::StopOptions options, ctkPluginContext* context)
  532. {
  533. if (d->fwFactory == 0) return true;
  534. ctkPluginContext* pc = context ? context : getPluginContext();
  535. if (pc == 0)
  536. {
  537. qWarning() << "No valid plug-in context available";
  538. return false;
  539. }
  540. if(!symbolicName.isEmpty())
  541. {
  542. QString pluginPath = getPluginPath(symbolicName);
  543. if (pluginPath.isEmpty()) return false;
  544. try
  545. {
  546. QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins();
  547. foreach(QSharedPointer<ctkPlugin> plugin, plugins)
  548. {
  549. if (plugin->getSymbolicName() == symbolicName)
  550. {
  551. plugin->stop(options);
  552. return true;
  553. }
  554. }
  555. qWarning() << "Plug-in" << symbolicName << "not found";
  556. return false;
  557. }
  558. catch (const ctkPluginException& exc)
  559. {
  560. qWarning() << "Failed to stop plug-in:" << exc;
  561. return false;
  562. }
  563. }
  564. else
  565. {
  566. // stop the framework
  567. QSharedPointer<ctkPluginFramework> fw =
  568. qSharedPointerCast<ctkPluginFramework>(pc->getPlugin(0));
  569. try
  570. {
  571. fw->stop();
  572. ctkPluginFrameworkEvent fe = fw->waitForStop(5000);
  573. if (fe.getType() == ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT)
  574. {
  575. qWarning() << "Stopping the plugin framework timed out";
  576. return false;
  577. }
  578. }
  579. catch (const ctkRuntimeException& e)
  580. {
  581. qWarning() << "Stopping the plugin framework failed: " << e;
  582. return false;
  583. }
  584. return true;
  585. }
  586. }
  587. //----------------------------------------------------------------------------
  588. ctkPluginContext* ctkPluginFrameworkLauncher::getPluginContext()
  589. {
  590. if (d->fwFactory == 0) return 0;
  591. return d->fwFactory->getFramework()->getPluginContext();
  592. }
  593. //----------------------------------------------------------------------------
  594. QSharedPointer<ctkPluginFramework> ctkPluginFrameworkLauncher::getPluginFramework()
  595. {
  596. if (d->fwFactory)
  597. return d->fwFactory->getFramework();
  598. return QSharedPointer<ctkPluginFramework>();
  599. }
  600. //----------------------------------------------------------------------------
  601. void ctkPluginFrameworkLauncher::appendPathEnv(const QString& path)
  602. {
  603. #ifdef _WIN32
  604. #ifdef __MINGW32__
  605. QString pathVar("PATH");
  606. QString oldPath(getenv("PATH"));
  607. pathVar += "=" + oldPath + ";" + path;
  608. if(_putenv(qPrintable(pathVar)))
  609. #else
  610. std::size_t bufferLength;
  611. getenv_s(&bufferLength, NULL, 0, "PATH");
  612. QString newPath = path;
  613. if (bufferLength > 0)
  614. {
  615. char* oldPath = new char[bufferLength];
  616. getenv_s(&bufferLength, oldPath, bufferLength, "PATH");
  617. newPath.append(";").append(oldPath);
  618. delete[] oldPath;
  619. }
  620. qDebug() << "new PATH:" << newPath;
  621. if(_putenv_s("PATH", qPrintable(newPath)))
  622. #endif
  623. {
  624. LPVOID lpMsgBuf;
  625. DWORD dw = GetLastError();
  626. FormatMessageW(
  627. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  628. FORMAT_MESSAGE_FROM_SYSTEM |
  629. FORMAT_MESSAGE_IGNORE_INSERTS,
  630. NULL,
  631. dw,
  632. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  633. (LPWSTR) &lpMsgBuf,
  634. 0, NULL );
  635. // Avoid project configuration conflicts regarding wchar_t considered
  636. // a built-in type or not by using QString::fromUtf16 instead of
  637. // QString::fromWCharArray
  638. // sa http://qt-project.org/wiki/toStdWStringAndBuiltInWchar
  639. QString msg = QString("Adding '%1' to the PATH environment variable failed: %2")
  640. .arg(path).arg(QString::fromUtf16(reinterpret_cast<const ushort*>(lpMsgBuf)));
  641. qWarning() << msg;
  642. LocalFree(lpMsgBuf);
  643. }
  644. #else
  645. Q_UNUSED(path)
  646. #endif
  647. }
  648. //----------------------------------------------------------------------------
  649. void ctkPluginFrameworkLauncher::addSearchPath(const QString& searchPath, bool addToPathEnv)
  650. {
  651. d->pluginSearchPaths.prepend(searchPath);
  652. if (addToPathEnv) appendPathEnv(searchPath);
  653. }
  654. //----------------------------------------------------------------------------
  655. QString ctkPluginFrameworkLauncher::getPluginPath(const QString& symbolicName)
  656. {
  657. QString pluginFileName(symbolicName);
  658. pluginFileName.replace(".", "_");
  659. foreach(QString searchPath, d->pluginSearchPaths)
  660. {
  661. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  662. while(dirIter.hasNext())
  663. {
  664. dirIter.next();
  665. QFileInfo fileInfo = dirIter.fileInfo();
  666. QString fileBaseName = fileInfo.baseName();
  667. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  668. if (fileBaseName == pluginFileName)
  669. {
  670. return fileInfo.canonicalFilePath();
  671. }
  672. }
  673. }
  674. return QString();
  675. }
  676. //----------------------------------------------------------------------------
  677. QStringList ctkPluginFrameworkLauncher::getPluginSymbolicNames(const QString& searchPath)
  678. {
  679. QStringList result;
  680. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  681. while(dirIter.hasNext())
  682. {
  683. dirIter.next();
  684. QFileInfo fileInfo = dirIter.fileInfo();
  685. QString fileBaseName = fileInfo.baseName();
  686. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  687. result << fileBaseName.replace("_", ".");
  688. }
  689. return result;
  690. }