<commit_before>/*=========================================================================

Program:   BlueBerry Platform
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "berryInternalPlatform.h"
#include "berryLog.h"

#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/FileStream.h>
#include <Poco/AutoPtr.h>
#include <Poco/Util/PropertyFileConfiguration.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/OptionException.h>

#include <ctkPluginFrameworkLauncher.h>
#include <ctkPluginFrameworkFactory.h>
#include <ctkPluginFramework.h>
#include <ctkPluginContext.h>
#include <ctkPlugin.h>
#include <ctkPluginException.h>

#include <iostream>

#include "../berryPlatform.h"
#include "../berryPlatformException.h"
#include "../berryDebugUtil.h"
#include "../event/berryPlatformEvents.h"
#include "berryPlatformLogChannel.h"
#include "../berryIBundle.h"
#include "berryCodeCache.h"
#include "../berryBundleLoader.h"
#include "berrySystemBundle.h"
#include "berryBundleDirectory.h"
#include "berryProvisioningInfo.h"

#include <QCoreApplication>
#include <QDesktopServices>
#include <QDebug>

namespace berry {

Poco::Mutex InternalPlatform::m_Mutex;

InternalPlatform::InternalPlatform() : m_Initialized(false), m_Running(false),
  m_ConsoleLog(false), m_ServiceRegistry(0),
  m_CodeCache(0), m_BundleLoader(0), m_SystemBundle(0), m_PlatformLogger(0),
  m_ctkPluginFrameworkFactory(0),
  m_EventStarted(PlatformEvent::EV_PLATFORM_STARTED)
{
}

InternalPlatform::~InternalPlatform()
{

}

InternalPlatform* InternalPlatform::GetInstance()
{
  Poco::Mutex::ScopedLock lock(m_Mutex);
  static InternalPlatform instance;
  return &instance;
}

bool InternalPlatform::ConsoleLog() const
{
  return m_ConsoleLog;
}

ctkPluginContext* InternalPlatform::GetCTKPluginFrameworkContext() const
{
  if (m_ctkPluginFrameworkFactory)
  {
    return m_ctkPluginFrameworkFactory->getFramework()->getPluginContext();
  }
  return 0;
}

ServiceRegistry& InternalPlatform::GetServiceRegistry()
{
  AssertInitialized();
  return *m_ServiceRegistry;
}

void InternalPlatform::Initialize(int& argc, char** argv, Poco::Util::AbstractConfiguration* config)
{
  // initialization
  Poco::Mutex::ScopedLock lock(m_Mutex);

  m_Argc = &argc;
  m_Argv = argv;

  try
  {
    this->init(argc, argv);
  }
  catch (const Poco::Util::UnknownOptionException& e)
  { 
    BERRY_WARN << e.displayText();
  } 
  this->loadConfiguration();
  if (config)
  {
    this->config().add(config, 50, false);
  }

  m_ServiceRegistry = new ServiceRegistry();

  m_ConsoleLog = this->GetConfiguration().hasProperty(Platform::ARG_CONSOLELOG);

  m_ConfigPath.assign(this->GetConfiguration().getString("application.configDir"));
  m_InstancePath.assign(this->GetConfiguration().getString("application.dir"));
  try
  {
    m_InstallPath.assign(this->GetConfiguration().getString(Platform::ARG_HOME));
  }
  catch (Poco::NotFoundException& )
  {
    m_InstallPath.assign(m_InstancePath);
  }

  if (this->GetConfiguration().hasProperty(Platform::ARG_STORAGE_DIR))
  {
    std::string dataLocation = this->GetConfiguration().getString(Platform::ARG_STORAGE_DIR, "");
    if (dataLocation.at(dataLocation.size()-1) != '/')
    {
      dataLocation += '/';
    }
    m_UserPath.assign(dataLocation);
  }
  else
  {
    // Append a hash value of the absolute path of the executable to the data location.
    // This allows to start the same application from different build or install trees.
    QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + '_';
    dataLocation += QString::number(qHash(QCoreApplication::applicationDirPath())) + "/";
    m_UserPath.assign(dataLocation.toStdString());
  }

  Poco::File userFile(m_UserPath);
  
  try
  {
    userFile.createDirectories();
    userFile.canWrite();
  }
  catch(const Poco::IOException& e)
  {
    BERRY_WARN << e.displayText();
    m_UserPath.assign(Poco::Path::temp());
    m_UserPath.pushDirectory("." + this->commandName());
    userFile = m_UserPath;
  }

  // Initialize the CTK Plugin Framework
  ctkProperties fwProps;
  fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE, QString::fromStdString(userFile.path()));
#if defined(Q_CC_GNU) && ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 5)))
  fwProps.insert(ctkPluginConstants::FRAMEWORK_PLUGIN_LOAD_HINTS, QVariant::fromValue<QLibrary::LoadHints>(QLibrary::ExportExternalSymbolsHint));
#endif
  if (this->GetConfiguration().hasProperty(Platform::ARG_CLEAN))
  {
    fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN, ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  }
  if (this->GetConfiguration().hasProperty(Platform::ARG_CONSOLELOG))
  {
    fwProps.insert("org.commontk.pluginfw.debug.framework", true);
    fwProps.insert("org.commontk.pluginfw.debug.errors", true);
    fwProps.insert("org.commontk.pluginfw.debug.pluginfw", true);
    fwProps.insert("org.commontk.pluginfw.debug.lazy_activation", true);
    fwProps.insert("org.commontk.pluginfw.debug.resolve", true);
  }
  if (this->GetConfiguration().hasProperty(Platform::ARG_PRELOAD_LIBRARY))
  {
    QString preloadLibs = QString::fromStdString(this->GetConfiguration().getString(Platform::ARG_PRELOAD_LIBRARY));<commit_msg>Do not make symbols from loaded plug-ins globally visible.<commit_after>/*=========================================================================

Program:   BlueBerry Platform
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "berryInternalPlatform.h"
#include "berryLog.h"

#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/FileStream.h>
#include <Poco/AutoPtr.h>
#include <Poco/Util/PropertyFileConfiguration.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Util/HelpFormatter.h>
#include <Poco/Util/OptionException.h>

#include <ctkPluginFrameworkLauncher.h>
#include <ctkPluginFrameworkFactory.h>
#include <ctkPluginFramework.h>
#include <ctkPluginContext.h>
#include <ctkPlugin.h>
#include <ctkPluginException.h>

#include <iostream>

#include "../berryPlatform.h"
#include "../berryPlatformException.h"
#include "../berryDebugUtil.h"
#include "../event/berryPlatformEvents.h"
#include "berryPlatformLogChannel.h"
#include "../berryIBundle.h"
#include "berryCodeCache.h"
#include "../berryBundleLoader.h"
#include "berrySystemBundle.h"
#include "berryBundleDirectory.h"
#include "berryProvisioningInfo.h"

#include <QCoreApplication>
#include <QDesktopServices>
#include <QDebug>

namespace berry {

Poco::Mutex InternalPlatform::m_Mutex;

InternalPlatform::InternalPlatform() : m_Initialized(false), m_Running(false),
  m_ConsoleLog(false), m_ServiceRegistry(0),
  m_CodeCache(0), m_BundleLoader(0), m_SystemBundle(0), m_PlatformLogger(0),
  m_ctkPluginFrameworkFactory(0),
  m_EventStarted(PlatformEvent::EV_PLATFORM_STARTED)
{
}

InternalPlatform::~InternalPlatform()
{

}

InternalPlatform* InternalPlatform::GetInstance()
{
  Poco::Mutex::ScopedLock lock(m_Mutex);
  static InternalPlatform instance;
  return &instance;
}

bool InternalPlatform::ConsoleLog() const
{
  return m_ConsoleLog;
}

ctkPluginContext* InternalPlatform::GetCTKPluginFrameworkContext() const
{
  if (m_ctkPluginFrameworkFactory)
  {
    return m_ctkPluginFrameworkFactory->getFramework()->getPluginContext();
  }
  return 0;
}

ServiceRegistry& InternalPlatform::GetServiceRegistry()
{
  AssertInitialized();
  return *m_ServiceRegistry;
}

void InternalPlatform::Initialize(int& argc, char** argv, Poco::Util::AbstractConfiguration* config)
{
  // initialization
  Poco::Mutex::ScopedLock lock(m_Mutex);

  m_Argc = &argc;
  m_Argv = argv;

  try
  {
    this->init(argc, argv);
  }
  catch (const Poco::Util::UnknownOptionException& e)
  { 
    BERRY_WARN << e.displayText();
  } 
  this->loadConfiguration();
  if (config)
  {
    this->config().add(config, 50, false);
  }

  m_ServiceRegistry = new ServiceRegistry();

  m_ConsoleLog = this->GetConfiguration().hasProperty(Platform::ARG_CONSOLELOG);

  m_ConfigPath.assign(this->GetConfiguration().getString("application.configDir"));
  m_InstancePath.assign(this->GetConfiguration().getString("application.dir"));
  try
  {
    m_InstallPath.assign(this->GetConfiguration().getString(Platform::ARG_HOME));
  }
  catch (Poco::NotFoundException& )
  {
    m_InstallPath.assign(m_InstancePath);
  }

  if (this->GetConfiguration().hasProperty(Platform::ARG_STORAGE_DIR))
  {
    std::string dataLocation = this->GetConfiguration().getString(Platform::ARG_STORAGE_DIR, "");
    if (dataLocation.at(dataLocation.size()-1) != '/')
    {
      dataLocation += '/';
    }
    m_UserPath.assign(dataLocation);
  }
  else
  {
    // Append a hash value of the absolute path of the executable to the data location.
    // This allows to start the same application from different build or install trees.
    QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + '_';
    dataLocation += QString::number(qHash(QCoreApplication::applicationDirPath())) + "/";
    m_UserPath.assign(dataLocation.toStdString());
  }

  Poco::File userFile(m_UserPath);
  
  try
  {
    userFile.createDirectories();
    userFile.canWrite();
  }
  catch(const Poco::IOException& e)
  {
    BERRY_WARN << e.displayText();
    m_UserPath.assign(Poco::Path::temp());
    m_UserPath.pushDirectory("." + this->commandName());
    userFile = m_UserPath;
  }

  // Initialize the CTK Plugin Framework
  ctkProperties fwProps;
  fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE, QString::fromStdString(userFile.path()));
  if (this->GetConfiguration().hasProperty(Platform::ARG_CLEAN))
  {
    fwProps.insert(ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN, ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
  }
  if (this->GetConfiguration().hasProperty(Platform::ARG_CONSOLELOG))
  {
    fwProps.insert("org.commontk.pluginfw.debug.framework", true);
    fwProps.insert("org.commontk.pluginfw.debug.errors", true);
    fwProps.insert("org.commontk.pluginfw.debug.pluginfw", true);
    fwProps.insert("org.commontk.pluginfw.debug.lazy_activation", true);
    fwProps.insert("org.commontk.pluginfw.debug.resolve", true);
  }
  if (this->GetConfiguration().hasProperty(Platform::ARG_PRELOAD_LIBRARY))
  {
    QString preloadLibs = QString::fromStdString(this->GetConfiguration().getString(Platform::ARG_PRELOAD_LIBRARY));
{'fraction_non_alphanumeric': 0.156081618231605, 'fraction_numerical': 0.0020316226481759563, 'mean_word_length': 5.849364791288567, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022789', 'n_tokens_mistral': 3568, 'n_tokens_neox': 3520, 'n_words': 608}