ctkExampleHostedAppMain.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Qt includes
  16. #include <QApplication>
  17. #include <QString>
  18. #include <QStringList>
  19. #include <QDirIterator>
  20. #include <QWidget>
  21. #include <QFileInfo>
  22. #include <QUrl>
  23. #include <QDebug>
  24. // CTK includes
  25. #include <ctkConfig.h>
  26. #include <ctkCommandLineParser.h>
  27. #include <ctkPluginFrameworkFactory.h>
  28. #include <ctkPluginFramework.h>
  29. #include <ctkPluginException.h>
  30. #include <ctkPluginContext.h>
  31. // For testing purposes use:
  32. // --hostURL http://localhost:8081/host --applicationURL http://localhost:8082/app dicomapp
  33. //----------------------------------------------------------------------------
  34. void print_usage()
  35. {
  36. qCritical() << "Usage:";
  37. qCritical() << " " << QFileInfo(qApp->arguments().at(0)).fileName() << " --hostURL url1 --applicationURL url2 <plugin-name>";
  38. }
  39. //----------------------------------------------------------------------------
  40. int main(int argv, char** argc)
  41. {
  42. QApplication app(argv, argc);
  43. qApp->setOrganizationName("CTK");
  44. qApp->setOrganizationDomain("commontk.org");
  45. qApp->setApplicationName("ctkExampleHostedApp");
  46. ctkCommandLineParser parser;
  47. parser.setArgumentPrefix("--", "-"); // Use Unix-style argument names
  48. // Add command line argument names
  49. parser.addArgument("hostURL", "", QVariant::String, "Hosting system URL");
  50. parser.addArgument("applicationURL", "", QVariant::String, "Hosted Application URL");
  51. parser.addArgument("plugin", "", QVariant::String, "Plugin implementing the DicomAppInterface", "org_commontk_dah_exampleapp");
  52. parser.addArgument("help", "h", QVariant::Bool, "Show this help text");
  53. bool ok = false;
  54. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  55. if (!ok)
  56. {
  57. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  58. << parser.errorString() << "\n";
  59. return EXIT_FAILURE;
  60. }
  61. // Show a help message
  62. if (parsedArgs.contains("help"))
  63. {
  64. print_usage();
  65. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  66. return EXIT_SUCCESS;
  67. }
  68. if (parsedArgs.contains("hostURL") == false)
  69. {
  70. qCritical() << "Missing parameter hostURL.";
  71. print_usage();
  72. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  73. return EXIT_FAILURE;
  74. }
  75. if (parsedArgs.contains("applicationURL") == false)
  76. {
  77. qCritical() << "Missing parameter hostURL.";
  78. print_usage();
  79. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  80. return EXIT_FAILURE;
  81. }
  82. QString hostURL = parsedArgs.value("hostURL").toString();
  83. QString appURL = parsedArgs.value("applicationURL").toString();
  84. qDebug() << "appURL is: " << appURL << " . Extracted port is: " << QUrl(appURL).port();
  85. // Get the name of the plugin with the business logic
  86. // (thus the actual logic of the hosted app)
  87. QString pluginName = parsedArgs.value("plugin").toString();
  88. qCritical() << " Plugin name: " << pluginName;
  89. ctkProperties fwProps;
  90. // pass further parameters the plugins
  91. if(parser.unparsedArguments().count() > 0)
  92. {
  93. QString args = parser.unparsedArguments().join(" ");
  94. fwProps.insert("dah.args", parser.unparsedArguments().join(" "));
  95. }
  96. // setup the plugin framework
  97. fwProps.insert("dah.hostURL", hostURL);
  98. fwProps.insert("dah.appURL", appURL);
  99. fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN, ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  100. ctkPluginFrameworkFactory fwFactory(fwProps);
  101. QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
  102. try
  103. {
  104. framework->init();
  105. }
  106. catch (const ctkPluginException& exc)
  107. {
  108. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  109. return EXIT_FAILURE;
  110. }
  111. #ifdef CMAKE_INTDIR
  112. QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
  113. #else
  114. QString pluginPath = CTK_PLUGIN_DIR;
  115. #endif
  116. qApp->addLibraryPath(pluginPath);
  117. // try to find the plugin and install all plugins available in
  118. // pluginPath containing the string "org_commontk_dah" (but do not start them)
  119. QSharedPointer<ctkPlugin> appPlugin;
  120. QStringList libFilter;
  121. libFilter << "*.dll" << "*.so" << "*.dylib";
  122. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  123. while(dirIter.hasNext())
  124. {
  125. try
  126. {
  127. QString fileLocation = dirIter.next();
  128. if (fileLocation.contains("org_commontk_dah"))
  129. {
  130. QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  131. if (fileLocation.contains(pluginName))
  132. {
  133. appPlugin = plugin;
  134. }
  135. //plugin->start(ctkPlugin::START_TRANSIENT);
  136. }
  137. }
  138. catch (const ctkPluginException& e)
  139. {
  140. qCritical() << e.what();
  141. }
  142. }
  143. // if we did not find the business logic: abort
  144. if(!appPlugin)
  145. {
  146. qCritical() << "Could not find plugin.";
  147. qCritical() << " Plugin name: " << pluginName;
  148. qCritical() << " Plugin path: " << pluginPath;
  149. return EXIT_FAILURE;
  150. }
  151. // start the plugin framework
  152. framework->start();
  153. // start the plugin with the business logic
  154. try
  155. {
  156. appPlugin->start();
  157. }
  158. catch (const ctkPluginException& e)
  159. {
  160. qCritical() << e;
  161. }
  162. return app.exec();
  163. }