ctkExampleHostedAppMain.cpp 4.7 KB

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