ctkPluginFrameworkLauncher.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. , 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. void resolvePlugin(const QSharedPointer<ctkPlugin>& plugin)
  236. {
  237. if (plugin)
  238. {
  239. plugin->d_func()->getUpdatedState();
  240. }
  241. }
  242. /*
  243. * Ensure all basic plugins are installed, resolved and scheduled to start. Returns a list containing
  244. * all basic bundles that are marked to start.
  245. */
  246. //----------------------------------------------------------------------------
  247. void loadBasicPlugins()
  248. {
  249. QVariant pluginsProp = ctkPluginFrameworkProperties::getProperty(ctkPluginFrameworkLauncher::PROP_PLUGINS);
  250. QVariant startOptionsProp = ctkPluginFrameworkProperties::getProperty(ctkPluginFrameworkLauncher::PROP_PLUGINS_START_OPTIONS);
  251. ctkPlugin::StartOptions startOptions = ctkPlugin::START_ACTIVATION_POLICY;
  252. if (startOptionsProp.isValid())
  253. {
  254. bool okay = false;
  255. int startOptionsInt = startOptionsProp.toInt(&okay);
  256. if (okay)
  257. {
  258. startOptions = ctkPlugin::StartOptions(startOptionsInt);
  259. }
  260. }
  261. QStringList installEntries;
  262. if (pluginsProp.type() == QVariant::StringList)
  263. {
  264. installEntries = pluginsProp.toStringList();
  265. }
  266. else
  267. {
  268. installEntries = pluginsProp.toString().split(',');
  269. }
  270. QList<QSharedPointer<ctkPlugin> > startEntries;
  271. ctkPluginContext* context = fwFactory->getFramework()->getPluginContext();
  272. foreach(const QString& installEntry, installEntries)
  273. {
  274. QUrl pluginUrl(installEntry);
  275. if (pluginUrl.isValid() && pluginUrl.scheme().isEmpty())
  276. {
  277. // try a local file path
  278. QFileInfo installFileInfo(installEntry);
  279. if (installFileInfo.exists())
  280. {
  281. pluginUrl = QUrl::fromLocalFile(installFileInfo.absoluteFilePath());
  282. }
  283. else
  284. {
  285. pluginUrl.clear();
  286. }
  287. }
  288. if (pluginUrl.isValid())
  289. {
  290. QSharedPointer<ctkPlugin> plugin = install(pluginUrl, context);
  291. if (plugin)
  292. {
  293. startEntries.push_back(plugin);
  294. }
  295. }
  296. else
  297. {
  298. QSharedPointer<ctkPlugin> plugin = install(installEntry, context);
  299. if (plugin)
  300. {
  301. // schedule all basic bundles to be started
  302. startEntries.push_back(plugin);
  303. }
  304. }
  305. }
  306. foreach(QSharedPointer<ctkPlugin> plugin, startEntries)
  307. {
  308. this->resolvePlugin(plugin);
  309. }
  310. foreach(QSharedPointer<ctkPlugin> plugin, startEntries)
  311. {
  312. plugin->start(startOptions);
  313. }
  314. }
  315. QStringList pluginSearchPaths;
  316. QStringList pluginLibFilter;
  317. ctkProperties fwProps;
  318. QScopedPointer<ctkPluginFrameworkFactory> fwFactory;
  319. bool running;
  320. QScopedPointer<ctkDefaultApplicationLauncher> appLauncher;
  321. ctkServiceRegistration appLauncherRegistration;
  322. QProcessEnvironment processEnv;
  323. };
  324. const QScopedPointer<ctkPluginFrameworkLauncherPrivate> ctkPluginFrameworkLauncher::d(
  325. new ctkPluginFrameworkLauncherPrivate());
  326. //----------------------------------------------------------------------------
  327. void ctkPluginFrameworkLauncher::setFrameworkProperties(const ctkProperties& props)
  328. {
  329. ctkPluginFrameworkProperties::setProperties(props);
  330. }
  331. //----------------------------------------------------------------------------
  332. QVariant ctkPluginFrameworkLauncher::run(QRunnable* endSplashHandler, const QVariant& argument)
  333. {
  334. if (d->running)
  335. {
  336. throw ctkIllegalStateException("Framework already running");
  337. }
  338. {
  339. struct Finalize {
  340. ~Finalize()
  341. {
  342. try
  343. {
  344. // The application typically sets the exit code however the framework can request that
  345. // it be re-started. We need to check for this and potentially override the exit code.
  346. if (d->isForcedRestart())
  347. {
  348. ctkPluginFrameworkProperties::setProperty(PROP_EXITCODE, "23");
  349. }
  350. if (!ctkPluginFrameworkProperties::getProperty(PROP_NOSHUTDOWN).toBool())
  351. {
  352. shutdown();
  353. }
  354. }
  355. catch (const std::exception& e)
  356. {
  357. qWarning() << "Shutdown error:" << e.what();
  358. }
  359. }
  360. };
  361. Finalize finalizer;
  362. Q_UNUSED(finalizer)
  363. try
  364. {
  365. startup(endSplashHandler);
  366. if (ctkPluginFrameworkProperties::getProperty(PROP_IGNOREAPP).toBool() || d->isForcedRestart())
  367. {
  368. return argument;
  369. }
  370. return run(argument);
  371. }
  372. catch (const std::exception& e)
  373. {
  374. // ensure the splash screen is down
  375. if (endSplashHandler != NULL)
  376. {
  377. endSplashHandler->run();
  378. }
  379. // may use startupFailed to understand where the error happened
  380. if (const ctkException* ce = dynamic_cast<const ctkException*>(&e))
  381. {
  382. qWarning() << "Startup error:" << ce->printStackTrace();
  383. }
  384. else
  385. {
  386. qWarning() << "Startup error:" << e.what();
  387. }
  388. }
  389. }
  390. // we only get here if an error happened
  391. if (ctkPluginFrameworkProperties::getProperty(PROP_EXITCODE).isNull())
  392. {
  393. ctkPluginFrameworkProperties::setProperty(PROP_EXITCODE, "13");
  394. ctkPluginFrameworkProperties::setProperty(PROP_EXITDATA, QString("An error has occured. See the console output and log file for details."));
  395. }
  396. return QVariant();
  397. }
  398. //----------------------------------------------------------------------------
  399. QVariant ctkPluginFrameworkLauncher::run(const QVariant& argument)
  400. {
  401. if (!d->running)
  402. {
  403. throw ctkIllegalStateException("Framework not running.");
  404. }
  405. // if we are just initializing, do not run the application just return.
  406. /*
  407. if (d->initialize)
  408. {
  409. return 0;
  410. }
  411. */
  412. try
  413. {
  414. if (!d->appLauncher)
  415. {
  416. bool launchDefault = ctkPluginFrameworkProperties::getProperty(PROP_APPLICATION_LAUNCHDEFAULT, true).toBool();
  417. // create the ApplicationLauncher and register it as a service
  418. d->appLauncher.reset(new ctkDefaultApplicationLauncher(d->fwFactory->getFramework()->getPluginContext(),
  419. ctkPluginFrameworkProperties::getProperty(PROP_ALLOW_APPRELAUNCH).toBool(),
  420. launchDefault));
  421. d->appLauncherRegistration = d->fwFactory->getFramework()->getPluginContext()->
  422. registerService<ctkApplicationLauncher>(d->appLauncher.data());
  423. // must start the launcher AFTER service registration because this method
  424. // blocks and runs the application on the current thread. This method
  425. // will return only after the application has stopped.
  426. return d->appLauncher->start(argument);
  427. }
  428. return d->appLauncher->reStart(argument);
  429. }
  430. catch (const ctkException& e)
  431. {
  432. qWarning() << "Application launch failed:" << e.printStackTrace();
  433. throw;
  434. }
  435. catch (const std::exception& e)
  436. {
  437. qWarning() << "Application launch failed:" << e.what();
  438. throw;
  439. }
  440. }
  441. //----------------------------------------------------------------------------
  442. ctkPluginContext* ctkPluginFrameworkLauncher::startup(QRunnable* endSplashHandler)
  443. {
  444. if (d->running)
  445. {
  446. throw ctkIllegalStateException("Framework already running.");
  447. }
  448. ctkPluginFrameworkProperties::initializeProperties();
  449. //processCommandLine(args);
  450. ctkLocationManager::initializeLocations();
  451. d->loadConfigurationInfo();
  452. //finalizeProperties();
  453. d->fwFactory.reset(new ctkPluginFrameworkFactory(ctkPluginFrameworkProperties::getProperties()));
  454. //d->context = framework.getBundle(0).getBundleContext();
  455. //registerFrameworkShutdownHandlers();
  456. //publishSplashScreen(endSplashHandler);
  457. //consoleMgr = ConsoleManager.startConsole(framework);
  458. d->fwFactory->getFramework()->start();
  459. d->loadBasicPlugins();
  460. d->running = true;
  461. if (endSplashHandler != NULL)
  462. {
  463. endSplashHandler->run();
  464. }
  465. return d->fwFactory->getFramework()->getPluginContext();
  466. }
  467. void ctkPluginFrameworkLauncher::shutdown()
  468. {
  469. if (!d->running || d->fwFactory == NULL)
  470. return;
  471. //if (appLauncherRegistration != null)
  472. // appLauncherRegistration.unregister();
  473. //if (splashStreamRegistration != null)
  474. // splashStreamRegistration.unregister();
  475. //if (defaultMonitorRegistration != null)
  476. // defaultMonitorRegistration.unregister();
  477. if (d->appLauncher)
  478. {
  479. d->appLauncher->shutdown();
  480. }
  481. //appLauncherRegistration = null;
  482. //appLauncher = null;
  483. //splashStreamRegistration = null;
  484. //defaultMonitorRegistration = null;
  485. //d->fwFactory.reset();
  486. stop();
  487. d->running = false;
  488. }
  489. //----------------------------------------------------------------------------
  490. long ctkPluginFrameworkLauncher::install(const QString& symbolicName, ctkPluginContext* context)
  491. {
  492. QSharedPointer<ctkPlugin> plugin = d->install(symbolicName, context);
  493. if (plugin) return plugin->getPluginId();
  494. return -1;
  495. }
  496. //----------------------------------------------------------------------------
  497. bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::StartOptions options,
  498. ctkPluginContext* context)
  499. {
  500. // instantiate and start the framework
  501. if (context == 0 && d->fwFactory == 0) {
  502. d->fwFactory.reset(new ctkPluginFrameworkFactory(d->fwProps));
  503. try
  504. {
  505. d->fwFactory->getFramework()->start();
  506. }
  507. catch (const ctkPluginException& exc)
  508. {
  509. qCritical() << "Failed to start the plug-in framework:" << exc;
  510. d->fwFactory.reset();
  511. return false;
  512. }
  513. }
  514. else if (context == 0 && d->fwFactory->getFramework()->getState() != ctkPlugin::ACTIVE)
  515. {
  516. try
  517. {
  518. d->fwFactory->getFramework()->start(options);
  519. }
  520. catch (const ctkPluginException& exc)
  521. {
  522. qCritical() << "Failed to start the plug-in framework:" << exc;
  523. d->fwFactory.reset();
  524. return false;
  525. }
  526. }
  527. if(!symbolicName.isEmpty())
  528. {
  529. QString pluginPath = getPluginPath(symbolicName);
  530. if (pluginPath.isEmpty()) return false;
  531. ctkPluginContext* pc = context ? context : getPluginContext();
  532. try
  533. {
  534. pc->installPlugin(QUrl::fromLocalFile(pluginPath))->start(options);
  535. }
  536. catch (const ctkPluginException& exc)
  537. {
  538. qWarning() << "Failed to install plugin:" << exc;
  539. return false;
  540. }
  541. }
  542. return true;
  543. }
  544. //----------------------------------------------------------------------------
  545. bool ctkPluginFrameworkLauncher::stop(const QString& symbolicName,
  546. ctkPlugin::StopOptions options, ctkPluginContext* context)
  547. {
  548. if (d->fwFactory == 0) return true;
  549. ctkPluginContext* pc = context ? context : getPluginContext();
  550. if (pc == 0)
  551. {
  552. qWarning() << "No valid plug-in context available";
  553. return false;
  554. }
  555. if(!symbolicName.isEmpty())
  556. {
  557. QString pluginPath = getPluginPath(symbolicName);
  558. if (pluginPath.isEmpty()) return false;
  559. try
  560. {
  561. QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins();
  562. foreach(QSharedPointer<ctkPlugin> plugin, plugins)
  563. {
  564. if (plugin->getSymbolicName() == symbolicName)
  565. {
  566. plugin->stop(options);
  567. return true;
  568. }
  569. }
  570. qWarning() << "Plug-in" << symbolicName << "not found";
  571. return false;
  572. }
  573. catch (const ctkPluginException& exc)
  574. {
  575. qWarning() << "Failed to stop plug-in:" << exc;
  576. return false;
  577. }
  578. }
  579. else
  580. {
  581. // stop the framework
  582. QSharedPointer<ctkPluginFramework> fw =
  583. qSharedPointerCast<ctkPluginFramework>(pc->getPlugin(0));
  584. try
  585. {
  586. fw->stop();
  587. ctkPluginFrameworkEvent fe = fw->waitForStop(5000);
  588. if (fe.getType() == ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT)
  589. {
  590. qWarning() << "Stopping the plugin framework timed out";
  591. return false;
  592. }
  593. }
  594. catch (const ctkRuntimeException& e)
  595. {
  596. qWarning() << "Stopping the plugin framework failed: " << e;
  597. return false;
  598. }
  599. return true;
  600. }
  601. }
  602. //----------------------------------------------------------------------------
  603. void ctkPluginFrameworkLauncher::resolve(const QSharedPointer<ctkPlugin>& plugin)
  604. {
  605. d->resolvePlugin(plugin);
  606. }
  607. //----------------------------------------------------------------------------
  608. void ctkPluginFrameworkLauncher::resolve()
  609. {
  610. QList<QSharedPointer<ctkPlugin> > plugins = getPluginFramework()->getPluginContext()->getPlugins();
  611. foreach(const QSharedPointer<ctkPlugin>& plugin, plugins)
  612. {
  613. resolve(plugin);
  614. }
  615. }
  616. //----------------------------------------------------------------------------
  617. ctkPluginContext* ctkPluginFrameworkLauncher::getPluginContext()
  618. {
  619. if (d->fwFactory == 0) return 0;
  620. return d->fwFactory->getFramework()->getPluginContext();
  621. }
  622. //----------------------------------------------------------------------------
  623. QSharedPointer<ctkPluginFramework> ctkPluginFrameworkLauncher::getPluginFramework()
  624. {
  625. if (d->fwFactory)
  626. return d->fwFactory->getFramework();
  627. return QSharedPointer<ctkPluginFramework>();
  628. }
  629. //----------------------------------------------------------------------------
  630. void ctkPluginFrameworkLauncher::appendPathEnv(const QString& path)
  631. {
  632. #ifdef _WIN32
  633. #ifdef __MINGW32__
  634. QString pathVar("PATH");
  635. QString oldPath(getenv("PATH"));
  636. pathVar += "=" + oldPath + ";" + path;
  637. if(_putenv(qPrintable(pathVar)))
  638. #else
  639. std::size_t bufferLength;
  640. getenv_s(&bufferLength, NULL, 0, "PATH");
  641. QString newPath = path;
  642. if (bufferLength > 0)
  643. {
  644. char* oldPath = new char[bufferLength];
  645. getenv_s(&bufferLength, oldPath, bufferLength, "PATH");
  646. newPath.append(";").append(oldPath);
  647. delete[] oldPath;
  648. }
  649. qDebug() << "new PATH:" << newPath;
  650. if(_putenv_s("PATH", qPrintable(newPath)))
  651. #endif
  652. {
  653. LPVOID lpMsgBuf;
  654. DWORD dw = GetLastError();
  655. FormatMessageW(
  656. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  657. FORMAT_MESSAGE_FROM_SYSTEM |
  658. FORMAT_MESSAGE_IGNORE_INSERTS,
  659. NULL,
  660. dw,
  661. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  662. (LPWSTR) &lpMsgBuf,
  663. 0, NULL );
  664. // Avoid project configuration conflicts regarding wchar_t considered
  665. // a built-in type or not by using QString::fromUtf16 instead of
  666. // QString::fromWCharArray
  667. // sa http://qt-project.org/wiki/toStdWStringAndBuiltInWchar
  668. QString msg = QString("Adding '%1' to the PATH environment variable failed: %2")
  669. .arg(path).arg(QString::fromUtf16(reinterpret_cast<const ushort*>(lpMsgBuf)));
  670. qWarning() << msg;
  671. LocalFree(lpMsgBuf);
  672. }
  673. #else
  674. Q_UNUSED(path)
  675. #endif
  676. }
  677. //----------------------------------------------------------------------------
  678. void ctkPluginFrameworkLauncher::addSearchPath(const QString& searchPath, bool addToPathEnv)
  679. {
  680. d->pluginSearchPaths.prepend(searchPath);
  681. if (addToPathEnv) appendPathEnv(searchPath);
  682. }
  683. //----------------------------------------------------------------------------
  684. QString ctkPluginFrameworkLauncher::getPluginPath(const QString& symbolicName)
  685. {
  686. QString pluginFileName(symbolicName);
  687. pluginFileName.replace(".", "_");
  688. foreach(QString searchPath, d->pluginSearchPaths)
  689. {
  690. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  691. while(dirIter.hasNext())
  692. {
  693. dirIter.next();
  694. QFileInfo fileInfo = dirIter.fileInfo();
  695. QString fileBaseName = fileInfo.baseName();
  696. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  697. if (fileBaseName == pluginFileName)
  698. {
  699. return fileInfo.canonicalFilePath();
  700. }
  701. }
  702. }
  703. return QString();
  704. }
  705. //----------------------------------------------------------------------------
  706. QStringList ctkPluginFrameworkLauncher::getPluginSymbolicNames(const QString& searchPath)
  707. {
  708. QStringList result;
  709. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  710. while(dirIter.hasNext())
  711. {
  712. dirIter.next();
  713. QFileInfo fileInfo = dirIter.fileInfo();
  714. QString fileBaseName = fileInfo.baseName();
  715. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  716. result << fileBaseName.replace("_", ".");
  717. }
  718. return result;
  719. }