Application.cc 5.37 KB
/*
 * Main.cc
 *
 *  Created on: Feb 1, 2013
 *      Author: f.casimir
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


#include <iostream>
#include <string>
#include <sstream>

//includes boost
#include <boost/shared_ptr.hpp>

// include log4cxx header files.
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
#include "log4cxx/propertyconfigurator.h"
#include "log4cxx/mdc.h"


// DD_Client_r_lib module includes
#include "TimeUtil.hh"

#include "Properties.hh"
#include "Helper.hh"

#include "AMDA_exception.hh"
#include "DicError.hh"
#include "Application.hh"

// Parameters module include
#include "ServicesServer.hh"

#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "InstrumentMgr.hh"
#include "MissionMgr.hh"
#include "PluginsParser.hh" 

#include "SpiceKernelMgr.hh"


// Plugin module include
#include "PluginManager.hh"

// XMLRequest module include
#include "XMLParameterConfigurator.hh"


using namespace std;
using namespace boost;
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace AMDA::Parameters;
using namespace AMDA::XMLParameterConfigurator;

LoggerPtr logger(Logger::getLogger("AMDA-Kernel"));

namespace AMDA {
    namespace Common {

        int Application::main(int argc, char *argv[],
                std::function<int(int argc, char *argv[], AMDA::helpers::Properties&) > exec, bool skipPluginLoad) {
            int result = AMDA_EXIT_OK;

            try {
                if (access("app.properties", F_OK) != 0) {
                    BOOST_THROW_EXCEPTION(
                            AMDA::AMDA_exception() << AMDA::errno_code(AMDA_FILEPROPERTIES_ERR) << AMDA::ex_msg("Properties file not found"));
                }
                try {
                    AMDA::helpers::Properties lProperties("app.properties");

                    std::string lLoggerFileconfiguration =
                            lProperties["app.log4cxx.configfile"];

                    if (lLoggerFileconfiguration.empty()) {
                        /// Set up a simple configuration that logs on the console.
                        BasicConfigurator::configure();
                        LOG4CXX_WARN(logger,
                                "No properties: 'app.log4cxx.configfile' found. log4cxx basic configuration set ");
                    } else {
                        PropertyConfigurator::configure(
                                lLoggerFileconfiguration.c_str());
                        MDC::put("PID", AMDA::Helpers::Helper::getPID());
                    }

                    ///Create ParameterManager
                    ServicesServer *servicesServer = ServicesServer::getInstance();
                    ParameterConfiguratorSPtr configurator(new AMDA::XMLParameterConfigurator::XMLParameterConfigurator(lProperties["app.parameter.xsd"].c_str()));
                    configurator->setParamPath(lProperties["app.param.path"]);
                    servicesServer->setConfigurator(configurator);

                    std::string lPluginPath = lProperties["app.plugin"];
                    if (!lPluginPath.empty() && !skipPluginLoad) {
                        bool b;
                        istringstream(lProperties["app.process.DYNAMIC_LOADING"]) >> std::boolalpha >> b;
                        if (!b) {
                            AMDA::Plugins::PluginManager::getInstance()->loadPluginFromPath(
                                    lPluginPath);
                               servicesServer->generatePluginsXml((lProperties["app.process.plugins.xml"]).c_str());
                        } else {
                            std::string xmlFile= (lProperties["app.process.plugins.xml"]).c_str();
                             std::string xsdFile= (lProperties["app.process.plugins.xsd"]).c_str();
                             servicesServer->setPluginsDynamicLoading(true);
                             servicesServer->fillPluginsMap(xmlFile,xsdFile);
                        }
                    }

                    LOG4CXX_INFO(logger, "Entering application.");
                    result = exec(argc, argv, lProperties);

                } catch (AMDA::AMDA_exception & e) {
                    LOG4CXX_ERROR(logger,
                            "Error resume: " << AMDA::traitException(result, e));
                }

            } catch (AMDA::AMDA_exception & e) {
                std::cerr << "Error resume: " << AMDA::traitException(result, e);
            } catch (...) {
                result = AMDA_ERROR_UNKNOWN;
            }

            ServicesServer::releaseInstance();


            AMDA::Info::ParamMgr::releaseInstance();

            AMDA::Info::DataSetMgr::releaseInstance();

            AMDA::Info::InstrumentMgr::releaseInstance();

            AMDA::Info::MissionMgr::releaseInstance();

            AMDA::Plugins::PluginManager::getInstance()->releaseInstance();

            AMDA::SpiceKernel::SpiceKernelMgr::getInstance()->releaseInstance();

            if (result != 0) {
                LOG4CXX_ERROR(logger, "Exception: " << getErrorMsg(result));
            }
            LOG4CXX_INFO(logger, "Exiting application (" << result << ")");

            //release log4cxx
            MDC::remove("PID");
            cout << "-----------------------------------------------------" << endl;

            return result;
        }

    } /* namespace Common */
} /* namespace AMDA */