summaryrefslogtreecommitdiffstats
path: root/lib/MiddleWare/PluginManager.cpp
blob: 03e636ca453de94d691e511fad0050c075b6cec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
    PluginManager.cpp

    Copyright (C) 2009  Zdenek Prikryl (zprikryl@redhat.com)
    Copyright (C) 2009  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    */

#include <iostream>
#include "PluginManager.h"
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include "Settings.h"

CPluginManager::CPluginManager(const std::string& pPlugisConfDir,
							   const std::string& pPlugisLibDir) :
	m_sPlugisConfDir(pPlugisConfDir),
	m_sPlugisLibDir(pPlugisLibDir)
{}

CPluginManager::~CPluginManager()
{}

void CPluginManager::LoadPlugins()
{
	DIR *dir = opendir(m_sPlugisLibDir.c_str());
	struct dirent *dent = NULL;
	if (dir != NULL)
	{
		while ((dent = readdir(dir)) != NULL)
		{
			if (dent->d_type == DT_REG)
			{
				std::string name = dent->d_name;
				std::string extension = name.substr(name.length()-sizeof(PLUGINS_LIB_EXTENSION)+1);
				if (extension == PLUGINS_LIB_EXTENSION)
				{
				    name.erase(0, sizeof(PLUGINS_LIB_PREFIX) - 1);
					name.erase(name.length() - sizeof(PLUGINS_LIB_EXTENSION));
					LoadPlugin(name);
				}
			}
		}
		closedir(dir);
	}
}

void CPluginManager::UnLoadPlugins()
{
    map_abrt_plugins_t::iterator it_p;
      while ((it_p = m_mapABRTPlugins.begin()) != m_mapABRTPlugins.end())
      {
          std::string pluginName = it_p->first;
          UnLoadPlugin(pluginName);
      }
}

void CPluginManager::LoadPlugin(const std::string& pName)
{
	if (m_mapABRTPlugins.find(pName) == m_mapABRTPlugins.end())
	{
		CABRTPlugin* abrtPlugin = NULL;
		try
		{
			std::string libPath = m_sPlugisLibDir + "/" + PLUGINS_LIB_PREFIX + pName + "." + PLUGINS_LIB_EXTENSION;
			abrtPlugin = new CABRTPlugin(libPath);
			if (abrtPlugin->GetMagicNumber() != PLUGINS_MAGIC_NUMBER ||
			    (abrtPlugin->GetType() < ANALYZER && abrtPlugin->GetType() > DATABASE))
			{
				throw std::string("non-compatible plugin");
			}
			std::cerr << "Plugin " << pName << " (" << abrtPlugin->GetVersion() << ") " << "succesfully loaded." << std::endl;
			m_mapABRTPlugins[pName] = abrtPlugin;
		}
		catch (std::string sError)
		{
			if (abrtPlugin != NULL)
			{
				delete abrtPlugin;
			}
			std::cerr << "Failed to load plugin " << pName << " (" << sError << ")." << std::endl;
		}
	}
}

void CPluginManager::UnLoadPlugin(const std::string& pName)
{
	if (m_mapABRTPlugins.find(pName) != m_mapABRTPlugins.end())
	{
		UnRegisterPlugin(pName);
		delete m_mapABRTPlugins[pName];
		m_mapABRTPlugins.erase(pName);
		std::cerr << "Plugin " << pName << " sucessfully unloaded." << std::endl;
	}
}


void CPluginManager::RegisterPlugin(const std::string& pName)
{
    if (m_mapABRTPlugins.find(pName) != m_mapABRTPlugins.end())
    {
        if (m_mapPlugins.find(pName) == m_mapPlugins.end())
        {
            map_settings_t settings;
            std::string path = m_sPlugisConfDir + "/" + pName + "." + PLUGINS_CONF_EXTENSION;
            CPlugin* plugin = m_mapABRTPlugins[pName]->PluginNew();
            plugin->Init();
            plugin->LoadSettings(path);
            m_mapPlugins[pName] = plugin;
            std::cerr << "Registred plugin " << pName << "("
                      << plugin_type_str_t[m_mapABRTPlugins[pName]->GetType()]
                      << ")" << std::endl;
        }
    }
}

void CPluginManager::UnRegisterPlugin(const std::string& pName)
{
    if (m_mapABRTPlugins.find(pName) != m_mapABRTPlugins.end())
    {
        if (m_mapPlugins.find(pName) != m_mapPlugins.end())
        {
            m_mapPlugins[pName]->DeInit();
            delete m_mapPlugins[pName];
            m_mapPlugins.erase(pName);
            std::cerr << "UnRegistred plugin " << pName << "("
                      << plugin_type_str_t[m_mapABRTPlugins[pName]->GetType()]
                      << ")" << std::endl;
        }
    }
}

CAnalyzer* CPluginManager::GetAnalyzer(const std::string& pName)
{
	if (m_mapPlugins.find(pName) == m_mapPlugins.end())
	{
		throw std::string("CPluginManager::GetAnalyzer():"
				          "Analyzer plugin: '"+pName+"' is not loaded.");
	}
	return dynamic_cast<CAnalyzer*>(m_mapPlugins[pName]);
}

CReporter* CPluginManager::GetReporter(const std::string& pName)
{
	if (m_mapPlugins.find(pName) == m_mapPlugins.end())
	{
		throw std::string("CPluginManager::GetReporter():"
				          "Reporter plugin: '"+pName+"' is not loaded.");
	}
	return dynamic_cast<CReporter*>(m_mapPlugins[pName]);
}

CAction* CPluginManager::GetAction(const std::string& pName)
{
	if (m_mapPlugins.find(pName) == m_mapPlugins.end())
	{
		throw std::string("CPluginManager::GetAction():"
				          "Action plugin: '"+pName+"' is not loaded.");
	}
	return dynamic_cast<CAction*>(m_mapPlugins[pName]);
}

CDatabase* CPluginManager::GetDatabase(const std::string& pName)
{
	if (m_mapPlugins.find(pName) == m_mapPlugins.end())
	{
		throw std::string("CPluginManager::GetDatabase():"
				          "Database plugin: '"+pName+"' is not loaded.");
	}
	return dynamic_cast<CDatabase*>(m_mapPlugins[pName]);
}