| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

VFSSvc.cpp
Go to the documentation of this file.
1 #include "VFSSvc.h"
2
3 #include "GaudiKernel/IAlgTool.h"
4 #include "GaudiKernel/IToolSvc.h"
5 #include "GaudiKernel/MsgStream.h"
6
7 DECLARE_COMPONENT( VFSSvc )
8
9 //------------------------------------------------------------------------------
10 StatusCode VFSSvc::initialize()
11 {
12 StatusCode sc = Service::initialize();
13 if ( sc.isFailure() ) return sc;
14
15 m_toolSvc = serviceLocator()->service( "ToolSvc" );
16 if ( !m_toolSvc ) {
17 error() << "Cannot locate ToolSvc" << endmsg;
18 return StatusCode::FAILURE;
19 }
20
21 IAlgTool* tool;
22 for ( const auto& i : m_urlHandlersNames ) {
23 // retrieve the tool and the pointer to the interface
24 sc = m_toolSvc->retrieve( i, IAlgTool::interfaceID(), tool, nullptr, true );
25 if ( sc.isFailure() ) {
26 error() << "Cannot get tool " << i << endmsg;
27 return sc;
28 }
29 m_acquiredTools.push_back( tool ); // this is one tool that we will have to release
30 auto hndlr = SmartIF<IFileAccess>( tool );
31 if ( !hndlr ) {
32 error() << i << " does not implement IFileAccess" << endmsg;
33 return StatusCode::FAILURE;
34 }
35 // We do not need to increase the reference count for the IFileAccess interface
36 // because we hold the tool by its IAlgTool interface.
37 // loop over the list of supported protocols and add them to the map (for quick access)
38 for ( const auto& prot : hndlr->protocols() ) m_urlHandlers[prot].emplace_back( hndlr.get() );
39 }
40
41 // Now let's check if we can handle the fallback protocol
42 if ( m_urlHandlers.find( m_fallBackProtocol ) == m_urlHandlers.end() ) {
43 error() << "No handler specified for fallback protocol prefix " << m_fallBackProtocol.value() << endmsg;
44 return StatusCode::FAILURE;
45 }
46
47 // Note: the list of handled protocols will be filled only if requested
48
49 return sc;
50 }
51 //------------------------------------------------------------------------------
52 StatusCode VFSSvc::finalize()
53 {
54 m_urlHandlers.clear(); // clear the map
55 m_protocols.clear();
56
57 if ( m_toolSvc ) {
58 // release the acquired tools (from the last acquired one)
59 while ( !m_acquiredTools.empty() ) {
60 m_toolSvc->releaseTool( m_acquiredTools.back() ).ignore();
61 m_acquiredTools.pop_back();
62 }
63 m_toolSvc.reset(); // release the tool service
64 }
65 return Service::finalize();
66 }
67 //------------------------------------------------------------------------------
68 std::unique_ptr<std::istream> VFSSvc::open( const std::string& url )
69 {
70 // get the url prefix endpos
71 auto pos = url.find( "://" );
72
73 if ( std::string::npos == pos ) {
74 // no url prefix, try fallback protocol
75 return VFSSvc::open( m_fallBackProtocol + "://" + url );
76 }
77
78 const std::string url_prefix( url, 0, pos );
79 const auto handlers = m_urlHandlers.find( url_prefix );
80 if ( handlers == m_urlHandlers.end() ) {
81 // if we do not have a handler for the URL prefix,
82 // use the fall back one
83 return VFSSvc::open( m_fallBackProtocol + url.substr( pos ) );
84 }
85
86 std::unique_ptr<std::istream> out; // this might help RVO
87 // try the hendlers for the protocol one after the other until one succeds
88 for ( auto hndlr : handlers->second ) {
89 out = hndlr->open( url );
90 if ( out ) break;
91 }
92 return out;
93 }
94 //------------------------------------------------------------------------------
95 namespace
96 {
99 constexpr struct select1st_t {
100 template <typename S, typename T>
101 const S& operator()( const std::pair<S, T>& x ) const
102 {
103 return x.first;
104 }
105 } select1st{};
106 }
107
108 const std::vector<std::string>& VFSSvc::protocols() const
109 {
110 if ( m_protocols.empty() ) {
111 // prepare the list of handled protocols
112 std::transform( m_urlHandlers.begin(), m_urlHandlers.end(),
113 std::back_inserter( const_cast<VFSSvc*>( this )->m_protocols ), select1st );
114 }
115 return m_protocols;
116 }
VFSSvc::m_toolSvc
SmartIF< IToolSvc > m_toolSvc
Handle to the tool service.
Definition: VFSSvc.h:58
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:64
SmartIF< IFileAccess >
VFSSvc.h
VFSSvc::open
std::unique_ptr< std::istream > open(const std::string &url) override
Definition: VFSSvc.cpp:68
std::vector::empty
T empty(T...args)
VFSSvc::m_protocols
std::vector< std::string > m_protocols
Protocols registered.
Definition: VFSSvc.h:52
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:174
MsgStream.h
VFSSvc::protocols
const std::vector< std::string > & protocols() const override
Definition: VFSSvc.cpp:108
IToolSvc.h
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:61
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
std::string
STL class.
GaudiUtils::Map::end
iterator end()
Definition: Map.h:134
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:151
std::vector::pop_back
T pop_back(T...args)
IAlgTool.h
VFSSvc::m_fallBackProtocol
Gaudi::Property< std::string > m_fallBackProtocol
Definition: VFSSvc.h:48
std::vector::clear
T clear(T...args)
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:133
std::pair
std::string::find
T find(T...args)
std::vector< std::string >
std::unique_ptr
STL class.
Gaudi::Functional::operator()
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
VFSSvc::finalize
StatusCode finalize() override
Finalize Service.
Definition: VFSSvc.cpp:52
std::back_inserter
T back_inserter(T...args)
VFSSvc::m_acquiredTools
std::vector< IAlgTool * > m_acquiredTools
List of acquired tools (needed to release them).
Definition: VFSSvc.h:61
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
std::vector::back
T back(T...args)
std::string::substr
T substr(T...args)
GaudiUtils::Map::clear
void clear()
Definition: Map.h:195
IToolSvc::releaseTool
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
std::transform
T transform(T...args)
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
VFSSvc
Simple service that allows to read files independently from the storage.
Definition: VFSSvc.h:26
StatusCode::FAILURE
Definition: StatusCode.h:29
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
VFSSvc::m_urlHandlers
GaudiUtils::HashMap< std::string, std::vector< IFileAccess * > > m_urlHandlers
Map of the tools handling the known protocols.
Definition: VFSSvc.h:55