2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
//$Id: connection_manager.hpp 17 2005-03-08 23:58:43Z pavlenko $
|
|
|
|
|
|
|
|
#ifndef CONNECTION_MANAGER_HPP
|
|
|
|
#define CONNECTION_MANAGER_HPP
|
|
|
|
|
|
|
|
#include <string>
|
2006-10-05 11:00:36 +02:00
|
|
|
#include <mapnik/pool.hpp>
|
|
|
|
#include <mapnik/utils.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
#include "connection.hpp"
|
2005-12-12 14:15:33 +01:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2005-12-23 13:31:54 +01:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
using namespace mapnik;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ConnectionCreator
|
|
|
|
{
|
|
|
|
string url_;
|
2006-11-17 22:10:28 +01:00
|
|
|
string port_;
|
2005-06-14 17:06:59 +02:00
|
|
|
string dbname_;
|
|
|
|
string user_;
|
|
|
|
string pass_;
|
|
|
|
public:
|
2006-11-17 22:10:28 +01:00
|
|
|
ConnectionCreator(string const& url,
|
|
|
|
string const& port,
|
|
|
|
string const& dbname,
|
|
|
|
string const& user,
|
|
|
|
string const& pass)
|
|
|
|
: url_(url),
|
|
|
|
port_(port),
|
|
|
|
dbname_(dbname),
|
|
|
|
user_(user),
|
|
|
|
pass_(pass) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
T* operator()() const
|
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
return new T(url_,port_,dbname_,user_,pass_);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
std::string id() const
|
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
return url_ + ":" + dbname_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
|
|
|
|
{
|
|
|
|
|
|
|
|
friend class CreateStatic<ConnectionManager>;
|
|
|
|
typedef Pool<Connection,ConnectionCreator> PoolType;
|
2005-12-12 14:15:33 +01:00
|
|
|
typedef std::map<std::string,boost::shared_ptr<PoolType> > ContType;
|
|
|
|
typedef boost::shared_ptr<Connection> HolderType;
|
2005-06-14 17:06:59 +02:00
|
|
|
ContType pools_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2006-11-27 22:13:15 +01:00
|
|
|
bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
|
|
|
if (pools_.find(creator.id())==pools_.end())
|
|
|
|
{
|
|
|
|
return pools_.insert(std::make_pair(creator.id(),
|
|
|
|
boost::shared_ptr<PoolType>(new PoolType(creator,initialSize,maxSize)))).second;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-11-17 22:10:28 +01:00
|
|
|
return false;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
}
|
2006-11-17 22:10:28 +01:00
|
|
|
|
2006-11-27 22:13:15 +01:00
|
|
|
boost::shared_ptr<PoolType> getPool(std::string const& key)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
|
|
|
ContType::const_iterator itr=pools_.find(key);
|
|
|
|
if (itr!=pools_.end())
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
static const boost::shared_ptr<PoolType> emptyPool;
|
|
|
|
return emptyPool;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2006-11-27 22:13:15 +01:00
|
|
|
HolderType get(std::string const& key)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-11-17 22:10:28 +01:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
|
|
|
ContType::const_iterator itr=pools_.find(key);
|
|
|
|
if (itr!=pools_.end())
|
|
|
|
{
|
|
|
|
boost::shared_ptr<PoolType> pool=itr->second;
|
|
|
|
return pool->borrowObject();
|
|
|
|
}
|
2006-11-27 22:13:15 +01:00
|
|
|
return HolderType();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-11-27 22:13:15 +01:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
private:
|
|
|
|
ConnectionManager() {}
|
|
|
|
ConnectionManager(const ConnectionManager&);
|
|
|
|
ConnectionManager& operator=(const ConnectionManager);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CONNECTION_MANAGER_HPP
|