ctkExampleHostedAppMain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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("help", "h", QVariant::Bool, "Show this help text");
  52. bool ok = false;
  53. QHash<QString, QVariant> parsedArgs = parser.parseArguments(QCoreApplication::arguments(), &ok);
  54. if (!ok)
  55. {
  56. QTextStream(stderr, QIODevice::WriteOnly) << "Error parsing arguments: "
  57. << parser.errorString() << "\n";
  58. return EXIT_FAILURE;
  59. }
  60. // Show a help message
  61. if (parsedArgs.contains("help"))
  62. {
  63. print_usage();
  64. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  65. return EXIT_SUCCESS;
  66. }
  67. if(parsedArgs.count() != 2)
  68. {
  69. qCritical() << "Wrong number of command line arguments.";
  70. print_usage();
  71. QTextStream(stdout, QIODevice::WriteOnly) << parser.helpText();
  72. return EXIT_FAILURE;
  73. }
  74. QString hostURL = parsedArgs.value("hostURL").toString();
  75. QString appURL = parsedArgs.value("applicationURL").toString();
  76. qDebug() << "appURL is: " << appURL << " . Extracted port is: " << QUrl(appURL).port();
  77. // setup the plugin framework
  78. ctkProperties fwProps;
  79. fwProps.insert("dah.hostURL", hostURL);
  80. fwProps.insert("dah.appURL", appURL);
  81. ctkPluginFrameworkFactory fwFactory(fwProps);
  82. QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
  83. try
  84. {
  85. framework->init();
  86. }
  87. catch (const ctkPluginException& exc)
  88. {
  89. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  90. return EXIT_FAILURE;
  91. }
  92. #ifdef CMAKE_INTDIR
  93. QString pluginPath = CTK_PLUGIN_DIR CMAKE_INTDIR "/";
  94. #else
  95. QString pluginPath = CTK_PLUGIN_DIR;
  96. #endif
  97. qApp->addLibraryPath(pluginPath);
  98. // Construct the name of the plugin with the business logic
  99. // (thus the actual logic of the hosted app)
  100. QString pluginName("org_commontk_dah_exampleapp");
  101. if(parser.unparsedArguments().count() > 0)
  102. {
  103. pluginName = parser.unparsedArguments().at(0);
  104. }
  105. // try to find the plugin and install all plugins available in
  106. // pluginPath containing the string "org_commontk_dah" (but do not start them)
  107. QSharedPointer<ctkPlugin> appPlugin;
  108. QStringList libFilter;
  109. libFilter << "*.dll" << "*.so" << "*.dylib";
  110. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  111. while(dirIter.hasNext())
  112. {
  113. try
  114. {
  115. QString fileLocation = dirIter.next();
  116. if (fileLocation.contains("org_commontk_dah"))
  117. {
  118. QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  119. if (fileLocation.contains(pluginName))
  120. {
  121. appPlugin = plugin;
  122. }
  123. //plugin->start(ctkPlugin::START_TRANSIENT);
  124. }
  125. }
  126. catch (const ctkPluginException& e)
  127. {
  128. qCritical() << e.what();
  129. }
  130. }
  131. // if we did not find the business logic: abort
  132. if(!appPlugin)
  133. {
  134. qCritical() << "Could not find plugin.";
  135. qCritical() << " Plugin name: " << pluginName;
  136. qCritical() << " Plugin path: " << pluginPath;
  137. return EXIT_FAILURE;
  138. }
  139. // start the plugin framework
  140. framework->start();
  141. // start the plugin with the business logic
  142. try
  143. {
  144. appPlugin->start();
  145. }
  146. catch (const ctkPluginException& e)
  147. {
  148. qCritical() << e;
  149. }
  150. return app.exec();
  151. }