PluginManager.cc 4.99 KB
/* -*- Base: 10 ; Mode: C++ -*- */
/*------------------------------------------------------------------------
 **	
                                FOST project
 **
--------------------------------------------------------------------------
--------------------------------------------------------------------------
                                FILE LOG
                $Revision: 1.3 $    $Date: 2012-06-15 13:04:40 $
--------------------------------------------------------------------------
CREATION
        F.CASIMIR

SUMMARY

DESCRIPTION

        The main function performs the following actions :
        <ul>
        <li>
        </ul>

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

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

/**

 */
//=============================================================================
// 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 {
    namespace Plugins {

        log4cxx::LoggerPtr gLogger = log4cxx::Logger::getLogger("AMDA-Kernel.Plugins");

        //=============================================================================
        // Methods of Class PluginManager
        //=============================================================================

        //=============================================================================
        // Other Methods

        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;
            }

            try {
                LOG4CXX_DEBUG(gLogger, "PluginManager::registerPlugin :: attempt to load: " << pluginPath << pluginName);

                Plugin plugin(pluginPath + "/" + pluginName);

                _currentPluginPath = pluginPath + "/" + pluginName;

                _loadedPlugins.insert(PluginMap::value_type(pluginName, plugin)).first->second.registerPlugin(*this);



                //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);
                }
            }
        }
         */


    } // namespace Plugins
} // namespace AMDA