Newer
Older
#ifndef MANTID_KERNEL_CONFIGSERVICE_H_
#define MANTID_KERNEL_CONFIGSERVICE_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/DllExport.h"
#include "MantidKernel/SingletonHolder.h"
Russell Taylor
committed
#include <vector>
#include <map>
Gigg, Martyn Anthony
committed
#include <set>
//----------------------------------------------------------------------
Russell Taylor
committed
// Forward declarations
//----------------------------------------------------------------------
/// @cond Exclude from doxygen documentation
namespace Poco
{
Gigg, Martyn Anthony
committed
namespace Util
{
class PropertyFileConfiguration;
class SystemConfiguration;
}
}
namespace Mantid
{
Gigg, Martyn Anthony
committed
namespace Kernel
{
Russell Taylor
committed
//----------------------------------------------------------------------
// More forward declarations
//----------------------------------------------------------------------
class Logger;
Gigg, Martyn Anthony
committed
/** The ConfigService class provides a simple facade to access the Configuration functionality of the Mantid Framework.
Russell Taylor
committed
The class gathers information from config files and the system variables.
This information is available to all the objects within the framework as well as being used to configure the logging framework.
This class currently uses the Logging functionality provided through the POCO (portable components library).
@author Nicholas Draper, Tessella Support Services plc
@date 15/10/2007
Copyright © 2007-2010 STFC Rutherford Appleton Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
Gigg, Martyn Anthony
committed
class EXPORT_OPT_MANTID_KERNEL ConfigServiceImpl
{
Russell Taylor
committed
public:
Gigg, Martyn Anthony
committed
/// Wipe out the current configuration and load a new one
void updateConfig(const std::string& filename, const bool append=false, const bool update_caches=true);
/// Save the configuration to the user file
void saveConfig(const std::string &filename) const;
/// Searches for a configuration property
std::string getString(const std::string& keyName, bool use_cache=true) const;
/// Sets a configuration property
void setString(const std::string & keyName, const std::string & keyValue);
Gigg, Martyn Anthony
committed
// Searches for a configuration property and returns its value
template<typename T>
Russell Taylor
committed
int getValue(const std::string& keyName, T& out);
Gigg, Martyn Anthony
committed
/// Return the user properties filename
std::string getUserFilename() const;
Gigg, Martyn Anthony
committed
// Searches for the given environment variable and returns it as a string
std::string getEnvironment(const std::string& keyName);
// Getters for properties of the host system
std::string getOSName();
std::string getComputerName();
std::string getOSArchitecture();
Russell Taylor
committed
std::string getOSVersion();
Gigg, Martyn Anthony
committed
std::string getCurrentDir();
std::string getTempDir();
std::string getBaseDir() const;
std::string getOutputDir() const;
/// Get the list of search paths
const std::vector<std::string>& getDataSearchDirs() const;
Gigg, Martyn Anthony
committed
/// Get the list of known instrument prefixes for the given facility
const std::vector<std::string>& getInstrumentPrefixes(const std::string& facility) const;
Gigg, Martyn Anthony
committed
private:
friend struct Mantid::Kernel::CreateUsingNew<ConfigServiceImpl>;
Russell Taylor
committed
Gigg, Martyn Anthony
committed
// Private constructors and destructor for singleton class
ConfigServiceImpl();
/// Private copy constructor. Prevents singleton being copied.
ConfigServiceImpl(const ConfigServiceImpl&);
Russell Taylor
committed
Gigg, Martyn Anthony
committed
virtual ~ConfigServiceImpl();
Gigg, Martyn Anthony
committed
/// Loads a config file
Russell Taylor
committed
void loadConfig(const std::string& filename, const bool append=false);
Gigg, Martyn Anthony
committed
/// Read a file and place its contents into the given string
bool readFile(const std::string& filename, std::string & contents) const;
Russell Taylor
committed
// Starts up the logging
void configureLogging();
Gigg, Martyn Anthony
committed
/// Provies a string of a default configuration
const std::string defaultConfig() const;
/// Writes out a fresh user properties file
void createUserPropertiesFile() const;
/// Convert any relative paths to absolute ones and store them locally so that
/// if the working directory is altered the paths will not be affected
void convertRelativeToAbsolute();
Gigg, Martyn Anthony
committed
///Make a relative path or a list of relative paths into an absolute one.
std::string makeAbsolute(const std::string & dir, const std::string & key) const;
Gigg, Martyn Anthony
committed
/// Create the storage of the data search directories
Gigg, Martyn Anthony
committed
void cacheDataSearchPaths();
/// Create the map of facility name to instrument prefix list
void cacheInstrumentPrefixes();
Russell Taylor
committed
// Forward declaration of inner class
template <class T>
class WrappedObject;
Gigg, Martyn Anthony
committed
/// the POCO file config object
WrappedObject<Poco::Util::PropertyFileConfiguration>* m_pConf;
/// the POCO system Config Object
WrappedObject<Poco::Util::SystemConfiguration>* m_pSysConfig;
Gigg, Martyn Anthony
committed
Russell Taylor
committed
/// reference to the logger class
Gigg, Martyn Anthony
committed
Logger& g_log;
Gigg, Martyn Anthony
committed
/// A set of property keys that have been changed
mutable std::set<std::string> m_changed_keys;
/// A map storing string/key pairs where the string denotes a path
/// that could be relative in the user properties file
Gigg, Martyn Anthony
committed
/// The boolean indicates whether the path needs to exist or not
std::map<std::string, bool> m_ConfigPaths;
Gigg, Martyn Anthony
committed
/// Local storage for the relative path key/values that have been changed
Gigg, Martyn Anthony
committed
std::map<std::string, std::string> m_AbsolutePaths;
Gigg, Martyn Anthony
committed
/// The directory that is considered to be the base directory
std::string m_strBaseDir;
///The configuration properties in string format
std::string m_PropertyString;
/// The filename of the Mantid properties file
const std::string m_properties_file_name;
/// The filename of the Mantid user properties file
const std::string m_user_properties_file_name;
/// Store a list of data search paths
Gigg, Martyn Anthony
committed
std::vector<std::string> m_DataSearchDirs;
Gigg, Martyn Anthony
committed
/// A map of facilities to instruments
std::map<std::string,std::vector<std::string> > m_instr_prefixes;
Gigg, Martyn Anthony
committed
};
Russell Taylor
committed
/// Forward declaration of a specialisation of SingletonHolder for AlgorithmFactoryImpl (needed for dllexport/dllimport) and a typedef for it.
Gigg, Martyn Anthony
committed
inline
Gigg, Martyn Anthony
committed
template class EXPORT_OPT_MANTID_KERNEL Mantid::Kernel::SingletonHolder<ConfigServiceImpl>;
typedef EXPORT_OPT_MANTID_KERNEL Mantid::Kernel::SingletonHolder<ConfigServiceImpl> ConfigService;
Gigg, Martyn Anthony
committed
} // namespace Kernel
} // namespace Mantid
#endif /*MANTID_KERNEL_CONFIGSERVICE_H_*/