Beispiel #1
0
	void printProperties(const std::string& base)
	{
		AbstractConfiguration::Keys keys;
		config().keys(base, keys);
		if (keys.empty())
		{
			if (config().hasProperty(base))
			{
				std::string msg;
				msg.append(base);
				msg.append(" = ");
				msg.append(config().getString(base));
				logger().information(msg);
			}
		}
		else
		{
			for (AbstractConfiguration::Keys::const_iterator it = keys.begin(); it != keys.end(); ++it)
			{
				std::string fullKey = base;
				if (!fullKey.empty()) fullKey += '.';
				fullKey.append(*it);
				printProperties(fullKey);
			}
		}
	}
void LayeredConfigurationTest::testOneLayer()
{
	AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
	AutoPtr<MapConfiguration> pMC = new MapConfiguration;
	
	pMC->setString("prop1", "value1");
	pMC->setString("prop2", "value2");
	
	pLC->addWriteable(pMC, 0);

	AbstractConfiguration::Keys keys;
	pLC->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	
	assert (pLC->getString("prop1") == "value1");
	assert (pLC->getString("prop2") == "value2");

	pLC->setString("prop3", "value3");
	assert (pLC->getString("prop3") == "value3");

	pLC->remove("prop3");
	assert (!pLC->hasProperty("prop3"));
}
void LayeredConfigurationTest::testEmpty()
{
	AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
	
	AbstractConfiguration::Keys keys;
	pLC->keys(keys);
	assert (keys.empty());
	
	assert (!pLC->hasProperty("foo"));
	try
	{
		pLC->setString("foo", "bar");
		fail("empty LayeredConfiguration - must throw");
	}
	catch (RuntimeException&)
	{
	}
	
	try
	{
		std::string s = pLC->getString("foo");
		fail("empty LayeredConfiguration - must throw");
	}
	catch (NotFoundException&)
	{
	}
}
void AMFWriter::writeObject(const AMFObject& amfObject) {
	beginObject();
	AbstractConfiguration::Keys keys;
	amfObject.keys(keys);
	AbstractConfiguration::Keys::const_iterator it;
	for(it=keys.begin();it!=keys.end();++it) {
		string name = *it;
		_writer.writeString16(name);
		int type = amfObject.getInt(name+".type",-1);
		switch(type) {
			case AMF_BOOLEAN:
				writeBool(amfObject.getBool(name));
				break;
			case AMF_STRING:
				write(amfObject.getString(name));
				break;
			case AMF_NUMBER:
				writeNumber(amfObject.getDouble(name));
				break;
			case AMF_UNDEFINED:
				write("");
				break;
			case AMF_NULL:
				writeNull();
				break;
			default:
				ERROR("Unknown AMF '%d' type",type);
		}
	}
	endObject();
}
void LayeredConfigurationTest::testRemove()
{
	AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
	AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
	AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
	
	pMC1->setString("prop1", "value1");
	pMC1->setString("prop2", "value2");
	pMC2->setString("prop2", "value3");
	pMC2->setString("prop3", "value4");
	
	pLC->add(pMC1, 0);
	pLC->add(pMC2, -1);

	AbstractConfiguration::Keys keys;
	pLC->keys(keys);
	assert (keys.size() == 3);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	
	assert (pLC->getString("prop1") == "value1");
	assert (pLC->getString("prop2") == "value3");
	assert (pLC->getString("prop3") == "value4");

	pLC->removeConfiguration(pMC2);
	keys.clear();
	pLC->keys(keys);
	assert (keys.size() == 2);
	
	assert (pLC->getString("prop1") == "value1");
	assert (pLC->getString("prop2") == "value2");
}
//============================================================================//
void Configuration::prepareApplicationLoggingFiles(AbstractConfiguration &config, const string &prefix)
{

    // find all files loggers
    AbstractConfiguration::Keys             channelsKeys;
    AbstractConfiguration::Keys::iterator   keyIt;

    config.keys("logging.channels", channelsKeys);

    config.setString("application.logger", prefix);


    for(keyIt = channelsKeys.begin(); keyIt != channelsKeys.end(); ++keyIt) {
        if(!config.getString("logging.channels." + *keyIt + ".class", "").
            compare("FileChannel")) {

            config.setString("logging.channels." + *keyIt + ".path",
                Path(config.getString("logging.channels." + *keyIt + ".path", "") +
                    Path::separator()).makeAbsolute().
                    setFileName(
                        "UpSys" + prefix + 
                        config.getString("args.software_identifier")
                        + ".log").toString());
        }
    }

}
Beispiel #7
0
void AMFWriter::writeSimpleObject(const AMFSimpleObject& object) {
	beginObject();
	AbstractConfiguration::Keys keys;
	object.keys(keys);
	AbstractConfiguration::Keys::const_iterator it;
	for(it=keys.begin();it!=keys.end();++it) {
		string name = *it;
		int type = object.getInt(name+".type",-1);
		switch(type) {
			case AMF::Boolean:
				writeObjectProperty(name,object.getBool(name));
				break;
			case AMF::String:
				writeObjectProperty(name,object.getString(name));
				break;
			case AMF::Number:
				writeObjectProperty(name,object.getDouble(name));
				break;
			case AMF::Integer:
				writeObjectProperty(name,object.getInt(name));
				break;
			case AMF::Date: {
				Timestamp date((Timestamp::TimeVal)object.getDouble(name)*1000);
				writeObjectProperty(name,date);
				break;
			}
			case AMF::Null:
				writeObjectProperty(name);
				break;
			default:
				ERROR("Unknown AMFObject %d type",type);
		}
	}
	endObject();
}
Channel* LoggingConfigurator::createChannel(AbstractConfiguration* pConfig)
{
    AutoPtr<Channel> pChannel(LoggingFactory::defaultFactory().createChannel(pConfig->getString("class")));
    AutoPtr<Channel> pWrapper(pChannel);
    AbstractConfiguration::Keys props;
    pConfig->keys(props);
    for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
    {
        if (*it == "pattern")
        {
            AutoPtr<Formatter> pPatternFormatter(new PatternFormatter(pConfig->getString(*it)));
            pWrapper = new FormattingChannel(pPatternFormatter, pChannel);
        }
        else if (*it == "formatter")
        {
            AutoPtr<FormattingChannel> pFormattingChannel(new FormattingChannel(0, pChannel));
            if (pConfig->hasProperty("formatter.class"))
            {
                AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(*it));
                AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
                pFormattingChannel->setFormatter(pFormatter);
            }
            else pFormattingChannel->setProperty(*it, pConfig->getString(*it));
#if defined(__GNUC__) && __GNUC__ < 3
            pWrapper = pFormattingChannel.duplicate();
#else
            pWrapper = pFormattingChannel;
#endif
        }
    }
    return pWrapper.duplicate();
}
void LoggingConfigurator::configureFormatters(AbstractConfiguration* pConfig)
{
    AbstractConfiguration::Keys formatters;
    pConfig->keys(formatters);
    for (AbstractConfiguration::Keys::const_iterator it = formatters.begin(); it != formatters.end(); ++it)
    {
        AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(*it));
        AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
        LoggingRegistry::defaultRegistry().registerFormatter(*it, pFormatter);
    }
}
void LoggingConfigurator::configureChannel(Channel* pChannel, AbstractConfiguration* pConfig)
{
    AbstractConfiguration::Keys props;
    pConfig->keys(props);
    for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
    {
        if (*it != "pattern" && *it != "formatter" && *it != "class")
        {
            pChannel->setProperty(*it, pConfig->getString(*it));
        }
    }
}
Formatter* LoggingConfigurator::createFormatter(AbstractConfiguration* pConfig)
{
    AutoPtr<Formatter> pFormatter(LoggingFactory::defaultFactory().createFormatter(pConfig->getString("class")));
    AbstractConfiguration::Keys props;
    pConfig->keys(props);
    for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
    {
        if (*it != "class")
            pFormatter->setProperty(*it, pConfig->getString(*it));
    }
    return pFormatter.duplicate();
}
void LayeredConfigurationTest::testTwoLayers()
{
	AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
	AutoPtr<MapConfiguration> pMC1 = new MapConfiguration;
	AutoPtr<MapConfiguration> pMC2 = new MapConfiguration;
	
	pMC1->setString("prop1", "value1");
	pMC1->setString("prop2", "value2");
	pMC2->setString("prop2", "value3");
	pMC2->setString("prop3", "value4");
	
	pLC->add(pMC1, 0);
	pLC->addWriteable(pMC2, 1);

	AbstractConfiguration::Keys keys;
	pLC->keys(keys);
	assert (keys.size() == 3);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	
	assert (pLC->getString("prop1") == "value1");
	assert (pLC->getString("prop2") == "value2");
	assert (pLC->getString("prop3") == "value4");

	pLC->setString("prop4", "value4");
	assert (pLC->getString("prop4") == "value4");

	assert (!pMC1->hasProperty("prop4"));
	assert (pMC2->hasProperty("prop4"));

	pLC->setString("prop1", "value11");
	assert (pLC->getString("prop1") == "value1");
	assert (pMC2->getString("prop1") == "value11");
}
void ConfigurationMapperTest::testMapper4()
{
	AutoPtr<AbstractConfiguration> pConf = createConfiguration();
	AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("prop5", "", pConf);

	assert (pMapper->hasProperty("string1"));
	assert (pMapper->hasProperty("string2"));

	AbstractConfiguration::Keys keys;
	pMapper->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "string1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "string2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "sub1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "sub2") != keys.end());
	
	assert (pMapper->getString("string1") == "foo");
	assert (pMapper->getString("sub1.string1") == "FOO");
	
	pMapper->setString("string3", "baz");
	assert (pMapper->getString("string3") == "baz");
	assert (pConf->getString("prop5.string3") == "baz");

	pMapper->remove("string3");
	assert (!pMapper->hasProperty("string3"));
	assert (!pConf->hasProperty("prop5.string3"));
}
Beispiel #14
0
void Client::copyProperties(const AbstractConfiguration& abstractConfigs,const string& root) {
	AbstractConfiguration::Keys keys;
	abstractConfigs.keys(root,keys);
	AbstractConfiguration::Keys::const_iterator it;
	for(it=keys.begin();it!=keys.end();++it) {
		string key(root);
		if(!key.empty())
			key+=".";
		key += (*it);
		if(abstractConfigs.hasOption(key))
			setString(key,abstractConfigs.getString(key));
		else
			copyProperties(abstractConfigs,key);
	}
}
Beispiel #15
0
//============================================================================//
void Configuration::prepareApplicationLoggingDisableConsole(AbstractConfiguration &config)
{
    // find all files loggers
    AbstractConfiguration::Keys             channelsKeys;
    AbstractConfiguration::Keys::iterator   keyIt;

    config.keys("logging.channels", channelsKeys);

    for(keyIt = channelsKeys.begin(); keyIt != channelsKeys.end(); ++keyIt) {
        if(!config.getString("logging.channels." + *keyIt + ".class", "").
            compare("ConsoleChannel")) {

            config.setString("logging.channels." + *keyIt + ".class",
                "NullChannel");
        }
    }
}
void LoggingConfigurator::configureChannels(AbstractConfiguration* pConfig)
{
    AbstractConfiguration::Keys channels;
    pConfig->keys(channels);
    for (AbstractConfiguration::Keys::const_iterator it = channels.begin(); it != channels.end(); ++it)
    {
        AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
        AutoPtr<Channel> pChannel = createChannel(pChannelConfig);
        LoggingRegistry::defaultRegistry().registerChannel(*it, pChannel);
    }
    for (AbstractConfiguration::Keys::const_iterator it = channels.begin(); it != channels.end(); ++it)
    {
        AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
        Channel* pChannel = LoggingRegistry::defaultRegistry().channelForName(*it);
        configureChannel(pChannel, pChannelConfig);
    }
}
void IniFileConfigurationTest::testCaseInsensitiveRemove()
{
	AutoPtr<AbstractConfiguration> pConf = createConfiguration();
	AbstractConfiguration::Keys keys;

	assert (pConf->hasProperty("Prop1"));
	assert (pConf->hasProperty("prop4.BOOL1"));
	assert (pConf->hasProperty("Prop4.bool2"));
	assert (pConf->hasProperty("PROP4.Bool3"));
	pConf->keys(keys);
	assert (keys.size() == 13);
	pConf->keys("prop4", keys);
	assert (keys.size() == 17);

	pConf->remove("PROP4.Bool1");
	assert (pConf->hasProperty("Prop1"));
	assert (!pConf->hasProperty("prop4.BOOL1"));
	assert (pConf->hasProperty("Prop4.bool2"));
	assert (pConf->hasProperty("PROP4.Bool3"));
	pConf->keys(keys);
	assert (keys.size() == 13);
	pConf->keys("PROP4", keys);
	assert (keys.size() == 16);

	pConf->remove("Prop4");
	assert (pConf->hasProperty("Prop1"));
	assert (!pConf->hasProperty("prop4.BOOL1"));
	assert (!pConf->hasProperty("Prop4.bool2"));
	assert (!pConf->hasProperty("PROP4.Bool3"));
	pConf->keys(keys);
	assert (keys.size() == 12);
	pConf->keys("prop4", keys);
	assert (keys.size() == 0);
}
void LoggingConfigurator::configureLoggers(AbstractConfiguration* pConfig)
{
    typedef std::map<std::string, AutoPtr<AbstractConfiguration> > LoggerMap;

    AbstractConfiguration::Keys loggers;
    pConfig->keys(loggers);
    // use a map to sort loggers by their name, ensuring initialization in correct order (parents before children)
    LoggerMap loggerMap;
    for (AbstractConfiguration::Keys::const_iterator it = loggers.begin(); it != loggers.end(); ++it)
    {
        AutoPtr<AbstractConfiguration> pLoggerConfig(pConfig->createView(*it));
        loggerMap[pLoggerConfig->getString("name", "")] = pLoggerConfig;
    }
    for (LoggerMap::iterator it = loggerMap.begin(); it != loggerMap.end(); ++it)
    {
        configureLogger(it->second);
    }
}
//============================================================================//
void AnalyzerDispatcher::create(AbstractConfiguration &config)
{
    AbstractConfiguration::Keys             analyzersCodes;
    AbstractConfiguration::Keys::iterator   iter;

    config.keys("analyzers.list", analyzersCodes);

    for(iter = analyzersCodes.begin(); iter != analyzersCodes.end(); ++iter) {

        string type = config.getString("analyzers.list."+(*iter)+".type");

        if(config.has("analyzers.list."+(*iter)+".disabled")) {
            poco_information_f2(Logger::get(_loggerName),
                "analyzer type of [%s] with code [%s] disabled",
                type, string(*iter));
            continue;
        }

        if(_fabrics->isClass(type)) {

            poco_information_f1(Logger::get(_loggerName), "create analyzer for type [%s]", type);

            Analyzer::Arguments  args(*iter, config);
            Analyzer *a = _fabrics->create(type, args);

            poco_information_f2(Logger::get(_loggerName),
                "starting analyzer type of [%s] with code [%s]",
                type, string(a->getCode()));

            try {
                a->start();
                _analyzers.insert(pair<string, SharedPtr<Analyzer> >(*iter, a));
            }
            catch(exception &e) {
                delete a;
            }
        }
        else {
            poco_warning_f1(Logger::get(_loggerName), "unknown analyzer type [%s]... skip this.", type);
        }
    }

    poco_information_f1(Logger::get(_loggerName), "[%d] analyzers was registered", (int)_analyzers.size());
}
void LoggingConfigurator::configureLogger(AbstractConfiguration* pConfig)
{
    Logger& logger = Logger::get(pConfig->getString("name", ""));
    AbstractConfiguration::Keys props;
    pConfig->keys(props);
    for (AbstractConfiguration::Keys::const_iterator it = props.begin(); it != props.end(); ++it)
    {
        if (*it == "channel" && pConfig->hasProperty("channel.class"))
        {
            AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(*it));
            AutoPtr<Channel> pChannel(createChannel(pChannelConfig));
            configureChannel(pChannel, pChannelConfig);
            Logger::setChannel(logger.name(), pChannel);
        }
        else if (*it != "name")
        {
            Logger::setProperty(logger.name(), *it, pConfig->getString(*it));
        }
    }
}
Beispiel #21
0
void ConfigurationMapperTest::testMapper4()
{
	AutoPtr<AbstractConfiguration> pConf = createConfiguration();
	AutoPtr<AbstractConfiguration> pMapper = new ConfigurationMapper("config", "", pConf);

	assert (pMapper->hasProperty("value1"));
	assert (pMapper->hasProperty("value2"));

	AbstractConfiguration::Keys keys;
	pMapper->keys(keys);
	assert (keys.size() == 3);
	assert (std::find(keys.begin(), keys.end(), "value1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "value2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "sub") != keys.end());
	
	assert (pMapper->getString("value1") == "v1");
	assert (pMapper->getString("sub.value2") == "v4");
	
	pMapper->setString("value3", "v5");
	assert (pMapper->getString("value3") == "v5");
	assert (pConf->getString("config.value3") == "v5");
}
void IniFileConfigurationTest::testLoad()
{
	static const std::string iniFile = 
		"; comment\n"
		"  ; comment  \n"
		"prop1=value1\n"
		"  prop2 = value2  \n"
		"[section1]\n"
		"prop1 = value3\r\n"
		"\tprop2=value4\r\n"
		";prop3=value7\r\n"
		"\n"
		"  [ section 2 ]\n"
		"prop1 = value 5\n"
		"\t   \n"
		"Prop2 = value6";
		
	std::istringstream istr(iniFile);	
	AutoPtr<IniFileConfiguration> pConf = new IniFileConfiguration(istr);
	
	assert (pConf->getString("prop1") == "value1");
	assert (pConf->getString("prop2") == "value2");
	assert (pConf->getString("section1.prop1") == "value3");
	assert (pConf->getString("Section1.Prop2") == "value4");
	assert (pConf->getString("section 2.prop1") == "value 5");
	assert (pConf->getString("section 2.prop2") == "value6");
	assert (pConf->getString("SECTION 2.PROP2") == "value6");
	
	AbstractConfiguration::Keys keys;
	pConf->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section 2") != keys.end());
	
	pConf->keys("Section1", keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	
	try
	{
		pConf->setString("foo", "bar");
		fail("Not supported - must throw");
	}
	catch (NotImplementedException&)
	{
	}
	
	try
	{
		std::string s = pConf->getString("foo");
		fail("No property - must throw");
	}
	catch (NotFoundException&)
	{
	}
}
void IniFileConfigurationTest::testLoad()
{
	static const std::string iniFile = 
		"; comment\n"
		"  ; comment  \n"
		"prop1=value1\n"
		"  prop2 = value2  \n"
		"[section1]\n"
		"prop1 = value3\r\n"
		"\tprop2=value4\r\n"
		";prop3=value7\r\n"
		"\n"
		"  [ section 2 ]\n"
		"prop1 = value 5\n"
		"\t   \n"
		"Prop2 = value6";
		
	std::istringstream istr(iniFile);	
	AutoPtr<IniFileConfiguration> pConf = new IniFileConfiguration(istr);
	
	assert (pConf->getString("prop1") == "value1");
	assert (pConf->getString("prop2") == "value2");
	assert (pConf->getString("section1.prop1") == "value3");
	assert (pConf->getString("Section1.Prop2") == "value4");
	assert (pConf->getString("section 2.prop1") == "value 5");
	assert (pConf->getString("section 2.prop2") == "value6");
	assert (pConf->getString("SECTION 2.PROP2") == "value6");
	
	AbstractConfiguration::Keys keys;
	pConf->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section 2") != keys.end());
	
	pConf->keys("Section1", keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	
	pConf->setString("prop1", "value11");
	assert (pConf->getString("PROP1") == "value11");
	pConf->setString("Prop1", "value12");
	assert (pConf->getString("prop1") == "value12");
	pConf->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "section 2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "Prop1") == keys.end());
}
void ConfigurationViewTest::testView()
{
	AutoPtr<AbstractConfiguration> pConf = createConfiguration();
	AutoPtr<AbstractConfiguration> pView = pConf->createView("");
	assert (pView->hasProperty("prop1"));
	assert (pView->hasProperty("prop2"));

	AbstractConfiguration::Keys keys;
	pView->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
	
	assert (pView->getString("prop1") == "foo");
	assert (pView->getString("prop3.string1") == "foo");
	
	pView->setString("prop5", "foobar");
	assert (pConf->getString("prop5") == "foobar");
	
	pView = pConf->createView("prop1");
	pView->keys(keys);
	assert (keys.empty());
	assert (pView->hasProperty("prop1"));

	pView->setString("prop11", "foobar");
	assert (pConf->getString("prop1.prop11") == "foobar");

	pView = pConf->createView("prop3");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "string1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "string2") != keys.end());
	
	assert (pView->getString("string1") == "foo");
	assert (pView->getString("string2") == "bar");

	pView->setString("string3", "foobar");
	assert (pConf->getString("prop3.string3") == "foobar");

	pView = pConf->createView("prop4");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop41") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop42") != keys.end());
	
	assert (pView->getString("prop41.string1") == "FOO");
	assert (pView->getString("prop42.string2") == "Bar");

	pView = pConf->createView("prop4.prop41");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "string1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "string2") != keys.end());
	
	assert (pView->getString("string1") == "FOO");
	assert (pView->getString("string2") == "BAR");
	
	pView->setString("string3", "foobar");
	assert (pConf->getString("prop4.prop41.string3") == "foobar");
}
void PropertyFileConfigurationTest::testLoad()
{
	static const std::string propFile = 
		"! comment\n"
		"! comment\n"
		"prop1=value1\n"
		"prop2 = value2  \n"
		"prop3.prop31: value3\n"
		"# comment\n"
		"  prop3.prop32: value 4\n"
		"prop3.prop33: value5, value6, \\\n"
		"value7, value8, \\\r\n"
		"value9\n"
		"prop4 = escaped[\\t\\r\\n\\f]\n"
		"#prop4 = foo\n"
		"prop5:foo";
		
	std::istringstream istr(propFile);
	AutoPtr<PropertyFileConfiguration> pConf = new PropertyFileConfiguration(istr);
	
	assert (pConf->getString("prop1") == "value1");
	assert (pConf->getString("prop2") == "value2");
	assert (pConf->getString("prop3.prop31") == "value3");
	assert (pConf->getString("prop3.prop32") == "value 4");
	assert (pConf->getString("prop3.prop33") == "value5, value6, value7, value8, value9");
	assert (pConf->getString("prop4") == "escaped[\t\r\n\f]");
	assert (pConf->getString("prop5") == "foo");
	
	AbstractConfiguration::Keys keys;
	pConf->keys(keys);
	assert (keys.size() == 5);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
	
	pConf->keys("prop3", keys);
	assert (keys.size() == 3);
	assert (std::find(keys.begin(), keys.end(), "prop31") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop32") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop33") != keys.end());
	
	try
	{
		std::string s = pConf->getString("foo");
		fail("No property - must throw");
	}
	catch (NotFoundException&)
	{
	}
}
Beispiel #26
0
/// Строит необходимые логгеры
void BaseDaemon::buildLoggers()
{
	/// Сменим директорию для лога
	if (config().hasProperty("logger.log") && !log_to_console)
	{
		std::string path = createDirectory(config().getString("logger.log"));
		if (config().getBool("application.runAsDaemon", false)
			&& chdir(path.c_str()) != 0)
			throw Poco::Exception("Cannot change directory to " + path);
	}
	else
	{
		if (config().getBool("application.runAsDaemon", false)
			&& chdir("/tmp") != 0)
			throw Poco::Exception("Cannot change directory to /tmp");
	}

	if (config().hasProperty("logger.errorlog") && !log_to_console)
		createDirectory(config().getString("logger.errorlog"));

	if (config().hasProperty("logger.log") && !log_to_console)
	{
		std::cerr << "Should logs to " << config().getString("logger.log") << std::endl;

		// splitter
		Poco::AutoPtr<SplitterChannel> split = new SplitterChannel;

		// set up two channel chains
		Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
		pf->setProperty("times", "local");
		Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
		log_file = new FileChannel;
		log_file->setProperty("path", Poco::Path(config().getString("logger.log")).absolute().toString());
		log_file->setProperty("rotation", config().getRawString("logger.size", "100M"));
		log_file->setProperty("archive", "number");
		log_file->setProperty("compress", config().getRawString("logger.compress", "true"));
		log_file->setProperty("purgeCount", config().getRawString("logger.count", "1"));
		log->setChannel(log_file);
		split->addChannel(log);
		log_file->open();

		if (config().hasProperty("logger.errorlog"))
		{
			std::cerr << "Should error logs to " << config().getString("logger.errorlog") << std::endl;
			Poco::AutoPtr<Poco::LevelFilterChannel> level = new Poco::LevelFilterChannel;
			level->setLevel(Message::PRIO_NOTICE);
			Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
			pf->setProperty("times", "local");
			Poco::AutoPtr<FormattingChannel> errorlog = new FormattingChannel(pf);
			error_log_file = new FileChannel;
			error_log_file->setProperty("path", Poco::Path(config().getString("logger.errorlog")).absolute().toString());
			error_log_file->setProperty("rotation", config().getRawString("logger.size", "100M"));
			error_log_file->setProperty("archive", "number");
			error_log_file->setProperty("compress", config().getRawString("logger.compress", "true"));
			error_log_file->setProperty("purgeCount", config().getRawString("logger.count", "1"));
			errorlog->setChannel(error_log_file);
			level->setChannel(errorlog);
			split->addChannel(level);
			errorlog->open();
		}

		if (config().getBool("logger.use_syslog", false) || config().getBool("dynamic_layer_selection", false))
		{
			Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this, OwnPatternFormatter::ADD_LAYER_TAG);
			pf->setProperty("times", "local");
			Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
			syslog_channel = new Poco::SyslogChannel(commandName(), Poco::SyslogChannel::SYSLOG_CONS | Poco::SyslogChannel::SYSLOG_PID, Poco::SyslogChannel::SYSLOG_DAEMON);
			log->setChannel(syslog_channel);
			split->addChannel(log);
			syslog_channel->open();
		}

		split->open();
		logger().close();
		logger().setChannel(split);
	}
	else
	{
		// Выводим на консоль
		Poco::AutoPtr<ConsoleChannel> file = new ConsoleChannel;
		Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
		pf->setProperty("times", "local");
		Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
		log->setChannel(file);

		logger().close();
		logger().setChannel(log);
		logger().warning("Logging to console");
	}

	// Уровни для всех
	logger().setLevel(config().getString("logger.level", "trace"));

	// Прикрутим к корневому логгеру
	Logger::root().setLevel(logger().getLevel());
	Logger::root().setChannel(logger().getChannel());

	// Уровни для явно указанных логгеров
	AbstractConfiguration::Keys levels;
	config().keys("logger.levels", levels);

	if(!levels.empty())
		for(AbstractConfiguration::Keys::iterator it = levels.begin(); it != levels.end(); ++it)
			Logger::get(*it).setLevel(config().getString("logger.levels." + *it, "trace"));
}
Beispiel #27
0
void SystemConfigurationTest::testKeys()
{
	AutoPtr<SystemConfiguration> pConf = new SystemConfiguration;

	AbstractConfiguration::Keys keys;
	pConf->keys(keys);
	assert (keys.size() == 1);
	assert (std::find(keys.begin(), keys.end(), "system") != keys.end());

	pConf->keys("system", keys);
#if defined(POCO_VXWORKS)
	assert (keys.size() == 10);
#else
	assert (keys.size() == 11);
#endif
	assert (std::find(keys.begin(), keys.end(), "osName") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "osVersion") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "osArchitecture") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "nodeName") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "nodeId") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "currentDir") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "homeDir") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "tempDir") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "dateTime") != keys.end());
