DataSourceManager.h
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.h
22 
23  \brief This is a singleton for managing all data source instances available in the system.
24 */
25 
26 #ifndef __TERRALIB_DATAACCESS_INTERNAL_DATASOURCEMANAGER_H
27 #define __TERRALIB_DATAACCESS_INTERNAL_DATASOURCEMANAGER_H
28 
29 // TerraLib
30 #include "../../common/Comparators.h"
31 #include "../../common/Singleton.h"
32 #include "../../common/ThreadingPolicies.h"
33 #include "DataSource.h"
34 
35 // STL
36 #include <map>
37 #include <string>
38 
39 namespace te
40 {
41  namespace da
42  {
43  /*!
44  \class DataSourceManager
45 
46  \brief This is a singleton for managing all data source instances available in the system.
47 
48  Prefer using the methods from this singleton instead of using the data source factory
49  because it keeps track of the data sources available in the system.
50 
51  Another reason to use it in your application is that the application can choose any
52  strategy to label data sources, for examples, using a
53  descriptive title provided by the user or using an unique-universal-identifier (uid) generated by an algorithm.
54 
55  \ingroup dataaccess
56 
57  \sa DataSource, DataSourceFactory, te::common::Singleton, ConnectionPoolManager
58 
59  \warning Developers: take care when adding new methods to this class as it uses synchronization primitives!
60  */
62  ::boost::recursive_mutex,
63  ::boost::lock_guard< ::boost::recursive_mutex>,
64  ::boost::lock_guard< ::boost::recursive_mutex> >,
65  public te::common::Singleton<DataSourceManager>
66  {
68 
69  public:
70 
71  typedef std::map<std::string, DataSourcePtr>::const_iterator const_iterator;
72  typedef std::map<std::string, DataSourcePtr>::iterator iterator;
73 
74  /*!
75  \brief It creates a new data source, stores a reference to it in the manager and then returns a pointer to it.
76 
77  \param id The identification to be assigned to the data source.
78  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
79 
80  \return A pointer to the new data source.
81 
82  \exception Exception It throws an exception if a data source with the same identification already exist.
83 
84  \note Thread-safe!
85  */
86  DataSourcePtr make(const std::string& id, const std::string& dsType);
87 
88  /*!
89  \brief It opens the data source, makes it ready for use, stores a reference to it in the manager and returns a pointer to it.
90 
91  \param id The identification to be assigned to the data source.
92  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
93  \param connInfo The set of parameters used to set up the underlying access channel to the repository.
94 
95  \return A pointer to the new opened data source.
96 
97  \exception Exception It throws an exception if the data source can not be opened or if a data source with the same identification already exist.
98 
99  \note This method doesn't load the data source catalog.
100 
101  \note Thread-safe!
102  */
103  DataSourcePtr open(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo);
104 
105  /*!
106  \brief It opens the data source, makes it ready for use, store a reference to it in the manager and return a pointer to it.
107 
108  \param id The identification to be assigned to the data source.
109  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
110  \param connInfo The set of parameters used to set up the underlying access channel to the repository using a string notation with key-value-pairs delimited by '&' (ampersand).
111 
112  \return A pointer to the new opened data source.
113 
114  \exception Exception It throws an exception if the data source can not be opened or if a data source with the same identification already exist.
115 
116  \note This method doesn't load the data source catalog.
117 
118  \note Thread-safe!
119  */
120  DataSourcePtr open(const std::string& id, const std::string& dsType, const std::string& connInfo);
121 
122  /*!
123  \brief It searches for an opened data source with the given id or it opens a new one if it doesn't exists
124 
125  \param id The data source identification.
126  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
127  \param connInfo The set of parameters used to set up the underlying access channel to the repository.
128 
129  \return A pointer to the new opened data source.
130 
131  \exception Exception It throws an exception if the data source can not be opened.
132 
133  \note This method doesn't load the data source catalog.
134 
135  \note Thread-safe!
136  */
137  DataSourcePtr get(const std::string& id, const std::string& dsType, const std::map<std::string, std::string>& connInfo);
138 
139  /*!
140  \brief It returns the number of data sources that the manager are keeping track of.
141 
142  \return The number of tracked data sources.
143 
144  \note Thread-safe: but take care when relying on this value in a multi-threaded environment!
145  */
146  std::size_t size() const;
147 
148  /*!
149  \brief It returns the data source identified by the given id.
150 
151  \param id The data source identification.
152 
153  \return The data source with the given id, or NULL if none is found.
154 
155  \note Thread-safe!
156  */
157  DataSourcePtr find(const std::string& id) const;
158 
159  /*!
160  \brief It stores the data source in the manager.
161 
162  The data source must have an identification in order to be inserted.
163 
164  \param ds The data source to be stored in the manager.
165 
166  \note The manager will take the ownership of the data source.
167 
168  \exception Exception It throws an exception if a data source with the same identification already exist or if the data source id is empty.
169 
170  \note Thread-safe!
171  */
172  void insert(const DataSourcePtr& ds);
173 
174  /*!
175  \brief It changes the ownership of the data source to the caller.
176 
177  The memory used by the given data source will NOT BE released.
178  In other words, you will take the ownership of the data source pointer.
179 
180  \param ds The data source to be detached.
181 
182  \note Thread-safe!
183  */
184  void detach(const DataSourcePtr& ds);
185 
186  /*!
187  \brief It changes the ownership of the data source with the given identifier to the caller.
188 
189  \param id The data source identifier.
190 
191  \return The data source identified by id. The caller takes the data source ownership.
192 
193  \note Thread-safe!
194  */
195  DataSourcePtr detach(const std::string& id);
196 
197  /*!
198  \brief All data sources of the specified type are detached from the manager.
199 
200  \note Thread-safe!
201  */
202  void detachAll(const std::string& dsType);
203 
204  /*!
205  \brief All data sources are detached from the manager.
206 
207  \note Thread-safe!
208  */
209  void detachAll();
210 
211  /*!
212  \brief It returns an iterator to the beginning of the conteiner.
213 
214  \return An iterator to the beginning of the conteiner.
215  */
216  const_iterator begin() const;
217 
218  /*!
219  \brief It returns an iterator to the beginning of the conteiner.
220 
221  \return An iterator to the beginning of the conteiner.
222  */
223  iterator begin();
224 
225  /*!
226  \brief It returns an iterator to the end of the conteiner.
227 
228  \return An iterator to the beginning of the conteiner.
229  */
230  const_iterator end() const;
231 
232  /*!
233  \brief It returns an iterator to the end of the conteiner.
234 
235  \return An iterator to the beginning of the conteiner.
236  */
237  iterator end();
238 
239  protected:
240 
241  /*! \brief It initializes the singleton instance of the data source manager. */
243 
244  /*! \brief Singleton destructor. */
246 
247  private:
248 
249  std::map<std::string, DataSourcePtr> m_dss; //!< The data sources kept in the manager.
250  };
251 
252  inline std::size_t DataSourceManager::size() const
253  {
254  return m_dss.size();
255  }
256 
258  {
259  return m_dss.begin();
260  }
261 
263  {
264  return m_dss.begin();
265  }
266 
268  {
269  return m_dss.end();
270  }
271 
273  {
274  return m_dss.end();
275  }
276 
277  } // end namespace da
278 } // end namespace te
279 
280 
281 #endif // __TERRALIB_DATAACCESS_INTERNAL_DATASOURCEMANAGER_H
282 
std::map< std::string, DataSourcePtr >::iterator iterator
const_iterator begin() const
It returns an iterator to the beginning of the conteiner.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
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.
const_iterator end() const
It returns an iterator to the end of the conteiner.
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
URI C++ Library.
std::size_t size() const
It returns the number of data sources that the manager are keeping track of.
std::map< std::string, DataSourcePtr > m_dss
The data sources kept in the manager.
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
This policy assures an object-level locking scheme for a derived class.
std::map< std::string, DataSourcePtr >::const_iterator const_iterator
Template support for singleton pattern.
Definition: Singleton.h:100