2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00: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 09:00:36 +00:00
|
|
|
#include <mapnik/pool.hpp>
|
|
|
|
#include <mapnik/utils.hpp>
|
2005-06-14 15:06:59 +00:00
|
|
|
#include "connection.hpp"
|
2005-12-12 13:15:33 +00:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2007-06-12 08:59:54 +00:00
|
|
|
#include <boost/optional.hpp>
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2008-02-04 16:12:13 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
using boost::mutex;
|
|
|
|
#endif
|
|
|
|
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::Pool;
|
|
|
|
using mapnik::singleton;
|
|
|
|
using mapnik::CreateStatic;
|
2005-06-14 15:06:59 +00:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ConnectionCreator
|
|
|
|
{
|
2007-01-16 15:22:49 +00:00
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
public:
|
2010-06-25 15:23:09 +00:00
|
|
|
ConnectionCreator(boost::optional<string> const& host,
|
|
|
|
boost::optional<string> const& port,
|
|
|
|
boost::optional<string> const& dbname,
|
|
|
|
boost::optional<string> const& user,
|
2010-07-02 11:42:35 +00:00
|
|
|
boost::optional<string> const& pass,
|
|
|
|
boost::optional<string> const& connect_timeout)
|
2010-06-25 15:23:09 +00:00
|
|
|
: host_(host),
|
|
|
|
port_(port),
|
|
|
|
dbname_(dbname),
|
|
|
|
user_(user),
|
2010-07-02 11:42:35 +00:00
|
|
|
pass_(pass),
|
|
|
|
connect_timeout_(connect_timeout) {}
|
2007-06-12 08:59:54 +00:00
|
|
|
|
2010-06-25 15:23:09 +00:00
|
|
|
T* operator()() const
|
|
|
|
{
|
|
|
|
return new T(connection_string());
|
|
|
|
}
|
2007-06-12 08:59:54 +00:00
|
|
|
|
2010-06-25 15:23:09 +00:00
|
|
|
inline std::string id() const
|
|
|
|
{
|
|
|
|
return connection_string();
|
|
|
|
}
|
2007-06-12 08:59:54 +00:00
|
|
|
|
2010-06-25 15:23:09 +00:00
|
|
|
inline std::string connection_string() const
|
|
|
|
{
|
|
|
|
std::string connect_str;
|
|
|
|
if (host_ && (*host_).size()) connect_str += "host=" + *host_;
|
|
|
|
if (port_ && (*port_).size()) connect_str += " port=" + *port_;
|
|
|
|
if (dbname_ && (*dbname_).size()) connect_str += " dbname=" + *dbname_;
|
|
|
|
if (user_ && (*user_).size()) connect_str += " user=" + *user_;
|
|
|
|
if (pass_ && (*pass_).size()) connect_str += " password=" + *pass_;
|
2010-07-02 11:42:35 +00:00
|
|
|
if (connect_timeout_ && (*connect_timeout_).size())
|
2010-07-02 11:52:11 +00:00
|
|
|
connect_str +=" connect_timeout=" + *connect_timeout_;
|
2010-06-25 15:23:09 +00:00
|
|
|
return connect_str;
|
|
|
|
}
|
2007-06-12 08:59:54 +00:00
|
|
|
|
2007-01-16 15:22:49 +00:00
|
|
|
private:
|
2010-06-25 15:23:09 +00:00
|
|
|
boost::optional<string> host_;
|
|
|
|
boost::optional<string> port_;
|
|
|
|
boost::optional<string> dbname_;
|
|
|
|
boost::optional<string> user_;
|
|
|
|
boost::optional<string> pass_;
|
2010-07-02 11:42:35 +00:00
|
|
|
boost::optional<string> connect_timeout_;
|
2005-06-14 15:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
|
|
|
|
{
|
|
|
|
|
|
|
|
friend class CreateStatic<ConnectionManager>;
|
|
|
|
typedef Pool<Connection,ConnectionCreator> PoolType;
|
2005-12-12 13:15:33 +00:00
|
|
|
typedef std::map<std::string,boost::shared_ptr<PoolType> > ContType;
|
|
|
|
typedef boost::shared_ptr<Connection> HolderType;
|
2005-06-14 15:06:59 +00:00
|
|
|
ContType pools_;
|
|
|
|
|
|
|
|
public:
|
2010-06-25 15:23:09 +00:00
|
|
|
|
2006-11-27 21:13:15 +00:00
|
|
|
bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
|
2010-06-25 15:23:09 +00:00
|
|
|
{
|
2008-02-04 16:12:13 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2006-11-17 21:10:28 +00:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 16:12:13 +00:00
|
|
|
#endif
|
2006-11-17 21:10:28 +00:00
|
|
|
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 15:06:59 +00:00
|
|
|
|
2006-11-17 21:10:28 +00:00
|
|
|
return false;
|
2010-06-25 15:23:09 +00:00
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2006-11-17 21:10:28 +00:00
|
|
|
|
2006-11-27 21:13:15 +00:00
|
|
|
boost::shared_ptr<PoolType> getPool(std::string const& key)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2008-02-04 16:12:13 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2006-11-17 21:10:28 +00:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 16:12:13 +00:00
|
|
|
#endif
|
2006-11-17 21:10:28 +00:00
|
|
|
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 15:06:59 +00:00
|
|
|
}
|
2010-06-25 15:23:09 +00:00
|
|
|
|
2006-11-27 21:13:15 +00:00
|
|
|
HolderType get(std::string const& key)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2008-02-04 16:12:13 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2006-11-17 21:10:28 +00:00
|
|
|
mutex::scoped_lock lock(mutex_);
|
2008-02-04 16:12:13 +00:00
|
|
|
#endif
|
2006-11-17 21:10:28 +00:00
|
|
|
ContType::const_iterator itr=pools_.find(key);
|
|
|
|
if (itr!=pools_.end())
|
|
|
|
{
|
|
|
|
boost::shared_ptr<PoolType> pool=itr->second;
|
|
|
|
return pool->borrowObject();
|
|
|
|
}
|
2006-11-27 21:13:15 +00:00
|
|
|
return HolderType();
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
ConnectionManager() {}
|
2007-11-08 21:15:45 +00:00
|
|
|
private:
|
2005-06-14 15:06:59 +00:00
|
|
|
ConnectionManager(const ConnectionManager&);
|
|
|
|
ConnectionManager& operator=(const ConnectionManager);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CONNECTION_MANAGER_HPP
|