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  mongocxx::client *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
fawkes::MongoDBAspect::mongodb_config_name
const std::string & mongodb_config_name() const
Get MongoDB configuration name.
Definition: mongodb.h:48
fawkes::MongoDBAspect
Thread aspect to access MongoDB.
Definition: mongodb.h:39
fawkes::MongoDBConnCreator::create_client
virtual mongocxx::client * create_client(const std::string &config_name="")=0
Create a new MongoDB client.
fawkes::MongoDBConnCreator
Interface for a MongoDB connection creator.
Definition: mongodb_conncreator.h:38
fawkes::MongoDBAspect::mongodb_client
mongocxx::client * mongodb_client
MongoDB client to use to interact with the database.
Definition: mongodb.h:54
fawkes::CannotInitializeThreadException
Thread cannot be initialized.
Definition: thread_initializer.h:34
fawkes::Thread::name
const char * name() const
Get name of thread.
Definition: thread.h:100
fawkes::MongoDBAspectIniFin::MongoDBAspectIniFin
MongoDBAspectIniFin(MongoDBConnCreator *conn_creator)
Constructor.
Definition: mongodb_inifin.cpp:42
fawkes::AspectIniFin
Aspect initializer/finalizer base class.
Definition: inifin.h:34
fawkes::MongoDBAspectIniFin::finalize
virtual void finalize(Thread *thread)
Finalize thread.
Definition: mongodb_inifin.cpp:70
fawkes
Fawkes library namespace.
fawkes::MongoDBAspectIniFin::init
virtual void init(Thread *thread)
Initialize thread.
Definition: mongodb_inifin.cpp:49
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::MongoDBConnCreator::delete_client
virtual void delete_client(mongocxx::client *client)=0
Delete a client.
fawkes::CannotFinalizeThreadException
Thread cannot be finalized.
Definition: thread_finalizer.h:34