VirtualInstrumentManager.cc 2.33 KB
/*
 * VirtualInstrumentManager.cc
 *
 *  Created on: Nov 22, 2014
 *      Author: AKKA
 */

#include "VirtualInstrumentManager.hh"

#include "LocalFileInterfaceConfig.hh"
#include "VirtualInstrument.hh"
#include "Properties.hh"

namespace AMDA {
namespace LocalFileInterface {

VirtualInstrumentManager::VirtualInstrumentManager() : _localBasePath("") {
}

VirtualInstrumentManager::~VirtualInstrumentManager() {
}

VirtualInstrumentSPtr VirtualInstrumentManager::getVirtualInstrument(const std::string& pVirtualInstrumentId) {
	VirtualInstrumentSPtr lResult;
	auto lIt = _virtualInstrumentMap.find(pVirtualInstrumentId);
	if (lIt == _virtualInstrumentMap.end())
	{
		lResult = VirtualInstrumentSPtr(new VirtualInstrument(*this,pVirtualInstrumentId));

		//init new virtual instrument
		if (!initNewVirtualInstrument(lResult))
		{
			lResult.reset();
			//ToDo Exception
			return lResult;
		}
		_virtualInstrumentMap[pVirtualInstrumentId] = lResult;
	} else {
		lResult = lIt->second.lock();
		if ( ! lResult) {
			_virtualInstrumentMap.erase(lIt);
			lResult = VirtualInstrumentSPtr(new VirtualInstrument(*this,pVirtualInstrumentId));
			//init new virtual instrument
			if (!initNewVirtualInstrument(lResult))
			{
				lResult.reset();
				//ToDo Exception
				return lResult;
			}
			_virtualInstrumentMap[pVirtualInstrumentId]=lResult;
		}
	}
	return lResult;
}

bool VirtualInstrumentManager::initNewVirtualInstrument(VirtualInstrumentSPtr lResult)
{
	//load virtual instrument info in base xml file
	if (!loadVirtualInstrumentInfo(lResult))
		return false;

	return true;
}

bool VirtualInstrumentManager::loadVirtualInstrumentInfo(VirtualInstrumentSPtr lResult)
{
	if (_virtualInstrumentParserSPtr == nullptr)
	{
		AMDA::helpers::Properties lProperties("app.properties");

		_localBasePath = lProperties["app.localbase.path"];

		std::string baseFileName = _localBasePath + "/base.xml";
		std::string xsdFileName = lProperties["app.localbase.xsd"];

		_virtualInstrumentParserSPtr.reset(new VirtualInstrumentBaseParser(xsdFileName.c_str(),baseFileName.c_str()));
	}
	try {
		_virtualInstrumentParserSPtr->parse(lResult);
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "VirtualInstrumentManager::loadVirtualInstrumentInfo Cannot get VirtualInstrument info for " << lResult->getVIId());
		return false;
	}
	return true;
}


	} /* namespace DDServerInterface */
} /* namespace AMDA */