All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSourceManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 // we are optimistic: do the hard job and then see if another thread or the data source already had been inserted in the manager
45 
46  if(ds.get() == 0)
47  throw Exception(TR_DATAACCESS("Could not create the required data source instance!"));
48 
49  ds->setId(id);
50 
51  insert(ds);
52 
53  return ds;
54 }
55 
56 te::da::DataSourcePtr te::da::DataSourceManager::open(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo)
57 {
58 // we are optimistic: do the hard job and then see if another thread or the data source already had been inserted in the manager
60 
61  if(ds.get() == 0)
62  throw Exception(TR_DATAACCESS("Could not create the required data source instance!"));
63 
64  ds->setConnectionInfo(connInfo);
65  ds->open();
66 
67  ds->setId(id);
68 
69  insert(ds);
70 
71  return ds;
72 }
73 
74 te::da::DataSourcePtr te::da::DataSourceManager::open(const std::string& id, const std::string& dsType, const std::string& connInfo)
75 {
76  std::map<std::string, std::string> connInfoMap;
77 
78  te::common::ExtractKVP(connInfo, connInfoMap, "&", "=", false);
79 
80  return open(id, dsType, connInfoMap);
81 }
82 
83 te::da::DataSourcePtr te::da::DataSourceManager::get(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo)
84 {
85  LockRead l(this);
86 
87  const_iterator it = m_dss.find(id);
88 
89  if(it != m_dss.end())
90  return it->second;
91 
92  DataSourcePtr newds(DataSourceFactory::make(dsType).release());
93 
94  newds->setConnectionInfo(connInfo);
95 
96  newds->open();
97 
98  newds->setId(id);
99 
100  insert(newds);
101 
102  return newds;
103 }
104 
106 {
107  LockRead l(this);
108 
109  const_iterator it = m_dss.find(id);
110 
111  if(it != m_dss.end())
112  return it->second;
113  else
114  return DataSourcePtr();
115 }
116 
118 {
119  if(ds.get() == 0)
120  throw Exception(TR_DATAACCESS("Please, specifify a non-null data source to be managed!"));
121 
122  LockWrite l(this);
123 
124  if(m_dss.find(ds->getId()) != m_dss.end())
125  throw Exception(TR_DATAACCESS("There is already a data source with the given identification!"));
126 
127  m_dss[ds->getId()] = ds;
128 }
129 
131 {
132  if(ds.get() == 0)
133  return;
134 
135  detach(ds->getId());
136 }
137 
139 {
140  LockWrite l(this);
141 
142  std::map<std::string, DataSourcePtr>::iterator it = m_dss.find(id);
143 
144  if(it == m_dss.end())
145  throw Exception(TR_DATAACCESS("Invalid data source to detach!"));
146 
147  DataSourcePtr ds = it->second;
148 
149  m_dss.erase(it);
150 
151  return ds;
152 }
153 
154 void te::da::DataSourceManager::detachAll(const std::string& dsType)
155 {
156  LockWrite l(this);
157 
158  std::map<std::string, DataSourcePtr>::iterator it = m_dss.begin();
159 
160  while(it != m_dss.end())
161  if(it->second->getType() == dsType)
162  m_dss.erase(it++);
163  else
164  ++it;
165  //std::map<std::string, DataSourcePtr>::iterator it = m_dss.begin();
166  //std::map<std::string, DataSourcePtr>::iterator itend = m_dss.end();
167 
168  //while(it != itend)
169  //{
170  // const std::string& ttype = it->second->getType();
171 
172  // if(it->second->getType() == dsType)
173  // {
174  // std::map<std::string, DataSourcePtr>::iterator itaux = it;
175  // ++it;
176  // m_dss.erase(itaux);
177  // }
178  // else
179  // {
180  // ++it;
181  // }
182  //}
183 }
184 
186 {
187  LockWrite l(this);
188 
189  m_dss.clear();
190 }
191 
193 {
194 }
195 
197 {
198 }
199 
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...
A factory for data sources.
void insert(const DataSourcePtr &ds)
It stores the data source in the manager.
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...
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
DataSourceManager()
It initializes the singleton instance of the data source manager.
static std::auto_ptr< DataSource > make(const std::string &dsType)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
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
void detachAll()
All data sources are detached from the manager.
void detach(const DataSourcePtr &ds)
It changes the ownership of the data source to the caller.
~DataSourceManager()
Singleton destructor.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
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&#39;t exists...
#define TR_DATAACCESS(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:95
This is a singleton for managing all data source instances available in the system.
std::map< std::string, DataSourcePtr >::const_iterator const_iterator
DataSourcePtr find(const std::string &id) const
It returns the data source identified by the given id.