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