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