GPL v2.
[metaproxy-moved-to-github.git] / src / factory_filter.cpp
1 /* $Id: factory_filter.cpp,v 1.8 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23
24 #include "factory_filter.hpp"
25
26 #if HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 #include <stdexcept>
30 #include <iostream>
31 #include <string>
32 #include <map>
33
34 namespace mp = metaproxy_1;
35
36 namespace metaproxy_1 {
37     class FactoryFilter::Rep {
38         typedef std::map<std::string, CreateFilterCallback> CallbackMap;
39         typedef std::map<std::string, CreateFilterCallback>::iterator 
40             CallbackMapIt;
41     public:
42         friend class FactoryFilter;
43         CallbackMap m_fcm;
44         Rep();
45         ~Rep();
46     };
47 }
48
49 mp::FactoryFilter::NotFound::NotFound(const std::string message)
50     : std::runtime_error(message)
51 {
52 }
53
54 mp::FactoryFilter::Rep::Rep()
55 {
56 }
57
58 mp::FactoryFilter::Rep::~Rep()
59 {
60 }
61
62 mp::FactoryFilter::FactoryFilter() : m_p(new mp::FactoryFilter::Rep)
63 {
64
65 }
66
67 mp::FactoryFilter::~FactoryFilter()
68 {
69
70 }
71
72 bool mp::FactoryFilter::add_creator(std::string fi,
73                                      CreateFilterCallback cfc)
74 {
75     return m_p->m_fcm.insert(Rep::CallbackMap::value_type(fi, cfc)).second;
76 }
77
78
79 bool mp::FactoryFilter::drop_creator(std::string fi)
80 {
81     return m_p->m_fcm.erase(fi) == 1;
82 }
83
84 bool mp::FactoryFilter::exist(std::string fi)
85 {
86     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
87     
88     if (it == m_p->m_fcm.end())
89     {
90         return false;
91     }
92     return true;
93 }
94
95 mp::filter::Base* mp::FactoryFilter::create(std::string fi)
96 {
97     Rep::CallbackMap::const_iterator it = m_p->m_fcm.find(fi);
98     
99     if (it == m_p->m_fcm.end()){
100         std::string msg = "filter type '" + fi + "' not found";
101             throw NotFound(msg);
102     }
103     // call create function
104     return (it->second());
105 }
106
107 bool mp::FactoryFilter::have_dl_support()
108 {
109 #if HAVE_DLFCN_H
110     return true;
111 #else
112     return false;
113 #endif
114 }
115
116 bool mp::FactoryFilter::add_creator_dl(const std::string &fi,
117                                         const std::string &path)
118 {
119 #if HAVE_DLFCN_H
120     if (m_p->m_fcm.find(fi) != m_p->m_fcm.end())
121     {
122         return true;
123     }
124
125     std::string full_path = path + "/metaproxy_filter_" + fi + ".so";
126     void *dl_handle = dlopen(full_path.c_str(), RTLD_GLOBAL|RTLD_NOW);
127     if (!dl_handle)
128     {
129         const char *dl = dlerror();
130         std::cout << "dlopen " << full_path << " failed. dlerror=" << dl << 
131             std::endl;
132         return false;
133     }
134
135     std::string full_name = "metaproxy_1_filter_" + fi;
136     
137     void *dlsym_ptr = dlsym(dl_handle, full_name.c_str());
138     if (!dlsym_ptr)
139     {
140         std::cout << "dlsym " << full_name << " failed\n";
141         return false;
142     }
143     struct metaproxy_1_filter_struct *s = (struct metaproxy_1_filter_struct *) dlsym_ptr;
144     return add_creator(fi, s->creator);
145 #else
146     return false;
147 #endif
148 }
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * c-file-style: "stroustrup"
155  * End:
156  * vim: shiftwidth=4 tabstop=8 expandtab
157  */