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