ctkPluginFramework.cpp 6.7 KB

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