ctkExampleHostedAppMain.cpp 5.1 KB

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