ServicesServer.cpp 7.02 KB
/*
 * ServicesServer.cpp
 *
 *  Created on: Nov 12, 2012
 *      Author: g.schneller
 */

#include "ServicesServer.hh"
#include <libxml/xmlwriter.h>
#include "PluginsParser.hh" 
#include "PluginManager.hh"
#include <iostream>
#include <sstream>
#include <fstream>



namespace AMDA {
    namespace Parameters {

        log4cxx::LoggerPtr ServicesServer::_logger(log4cxx::Logger::getLogger("AMDA-Kernel.ServicesServer"));

        ServicesServer::ServicesServer() {
        }

        ServicesServer::~ServicesServer() {
        }

        bool ServicesServer::_PLUGINS_DYNAMIC_LOADING = false;

        /**
         * This function generates plugin xml that contains all process and corresponding plugin paths 
         <plugins xml:id="plugins">
             <requestProcess type="paramGet" name="CONSTANT">
                <plugin>./../build/Debug/plugin//libConstantInterface.so</plugin>
             </requestProcess>
             <requestProcess type="paramGet" name="DDBASE">
                  <plugin>./../build/Debug/plugin//libDDServerInterface.so</plugin>
             </requestProcess>
         .........
         </plugins>
         */

        bool ServicesServer::generatePluginsXml(const char* filePath) {
            LIBXML_TEST_VERSION

            xmlTextWriterPtr writer = xmlNewTextWriterFilename(filePath, 0);
            if (writer == NULL) {
                return false;
            }

            int rc;
            rc = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);

            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            rc = xmlTextWriterStartElement(writer, BAD_CAST "plugins");
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }
            rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:id", BAD_CAST "plugins");
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            for (auto plugMap : _processPluginMaps) {
                for (auto plug : plugMap.second) {
                    rc = xmlTextWriterStartElement(writer, BAD_CAST "requestProcess");
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }

                    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST plugMap.first.c_str());
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }

                    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST plug.first.c_str());
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }
                    rc = xmlTextWriterWriteElement(writer, BAD_CAST "plugin", BAD_CAST plug.second.c_str());
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }


                    // process node
                    rc = xmlTextWriterEndElement(writer);
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }
                }

            }

            // close plugins node
            rc = xmlTextWriterEndElement(writer);
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            rc = xmlTextWriterEndDocument(writer);
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            xmlFreeTextWriter(writer);

            return true;
        }

        /**
         * Thins function reads the xml plugin file and fills the plugin map 
         */
        bool ServicesServer::fillPluginsMap(std::string xmlFile, std::string xsdFile) {
            try {
                AMDA::Parameters::PluginsParser parser(xsdFile.c_str());
                _processPluginMaps = parser.parse(xmlFile.c_str());
            } catch (...) {
                throw;
            }
            return true;
        }

        /**
         * this function laod plugins by plugin type : paramGet process ........
         */
        bool ServicesServer::loadPluginByType(const std::string& type) {
            if (_processPluginMaps.count(type) != 1) {
                LOG4CXX_WARN(_logger, "Unknown Plugin Type : " << type);
                return false;
            }
            for (auto plugMap : _processPluginMaps[type]) {
                loadPlugin(plugMap.first, type);
            }
            return true;
        }

        /**
         * this function laod plugin. type argument  is optional 
         */
        bool ServicesServer::loadPlugin(const std::string& process, const std::string& type) {
            if (type == "") {
                for (auto plugMap : _processPluginMaps) {
                    if (plugMap.second.count(process) == 1) {
                        unsigned found = plugMap.second[process].find_last_of("/\\");
                        std::string path = plugMap.second[process].substr(0, found);
                        std::string file = plugMap.second[process].substr(found + 1);
                        AMDA::Plugins::PluginManager::getInstance()->loadPlugin(path.c_str(), file.c_str());
                        return true;
                    }
                }
                LOG4CXX_WARN(_logger, "Unknown Plugin : " << process);
                return false;
            } else {
                if (_processPluginMaps.count(type) == 1 && _processPluginMaps[type].count(process) == 1) {
                    unsigned found = _processPluginMaps[type][process].find_last_of("/\\");
                    std::string path = _processPluginMaps[type][process].substr(0, found);
                    std::string file = _processPluginMaps[type][process].substr(found + 1);
                    AMDA::Plugins::PluginManager::getInstance()->loadPlugin(path.c_str(), file.c_str());
                    return true;
                }
                LOG4CXX_WARN(_logger, "Unknown Plugin : " << type << " " << process);
                return false;
            }
        }

        /*
         * this function laod needed outputs (download , plot ...) xplugin from the request file
         */
        bool ServicesServer::loadPluginsFromRequest(const char* requestXML) {
            std::string line;
            std::ifstream infile(requestXML);
            while (std::getline(infile, line)) {
                for (std::string output : _outputs) {
                    if (line.find(output, 0) != std::string::npos) {
                        _pluginsToLoad.push_back(output.erase(0, 1));
                        return true;
                    }
                }
               
            }
            // if no outputs found dataMining is loaded
             _pluginsToLoad.push_back("dataMining");
            return true;
        }

    } /* namespace Parameters */
} /* namespace AMDA */