00001 /* 00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico 00003 00004 This file is part of MeRMaID. 00005 00006 MeRMaID is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 MeRMaID is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with MeRMaID. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 00021 00022 /** 00023 * @Filename ServiceTypeDescriptionRepository.cpp 00024 * @Description Support ServiceTypeDescriptionRepository implementation 00025 * @Status Work in Progress 00026 * @Version $Id: ServiceTypeDescriptionRepository.cpp 1 2011-03-04 18:13:18Z jreis $ 00027 * @Maintainer Marco (mafb@isr.ist.utl.pt) 00028 */ 00029 00030 00031 #include "config.h" 00032 00033 #include "ServiceTypeDescriptionRepository.hpp" 00034 #include "ServiceInterfaceDescription.hpp" 00035 #include "Service.hpp" 00036 00037 #include <Exception.hpp> 00038 #include <XmlElement.hpp> 00039 #include <XmlElementVector.hpp> 00040 00041 #include <iostream> 00042 #include <sstream> 00043 00044 using namespace mermaid::support::service; 00045 using mermaid::support::errorhandling::Exception; 00046 using mermaid::support::xml::XmlElement; 00047 using mermaid::support::xml::XmlElementVector; 00048 00049 shared_ptr<ServiceTypeDescriptionRepository> ServiceTypeDescriptionRepository::instance_ = shared_ptr<ServiceTypeDescriptionRepository> (new ServiceTypeDescriptionRepository()); 00050 00051 ServiceTypeDescriptionRepository::ServiceTypeDescriptionRepository() 00052 { 00053 descriptionMap_ = map<std::string, shared_ptr<ServiceTypeDescription> >(); 00054 }; // ServiceDescriptionRepository() 00055 00056 shared_ptr<ServiceTypeDescriptionRepository> ServiceTypeDescriptionRepository::getInstance() 00057 { 00058 return instance_; 00059 }; // getInstance() 00060 00061 void ServiceTypeDescriptionRepository::addServiceTypeDescription (shared_ptr<ServiceTypeDescription> description) 00062 { 00063 00064 std::string serviceTypeName = description->getServiceTypeName(); 00065 00066 if (descriptionMap_.find (serviceTypeName) != descriptionMap_.end()) { 00067 std::stringstream ss; 00068 ss << "ServiceTypeDescriptionRepository::addServiceTypeDescription : ServiceDescriptionRepository already has a ServiceTypeDescription with serviceTypeName=\"" << serviceTypeName << "\""; 00069 throw Exception (ss.str()); 00070 }; 00071 00072 std::cerr << "ServiceTypeDescriptionRepository::addServiceTypeDescription(serviceTypeName=\"" << serviceTypeName << "\")" << std::endl; 00073 00074 descriptionMap_[serviceTypeName] = description; 00075 }; // addServiceTypeDescription() 00076 00077 00078 /*void ServiceDescriptionRepository::addServiceDescriptionsFromEntityDescriptionList(shared_ptr<XmlElement> e) 00079 { 00080 XmlElementVector entityVector = e->getChildrenElements(); 00081 00082 XmlElementVector::iterator entityIt; 00083 00084 for(entityIt = entityVector.begin(); entityIt!=entityVector.end(); entityIt++) 00085 { 00086 shared_ptr<XmlElement> entityElement = *entityIt; 00087 if(entityElement->getName().compare("entity") != 0) 00088 { 00089 throw Exception("ServiceDescriptionRepository::addServiceDescriptionsFromEntityDescriptionList : XML file incorrectly formatted : unable to get entity element"); 00090 } 00091 00092 XmlElementVector entityChildren = entityElement->getChildrenElements(); 00093 shared_ptr<XmlElement> entityNameElement = entityChildren.getFirstElementWithName("name"); 00094 if(entityNameElement == false) 00095 { 00096 throw Exception("ServiceDescriptionRepository::addServiceDescriptionsFromEntityDescriptionList : XML file incorrectly formatted : unable to get entity name element"); 00097 } 00098 00099 std::string entityName = (std::string)(*entityNameElement->getFirstNonBlankCharData().get()); 00100 00101 std::cerr << "ServiceDescriptionRepository::addServiceDescriptionsFromEntityDescriptionList : entityName=\"" << entityName << "\"" << std::endl; 00102 00103 XmlElementVector serviceVector = entityElement->getChildrenElements().getElementsWithName("service-description"); 00104 00105 XmlElementVector::iterator serviceIt; 00106 00107 for(serviceIt = serviceVector.begin(); serviceIt!=serviceVector.end(); serviceIt++) 00108 { 00109 shared_ptr<XmlElement> serviceDescriptionElement = * serviceIt; 00110 shared_ptr<ServiceDescription> serviceDescription = ServiceDescriptionFactory::buildServiceDescription(serviceDescriptionElement); 00111 std::string serviceName = serviceDescription->getServiceName(); 00112 00113 00114 std::cerr << "ServiceDescriptionRepository::addServiceDescriptionsFromEntityDescriptionList : adding: entityName=\"" << entityName << "\" serviceName=\"" << serviceName << "\"" << std::endl; 00115 this->addServiceDescription(entityName, serviceName, serviceDescription); 00116 }; // for serviceIt 00117 00118 }; // for entityIt 00119 00120 //throw Exception("ServiceDescriptionRepository::addServiceDescriptionsFromXmlDocument : not implemented"); 00121 }; // addServiceDescriptionsFromXmlDocument()*/ 00122 00123 shared_ptr<ServiceTypeDescription> ServiceTypeDescriptionRepository::getServiceTypeDescription (std::string serviceTypeName) 00124 { 00125 00126 shared_ptr<ServiceTypeDescription> description = descriptionMap_[serviceTypeName]; 00127 00128 if (description == false) { 00129 std::stringstream ss; 00130 ss << "ServiceTypeDescriptionRepository::getServiceTypeDescription : ServiceDescriptionRepository does not have a ServiceTypeDescription with serviceTypeName=\"" << serviceTypeName << "\""; 00131 throw Exception (ss.str()); 00132 } 00133 else { 00134 return description; 00135 } 00136 }; // getServiceTypeDescription()