blob: 3cb2ed12be863762d660d9b84182c7799c49fce6 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/config/propertysetter.h>
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/spi/optionhandler.h>
#include <log4cxx/helpers/properties.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/appender.h>
using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;
using namespace LOG4CXX_NS::spi;
using namespace LOG4CXX_NS::config;
PropertySetter::PropertySetter(const helpers::ObjectPtr& obj1) : obj(obj1)
{
}
void PropertySetter::setProperties(const helpers::ObjectPtr& obj, helpers::Properties& properties, const LogString& prefix)
{
PropertySetter(obj).setProperties(properties, prefix);
}
void PropertySetter::setProperties(helpers::Properties& properties, const LogString& prefix)
{
size_t len = prefix.length();
for (auto key : properties.propertyNames())
{
// handle only properties that start with the desired prefix.
if (key.find(prefix) == 0)
{
// ignore key if it contains dots after the prefix
if (key.find(0x2E /* '.' */, len + 1) != LogString::npos)
{
continue;
}
LogString value = OptionConverter::findAndSubst(key, properties);
key = key.substr(len);
if (key == LOG4CXX_STR("layout")
&& obj != 0
&& obj->instanceof(Appender::getStaticClass()))
{
continue;
}
setProperty(key, value);
}
}
activate();
}
void PropertySetter::setProperty(const LogString& option, const LogString& value)
{
if (value.empty())
{
return;
}
if (obj != 0 && obj->instanceof(OptionHandler::getStaticClass()))
{
if (LogLog::isDebugEnabled())
{
// Do not echo secret-bearing option values (e.g. the ODBCAppender
// "Password", SMTPAppender "SMTPPassword" or DBAppender
// "DriverParams" options) into diagnostic output: internal debug
// output routinely flows into console/log pipelines with weaker
// access control than the configuration file itself.
LogString lowerOption(StringHelper::toLowerCase(option));
bool sensitive =
lowerOption.find(LOG4CXX_STR("password")) != LogString::npos ||
lowerOption.find(LOG4CXX_STR("driverparams")) != LogString::npos;
LogLog::debug(LOG4CXX_STR("Setting option name=[") +
option + LOG4CXX_STR("], value=[") +
(sensitive ? LogString(LOG4CXX_STR("****")) : value) +
LOG4CXX_STR("]"));
}
OptionHandlerPtr handler = LOG4CXX_NS::cast<OptionHandler>(obj);
handler->setOption(option, value);
}
}
void PropertySetter::activate()
{
if (obj != 0 && obj->instanceof(OptionHandler::getStaticClass()))
{
OptionHandlerPtr handler = LOG4CXX_NS::cast<OptionHandler>(obj);
handler->activateOptions();
}
}