VFSSvc.cpp
Go to the documentation of this file.
1 #include "VFSSvc.h"
2 
4 #include "GaudiKernel/IToolSvc.h"
5 #include "GaudiKernel/IAlgTool.h"
6 
8 
9 //------------------------------------------------------------------------------
10 VFSSvc::VFSSvc(const std::string& name, ISvcLocator* svc):
11  base_class(name,svc)
12 {
13  declareProperty("FileAccessTools",m_urlHandlersNames,
14  "List of tools implementing the IFileAccess interface.");
15 
16  declareProperty("FallBackProtocol",m_fallBackProtocol = "file",
17  "URL prefix to use if the prefix is not present.");
18 }
19 //------------------------------------------------------------------------------
22  if (sc.isFailure()) return sc;
23 
24 
25  m_toolSvc = serviceLocator()->service("ToolSvc");
26  if (!m_toolSvc){
27  error() << "Cannot locate ToolSvc" << endmsg;
28  return StatusCode::FAILURE;
29  }
30 
31  IAlgTool *tool;
32  for(const auto& i : m_urlHandlersNames) {
33  // retrieve the tool and the pointer to the interface
34  sc = m_toolSvc->retrieve(i,IAlgTool::interfaceID(),tool,nullptr,true);
35  if (sc.isFailure()){
36  error() << "Cannot get tool " << i << endmsg;
37  return sc;
38  }
39  m_acquiredTools.push_back(tool); // this is one tool that we will have to release
40  auto hndlr = SmartIF<IFileAccess>(tool);
41  if (!hndlr){
42  error() << i << " does not implement IFileAccess" << endmsg;
43  return StatusCode::FAILURE;
44  }
45  // We do not need to increase the reference count for the IFileAccess interface
46  // because we hold the tool by its IAlgTool interface.
47  // loop over the list of supported protocols and add them to the map (for quick access)
48  for ( const auto& prot : hndlr->protocols() ) m_urlHandlers[prot] = hndlr.get();
49  }
50 
51  // Now let's check if we can handle the fallback protocol
53  error() << "No handler specified for fallback protocol prefix "
55  return StatusCode::FAILURE;
56  }
57 
58  // Note: the list of handled protocols will be filled only if requested
59 
60  return sc;
61 }
62 //------------------------------------------------------------------------------
64  m_urlHandlers.clear(); // clear the map
66 
67  if (m_toolSvc) {
68  // release the acquired tools (from the last acquired one)
69  while ( !m_acquiredTools.empty() ){
72  }
73  m_toolSvc.reset() ; // release the tool service
74  }
75  return Service::finalize();
76 }
77 //------------------------------------------------------------------------------
79 
80  // get the url prefix endpos
81  auto pos = url.find("://");
82 
83  if (std::string::npos == pos) { // no url prefix
84  return m_urlHandlers[m_fallBackProtocol]->open(url);
85  }
86 
87  std::string url_prefix(url,0,pos);
88  if ( m_urlHandlers.find(url_prefix) == m_urlHandlers.end() ) {
89  // if we do not have a handler for the URL prefix,
90  // use the fall back one and pass only the path
91  return m_urlHandlers[m_fallBackProtocol]->open(url.substr(pos+3));
92  }
93  return m_urlHandlers[url_prefix]->open(url);
94 }
95 //------------------------------------------------------------------------------
96 namespace {
99  constexpr struct select1st_t
100  {
101  template <typename S, typename T>
102  const S& operator() (const std::pair<S,T> &x) const { return x.first; }
103  } select1st {} ;
104 }
105 
107 {
108  if (m_protocols.empty()){
109  // prepare the list of handled protocols
111  std::back_inserter(const_cast<VFSSvc*>(this)->m_protocols),
112  select1st );
113  }
114  return m_protocols;
115 }
SmartIF< IToolSvc > m_toolSvc
Handle to the tool service.
Definition: VFSSvc.h:60
StatusCode initialize() override
Definition: Service.cpp:68
virtual StatusCode retrieve(const std::string &type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent=0, bool createIf=true)=0
Retrieve tool with tool dependent part of the name automatically assigned.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
std::unique_ptr< std::istream > open(const std::string &url) override
Definition: VFSSvc.cpp:78
T empty(T...args)
std::vector< std::string > m_protocols
Protocols registered.
Definition: VFSSvc.h:51
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode finalize() override
Definition: Service.cpp:193
StatusCode initialize() override
Initialize Service.
Definition: VFSSvc.cpp:20
constexpr struct select1st_t select1st
STL namespace.
std::string m_fallBackProtocol
Protocol to use in case there is not a specific tool to handle the URL.
Definition: VFSSvc.h:54
GaudiUtils::HashMap< std::string, IFileAccess * > m_urlHandlers
Map of the tools handling the known protocols.
Definition: VFSSvc.h:57
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
iterator end()
Definition: Map.h:132
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
iterator find(const key_type &key)
Definition: Map.h:149
T pop_back(T...args)
prot
Definition: ana.py:53
T clear(T...args)
iterator begin()
Definition: Map.h:131
T find(T...args)
STL class.
StatusCode finalize() override
Finalize Service.
Definition: VFSSvc.cpp:63
T back_inserter(T...args)
std::vector< IAlgTool * > m_acquiredTools
List of acquired tools (needed to release them).
Definition: VFSSvc.h:63
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
T back(T...args)
T substr(T...args)
void clear()
Definition: Map.h:178
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:88
Simple service that allows to read files independently from the storage.
Definition: VFSSvc.h:26
const std::vector< std::string > & protocols() const override
Definition: VFSSvc.cpp:106
list i
Definition: ana.py:128
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::vector< std::string > m_urlHandlersNames
Names of the handlers to use.
Definition: VFSSvc.h:48