ctkPluginFramework.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "ctkPluginFramework.h"
  16. #include "ctkPluginFrameworkPrivate_p.h"
  17. #include "ctkPluginPrivate_p.h"
  18. #include "ctkPluginFrameworkContext_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkPluginArchive_p.h"
  21. #include "service/event/ctkEvent.h"
  22. //----------------------------------------------------------------------------
  23. ctkPluginFramework::ctkPluginFramework()
  24. : ctkPlugin()
  25. {
  26. qRegisterMetaType<ctkPluginFrameworkEvent>("ctkPluginFrameworkEvent");
  27. qRegisterMetaType<ctkPluginEvent>("ctkPluginEvent");
  28. qRegisterMetaType<ctkServiceEvent>("ctkServiceEvent");
  29. qRegisterMetaType<ctkEvent>("ctkEvent");
  30. qRegisterMetaType<ctkProperties>("ctkProperties");
  31. qRegisterMetaType<ctkDictionary>("ctkDictionary");
  32. }
  33. //----------------------------------------------------------------------------
  34. void ctkPluginFramework::init()
  35. {
  36. Q_D(ctkPluginFramework);
  37. ctkPluginPrivate::Locker sync(&d->lock);
  38. d->waitOnOperation(&d->lock, "Framework.init", true);
  39. switch (d->state)
  40. {
  41. case ctkPlugin::INSTALLED:
  42. case ctkPlugin::RESOLVED:
  43. break;
  44. case ctkPlugin::STARTING:
  45. case ctkPlugin::ACTIVE:
  46. return;
  47. default:
  48. throw ctkIllegalStateException("INTERNAL ERROR, Illegal state");
  49. }
  50. d->init();
  51. }
  52. //----------------------------------------------------------------------------
  53. ctkPluginFrameworkEvent ctkPluginFramework::waitForStop(unsigned long timeout)
  54. {
  55. Q_D(ctkPluginFramework);
  56. ctkPluginPrivate::Locker sync(&d->lock);
  57. // Already stopped?
  58. if ((d->state & (INSTALLED | RESOLVED)) == 0)
  59. {
  60. d->stopEvent.isNull = true;
  61. d->lock.wait(timeout ? timeout : ULONG_MAX);
  62. if (d->stopEvent.isNull)
  63. {
  64. return ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT, this->d_func()->q_func());
  65. }
  66. }
  67. else if (d->stopEvent.isNull)
  68. {
  69. // Return this if stop or update have not been called and framework is stopped.
  70. d->stopEvent.isNull = false;
  71. d->stopEvent.type = ctkPluginFrameworkEvent::FRAMEWORK_STOPPED;
  72. }
  73. return d->stopEvent.isNull ? ctkPluginFrameworkEvent() :
  74. ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::FRAMEWORK_STOPPED, this->d_func()->q_func());
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkPluginFramework::start(const ctkPlugin::StartOptions& options)
  78. {
  79. Q_UNUSED(options);
  80. Q_D(ctkPluginFramework);
  81. QStringList pluginsToStart;
  82. {
  83. ctkPluginPrivate::Locker sync(&d->lock);
  84. d->waitOnOperation(&d->lock, "ctkPluginFramework::start", true);
  85. switch (d->state)
  86. {
  87. case INSTALLED:
  88. case RESOLVED:
  89. d->init();
  90. case STARTING:
  91. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::ACTIVATING);
  92. break;
  93. case ACTIVE:
  94. return;
  95. default:
  96. throw ctkIllegalStateException("INTERNAL ERROR, Illegal state");
  97. }
  98. pluginsToStart = d->fwCtx->storage->getStartOnLaunchPlugins();
  99. }
  100. // Start plugins according to their autostart setting.
  101. QStringListIterator i(pluginsToStart);
  102. while (i.hasNext())
  103. {
  104. QSharedPointer<ctkPlugin> plugin = d->fwCtx->plugins->getPlugin(i.next());
  105. try {
  106. const int autostartSetting = plugin->d_func()->archive->getAutostartSetting();
  107. // Launch must not change the autostart setting of a plugin
  108. StartOptions option = ctkPlugin::START_TRANSIENT;
  109. if (ctkPlugin::START_ACTIVATION_POLICY == autostartSetting)
  110. {
  111. // Transient start according to the plugins activation policy.
  112. option |= ctkPlugin::START_ACTIVATION_POLICY;
  113. }
  114. plugin->start(option);
  115. }
  116. catch (const ctkPluginException& pe)
  117. {
  118. d->fwCtx->listeners.frameworkError(plugin, pe);
  119. }
  120. }
  121. {
  122. ctkPluginPrivate::Locker sync(&d->lock);
  123. d->state = ACTIVE;
  124. d->operation = ctkPluginPrivate::IDLE;
  125. d->lock.wakeAll();
  126. d->fwCtx->listeners.emitFrameworkEvent(
  127. ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::FRAMEWORK_STARTED, this->d_func()->q_func()));
  128. }
  129. }
  130. //----------------------------------------------------------------------------
  131. void ctkPluginFramework::stop(const StopOptions& options)
  132. {
  133. Q_UNUSED(options)
  134. Q_D(ctkPluginFramework);
  135. d->shutdown(false);
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkPluginFramework::uninstall()
  139. {
  140. throw ctkPluginException("uninstall of System plugin is not allowed",
  141. ctkPluginException::INVALID_OPERATION);
  142. }
  143. //----------------------------------------------------------------------------
  144. QStringList ctkPluginFramework::getResourceList(const QString& path) const
  145. {
  146. QString resourcePath = QString(":/") + ctkPluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
  147. if (path.startsWith('/'))
  148. resourcePath += path;
  149. else
  150. resourcePath += QString("/") + path;
  151. QStringList paths;
  152. QFileInfoList entryInfoList = QDir(resourcePath).entryInfoList();
  153. QListIterator<QFileInfo> infoIter(entryInfoList);
  154. while (infoIter.hasNext())
  155. {
  156. const QFileInfo& resInfo = infoIter.next();
  157. QString entry = resInfo.canonicalFilePath().mid(resourcePath.size());
  158. if (resInfo.isDir())
  159. entry += "/";
  160. paths << entry;
  161. }
  162. return paths;
  163. }
  164. //----------------------------------------------------------------------------
  165. QByteArray ctkPluginFramework::getResource(const QString& path) const
  166. {
  167. QString resourcePath = QString(":/") + ctkPluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
  168. if (path.startsWith('/'))
  169. resourcePath += path;
  170. else
  171. resourcePath += QString("/") + path;
  172. QFile resourceFile(resourcePath);
  173. resourceFile.open(QIODevice::ReadOnly);
  174. return resourceFile.readAll();
  175. }
  176. //----------------------------------------------------------------------------
  177. QHash<QString, QString> ctkPluginFramework::getHeaders()
  178. {
  179. //TODO security
  180. Q_D(ctkPluginFramework);
  181. return d->systemHeaders;
  182. }