ctkExampleHostedAppMain.cpp 4.7 KB

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