ctkExampleHostedAppMain.cpp 4.6 KB

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