Fawkes API  Fawkes Development Version
mongodb_inifin.cpp
1 
2 /***************************************************************************
3  * mongodb_inifin.cpp - Fawkes MongoDBAspect initializer/finalizer
4  *
5  * Created: Mon Dec 06 22:33:03 2010
6  * Copyright 2006-2017 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #include <core/threading/thread_finalizer.h>
24 #include <plugins/mongodb/aspect/mongodb.h>
25 #include <plugins/mongodb/aspect/mongodb_conncreator.h>
26 #include <plugins/mongodb/aspect/mongodb_inifin.h>
27 
28 #include <cstddef>
29 
30 namespace fawkes {
31 
32 /** @class MongoDBAspectIniFin <plugins/mongodb/aspect/mongodb_inifin.h>
33  * MongoDBAspect initializer/finalizer.
34  * This initializer/finalizer will use the MongoDBConnCreator instance to
35  * create client instances as requested by a thread.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param conn_creator connection creator to use for initializing threads
41  */
43 : AspectIniFin("MongoDBAspect")
44 {
45  conn_creator_ = conn_creator;
46 }
47 
48 void
50 {
51  MongoDBAspect *mongodb_thread;
52  mongodb_thread = dynamic_cast<MongoDBAspect *>(thread);
53  if (mongodb_thread == NULL) {
54  throw CannotInitializeThreadException("Thread '%s' claims to have the "
55  "MongoDBAspect, but RTTI says it "
56  "has not. ",
57  thread->name());
58  }
59 
60  mongo::DBClientBase *client = nullptr;
61 
62  if (!mongodb_thread->mongodb_config_name().empty()) {
63  conn_creator_->create_client(mongodb_thread->mongodb_config_name());
64  }
65 
66  mongodb_thread->init_MongoDBAspect(client, conn_creator_);
67 }
68 
69 void
71 {
72  MongoDBAspect *mongodb_thread;
73  mongodb_thread = dynamic_cast<MongoDBAspect *>(thread);
74  if (mongodb_thread == NULL) {
75  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
76  "MongoDBAspect, but RTTI says it "
77  "has not. ",
78  thread->name());
79  }
80 
81  conn_creator_->delete_client(mongodb_thread->mongodb_client);
82 }
83 
84 } // end namespace fawkes
MongoDBAspectIniFin(MongoDBConnCreator *conn_creator)
Constructor.
virtual void init(Thread *thread)
Initialize thread.
Fawkes library namespace.
mongo::DBClientBase * mongodb_client
MongoDB client to use to interact with the database.
Definition: mongodb.h:55
Thread class encapsulation of pthreads.
Definition: thread.h:45
Thread aspect to access MongoDB.
Definition: mongodb.h:39
virtual mongo::DBClientBase * create_client(const std::string &config_name="")=0
Create a new MongoDB client.
const char * name() const
Get name of thread.
Definition: thread.h:100
Interface for a MongoDB connection creator.
virtual void finalize(Thread *thread)
Finalize thread.
Thread cannot be finalized.
const std::string & mongodb_config_name() const
Get MongoDB configuration name.
Definition: mongodb.h:49
virtual void delete_client(mongo::DBClientBase *client)=0
Delete a client.
Aspect initializer/finalizer base class.
Definition: inifin.h:33