ctkPluginFrameworkLauncher.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #ifndef CTKPLUGINFRAMEWORKLAUNCHER_H
  16. #define CTKPLUGINFRAMEWORKLAUNCHER_H
  17. #include <QString>
  18. #include <QScopedPointer>
  19. #include <ctkPluginFrameworkExport.h>
  20. #include "ctkPlugin.h"
  21. #include "ctkPluginFramework_global.h"
  22. class ctkPluginFramework;
  23. class ctkPluginFrameworkLauncherPrivate;
  24. class QRunnable;
  25. /**
  26. * \ingroup PluginFramework
  27. *
  28. * Sets up and starts the CTK Plugin Framework.
  29. *
  30. * This is a convenience class to start an instance of the CTK Plugin
  31. * Framework and to install and start plugins.
  32. */
  33. class CTK_PLUGINFW_EXPORT ctkPluginFrameworkLauncher
  34. {
  35. public:
  36. // System properties
  37. static const QString PROP_USER_HOME; // = "user.home";
  38. static const QString PROP_USER_DIR; // = "user.dir";
  39. // Framework properties
  40. static const QString PROP_PLUGINS; // = "ctk.plugins";
  41. static const QString PROP_PLUGINS_START_OPTIONS; // = "ctk.plugins.startOptions";
  42. static const QString PROP_DEBUG; // = "ctk.debug";
  43. static const QString PROP_DEV; // = "ctk.dev";
  44. static const QString PROP_CONSOLE; // = "ctk.console";
  45. static const QString PROP_OS; // = "ctk.os";
  46. static const QString PROP_ARCH; // = "ctk.arch";
  47. static const QString PROP_NOSHUTDOWN; // = "ctk.noShutdown";
  48. static const QString PROP_IGNOREAPP; // = "ctk.ignoreApp";
  49. static const QString PROP_INSTALL_AREA; // = "ctk.install.area";
  50. static const QString PROP_CONFIG_AREA; // = "ctk.configuration.area";
  51. static const QString PROP_SHARED_CONFIG_AREA; // = "ctk.sharedConfiguration.area";
  52. static const QString PROP_INSTANCE_AREA; // = "ctk.instance.area";
  53. static const QString PROP_USER_AREA; // = "ctk.user.area";
  54. static const QString PROP_HOME_LOCATION_AREA; // = "ctk.home.location";
  55. static const QString PROP_CONFIG_AREA_DEFAULT; // = "ctk.configuration.area.default";
  56. static const QString PROP_INSTANCE_AREA_DEFAULT; // = "ctk.instance.area.default";
  57. static const QString PROP_USER_AREA_DEFAULT; // = "ctk.user.area.default";
  58. static const QString PROP_EXITCODE; // = "ctk.exitcode";
  59. static const QString PROP_EXITDATA; // = "ctk.exitdata";
  60. static const QString PROP_CONSOLE_LOG; // = "ctk.consoleLog";
  61. static const QString PROP_ALLOW_APPRELAUNCH; // = "ctk.allowAppRelaunch";
  62. static const QString PROP_APPLICATION_LAUNCHDEFAULT; // = "ctk.application.launchDefault";
  63. static const QString PROP_OSGI_RELAUNCH; // = "ctk.pluginfw.relaunch";
  64. /**
  65. * Specify the set of framework properties to be used when
  66. * initializing the Plugin Framework.
  67. *
  68. * <p>
  69. * If the framework has already been initialized by a call
  70. * to #install or #start, the
  71. * new properties do not have any effect until the framework
  72. * is restarted.
  73. *
  74. * \param props The initial Plugin Framework properties.
  75. */
  76. static void setFrameworkProperties(const ctkProperties& props);
  77. /**
  78. * Launches the platform and runs a single application. The application is either identified
  79. * in the given arguments (e.g., -application &lt;app id&gt;) or in the <code>eclipse.application</code>
  80. * System property. This convenience method starts
  81. * up the platform, runs the indicated application, and then shuts down the
  82. * platform. The platform must not be running already.
  83. *
  84. * @param endSplashHandler the block of code to run to tear down the splash
  85. * screen or <code>null</code> if no tear down is required
  86. * @param argument the argument passed to the application. Default is QVariant().
  87. * @return the result of running the application
  88. * @throws Exception if anything goes wrong
  89. */
  90. static QVariant run(QRunnable* endSplashHandler = NULL, const QVariant& argument = QVariant());
  91. /**
  92. * Runs the application for which the platform was started. The platform
  93. * must be running.
  94. * <p>
  95. * The given argument is passed to the application being run. If it is <code>null</code>
  96. * then the command line arguments used in starting the platform, and not consumed
  97. * by the platform code, are passed to the application as a <code>String[]</code>.
  98. * </p>
  99. * @param argument the argument passed to the application. May be <code>null</code>
  100. * @return the result of running the application
  101. * @throws std::exception if anything goes wrong
  102. */
  103. static QVariant run(const QVariant& argument);
  104. /**
  105. * Starts the platform and sets it up to run a single application. The application is either identified
  106. * in the given arguments (e.g., -application &lt;app id&gt;) or in the <code>eclipse.application</code>
  107. * System property. The platform must not be running already.
  108. * <p>
  109. * The given runnable (if not <code>null</code>) is used to tear down the splash screen if required.
  110. * </p>
  111. * @param args the arguments passed to the application
  112. * @return BundleContext the context of the system bundle
  113. * @throws Exception if anything goes wrong
  114. */
  115. static ctkPluginContext* startup(QRunnable* endSplashHandler);
  116. /**
  117. * Shuts down the Platform. The state of the Platform is not automatically
  118. * saved before shutting down.
  119. * <p>
  120. * On return, the Platform will no longer be running (but could be re-launched
  121. * with another call to startup). If relaunching, care must be taken to reinitialize
  122. * any System properties which the platform uses (e.g., osgi.instance.area) as
  123. * some policies in the platform do not allow resetting of such properties on
  124. * subsequent runs.
  125. * </p><p>
  126. * Any objects handed out by running Platform,
  127. * including Platform runnables obtained via getRunnable, will be
  128. * permanently invalid. The effects of attempting to invoke methods
  129. * on invalid objects is undefined.
  130. * </p>
  131. * @throws std::exception if anything goes wrong
  132. */
  133. static void shutdown();
  134. /**
  135. * Install the plugin with the given symbolic name using the provided
  136. * <code>ctkPluginContext</code>.
  137. *
  138. * <p>
  139. * This method instantiates and initializes an instance of a
  140. * ctkPluginFramework object, if no plugin context is provided and
  141. * if there is no framework already initialized.
  142. *
  143. * <p>
  144. * The plugin is searched in the paths previously given by calls
  145. * to #addSearchPath(const QString&, bool) and in the default
  146. * CTK plugin installation path.
  147. *
  148. * \param symbolicName The symbolic name of the plugin to install.
  149. * \param context The plugin context used for installing the plugin.
  150. * \return The plugin id if the plugin was found and successfully
  151. * installed, <code>-1</code> otherwise.
  152. */
  153. static long install(const QString& symbolicName, ctkPluginContext* context = 0);
  154. /**
  155. * This method instantiates, initializes, and starts an instance of a
  156. * ctkPluginFramework object, if no plugin context is provided and
  157. * if there is no framework already running. It then installs and
  158. * starts the plugin with the given symbolic name (if not empty).
  159. *
  160. * <p>
  161. * If a symbolic name is given, the plugin is searched in the paths previously given by calls
  162. * to #addSearchPath(const QString&, bool) and in the default
  163. * CTK plugin installation path and is started using the given <code>options</code>.
  164. *
  165. * <p>
  166. * If a plugin context is provided, this context is used to install the plugin,
  167. * otherwise the Plugin Framework context is used.
  168. *
  169. * \param symbolicName The symbolic name of the plugin to install.
  170. * \param options The options used to start the plugin.
  171. * \param context The plugin context to use for installing the plugin.
  172. * \return <code>true</code> if the plugin was found and successfully
  173. * installed, <code>false</code> otherwise.
  174. *
  175. * \see ctkPlugin::StartOptions
  176. */
  177. static bool start(const QString& symbolicName = QString(),
  178. ctkPlugin::StartOptions options = ctkPlugin::START_ACTIVATION_POLICY,
  179. ctkPluginContext* context = 0);
  180. /**
  181. * This method either stops the plug-in with the given <code>symbolicName</code> using
  182. * the supplied stop options <code>options</code>
  183. * or the complete framework (if <code>symbolicName</code> is empty).
  184. *
  185. * <p>
  186. * If a plugin context is provided, this context is used to find the plug-in,
  187. * otherwise the Plugin Framework context is used.
  188. *
  189. * \param symbolicName The symbolic name of the plugin to stop.
  190. * \param options The options used to stop the plugin.
  191. * \param context The plugin context to use for finding the plugin.
  192. * \return <code>true</code> if the plugin was found and successfully
  193. * stopped or the complete framework was successfully stopped,
  194. * <code>false</code> otherwise.
  195. *
  196. * \see ctkPlugin::StopOptions
  197. */
  198. static bool stop(const QString& symbolicName = QString(),
  199. ctkPlugin::StopOptions options = 0, ctkPluginContext* context = 0);
  200. /**
  201. * Resolve the given plug-in.
  202. *
  203. * This method tries to resolv the given plug-in. In case of an error,
  204. * a framework event of type ctkPluginFrameworkEvent::PLUGIN_ERROR is send.
  205. *
  206. * @param plugin The plug-in to be resolved.
  207. */
  208. static void resolve(const QSharedPointer<ctkPlugin>& plugin);
  209. /**
  210. * Resolve all currently installed plug-ins.
  211. *
  212. * In case of an error, a framework event of type
  213. * ctkPluginFrameworkEvent::PLUGIN_ERROR is send.
  214. */
  215. static void resolve();
  216. /**
  217. * Get the plugin context for the Plugin Framework.
  218. *
  219. * \return The context associated to the Plugin Framework, or <code>null</code>
  220. * if the framework has not been initialized yet.
  221. */
  222. static ctkPluginContext* getPluginContext();
  223. /**
  224. * Get the Plugin Framework:
  225. *
  226. * \return The initialized Plugin Framework, or <code>null</code> if the
  227. * framework has not been initialized yet.
  228. */
  229. static QSharedPointer<ctkPluginFramework> getPluginFramework();
  230. /**
  231. * This is a utility method to append a path to the PATH environment variable
  232. * on Windows platforms.
  233. *
  234. * <p>
  235. * This method does nothing on non-Windows platforms.
  236. *
  237. * \param path The path to be appended to PATH
  238. */
  239. static void appendPathEnv(const QString& path);
  240. /**
  241. * Add a path to the list of search paths for plugins.
  242. *
  243. * When calling #install(const QString&, ctkPluginContext*), #start, or
  244. * #getPluginPath(const QString&), the plugin is searched in the
  245. * paths given as arguments to this method. The least recently added
  246. * path is searched first.
  247. *
  248. * \param searchPath The path to add.
  249. * \param addToPathEnv If <code>true</code>, add the given path to the
  250. * PATH environment variable, using #appendPathEnv(const QString&).
  251. */
  252. static void addSearchPath(const QString& searchPath, bool addToPathEnv = true);
  253. /**
  254. * Get the full path (including the file name) to the plugin with the
  255. * given symbolic name.
  256. *
  257. * <p>
  258. * The paths given by calls to #addSearchPath(const QString&, bool) are searched
  259. * for a shared library with a base name equaling <code>symbolicName</code>.
  260. *
  261. * \param symbolicName The symbolic name of the plugin to find.
  262. * \return The full path (including the file name) to the plugin (shared library)
  263. * or a null QString if the plugin was not found.
  264. */
  265. static QString getPluginPath(const QString& symbolicName);
  266. /**
  267. * Get a list of symbolic names for the plugins in <code>searchPath</code>.
  268. *
  269. * <p>
  270. * The symbolic names are deduced from the shared libraries found in
  271. * <code>searchPath</code>, which may not represent loadable CTK plugins.
  272. *
  273. * \param searchPath The path to look for plugins.
  274. * \return A list of potential plugin symbolic names.
  275. */
  276. static QStringList getPluginSymbolicNames(const QString& searchPath);
  277. private:
  278. static const QScopedPointer<ctkPluginFrameworkLauncherPrivate> d;
  279. Q_DISABLE_COPY(ctkPluginFrameworkLauncher)
  280. };
  281. #endif // CTKPLUGINFRAMEWORKLAUNCHER_H