<commit_before>/*
 * Copyright 2010 MQWeb - <NAME>
 *
 * Licensed under the EUPL, Version 1.1 or  as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the
 * Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

#include "MQ/QueueManagerPool.h"

namespace MQ
{

QueueManagerFactory::QueueManagerFactory(const std::string& qmgrName)
: _qmgrName(qmgrName)
{
}

QueueManagerFactory::QueueManagerFactory(const std::string& qmgrName, const Poco::Dynamic::Struct<std::string>& connectionInformation)
: _qmgrName(qmgrName)
, _connectionInformation(connectionInformation)
{
}

QueueManagerFactory::QueueManagerFactory(const QueueManagerFactory& copy)
: _qmgrName(copy._qmgrName)
, _connectionInformation(copy._connectionInformation)
{
}

QueueManagerFactory::~QueueManagerFactory()
{
}

QueueManager::Ptr QueueManagerFactory::createObject()
{
	return new QueueManager(_qmgrName);
}

void QueueManagerFactory::activateObject(QueueManager::Ptr qmgr)
{
	if ( !qmgr->connected() )
	{
		if ( _connectionInformation.size() == 0 )
		{
			qmgr->connect();
		}
		else
		{
			qmgr->connect(_connectionInformation);
		}
	}
}

bool QueueManagerFactory::validateObject(QueueManager::Ptr pObject)
{
	return pObject->connected();
}

void QueueManagerFactory::deactivateObject(QueueManager::Ptr pObject)
{
}

void QueueManagerFactory::destroyObject(QueueManager::Ptr pObject)
{
	pObject->disconnect();
}

QueueManagerPool::QueueManagerPool(Poco::SharedPtr<QueueManagerFactory> factory,
	std::size_t capacity,
	std::size_t peakCapacity,
	int idleTime) :
	_factory(factory),
	_capacity(capacity),
	_peakCapacity(peakCapacity),
	_size(0),
	_idleTime(idleTime),
	_janitorTimer(1000 * idleTime, 1000 * idleTime / 4)
{
	poco_assert(capacity <= peakCapacity);
	Poco::TimerCallback<QueueManagerPool> callback(*this, &QueueManagerPool::onJanitorTimer);
	_janitorTimer.start(callback);
}

QueueManagerPool::~QueueManagerPool()
{
	for (std::vector<Poco::SharedPtr<TimedQueueManager> >::iterator it = _pool.begin(); it != _pool.end(); ++it)
	{
		_factory->destroyObject((*it)->value());
	}
}

QueueManager::Ptr QueueManagerPool::borrowObject()
{<commit_msg>Don't throw an exception in destroyObject<commit_after>/*
 * Copyright 2010 MQWeb - <NAME>
 *
 * Licensed under the EUPL, Version 1.1 or  as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the
 * Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

#include "MQ/QueueManagerPool.h"

namespace MQ
{

QueueManagerFactory::QueueManagerFactory(const std::string& qmgrName)
: _qmgrName(qmgrName)
{
}

QueueManagerFactory::QueueManagerFactory(const std::string& qmgrName, const Poco::Dynamic::Struct<std::string>& connectionInformation)
: _qmgrName(qmgrName)
, _connectionInformation(connectionInformation)
{
}

QueueManagerFactory::QueueManagerFactory(const QueueManagerFactory& copy)
: _qmgrName(copy._qmgrName)
, _connectionInformation(copy._connectionInformation)
{
}

QueueManagerFactory::~QueueManagerFactory()
{
}

QueueManager::Ptr QueueManagerFactory::createObject()
{
	return new QueueManager(_qmgrName);
}

void QueueManagerFactory::activateObject(QueueManager::Ptr qmgr)
{
	if ( !qmgr->connected() )
	{
		if ( _connectionInformation.size() == 0 )
		{
			qmgr->connect();
		}
		else
		{
			qmgr->connect(_connectionInformation);
		}
	}
}

bool QueueManagerFactory::validateObject(QueueManager::Ptr pObject)
{
	return pObject->connected();
}

void QueueManagerFactory::deactivateObject(QueueManager::Ptr pObject)
{
}

void QueueManagerFactory::destroyObject(QueueManager::Ptr pObject)
{
	try
	{
		pObject->disconnect();
	}
	catch(...)
	{
	}
}

QueueManagerPool::QueueManagerPool(Poco::SharedPtr<QueueManagerFactory> factory,
	std::size_t capacity,
	std::size_t peakCapacity,
	int idleTime) :
	_factory(factory),
	_capacity(capacity),
	_peakCapacity(peakCapacity),
	_size(0),
	_idleTime(idleTime),
	_janitorTimer(1000 * idleTime, 1000 * idleTime / 4)
{
	poco_assert(capacity <= peakCapacity);
	Poco::TimerCallback<QueueManagerPool> callback(*this, &QueueManagerPool::onJanitorTimer);
	_janitorTimer.start(callback);
}

QueueManagerPool::~QueueManagerPool()
{
	for (std::vector<Poco::SharedPtr<TimedQueueManager> >::iterator it = _pool.begin(); it != _pool.end(); ++it)
	{
		_factory->destroyObject((*it)->value());
	}
}

QueueManager::Ptr QueueManagerPool::borrowObject()
{
{'fraction_non_alphanumeric': 0.12099509988692046, 'fraction_numerical': 0.006407840180927252, 'mean_word_length': 6.152291105121294, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4981506', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1679, 'n_words': 377}