Commit bbcc17e6 authored by Victor Kotlyar's avatar Victor Kotlyar
Browse files

Fixed usage of cta::excepion instead of castor::exception.

Removed unused files.
Added missed unittests.
parent 4f5e9ed5
......@@ -96,7 +96,7 @@ namespace cta { namespace common {
* Retrieves a configuration entry.
*
* Besides other possible exceptions, this method throws a
* castor::exception::NoEntry exception if the specified configuration
* cta::exception::NoEntry exception if the specified configuration
* entry is not in the configuration file.
*
* If this method is passed a logger object then this method will log the
......@@ -172,7 +172,7 @@ namespace cta { namespace common {
* Retrieves a configuration entry as an integer.
*
* Besides other possible exceptions, this method throws a
* castor::exception::NoEntry exception if the specified configuration
* cta::exception::NoEntry exception if the specified configuration
* entry is not in the configuration file.
*
* @param category category of the configuration parameter
......
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Exception.hpp"
#include "common/ProcessCap.hpp"
#include "castor/server/SmartCap.hpp"
#include "common/Utils.hpp"
#include <errno.h>
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::server::ProcessCap::~ProcessCap()
throw() {
}
//------------------------------------------------------------------------------
// getProcText
//------------------------------------------------------------------------------
std::string castor::server::ProcessCap::getProcText() {
try {
SmartCap cap(getProc());
return toText((cap_t)cap.get());
} catch(castor::exception::Exception &ne) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to get text representation of the capabilities of the process: "
<< ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// getProc
//------------------------------------------------------------------------------
cap_t castor::server::ProcessCap::getProc() {
cap_t cap = cap_get_proc();
if(NULL == cap) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to get the capabilities of the process: "
<< cta::utils::errnoToString(errno);
throw ex;
}
return cap;
}
//------------------------------------------------------------------------------
// toText
//------------------------------------------------------------------------------
std::string castor::server::ProcessCap::toText(
const cap_t cap) {
// Create a C++ string with the result of calling cap_to_text()
char *const text = cap_to_text(cap, NULL);
if(NULL == text) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to create string representation of capability state: "
<< cta::utils::errnoToString(errno);
throw ex;
}
std::string result(text);
// Free the memory allocated by cap_to_text()
if(cap_free(text)) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to free string representation of capability state: "
<< cta::utils::errnoToString(errno);
throw ex;
}
// Return the C++ string
return result;
}
//------------------------------------------------------------------------------
// setProcText
//------------------------------------------------------------------------------
void castor::server::ProcessCap::setProcText(const std::string &text) {
try {
SmartCap cap(fromText(text));
setProc(cap.get());
} catch(castor::exception::Exception &ne) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to set capabilities of process: " << ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// fromText
//------------------------------------------------------------------------------
cap_t castor::server::ProcessCap::fromText(const std::string &text) {
const cap_t cap = cap_from_text(text.c_str());
if(NULL == cap) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to create capability state from string representation"
": text='" << text << "': " << cta::utils::errnoToString(errno);
throw ex;
}
return cap;
}
//------------------------------------------------------------------------------
// setProc
//------------------------------------------------------------------------------
void castor::server::ProcessCap::setProc(const cap_t cap) {
if(cap_set_proc(cap)) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to set the capabilities of the process: "
<< cta::utils::errnoToString(errno);
throw ex;
}
}
......@@ -232,6 +232,7 @@ Unit tests and system tests with virtual tape drives
%attr(0755,root,root) %{_libdir}/libctatapeserverutilsunittests.so
%attr(0755,root,root) %{_libdir}/libctautilsunittests.so
%attr(0755,root,root) %{_libdir}/libctadaemonunittests.so
%attr(0755,root,root) %{_libdir}/libctamediachangerunittests.so
%attr(0755,root,root) %{_bindir}/cta-systemTests
%attr(0755,root,root) %{_libdir}/libctadaemonunittests-multiprocess.so
%attr(0644,root,root) %{_datadir}/%{name}-%{ctaVersion}/unittest/*.suppr
......
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
// Include Files
#include <vector>
#include <map>
#include "castor/exception/Exception.hpp"
// Local Includes
#include "ICnvFactory.hpp"
#include "BaseCnvSvc.hpp"
#include "Converters.hpp"
#include "IConverter.hpp"
#include "IAddress.hpp"
#include "IObject.hpp"
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
castor::BaseCnvSvc::BaseCnvSvc(const std::string name) :
BaseSvc(name) {}
// -----------------------------------------------------------------------
// Destructor
// -----------------------------------------------------------------------
castor::BaseCnvSvc::~BaseCnvSvc() throw() {
BaseCnvSvc::reset();
}
// -----------------------------------------------------------------------
// reset
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::reset() throw() {
// release all converters
std::map<unsigned int, IConverter*>::iterator it;
for (it = m_converters.begin(); it != m_converters.end(); it++) {
if (it->second != 0) {
delete it->second;
}
}
m_converters.clear();
// call parent's reset
BaseSvc::reset();
}
//-----------------------------------------------------------------------------
// addAlias
//-----------------------------------------------------------------------------
void castor::BaseCnvSvc::addAlias(const unsigned int alias,
const unsigned int real) {
m_aliases[alias] = real;
}
//-----------------------------------------------------------------------------
// removeAlias
//-----------------------------------------------------------------------------
void castor::BaseCnvSvc::removeAlias(const unsigned int id) {
m_aliases.erase(id);
}
//-----------------------------------------------------------------------------
// converter
//-----------------------------------------------------------------------------
castor::IConverter* castor::BaseCnvSvc::converter
(const unsigned int origObjType)
{
// First uses aliases
unsigned int objType = origObjType;
if (m_aliases.find(objType) != m_aliases.end()) {
objType = m_aliases[objType];
}
// check if we have one
IConverter* conv = m_converters[objType];
if (0 != conv) return conv;
// Try to find one
const castor::ICnvFactory* fac =
castor::Converters::instance()->cnvFactory(repType(), objType);
if (0 == fac) {
castor::exception::Exception e;
e.getMessage() << "No factory found for object type "
<< objType << " and representation type "
<< repType();
throw e;
}
m_converters[objType] = fac->instantiate(this);
if (0!= m_converters[objType]) return m_converters[objType];
// Throw an exception since we did not find any suitable converter
castor::exception::Exception e;
e.getMessage() << "No converter for object type "
<< objType << " and representation type "
<< repType();
throw e;
}
// -----------------------------------------------------------------------
// createRep
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::createRep(castor::IAddress* address,
castor::IObject* object,
bool endTransaction,
unsigned int type)
{
// If no object, nothing to create
if (0 != object) {
// Look for an adapted converter
// The converter is always valid if no exception is thrown
IConverter* conv = converter(object->type());
// convert
conv->createRep(address, object, endTransaction, type);
}
}
// -----------------------------------------------------------------------
// bulkCreateRep
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::bulkCreateRep(castor::IAddress* address,
std::vector<castor::IObject*> &objects,
bool endTransaction,
unsigned int type)
{
// If no object, nothing to create
if (objects.size() > 0) {
// Look for an adapted converter
// The converter is always valid if no exception is thrown
// Note that we assume that all objects have the same type
IConverter* conv = converter(objects[0]->type());
// convert
conv->bulkCreateRep(address, objects, endTransaction, type);
}
}
// -----------------------------------------------------------------------
// updateRep
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::updateRep(castor::IAddress* address,
castor::IObject* object,
bool endTransaction)
{
// If no object, nothing to update
if (0 != object) {
// Look for an adapted converter
// The converter is always valid if no exception is thrown
IConverter* conv = converter(object->type());
// convert
conv->updateRep(address, object, endTransaction);
}
}
// -----------------------------------------------------------------------
// deleteRep
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::deleteRep(castor::IAddress* address,
castor::IObject* object,
bool endTransaction)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
IConverter* conv = converter(object->type());
// convert
conv->deleteRep(address, object, endTransaction);
}
// -----------------------------------------------------------------------
// createObj
// -----------------------------------------------------------------------
castor::IObject* castor::BaseCnvSvc::createObj
(castor::IAddress* address)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
castor::IConverter* conv = converter(address->objType());
return conv->createObj(address);
}
// -----------------------------------------------------------------------
// bulkCreateObj
// -----------------------------------------------------------------------
std::vector<castor::IObject*> castor::BaseCnvSvc::bulkCreateObj
(castor::IAddress* address)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
castor::IConverter* conv = converter(address->objType());
return conv->bulkCreateObj(address);
}
// -----------------------------------------------------------------------
// updateObj
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::updateObj(castor::IObject* object)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
castor::IConverter* conv = converter(object->type());
return conv->updateObj(object);
}
//------------------------------------------------------------------------------
// fillRep
//------------------------------------------------------------------------------
void castor::BaseCnvSvc::fillRep(castor::IAddress* address,
castor::IObject* object,
unsigned int type,
bool endTransaction)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
castor::IConverter* conv = converter(object->type());
return conv->fillRep(address, object, type, endTransaction);
}
//------------------------------------------------------------------------------
// fillObj
//------------------------------------------------------------------------------
void castor::BaseCnvSvc::fillObj(castor::IAddress* address,
castor::IObject* object,
unsigned int type,
bool endTransaction)
{
// Look for an adapted converter
// The converter is always valid if no exception is thrown
castor::IConverter* conv = converter(object->type());
return conv->fillObj(address, object, type, endTransaction);
}
// -----------------------------------------------------------------------
// deleteRepByAddress
// -----------------------------------------------------------------------
void castor::BaseCnvSvc::deleteRepByAddress (castor::IAddress* address,
bool endTransaction)
{
castor::IObject* obj = createObj(address);
address->setObjType(obj->type());
deleteRep(address, obj, endTransaction);
}
//------------------------------------------------------------------------------
// commit
//------------------------------------------------------------------------------
void castor::BaseCnvSvc::commit()
{
// Default implementation, does nothing.
}
//------------------------------------------------------------------------------
// rollback
//------------------------------------------------------------------------------
void castor::BaseCnvSvc::rollback()
{
// Default implementation, does nothing.
}
//------------------------------------------------------------------------------
// createStatement
//------------------------------------------------------------------------------
castor::db::IDbStatement* castor::BaseCnvSvc::createStatement(const std::string&)
{
// Default implementation, does nothing.
return 0;
}
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
// Include Files
#include "castor/IClientFactory.hpp"
#include "castor/rh/Client.hpp"
#include "castor/IClient.hpp"
#include "castor/Constants.hpp"
#include "castor/exception/Exception.hpp"
#include "castor/exception/InvalidArgument.hpp"
#include <sstream>
//------------------------------------------------------------------------------
// client2String
//------------------------------------------------------------------------------
const std::string castor::IClientFactory::client2String
(const castor::IClient &cl)
{
switch (cl.type()) {
case castor::OBJ_Client :
{
const castor::rh::Client *c =
dynamic_cast<const castor::rh::Client*>(&cl);
std::ostringstream res;
res << cl.type() << ":"
<< ((c->ipAddress() & 0xFF000000) >> 24) << "."
<< ((c->ipAddress() & 0x00FF0000) >> 16) << "."
<< ((c->ipAddress() & 0x0000FF00) >> 8) << "."
<< ((c->ipAddress() & 0x000000FF))
<< ":" << c->port();
return res.str();
}
default:
castor::exception::InvalidArgument e;
e.getMessage() << "Unknown Client type "
<< castor::ObjectsIdStrings[cl.type()]
<< std::endl
<< "Cannot convert to string";
throw e;
}
}
//------------------------------------------------------------------------------
// string2Client
//------------------------------------------------------------------------------
castor::IClient* castor::IClientFactory::string2Client(const std::string &st)
{
std::istringstream in(st);
unsigned int type;
in >> type;
if (in.good()) {
switch (type) {
case castor::OBJ_Client :
{
char c = 0;
in >> c;
if (c == ':') {
unsigned short a[4];
for (int i = 0; i < 4; i++) {
in >> a[i];
if (a[i] > 255) {
castor::exception::InvalidArgument e;
e.getMessage() << "Invalid IP address in Client string : \""
<< st << "\"" << std::endl
<< "Cannot convert string to Client";
throw e;
}
if (i < 3) {
c = 0;
in >> c;
if (c != '.') {
castor::exception::InvalidArgument e;
e.getMessage() << "Invalid IP address in Client string : \""
<< st << "\"" << std::endl
<< "Cannot convert string to Client";
throw e;
}
}
}
c = 0;
in >> c;
if (c == ':') {
unsigned short port = 0;
in >> port;
if (in) {
castor::rh::Client *res = new castor::rh::Client();
unsigned long ip = a[0];
ip <<= 8;
ip += a[1];
ip <<= 8;
ip += a[2];
ip <<= 8;
ip += a[3];
res->setIpAddress(ip);
res->setPort(port);
return res;
}
}
}
}
break;
default:
castor::exception::InvalidArgument e;
e.getMessage() << "Unknown type " << type
<< " for a client" << std::endl
<< "Cannot convert string to IClient";
throw e;
}
}
castor::exception::InvalidArgument e;
e.getMessage() << "Unable to parse Client string : \""
<< st << "\"" << std::endl
<< "Cannot create Object";
throw e;
}
This diff is collapsed.
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
// Include Files