All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSourceManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/datasource/DataSourceManager.cpp
22 
23  \brief This is a singleton for managing all data source instances available in the system.
24 */
25 
26 // TerraLib
27 #include "../../common/Globals.h"
28 #include "../../common/STLUtils.h"
29 #include "../../common/StringUtils.h"
30 #include "../../common/Translator.h"
31 #include "../Exception.h"
32 #include "DataSource.h"
33 #include "DataSourceFactory.h"
34 #include "DataSourceManager.h"
35 
36 // STL
37 #include <algorithm>
38 #include <cassert>
39 #include <memory>
40 
41 te::da::DataSourcePtr te::da::DataSourceManager::make(const std::string& id, const std::string& dsType)
42 {
43  if(find(id))
44  return find(id);
45 
46 // we are optimistic: do the hard job and then see if another thread or the data source already had been inserted in the manager
48 
49  if(ds.get() == 0)
50  throw Exception(TE_TR("Could not create the required data source instance!"));
51 
52  ds->setId(id);
53 
54  insert(ds);
55 
56  return ds;
57 }
58 
59 te::da::DataSourcePtr te::da::DataSourceManager::open(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo)
60 {
61 // we are optimistic: do the hard job and then see if another thread or the data source already had been inserted in the manager
63 
64  if(ds.get() == 0)
65  throw Exception(TE_TR("Could not create the required data source instance!"));
66 
67  ds->setConnectionInfo(connInfo);
68  ds->open();
69 
70  ds->setId(id);
71 
72  insert(ds);
73 
74  return ds;
75 }
76 
77 te::da::DataSourcePtr te::da::DataSourceManager::open(const std::string& id, const std::string& dsType, const std::string& connInfo)
78 {
79  std::map<std::string, std::string> connInfoMap;
80 
81  te::common::ExtractKVP(connInfo, connInfoMap, "&", "=", false);
82 
83  return open(id, dsType, connInfoMap);
84 }
85 
86 te::da::DataSourcePtr te::da::DataSourceManager::get(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo)
87 {
88  LockRead l(this);
89 
90  const_iterator it = m_dss.find(id);
91 
92  if(it != m_dss.end())
93  return it->second;
94 
95  DataSourcePtr newds(DataSourceFactory::make(dsType).release());
96 
97  newds->setConnectionInfo(connInfo);
98 
99  newds->open();
100 
101  newds->setId(id);
102 
103  insert(newds);
104 
105  return newds;
106 }
107 
109 {
110  LockRead l(this);
111 
112  const_iterator it = m_dss.find(id);
113 
114  if(it != m_dss.end())
115  return it->second;
116  else
117  return DataSourcePtr();
118 }
119 
121 {
122  if(ds.get() == 0)
123  throw Exception(TE_TR("Please, specifify a non-null data source to be managed!"));
124 
125  LockWrite l(this);
126 
127  if(m_dss.find(ds->getId()) != m_dss.end())
128  throw Exception(TE_TR("There is already a data source with the given identification!"));
129 
130  m_dss[ds->getId()] = ds;
131 }
132 
134 {
135  if(ds.get() == 0)
136  return;
137 
138  detach(ds->getId());
139 }
140 
142 {
143  LockWrite l(this);
144 
145  std::map<std::string, DataSourcePtr>::iterator it = m_dss.find(id);
146 
147  if(it == m_dss.end())
148  throw Exception(TE_TR("Invalid data source to detach!"));
149 
150  DataSourcePtr ds = it->second;
151 
152  m_dss.erase(it);
153 
154  return ds;
155 }
156 
157 void te::da::DataSourceManager::detachAll(const std::string& dsType)
158 {
159  LockWrite l(this);
160 
161  std::map<std::string, DataSourcePtr>::iterator it = m_dss.begin();
162 
163  while(it != m_dss.end())
164  if(it->second->getType() == dsType)
165  m_dss.erase(it++);
166  else
167  ++it;
168  //std::map<std::string, DataSourcePtr>::iterator it = m_dss.begin();
169  //std::map<std::string, DataSourcePtr>::iterator itend = m_dss.end();
170 
171  //while(it != itend)
172  //{
173  // const std::string& ttype = it->second->getType();
174 
175  // if(it->second->getType() == dsType)
176  // {
177  // std::map<std::string, DataSourcePtr>::iterator itaux = it;
178  // ++it;
179  // m_dss.erase(itaux);
180  // }
181  // else
182  // {
183  // ++it;
184  // }
185  //}
186 }
187 
189 {
190  LockWrite l(this);
191 
192  m_dss.clear();
193 }
194 
196 {
197 }
198 
200 {
201 }
202 
~DataSourceManager()
Singleton destructor.
DataSourcePtr get(const std::string &id, const std::string &dsType, const std::map< std::string, std::string > &connInfo)
It searches for an opened data source with the given id or it opens a new one if it doesn't exists...
void detach(const DataSourcePtr &ds)
It changes the ownership of the data source to the caller.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
DataSourceManager()
It initializes the singleton instance of the data source manager.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
This is a singleton for managing all data source instances available in the system.
void ExtractKVP(const std::string &kvpStr, std::map< std::string, std::string > &kvp, const std::string &kvpDelimiter="&", const std::string &kvDelimiter="=", bool toUpper=false)
It extracts a key-value map from a string.
Definition: StringUtils.h:251
DataSourcePtr find(const std::string &id) const
It returns the data source identified by the given id.
void detachAll()
All data sources are detached from the manager.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
DataSourcePtr open(const std::string &id, const std::string &dsType, const std::map< std::string, std::string > &connInfo)
It opens the data source, makes it ready for use, stores a reference to it in the manager and returns...
static std::auto_ptr< DataSource > make(const std::string &dsType)
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
void insert(const DataSourcePtr &ds)
It stores the data source in the manager.
A factory for data sources.
DataSourcePtr make(const std::string &id, const std::string &dsType)
It creates a new data source, stores a reference to it in the manager and then returns a pointer to i...
std::map< std::string, DataSourcePtr >::const_iterator const_iterator