ctkExampleHostedAppMain.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // setup the plugin framework
  90. ctkProperties fwProps;
  91. fwProps.insert("dah.hostURL", hostURL);
  92. fwProps.insert("dah.appURL", appURL);
  93. fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN, ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  94. ctkPluginFrameworkFactory fwFactory(fwProps);
  95. QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
  96. // pass further parameters the plugins
  97. if(parser.unparsedArguments().count() > 0)
  98. {
  99. fwProps.insert("dah.args", parser.unparsedArguments().join(" "));
  100. }
  101. try
  102. {
  103. framework->init();
  104. }
  105. catch (const ctkPluginException& exc)
  106. {
  107. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  108. return EXIT_FAILURE;
  109. }
  110. #ifdef CMAKE_INTDIR
  111. QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
  112. #else
  113. QString pluginPath = CTK_PLUGIN_DIR;
  114. #endif
  115. qApp->addLibraryPath(pluginPath);
  116. // try to find the plugin and install all plugins available in
  117. // pluginPath containing the string "org_commontk_dah" (but do not start them)
  118. QSharedPointer<ctkPlugin> appPlugin;
  119. QStringList libFilter;
  120. libFilter << "*.dll" << "*.so" << "*.dylib";
  121. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  122. while(dirIter.hasNext())
  123. {
  124. try
  125. {
  126. QString fileLocation = dirIter.next();
  127. if (fileLocation.contains("org_commontk_dah"))
  128. {
  129. QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  130. if (fileLocation.contains(pluginName))
  131. {
  132. appPlugin = plugin;
  133. }
  134. //plugin->start(ctkPlugin::START_TRANSIENT);
  135. }
  136. }
  137. catch (const ctkPluginException& e)
  138. {
  139. qCritical() << e.what();
  140. }
  141. }
  142. // if we did not find the business logic: abort
  143. if(!appPlugin)
  144. {
  145. qCritical() << "Could not find plugin.";
  146. qCritical() << " Plugin name: " << pluginName;
  147. qCritical() << " Plugin path: " << pluginPath;
  148. return EXIT_FAILURE;
  149. }
  150. // start the plugin framework
  151. framework->start();
  152. // start the plugin with the business logic
  153. try
  154. {
  155. appPlugin->start();
  156. }
  157. catch (const ctkPluginException& e)
  158. {
  159. qCritical() << e;
  160. }
  161. return app.exec();
  162. }