Blame view

src/Plugins/PluginManager.cc 4.99 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
/* -*- Base: 10 ; Mode: C++ -*- */
/*------------------------------------------------------------------------
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
3
4
5
 **	
                                FOST project
 **
fbe3c2bb   Benjamin Renard   First commit
6
7
--------------------------------------------------------------------------
--------------------------------------------------------------------------
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
8
9
                                FILE LOG
                $Revision: 1.3 $    $Date: 2012-06-15 13:04:40 $
fbe3c2bb   Benjamin Renard   First commit
10
11
--------------------------------------------------------------------------
CREATION
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
12
        F.CASIMIR
fbe3c2bb   Benjamin Renard   First commit
13
14
15
16
17

SUMMARY

DESCRIPTION

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
18
19
20
21
        The main function performs the following actions :
        <ul>
        <li>
        </ul>
fbe3c2bb   Benjamin Renard   First commit
22
23
24
25
26
27
28
29
30
31
32
33
34
35

------------------------------------------------------------------------*/

//=============================================================================
// 
// History of code
//
// creation 
//
// modification 
//=============================================================================

/**

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
36
 */
fbe3c2bb   Benjamin Renard   First commit
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
//=============================================================================
// Include section
//=============================================================================

// Standard libraries include files
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <regex.h>
#include <sys/stat.h>

#include <cerrno>
#include <iostream>
#include <sstream> 
#include <vector>

#include "log4cxx/logger.h"
#include "Helper.hh"

// Module Kernel include files
//-----------------------------------------------------------------------------
#include <PluginManager.hh>

namespace AMDA {
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
65
    namespace Plugins {
fbe3c2bb   Benjamin Renard   First commit
66

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
67
        log4cxx::LoggerPtr gLogger = log4cxx::Logger::getLogger("AMDA-Kernel.Plugins");
fbe3c2bb   Benjamin Renard   First commit
68

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
69
70
71
        //=============================================================================
        // Methods of Class PluginManager
        //=============================================================================
fbe3c2bb   Benjamin Renard   First commit
72

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
73
74
        //=============================================================================
        // Other Methods
fbe3c2bb   Benjamin Renard   First commit
75

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
76
77
78
79
80
        void PluginManager::registerPlugin(const std::string &pluginName, const std::string &pluginPath) {
            if (_loadedPlugins.count(pluginName) == 1) {
                LOG4CXX_DEBUG(gLogger, "PluginManager::registerPlugin :: Plugin is already loaded: " << pluginPath << pluginName);
                return;
            }
fbe3c2bb   Benjamin Renard   First commit
81

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
82
83
            try {
                LOG4CXX_DEBUG(gLogger, "PluginManager::registerPlugin :: attempt to load: " << pluginPath << pluginName);
fbe3c2bb   Benjamin Renard   First commit
84

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
85
                Plugin plugin(pluginPath + "/" + pluginName);
a851633d   Hacene SI HADJ MOHAND   xml generated
86

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
87
                _currentPluginPath = pluginPath + "/" + pluginName;
fbe3c2bb   Benjamin Renard   First commit
88

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
89
                _loadedPlugins.insert(PluginMap::value_type(pluginName, plugin)).first->second.registerPlugin(*this);
fbe3c2bb   Benjamin Renard   First commit
90

fbe3c2bb   Benjamin Renard   First commit
91

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
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

                //PluginWatcher::getInstance()->addPluginToWatch(pluginPath);

                LOG4CXX_INFO(gLogger, "PluginManager::loadPlugin :: " << pluginName << " plugin load successfully");
            } catch (AMDA::AMDA_exception & e) {
                LOG4CXX_WARN(gLogger, "PluginManager::registerPlugin exception resume: " << boost::diagnostic_information(e));
            } catch (...) {
                LOG4CXX_WARN(gLogger, "Problem in loadPlugin " << pluginPath);
            }
        }

        /**
         */
        void PluginManager::loadPlugin(const std::string &sPluginName) {
            if (_loadedPlugins.find(sPluginName) == _loadedPlugins.end()) {
                registerPlugin(sPluginName + ".so", std::string(getenv(sPluginName.c_str())) + "/lib/");
            }
        }

        void PluginManager::loadPlugin(const std::string &path, const std::string &sPluginName) {
            int soPos = sPluginName.find(".so");
            if (soPos > 1) {
                registerPlugin(sPluginName, path);
            }
        }

        void PluginManager::loadPluginFromPath(const std::string& pluginPath) {
            if (!pluginPath.empty()) {
                const char *myDir = pluginPath.c_str();
                struct stat myStat;
                if ((stat(myDir, &myStat) == 0) && (((myStat.st_mode) & S_IFMT) == S_IFDIR)) {
                    // myDir exists and is a directory.
                    std::vector<std::string> files = std::vector<std::string>();
                    AMDA::Helpers::Helper::getMatchFiles(pluginPath.c_str(), files, ".*.so$");

                    for (unsigned int i = 0; i < files.size(); i++) {
                        loadPlugin(std::string(pluginPath), files[i]);
                    }
                    files.clear();
                }
            }
        }

        /**
        void PluginManager::loadNeededPlugins(std::list<std::string>pluginList , const std::string& pluginsPath){
            for (auto pluginName : lPlungList){
                if(_loadedPlugins.find(pluginName) == _loadedPlugins.end())
            {
                    registerPlugin(pluginsPath, pluginName);
                }
            }
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
143
        }
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
144
         */
fbe3c2bb   Benjamin Renard   First commit
145
146


9517e2ec   Hacene SI HADJ MOHAND   ok test ok
147
    } // namespace Plugins
fbe3c2bb   Benjamin Renard   First commit
148
} // namespace AMDA