diff --git a/openCherry/Bundles/org.opencherry.core.runtime/src/internal/cherryPreferencesService.cpp b/openCherry/Bundles/org.opencherry.core.runtime/src/internal/cherryPreferencesService.cpp index 58c4a50de8..23fdaa0742 100644 --- a/openCherry/Bundles/org.opencherry.core.runtime/src/internal/cherryPreferencesService.cpp +++ b/openCherry/Bundles/org.opencherry.core.runtime/src/internal/cherryPreferencesService.cpp @@ -1,158 +1,159 @@ #include "cherryPreferencesService.h" #include "cherryXMLPreferencesStorage.h" #include "Poco/ScopedLock.h" #include "Poco/DirectoryIterator.h" +#include "cherryPlatform.h" using namespace std; bool cherry::PreferencesService::IsA( const std::type_info& type ) const { std::string name(GetType().name()); return name == type.name() || Service::IsA(type); } const std::type_info& cherry::PreferencesService::GetType() const { return typeid(cherry::IPreferencesService); } std::string cherry::PreferencesService::GetDefaultPreferencesDirPath() { string _PreferencesDirPath; - _PreferencesDirPath = Poco::Path::home() + ".BlueBerryPrefs"; + _PreferencesDirPath = Platform::GetUserPath().getFileName() + ".BlueBerryPrefs"; return _PreferencesDirPath; } std::string cherry::PreferencesService::GetDefaultPreferencesFileName() { return "prefs.xml"; } cherry::PreferencesService::PreferencesService(string _PreferencesDir) : m_PreferencesDir(_PreferencesDir) { if(m_PreferencesDir.empty()) m_PreferencesDir = GetDefaultPreferencesDirPath(); Poco::File prefDir(m_PreferencesDir); if(!prefDir.exists()) prefDir.createDirectory(); Poco::DirectoryIterator dirIt(prefDir); Poco::File f; while(dirIt.path().getFileName() != "") { f = dirIt.path(); if(f.isFile()) { // check if this file is a preferences file string::size_type pos = dirIt.name().rfind(GetDefaultPreferencesFileName()); if(pos != string::npos) { string userName = dirIt.name().substr(0, pos); // set the storage to 0 (will be loaded later) m_PreferencesStorages[userName] = AbstractPreferencesStorage::Pointer(0); } } ++dirIt; } } cherry::PreferencesService::~PreferencesService() { // flush all preferences for (map::const_iterator it = m_PreferencesStorages.begin() ; it != m_PreferencesStorages.end(); ++it) { // the preferences storage may be 0 if the corresponding file was never loaded if(it->second != 0) it->second->GetRoot()->Flush(); } } cherry::IPreferences::Pointer cherry::PreferencesService::GetSystemPreferences() { Poco::ScopedLock scopedMutex(m_Mutex); // sys prefs are indicated by an empty user string return this->GetUserPreferences(""); } cherry::IPreferences::Pointer cherry::PreferencesService::GetUserPreferences( std::string name ) { Poco::ScopedLock scopedMutex(m_Mutex); IPreferences::Pointer userPrefs(0); map::iterator it = m_PreferencesStorages.find(name); // does not exist or is not loaded yet if(it == m_PreferencesStorages.end() || it->second.IsNull()) { std::string path = m_PreferencesDir; if(name.empty()) path = path + Poco::Path::separator() + GetDefaultPreferencesFileName(); // else path = path + Poco::Path::separator() + name + GetDefaultPreferencesFileName(); XMLPreferencesStorage::Pointer storage(new XMLPreferencesStorage(path)); m_PreferencesStorages[name] = storage; } userPrefs = m_PreferencesStorages[name]->GetRoot(); return userPrefs; } std::vector cherry::PreferencesService::GetUsers() const { Poco::ScopedLock scopedMutex(m_Mutex); vector users; for (map::const_iterator it = m_PreferencesStorages.begin() ; it != m_PreferencesStorages.end(); ++it) { users.push_back(it->first); } return users; } void cherry::PreferencesService::ImportPreferences( Poco::File f, std::string name ) { map::iterator it = m_PreferencesStorages.find(name); if(it->second == 0) { this->GetUserPreferences(name); } Poco::File defaultFile = it->second->GetFile(); XMLPreferencesStorage::Pointer storage(new XMLPreferencesStorage(f)); m_PreferencesStorages[name] = storage; storage->SetFile(defaultFile); } void cherry::PreferencesService::ExportPreferences( Poco::File f, std::string name ) { map::iterator it = m_PreferencesStorages.find(name); if(it->second == 0) { this->GetUserPreferences(name); } Poco::File temp = it->second->GetFile(); it->second->SetFile(f); it->second->GetRoot()->Flush(); it->second->SetFile(temp); } \ No newline at end of file diff --git a/openCherry/Bundles/org.opencherry.osgi/src/internal/cherryInternalPlatform.cpp b/openCherry/Bundles/org.opencherry.osgi/src/internal/cherryInternalPlatform.cpp index a08adf8e42..30ac298d03 100644 --- a/openCherry/Bundles/org.opencherry.osgi/src/internal/cherryInternalPlatform.cpp +++ b/openCherry/Bundles/org.opencherry.osgi/src/internal/cherryInternalPlatform.cpp @@ -1,368 +1,391 @@ /*========================================================================= Program: openCherry 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 "cherryInternalPlatform.h" #include "cherryLog.h" #include #include #include #include #include #include +#include #include #include "../cherryPlatform.h" #include "../cherryPlatformException.h" #include "../event/cherryPlatformEvents.h" #include "cherryPlatformLogChannel.h" #include "../cherryIBundle.h" #include "cherryCodeCache.h" #include "../cherryBundleLoader.h" #include "cherrySystemBundle.h" namespace cherry { Poco::Mutex InternalPlatform::m_Mutex; InternalPlatform::InternalPlatform() : m_Initialized(false), m_Running(false), m_CodeCache(0), m_BundleLoader(0), m_SystemBundle(0), m_PlatformLogger(0), m_EventStarted(PlatformEvent::EV_PLATFORM_STARTED) { } InternalPlatform::~InternalPlatform() { } InternalPlatform* InternalPlatform::GetInstance() { Poco::Mutex::ScopedLock lock(m_Mutex); static InternalPlatform instance; return &instance; } 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; this->init(argc, argv); this->loadConfiguration(); if (config) { this->config().add(config, 50, false); } 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); } - m_UserPath.assign(Poco::Path::home()); - m_UserPath.pushDirectory("." + this->commandName()); - Poco::File userFile(m_UserPath); - userFile.createDirectory(); + try{ + m_UserPath.assign(Poco::Path::home()); + } + catch(Poco::NotFoundException) + { + Poco::Environment::set( "homedrive", Poco::Path::temp() ); + m_UserPath.assign(Poco::Path::home()); + } + m_UserPath.pushDirectory("." + this->commandName()); + Poco::File userFile(m_UserPath); + + try{ + userFile.canWrite(); + + } + catch(Poco::FileException) + { + Poco::Environment::set( "homedrive", Poco::Path::temp() ); + m_UserPath.assign(Poco::Path::home()); + m_UserPath.pushDirectory("." + this->commandName()); + userFile = m_UserPath; + } + + userFile.createDirectory(); + + m_BaseStatePath = m_UserPath; m_BaseStatePath.pushDirectory(".metadata"); m_BaseStatePath.pushDirectory(".plugins"); Poco::Path logPath(m_UserPath); logPath.setFileName(this->commandName() + ".log"); m_PlatformLogChannel = new PlatformLogChannel(logPath.toString()); m_PlatformLogger = &Poco::Logger::create("PlatformLogger", m_PlatformLogChannel, Poco::Message::PRIO_TRACE); try { m_CodeCache = new CodeCache(this->GetConfiguration().getString(Platform::ARG_PLUGIN_CACHE)); } catch (Poco::NotFoundException&) { m_CodeCache = new CodeCache(m_UserPath.append(Poco::Path("plugin_cache")).toString()); } m_BundleLoader = new BundleLoader(m_CodeCache, *m_PlatformLogger); m_Initialized = true; // Clear the CodeCache if (this->GetConfiguration().hasProperty(Platform::ARG_CLEAN)) m_CodeCache->Clear(); try { // assemble a list of base plugin-directories (which contain // the real plugins as directories) std::vector pluginBaseDirs; Poco::StringTokenizer tokenizer(this->GetConfiguration().getString(Platform::ARG_PLUGIN_DIRS), ";", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); for (Poco::StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); ++token) { pluginBaseDirs.push_back(*token); } std::vector pluginPaths; for (std::vector::iterator pluginBaseDir = pluginBaseDirs.begin(); pluginBaseDir != pluginBaseDirs.end(); ++pluginBaseDir) { CHERRY_INFO << "Plugin base directory: " << *pluginBaseDir; Poco::File pluginDir(*pluginBaseDir); if (!pluginDir.exists() || !pluginDir.isDirectory()) { CHERRY_WARN << *pluginBaseDir << " is not a direcotry or does not exist. SKIPPED.\n"; continue; } std::vector pluginList; pluginDir.list(pluginList); std::vector::iterator iter; for (iter = pluginList.begin(); iter != pluginList.end(); iter++) { Poco::Path pluginPath = Poco::Path::forDirectory(*pluginBaseDir); pluginPath.pushDirectory(*iter); Poco::File file(pluginPath); if (file.exists() && file.isDirectory()) { pluginPaths.push_back(pluginPath); } } } std::vector::iterator pathIter; for (pathIter = pluginPaths.begin(); pathIter != pluginPaths.end(); pathIter++) { try { Bundle::Pointer bundle = m_BundleLoader->LoadBundle(*pathIter); std::cout << "Bundle state (" << pathIter->toString() << "): " << bundle->GetStateString() << std::endl; } catch (const BundleStateException& exc) { std::cout << exc.displayText() << std::endl; } } // resolve plugins m_BundleLoader->ResolveAllBundles(); } catch (Poco::Exception& exc) { this->logger().log(exc); } } void InternalPlatform::Launch() { AssertInitialized(); if (m_Running) return; m_Running = true; this->run(); } void InternalPlatform::Shutdown() { Poco::Mutex::ScopedLock lock(m_Mutex); AssertInitialized(); m_Initialized = false; this->uninitialize(); if (m_BundleLoader != 0) delete m_BundleLoader; if (m_CodeCache != 0) delete m_CodeCache; } void InternalPlatform::AssertInitialized() { if (!m_Initialized) throw Poco::SystemException("The Platform has not been initialized yet!"); } IExtensionPointService::Pointer InternalPlatform::GetExtensionPointService() { Poco::Mutex::ScopedLock lock(m_Mutex); this->AssertInitialized(); return m_ServiceRegistry.GetServiceById(IExtensionPointService::SERVICE_ID); } const Poco::Path& InternalPlatform::GetConfigurationPath() { return m_ConfigPath; } const Poco::Path& InternalPlatform::GetInstallPath() { return m_InstallPath; } const Poco::Path& InternalPlatform::GetInstancePath() { return m_InstancePath; } Poco::Path InternalPlatform::GetStatePath(IBundle::Pointer bundle) { Poco::Path statePath(m_BaseStatePath); statePath.pushDirectory(bundle->GetSymbolicName()); Poco::File stateFile(statePath); if (!stateFile.exists()) stateFile.createDirectories(); return statePath; } PlatformEvents& InternalPlatform::GetEvents() { return m_Events; } const Poco::Path& InternalPlatform::GetUserPath() { return m_UserPath; } bool InternalPlatform::IsRunning() const { Poco::Mutex::ScopedLock lock(m_Mutex); return (m_Initialized && m_Running); } IBundle::Pointer InternalPlatform::GetBundle(const std::string& id) { Poco::Mutex::ScopedLock lock(m_Mutex); AssertInitialized(); return m_BundleLoader->FindBundle(id); } Poco::Logger* InternalPlatform::GetLogger() { return m_PlatformLogger; } Poco::Util::LayeredConfiguration& InternalPlatform::GetConfiguration() const { return this->config(); } std::vector InternalPlatform::GetApplicationArgs() const { return m_FilteredArgs; } int& InternalPlatform::GetRawApplicationArgs(char**& argv) { argv = m_Argv; return *m_Argc; } void InternalPlatform::defineOptions(Poco::Util::OptionSet& options) { Poco::Util::Option helpOption("help", "h", "print this help text"); helpOption.callback(Poco::Util::OptionCallback(this, &InternalPlatform::PrintHelp)); options.addOption(helpOption); Poco::Util::Option cleanOption(Platform::ARG_CLEAN, Platform::ARG_CLEAN, "cleans the plugin cache"); cleanOption.binding(Platform::ARG_CLEAN); options.addOption(cleanOption); Poco::Util::Option appOption(Platform::ARG_APPLICATION, Platform::ARG_APPLICATION, "the id of the application extension to be executed"); appOption.argument(Platform::ARG_APPLICATION).binding(Platform::ARG_APPLICATION); options.addOption(appOption); Poco::Util::Application::defineOptions(options); } int InternalPlatform::main(const std::vector& args) { m_FilteredArgs = args; m_FilteredArgs.insert(m_FilteredArgs.begin(), this->config().getString("application.argv[0]")); SystemBundle::Pointer systemBundle = m_BundleLoader->FindBundle("system.bundle").Cast(); if (systemBundle == 0) throw PlatformException("Could not find the system bundle"); m_BundleLoader->StartSystemBundle(systemBundle); systemBundle->Resume(); return EXIT_OK; } void InternalPlatform::PrintHelp(const std::string&, const std::string&) { std::cout << "Usage: " << this->commandName() << " [OPTION]...\n"; const Poco::Util::OptionSet& opts = this->options(); for (Poco::Util::OptionSet::Iterator option = opts.begin(); option != opts.end(); ++option) { #ifdef CHERRY_OS_FAMILY_WINDOWS std::string optionPostFix = "/"; #else std::string optionPostFix = "-"; #endif std::cout.width(35); std::cout.setf(std::ios_base::left, std::ios_base::adjustfield); if (option->takesArgument()) { std::cout << (" " + optionPostFix + option->fullName() + "="); } else { std::cout << (" " + optionPostFix + option->fullName()); } std::cout << option->description() << std::endl; } exit(EXIT_OK); } }