#if !defined(POCO_VXWORKS)
	assert (std::find(keys.begin(), keys.end(), "pid") != keys.end());
#endif
	assert (std::find(keys.begin(), keys.end(), "env") != keys.end());
}
void FilesystemConfigurationTest::testFilesystemConfiguration()
{
	AutoPtr<FilesystemConfiguration> config = new FilesystemConfiguration("TestConfiguration");
	
	config->setString("logging.loggers.root.channel.class", "ConsoleChannel");
	config->setString("logging.loggers.app.name", "Application");
	config->setString("logging.loggers.app.channel", "c1");
	config->setString("logging.formatters.f1.class", "PatternFormatter");
	config->setString("logging.formatters.f1.pattern", "[%p] %t");
	config->setString("logging.channels.c1.class", "ConsoleChannel");

	assert (config->getString("logging.loggers.root.channel.class") == "ConsoleChannel");
	assert (config->getString("logging.loggers.app.name") == "Application");
	assert (config->getString("logging.loggers.app.channel") == "c1");
	assert (config->getString("logging.formatters.f1.class") == "PatternFormatter");
	assert (config->getString("logging.formatters.f1.pattern") == "[%p] %t");

	config->setString("logging.loggers.app.channel", "c2");
	assert (config->getString("logging.loggers.app.channel") == "c2");
	
	AbstractConfiguration::Keys keys;
	config->keys(keys);
	assert (keys.size() == 1);
	assert (std::find(keys.begin(), keys.end(), "logging") != keys.end());

	config->keys("logging", keys);
	assert (keys.size() == 3);
	assert (std::find(keys.begin(), keys.end(), "loggers") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "formatters") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "channels") != keys.end());

	config->keys("logging.formatters", keys);
	assert (keys.size() == 1);
	assert (std::find(keys.begin(), keys.end(), "f1") != keys.end());

	config->keys("logging.formatters.f1", keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "class") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "pattern") != keys.end());
	
	assert (config->hasProperty("logging.loggers.root.channel.class"));
	config->clear();
	assert (!config->hasProperty("logging.loggers.root.channel.class"));
}
Beispiel #29
0
void XMLConfigurationTest::testLoad()
{
	static const std::string xmlFile = 
		"<config>"
		"	<prop1>value1</prop1>"
		"	<prop2>value2</prop2>"
		"	<prop3>"
		"		<prop4 attr='value3'/>"
		"		<prop4 attr='value4'/>"
		"	</prop3>"
		"	<prop5 id='1'>value5</prop5>"
		"	<prop5 id='2'>value6</prop5>"
		"   <prop6 id='foo'>"
		"       <prop7>value7</prop7>"
		"   </prop6>"
		"   <prop6 id='bar'>"
		"       <prop7>value8</prop7>"
		"   </prop6>"
		"</config>";
		
	std::istringstream istr(xmlFile);	
	AutoPtr<XMLConfiguration> pConf = new XMLConfiguration(istr);
	
	assert (pConf->getString("prop1") == "value1");
	assert (pConf->getString("prop2") == "value2");
	assert (pConf->getString("prop3.prop4[@attr]") == "value3");
	assert (pConf->getString("prop3.prop4[1][@attr]") == "value4");
	assert (pConf->getString("prop5") == "value5");
	assert (pConf->getString("prop5[0]") == "value5");
	assert (pConf->getString("prop5[1]") == "value6");
	assert (pConf->getString("prop5[@id=1]") == "value5");
	assert (pConf->getString("prop5[@id='2']") == "value6");
	assert (pConf->getString("prop6[@id=foo].prop7") == "value7");
	assert (pConf->getString("prop6[@id='bar'].prop7") == "value8");
	
	AbstractConfiguration::Keys keys;
	pConf->keys(keys);
	assert (keys.size() == 7);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop5") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop5[1]") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop6") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop6[1]") != keys.end());
	
	pConf->keys("prop3", keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop4[1]") != keys.end());

	assert (pConf->hasProperty("prop3.prop4[@attr]"));
	pConf->remove("prop3.prop4[@attr]");
	assert (!pConf->hasProperty("prop3.prop4[@attr]"));

	assert (pConf->hasProperty("prop3"));
	pConf->remove("prop3");
	assert (!pConf->hasProperty("prop3"));
	
	try
	{
		std::string s = pConf->getString("foo");
		fail("No property - must throw");
	}
	catch (NotFoundException&)
	{
	}

	try
	{
		std::string s = pConf->getString("prop5[@id='3']");
		fail("No property - must throw");
	}
	catch (NotFoundException&)
	{
	}
}
Beispiel #30
0
void XplDevice::LoadConfig()
{

    poco_trace ( devLog, "loading config for  "  + GetCompleteId() );
    
    Poco::Path p = GetConfigFileLocation();

    PropertyFileConfiguration* cfgp;
    try{
        cfgp =  new PropertyFileConfiguration(p.toString());
    } catch (Poco::FileException e) {
        poco_debug ( devLog, "Failed to parse  " + p.toString() );
        cfgp = (new PropertyFileConfiguration());
    }
    m_configStore = cfgp;
        
    m_bConfigRequired = true;
  
    
    AbstractConfiguration::Keys itemKeys;
    m_configStore->keys(itemKeys);
    for ( AbstractConfiguration::Keys::iterator iter = itemKeys.begin(); iter != itemKeys.end(); ++iter )
    {
        poco_debug ( devLog, " item: " + *iter );
    }
    
    
    
    if ( m_configStore->hasProperty ( "instanceId" ) )
    {
        //looks like we really have a config
        poco_debug ( devLog, "found instance ID" );
        m_bConfigRequired = false;
        SetInstanceId ( m_configStore->getString ( "instanceId" ) );
        
        if ( m_configStore->hasProperty ( "configItems" )) {
            AbstractConfiguration::Keys confItemKeys;
            m_configStore->keys("configItems", confItemKeys);
            poco_debug ( devLog, "found " + NumberFormatter::format(confItemKeys.size()) + "keys" );
            for ( AbstractConfiguration::Keys::iterator iter = confItemKeys.begin(); iter != confItemKeys.end(); ++iter )
            {
                if(m_configStore->hasProperty ( "configItems."+(*iter)+".numValues" )){
                    // we have a config item entry with the required parts, but should only restore it if it matches a programatically-added configitem.
                    AutoPtr<XplConfigItem> cfgItem = GetConfigItem(*iter);
                    if (cfgItem.get()== NULL){
                        poco_warning ( devLog, "Found a config item for name " + *iter + ", but no programatically-created config item exists with that name" );
                        continue;
                    }
                    poco_trace ( devLog, "Config item: " + *iter );
                    int numValues = m_configStore->getInt ( "configItems."+(*iter)+".numValues");
                    
                    for (int i= 0; i<numValues; i++) {
                        poco_trace( devLog, "Value " );
                        string valname = "configItems."+(*iter)+".value" + NumberFormatter::format(i);
                        if(m_configStore->hasProperty (valname) ){
                            cfgItem->AddValue(m_configStore->getString ( valname ));
                        }
                    }
                }
            }
            
        }
    }
  
// 	// If the config data was read ok, then configure this device.
	if( !m_bConfigRequired )
	{
    // Configure the XplDevice
    Configure();
    m_bConfigRequired = false;

        // Set the config event
		//SetEvent( m_hConfig );
//     m_hConfig.notifyAsync(NULL, m_hConfig);
	}
}