Added support for dynamic load support for RouterFlexXML. The filter path
[metaproxy-moved-to-github.git] / src / factory_filter.cpp
1 /* $Id: factory_filter.cpp,v 1.3 2006-01-19 09:41:01 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include "factory_filter.hpp"
10
11 #if HAVE_DLFCN_H
12 #include <dlfcn.h>
13 #endif
14 #include <stdexcept>
15 #include <iostream>
16 #include <string>
17 #include <map>
18
19 namespace yp2 {
20     class FactoryFilter::Rep {
21         typedef std::map<std::string, CreateFilterCallback> CallbackMap;
22         typedef std::map<std::string, CreateFilterCallback>::iterator 
23             CallbackMapIt;
24     public:
25         friend class FactoryFilter;
26         CallbackMap m_fcm;
27         Rep();
28         ~Rep();
29     };
30 }
31
32 yp2::FactoryFilter::NotFound::NotFound(const std::string message)
33     : std::runtime_error(message)
34 {
35 }
36
37 yp2::FactoryFilter::Rep::Rep()
38 {
39 }
40
41 yp2::FactoryFilter::Rep::~Rep()
42 {
43 }
44
45 yp2::FactoryFilter::FactoryFilter() : m_p(new yp2::FactoryFilter::Rep)
46 {
47
48 }
49
50 yp2::FactoryFilter::~FactoryFilter()
51 {
52
53 }
54
55 bool yp2::FactoryFilter::add_creator(std::string fi,
56                                      CreateFilterCallback cfc)
57 {
58     return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
59 }
60
61
62 bool yp2::FactoryFilter::drop_creator(std::string fi)
63 {
64     return m_p->m_fcm.erase(fi) == 1;
65 }
66
67 bool yp2::FactoryFilter::exist(std::string fi)
68 {
69     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
70     
71     if (it == m_p->m_fcm.end())
72     {
73         return false;
74     }
75     return true;
76 }
77
78 yp2::filter::Base* yp2::FactoryFilter::create(std::string fi)
79 {
80     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
81     
82     if (it == m_p->m_fcm.end()){
83         std::string msg = "filter type '" + fi + "' not found";
84             throw NotFound(msg);
85     }
86     // call create function
87     return (it->second());
88 }
89
90 bool yp2::FactoryFilter::have_dl_support()
91 {
92 #if HAVE_DLFCN_H
93     return true;
94 #else
95     return false;
96 #endif
97 }
98
99 bool yp2::FactoryFilter::add_creator_dl(const std::string &fi,
100                                         const std::string &path)
101 {
102 #if HAVE_DLFCN_H
103     if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
104     {
105         return true;
106     }
107
108     std::string full_path = path + "/yp2_filter_" + fi + ".so";
109     void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
110     if (!dl_handle)
111     {
112         const char *dl = dlerror();
113         std::cout << "dlopen " << full_path << " failed. dlerror=" << dl << 
114             std::endl;
115         return false;
116     }
117
118     std::string full_name = "yp2_filter_" + fi;
119     
120     void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
121     if (!dlsym_ptr)
122     {
123         std::cout << "dlsym " << full_name << " failed\n";
124         return false;
125     }
126     struct yp2_filter_struct *s = (struct yp2_filter_struct *) dlsym_ptr;
127     return add_creator(fi, s->creator);
128 #else
129     return false;
130 #endif
131 }
132
133 /*
134  * Local variables:
135  * c-basic-offset: 4
136  * indent-tabs-mode: nil
137  * c-file-style: "stroustrup"
138  * End:
139  * vim: shiftwidth=4 tabstop=8 expandtab
140  */