ctkPluginFrameworkLauncher.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <QStringList>
  16. #include <QDirIterator>
  17. #include <QFileInfo>
  18. #include <QDebug>
  19. #include "ctkPluginFrameworkLauncher.h"
  20. #include "ctkPluginFrameworkFactory.h"
  21. #include "ctkPluginFramework.h"
  22. #include "ctkPluginContext.h"
  23. #include "ctkPluginException.h"
  24. #ifdef _WIN32
  25. #include <windows.h>
  26. #include <cstdlib>
  27. #endif // _WIN32
  28. #include <ctkConfig.h>
  29. class ctkPluginFrameworkLauncherPrivate
  30. {
  31. public:
  32. ctkPluginFrameworkLauncherPrivate()
  33. : fwFactory(0)
  34. {
  35. #ifdef CMAKE_INTDIR
  36. QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
  37. #else
  38. QString pluginPath = CTK_PLUGIN_DIR;
  39. #endif
  40. pluginSearchPaths.append(pluginPath);
  41. pluginLibFilter << "*.dll" << "*.so" << "*.dylib";
  42. }
  43. QStringList pluginSearchPaths;
  44. QStringList pluginLibFilter;
  45. ctkProperties fwProps;
  46. ctkPluginFrameworkFactory* fwFactory;
  47. };
  48. const QScopedPointer<ctkPluginFrameworkLauncherPrivate> ctkPluginFrameworkLauncher::d(
  49. new ctkPluginFrameworkLauncherPrivate());
  50. //----------------------------------------------------------------------------
  51. void ctkPluginFrameworkLauncher::setFrameworkProperties(const ctkProperties& props)
  52. {
  53. d->fwProps = props;
  54. }
  55. //----------------------------------------------------------------------------
  56. long ctkPluginFrameworkLauncher::install(const QString& symbolicName, ctkPluginContext* context)
  57. {
  58. QString pluginPath = getPluginPath(symbolicName);
  59. if (pluginPath.isEmpty()) return -1;
  60. ctkPluginContext* pc = context;
  61. if (pc == 0 && d->fwFactory == 0) {
  62. d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps);
  63. try
  64. {
  65. d->fwFactory->getFramework()->init();
  66. pc = getPluginContext();
  67. }
  68. catch (const ctkPluginException& exc)
  69. {
  70. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  71. delete d->fwFactory;
  72. d->fwFactory = 0;
  73. return -1;
  74. }
  75. }
  76. try
  77. {
  78. return pc->installPlugin(QUrl::fromLocalFile(pluginPath))->getPluginId();
  79. }
  80. catch (const ctkPluginException& exc)
  81. {
  82. qWarning() << "Failed to install plugin:" << exc;
  83. return -1;
  84. }
  85. }
  86. //----------------------------------------------------------------------------
  87. bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::StartOptions options,
  88. ctkPluginContext* context)
  89. {
  90. // instantiate and start the framework
  91. if (context == 0 && d->fwFactory == 0) {
  92. d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps);
  93. try
  94. {
  95. d->fwFactory->getFramework()->start();
  96. }
  97. catch (const ctkPluginException& exc)
  98. {
  99. qCritical() << "Failed to start the plug-in framework:" << exc;
  100. delete d->fwFactory;
  101. d->fwFactory = 0;
  102. return false;
  103. }
  104. }
  105. else if (context == 0 && d->fwFactory->getFramework()->getState() != ctkPlugin::ACTIVE)
  106. {
  107. try
  108. {
  109. d->fwFactory->getFramework()->start(options);
  110. }
  111. catch (const ctkPluginException& exc)
  112. {
  113. qCritical() << "Failed to start the plug-in framework:" << exc;
  114. delete d->fwFactory;
  115. d->fwFactory = 0;
  116. return false;
  117. }
  118. }
  119. if(!symbolicName.isEmpty())
  120. {
  121. QString pluginPath = getPluginPath(symbolicName);
  122. if (pluginPath.isEmpty()) return false;
  123. ctkPluginContext* pc = context ? context : getPluginContext();
  124. try
  125. {
  126. pc->installPlugin(QUrl::fromLocalFile(pluginPath))->start(options);
  127. }
  128. catch (const ctkPluginException& exc)
  129. {
  130. qWarning() << "Failed to install plugin:" << exc;
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. //----------------------------------------------------------------------------
  137. bool ctkPluginFrameworkLauncher::stop(const QString& symbolicName,
  138. ctkPlugin::StopOptions options, ctkPluginContext* context)
  139. {
  140. if (d->fwFactory == 0) return true;
  141. ctkPluginContext* pc = context ? context : getPluginContext();
  142. if (pc == 0)
  143. {
  144. qWarning() << "No valid plug-in context available";
  145. return false;
  146. }
  147. if(!symbolicName.isEmpty())
  148. {
  149. QString pluginPath = getPluginPath(symbolicName);
  150. if (pluginPath.isEmpty()) return false;
  151. try
  152. {
  153. QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins();
  154. foreach(QSharedPointer<ctkPlugin> plugin, plugins)
  155. {
  156. if (plugin->getSymbolicName() == symbolicName)
  157. {
  158. plugin->stop(options);
  159. return true;
  160. }
  161. }
  162. qWarning() << "Plug-in" << symbolicName << "not found";
  163. return false;
  164. }
  165. catch (const ctkPluginException& exc)
  166. {
  167. qWarning() << "Failed to stop plug-in:" << exc;
  168. return false;
  169. }
  170. }
  171. else
  172. {
  173. // stop the framework
  174. QSharedPointer<ctkPluginFramework> fw =
  175. qSharedPointerCast<ctkPluginFramework>(pc->getPlugin(0));
  176. try
  177. {
  178. fw->stop();
  179. ctkPluginFrameworkEvent fe = fw->waitForStop(5000);
  180. if (fe.getType() == ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT)
  181. {
  182. qWarning() << "Stopping the plugin framework timed out";
  183. return false;
  184. }
  185. }
  186. catch (const ctkRuntimeException& e)
  187. {
  188. qWarning() << "Stopping the plugin framework failed: " << e;
  189. return false;
  190. }
  191. return true;
  192. }
  193. }
  194. //----------------------------------------------------------------------------
  195. ctkPluginContext* ctkPluginFrameworkLauncher::getPluginContext()
  196. {
  197. if (d->fwFactory == 0) return 0;
  198. return d->fwFactory->getFramework()->getPluginContext();
  199. }
  200. //----------------------------------------------------------------------------
  201. QSharedPointer<ctkPluginFramework> ctkPluginFrameworkLauncher::getPluginFramework()
  202. {
  203. if (d->fwFactory)
  204. return d->fwFactory->getFramework();
  205. return QSharedPointer<ctkPluginFramework>();
  206. }
  207. //----------------------------------------------------------------------------
  208. void ctkPluginFrameworkLauncher::appendPathEnv(const QString& path)
  209. {
  210. #ifdef _WIN32
  211. #ifdef __MINGW32__
  212. QString pathVar("PATH");
  213. QString oldPath(getenv("PATH"));
  214. pathVar += "=" + oldPath + ";" + path;
  215. if(_putenv(qPrintable(pathVar)))
  216. #else
  217. std::size_t bufferLength;
  218. getenv_s(&bufferLength, NULL, 0, "PATH");
  219. QString newPath = path;
  220. if (bufferLength > 0)
  221. {
  222. char* oldPath = new char[bufferLength];
  223. getenv_s(&bufferLength, oldPath, bufferLength, "PATH");
  224. newPath.append(";").append(oldPath);
  225. delete[] oldPath;
  226. }
  227. qDebug() << "new PATH:" << newPath;
  228. if(_putenv_s("PATH", qPrintable(newPath)))
  229. #endif
  230. {
  231. LPVOID lpMsgBuf;
  232. DWORD dw = GetLastError();
  233. FormatMessageW(
  234. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  235. FORMAT_MESSAGE_FROM_SYSTEM |
  236. FORMAT_MESSAGE_IGNORE_INSERTS,
  237. NULL,
  238. dw,
  239. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  240. (LPWSTR) &lpMsgBuf,
  241. 0, NULL );
  242. QString msg = QString("Adding '%1' to the PATH environment variable failed: %2")
  243. .arg(path).arg(QString::fromWCharArray((LPWSTR)lpMsgBuf));
  244. qWarning() << msg;
  245. LocalFree(lpMsgBuf);
  246. }
  247. #else
  248. Q_UNUSED(path)
  249. #endif
  250. }
  251. //----------------------------------------------------------------------------
  252. void ctkPluginFrameworkLauncher::addSearchPath(const QString& searchPath, bool addToPathEnv)
  253. {
  254. d->pluginSearchPaths.prepend(searchPath);
  255. if (addToPathEnv) appendPathEnv(searchPath);
  256. }
  257. //----------------------------------------------------------------------------
  258. QString ctkPluginFrameworkLauncher::getPluginPath(const QString& symbolicName)
  259. {
  260. QString pluginFileName(symbolicName);
  261. pluginFileName.replace(".", "_");
  262. foreach(QString searchPath, d->pluginSearchPaths)
  263. {
  264. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  265. while(dirIter.hasNext())
  266. {
  267. dirIter.next();
  268. QFileInfo fileInfo = dirIter.fileInfo();
  269. QString fileBaseName = fileInfo.baseName();
  270. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  271. if (fileBaseName == pluginFileName)
  272. {
  273. return fileInfo.canonicalFilePath();
  274. }
  275. }
  276. }
  277. return QString();
  278. }
  279. //----------------------------------------------------------------------------
  280. QStringList ctkPluginFrameworkLauncher::getPluginSymbolicNames(const QString& searchPath)
  281. {
  282. QStringList result;
  283. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  284. while(dirIter.hasNext())
  285. {
  286. dirIter.next();
  287. QFileInfo fileInfo = dirIter.fileInfo();
  288. QString fileBaseName = fileInfo.baseName();
  289. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  290. result << fileBaseName.replace("_", ".");
  291. }
  292. return result;
  293. }