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 core { class URI; }
42  namespace da
43  {
44  /*!
45  \class DataSourceManager
46 
47  \brief This is a singleton for managing all data source instances available in the system.
48 
49  Prefer using the methods from this singleton instead of using the data source factory
50  because it keeps track of the data sources available in the system.
51 
52  Another reason to use it in your application is that the application can choose any
53  strategy to label data sources, for examples, using a
54  descriptive title provided by the user or using an unique-universal-identifier (uid) generated by an algorithm.
55 
56  \ingroup dataaccess
57 
58  \sa DataSource, DataSourceFactory, te::common::Singleton, ConnectionPoolManager
59 
60  \warning Developers: take care when adding new methods to this class as it uses synchronization primitives!
61  */
63  ::boost::recursive_mutex,
64  ::boost::lock_guard< ::boost::recursive_mutex>,
65  ::boost::lock_guard< ::boost::recursive_mutex> >,
66  public te::common::Singleton<DataSourceManager>
67  {
69 
70  public:
71 
72  typedef std::map<std::string, DataSourcePtr>::const_iterator const_iterator;
73  typedef std::map<std::string, DataSourcePtr>::iterator iterator;
74 
75  /*!
76  \brief It creates a new data source, stores a reference to it in the manager and then returns a pointer to it.
77 
78  \param id The identification to be assigned to the data source.
79  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
80  \param connInfo The URI, as a valid string, used to set up the underlying access channel to the repository.
81 
82  \return A pointer to the new data source.
83 
84  \exception Exception It throws an exception if a data source with the same identification already exist.
85 
86  \note Thread-safe!
87  */
88  DataSourcePtr make(const std::string& id, const std::string& dsType, const std::string& connInfo);
89 
90  /*!
91  \brief It creates a new data source, stores a reference to it in the manager and then returns a pointer to it.
92 
93  \param id The identification to be assigned to the data source.
94  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
95  \param connInfo The URI used to set up the underlying access channel to the repository.
96 
97  \return A pointer to the new data source.
98 
99  \exception Exception It throws an exception if a data source with the same identification already exist.
100 
101  \note Thread-safe!
102  */
103  DataSourcePtr make(const std::string& id, const std::string& dsType, const te::core::URI& connInfo);
104 
105  /*!
106  \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.
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 URI, as a valid string, used to set up the underlying access channel to the repository.
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 opens the data source, makes it ready for use, stores a reference to it in the manager and returns a pointer to it.
124 
125  \param id The identification to be assigned to the data source.
126  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
127  \param connInfo The URI 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 or if a data source with the same identification already exist.
132 
133  \note This method doesn't load the data source catalog.
134 
135  \note Thread-safe!
136  */
137  DataSourcePtr open(const std::string& id, const std::string& dsType, const te::core::URI& connInfo);
138 
139  /*!
140  \brief It searches for an opened data source with the given id or it opens a new one if it doesn't exists
141 
142  \param id The data source identification.
143  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
144  \param connInfo The set of parameters, as a valid URI string, used to set up the underlying access channel to the repository.
145 
146  \return A pointer to the new opened data source.
147 
148  \exception Exception It throws an exception if the data source can not be opened.
149 
150  \note This method doesn't load the data source catalog.
151 
152  \note Thread-safe!
153  */
154  DataSourcePtr get(const std::string& id, const std::string& dsType, const std::string& connInfo);
155 
156  /*!
157  \brief It searches for an opened data source with the given id or it opens a new one if it doesn't exists
158 
159  \param id The data source identification.
160  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
161  \param connInfo The set of parameters used to set up the underlying access channel to the repository.
162 
163  \return A pointer to the new opened data source.
164 
165  \exception Exception It throws an exception if the data source can not be opened.
166 
167  \note This method doesn't load the data source catalog.
168 
169  \note Thread-safe!
170  */
171  DataSourcePtr get(const std::string& id, const std::string& dsType, const te::core::URI& connInfo);
172 
173  /*!
174  \brief It returns the number of data sources that the manager are keeping track of.
175 
176  \return The number of tracked data sources.
177 
178  \note Thread-safe: but take care when relying on this value in a multi-threaded environment!
179  */
180  std::size_t size() const;
181 
182  /*!
183  \brief It returns the data source identified by the given id.
184 
185  \param id The data source identification.
186 
187  \return The data source with the given id, or NULL if none is found.
188 
189  \note Thread-safe!
190  */
191  DataSourcePtr find(const std::string& id) const;
192 
193  /*!
194  \brief It stores the data source in the manager.
195 
196  The data source must have an identification in order to be inserted.
197 
198  \param ds The data source to be stored in the manager.
199 
200  \note The manager will take the ownership of the data source.
201 
202  \exception Exception It throws an exception if a data source with the same identification already exist or if the data source id is empty.
203 
204  \note Thread-safe!
205  */
206  void insert(const DataSourcePtr& ds);
207 
208  /*!
209  \brief It changes the ownership of the data source to the caller.
210 
211  The memory used by the given data source will NOT BE released.
212  In other words, you will take the ownership of the data source pointer.
213 
214  \param ds The data source to be detached.
215 
216  \note Thread-safe!
217  */
218  void detach(const DataSourcePtr& ds);
219 
220  /*!
221  \brief It changes the ownership of the data source with the given identifier to the caller.
222 
223  \param id The data source identifier.
224 
225  \return The data source identified by id. The caller takes the data source ownership.
226 
227  \note Thread-safe!
228  */
229  DataSourcePtr detach(const std::string& id);
230 
231  /*!
232  \brief All data sources of the specified type are detached from the manager.
233 
234  \note Thread-safe!
235  */
236  void detachAll(const std::string& dsType);
237 
238  /*!
239  \brief All data sources are detached from the manager.
240 
241  \note Thread-safe!
242  */
243  void detachAll();
244 
245  /*!
246  \brief It returns an iterator to the beginning of the conteiner.
247 
248  \return An iterator to the beginning of the conteiner.
249  */
250  const_iterator begin() const;
251 
252  /*!
253  \brief It returns an iterator to the beginning of the conteiner.
254 
255  \return An iterator to the beginning of the conteiner.
256  */
257  iterator begin();
258 
259  /*!
260  \brief It returns an iterator to the end of the conteiner.
261 
262  \return An iterator to the beginning of the conteiner.
263  */
264  const_iterator end() const;
265 
266  /*!
267  \brief It returns an iterator to the end of the conteiner.
268 
269  \return An iterator to the beginning of the conteiner.
270  */
271  iterator end();
272 
273  protected:
274 
275  /*! \brief It initializes the singleton instance of the data source manager. */
277 
278  /*! \brief Singleton destructor. */
280 
281  private:
282 
283  std::map<std::string, DataSourcePtr> m_dss; //!< The data sources kept in the manager.
284  };
285 
286  inline std::size_t DataSourceManager::size() const
287  {
288  return m_dss.size();
289  }
290 
292  {
293  return m_dss.begin();
294  }
295 
297  {
298  return m_dss.begin();
299  }
300 
302  {
303  return m_dss.end();
304  }
305 
307  {
308  return m_dss.end();
309  }
310 
311  } // end namespace da
312 } // end namespace te
313 
314 
315 #endif // __TERRALIB_DATAACCESS_INTERNAL_DATASOURCEMANAGER_H
316 
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:1438
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.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
#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