ctkExampleHostedAppMain.cpp 5.3 KB

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