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