ctkExampleHostedAppMain.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <ctkPluginFrameworkFactory.h>
  16. #include <ctkPluginFramework.h>
  17. #include <ctkPluginException.h>
  18. // for testing purposes use:
  19. // --hostURL http://localhost:8081/host --applicationURL http://localhost:8082/app dicomapp
  20. #include <QApplication>
  21. #include <QString>
  22. #include <QStringList>
  23. #include <QDirIterator>
  24. #include <QWidget>
  25. #include <QFileInfo>
  26. void print_usage()
  27. {
  28. qCritical() << "Usage:";
  29. qCritical() << " " << QFileInfo(qApp->arguments().at(0)).fileName() << " --hostURL url1 --applicationURL url2 <plugin-name>";
  30. }
  31. int main(int argv, char** argc)
  32. {
  33. QApplication app(argv, argc);
  34. qDebug() << "################################################################";
  35. qApp->setOrganizationName("CTK");
  36. qApp->setOrganizationDomain("commontk.org");
  37. qApp->setApplicationName("ctkExampleHostedApp");
  38. // parse the command line
  39. qDebug() << "################################################################";
  40. if(qApp->arguments().size() < 5)
  41. {
  42. qCritical() << "Wrong number of command line arguments.";
  43. print_usage();
  44. exit(1);
  45. }
  46. if(qApp->arguments().at(1) != "--hostURL")
  47. {
  48. qCritical() << "First argument must be --hostURL.";
  49. print_usage();
  50. exit(1);
  51. }
  52. QString hostURL = qApp->arguments().at(2);
  53. qDebug() << "hostURL is: " << hostURL << " . Extracted port is: " << QUrl(hostURL).port();
  54. if(qApp->arguments().at(3) != "--applicationURL")
  55. {
  56. qCritical() << "First argument must be --applicationURL.";
  57. print_usage();
  58. exit(1);
  59. }
  60. QString appURL = qApp->arguments().at(4);
  61. qDebug() << "appURL is: " << appURL << " . Extracted port is: " << QUrl(appURL).port();
  62. // setup the plugin framework
  63. ctkProperties fwProps;
  64. fwProps.insert("dah.hostURL", hostURL);
  65. fwProps.insert("dah.appURL", appURL);
  66. ctkPluginFrameworkFactory fwFactory(fwProps);
  67. QSharedPointer<ctkPluginFramework> framework = fwFactory.getFramework();
  68. try {
  69. framework->init();
  70. }
  71. catch (const ctkPluginException& exc)
  72. {
  73. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  74. exit(2);
  75. }
  76. #ifdef CMAKE_INTDIR
  77. QString pluginPath = qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
  78. #else
  79. QString pluginPath = qApp->applicationDirPath() + "/plugins/";
  80. #endif
  81. qApp->addLibraryPath(pluginPath);
  82. // construct the name of the plugin with the business logic
  83. // (thus the actual logic of the hosted app)
  84. QString pluginName;
  85. if(qApp->arguments().size()>5)
  86. {
  87. pluginName = qApp->arguments().at(5);
  88. }
  89. else
  90. {
  91. pluginName = "org_commontk_dah_exampleapp";
  92. }
  93. // try to find the plugin and install all plugins available in
  94. // pluginPath containing the string "org_commontk_dah" (but do not start them)
  95. QSharedPointer<ctkPlugin> appPlugin;
  96. QStringList libFilter;
  97. libFilter << "*.dll" << "*.so" << "*.dylib";
  98. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  99. while(dirIter.hasNext())
  100. {
  101. try
  102. {
  103. QString fileLocation = dirIter.next();
  104. if (fileLocation.contains("org_commontk_dah"))
  105. {
  106. QSharedPointer<ctkPlugin> plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  107. if (fileLocation.contains(pluginName))
  108. {
  109. appPlugin = plugin;
  110. }
  111. //plugin->start(ctkPlugin::START_TRANSIENT);
  112. }
  113. }
  114. catch (const ctkPluginException& e)
  115. {
  116. qCritical() << e.what();
  117. }
  118. }
  119. // if we did not find the business logic: abort
  120. if(!appPlugin)
  121. {
  122. qCritical() << "Could not find plugin.";
  123. qCritical() << " Plugin name: " << pluginName;
  124. qCritical() << " Plugin path: " << pluginPath;
  125. exit(3);
  126. }
  127. // start the plugin framework
  128. framework->start();
  129. // start the plugin with the business logic
  130. try
  131. {
  132. appPlugin->start();
  133. }
  134. catch (const ctkPluginException& e)
  135. {
  136. qCritical() << e;
  137. }
  138. return app.exec();
  139. }