ctkPluginFrameworkLauncher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <stdlib.h>
  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. bool ctkPluginFrameworkLauncher::install(const QString& symbolicName)
  57. {
  58. QString pluginPath = getPluginPath(symbolicName);
  59. if (pluginPath.isEmpty()) return false;
  60. if (d->fwFactory == 0) {
  61. d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps);
  62. try
  63. {
  64. d->fwFactory->getFramework()->init();
  65. }
  66. catch (const ctkPluginException& exc)
  67. {
  68. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  69. delete d->fwFactory;
  70. d->fwFactory = 0;
  71. return false;
  72. }
  73. }
  74. try
  75. {
  76. getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginPath));
  77. }
  78. catch (const ctkPluginException& exc)
  79. {
  80. qWarning() << "Failed to install plugin:" << exc;
  81. return false;
  82. }
  83. return true;
  84. }
  85. //----------------------------------------------------------------------------
  86. bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::StartOptions options)
  87. {
  88. QString pluginPath = getPluginPath(symbolicName);
  89. if (pluginPath.isEmpty()) return false;
  90. if (d->fwFactory == 0) {
  91. d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps);
  92. try
  93. {
  94. d->fwFactory->getFramework()->start();
  95. }
  96. catch (const ctkPluginException& exc)
  97. {
  98. qCritical() << "Failed to start the plug-in framework:" << exc;
  99. delete d->fwFactory;
  100. d->fwFactory = 0;
  101. return false;
  102. }
  103. }
  104. else if (d->fwFactory->getFramework()->getState() != ctkPlugin::ACTIVE)
  105. {
  106. try
  107. {
  108. d->fwFactory->getFramework()->start(options);
  109. }
  110. catch (const ctkPluginException& exc)
  111. {
  112. qCritical() << "Failed to start the plug-in framework:" << exc;
  113. delete d->fwFactory;
  114. d->fwFactory = 0;
  115. return false;
  116. }
  117. }
  118. try
  119. {
  120. getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginPath))->start(options);
  121. }
  122. catch (const ctkPluginException& exc)
  123. {
  124. qWarning() << "Failed to install plugin:" << exc;
  125. return false;
  126. }
  127. return true;
  128. }
  129. //----------------------------------------------------------------------------
  130. ctkPluginContext* ctkPluginFrameworkLauncher::getPluginContext()
  131. {
  132. if (d->fwFactory == 0) return 0;
  133. return d->fwFactory->getFramework()->getPluginContext();
  134. }
  135. //----------------------------------------------------------------------------
  136. QSharedPointer<ctkPluginFramework> ctkPluginFrameworkLauncher::getPluginFramework()
  137. {
  138. if (d->fwFactory)
  139. return d->fwFactory->getFramework();
  140. return QSharedPointer<ctkPluginFramework>();
  141. }
  142. //----------------------------------------------------------------------------
  143. void ctkPluginFrameworkLauncher::appendPathEnv(const QString& path)
  144. {
  145. #ifdef _WIN32
  146. #ifdef __MINGW32__
  147. QString pathVar("PATH");
  148. QString oldPath(getenv("PATH"));
  149. pathVar += "=" + oldPath + ";" + path;
  150. if(_putenv(qPrintable(pathVar)))
  151. #else
  152. std::size_t bufferLength;
  153. getenv_s(&bufferLength, NULL, 0, "PATH");
  154. QString newPath = path;
  155. if (bufferLength > 0)
  156. {
  157. char* oldPath = new char[bufferLength];
  158. getenv_s(&bufferLength, oldPath, bufferLength, "PATH");
  159. newPath.append(";").append(oldPath);
  160. delete[] oldPath;
  161. }
  162. qDebug() << "new PATH:" << newPath;
  163. if(_putenv_s("PATH", qPrintable(newPath)))
  164. #endif
  165. {
  166. LPVOID lpMsgBuf;
  167. DWORD dw = GetLastError();
  168. FormatMessage(
  169. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  170. FORMAT_MESSAGE_FROM_SYSTEM |
  171. FORMAT_MESSAGE_IGNORE_INSERTS,
  172. NULL,
  173. dw,
  174. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  175. (LPTSTR) &lpMsgBuf,
  176. 0, NULL );
  177. QString msg = QString("Adding '%1' to the PATH environment variable failed: %2")
  178. .arg(path).arg(QString((LPCTSTR)lpMsgBuf));
  179. qWarning() << msg;
  180. LocalFree(lpMsgBuf);
  181. }
  182. #else
  183. Q_UNUSED(path)
  184. #endif
  185. }
  186. //----------------------------------------------------------------------------
  187. void ctkPluginFrameworkLauncher::addSearchPath(const QString& searchPath, bool addToPathEnv)
  188. {
  189. d->pluginSearchPaths.prepend(searchPath);
  190. if (addToPathEnv) appendPathEnv(searchPath);
  191. }
  192. //----------------------------------------------------------------------------
  193. QString ctkPluginFrameworkLauncher::getPluginPath(const QString& symbolicName)
  194. {
  195. QString pluginFileName(symbolicName);
  196. pluginFileName.replace(".", "_");
  197. foreach(QString searchPath, d->pluginSearchPaths)
  198. {
  199. QDirIterator dirIter(searchPath, d->pluginLibFilter, QDir::Files);
  200. while(dirIter.hasNext())
  201. {
  202. dirIter.next();
  203. QFileInfo fileInfo = dirIter.fileInfo();
  204. QString fileBaseName = fileInfo.baseName();
  205. if (fileBaseName.startsWith("lib")) fileBaseName = fileBaseName.mid(3);
  206. if (fileBaseName == pluginFileName)
  207. {
  208. return fileInfo.canonicalFilePath();
  209. }
  210. }
  211. }
  212. return QString();
  213. }