ctkPluginFrameworkLauncher.cpp 25 KB

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