| The Gaudi Framework v27r1

- 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/MsgStream.h"
4 #include "GaudiKernel/IToolSvc.h"
5 #include "GaudiKernel/IAlgTool.h"
6
7 DECLARE_COMPONENT(VFSSvc)
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 //------------------------------------------------------------------------------
20 StatusCode VFSSvc::initialize() {
21 StatusCode sc = Service::initialize();
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
52 if ( m_urlHandlers.find(m_fallBackProtocol) == m_urlHandlers.end() ) {
53 error() << "No handler specified for fallback protocol prefix "
54 << m_fallBackProtocol << endmsg;
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 //------------------------------------------------------------------------------
63 StatusCode VFSSvc::finalize() {
64 m_urlHandlers.clear(); // clear the map
65 m_protocols.clear();
66
67 if (m_toolSvc) {
68 // release the acquired tools (from the last acquired one)
69 while ( !m_acquiredTools.empty() ){
70 m_toolSvc->releaseTool(m_acquiredTools.back()).ignore();
71 m_acquiredTools.pop_back();
72 }
73 m_toolSvc.reset() ; // release the tool service
74 }
75 return Service::finalize();
76 }
77 //------------------------------------------------------------------------------
78 std::unique_ptr<std::istream> VFSSvc::open(const std::string &url){
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
106 const std::vector<std::string> &VFSSvc::protocols() const
107 {
108 if (m_protocols.empty()){
109 // prepare the list of handled protocols
110 std::transform( m_urlHandlers.begin(),m_urlHandlers.end(),
111 std::back_inserter(const_cast<VFSSvc*>(this)->m_protocols),
112 select1st );
113 }
114 return m_protocols;
115 }
VFSSvc::m_toolSvc
SmartIF< IToolSvc > m_toolSvc
Handle to the tool service.
Definition: VFSSvc.h:60
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:68
IToolSvc::retrieve
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< IFileAccess >
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
VFSSvc.h
VFSSvc::open
std::unique_ptr< std::istream > open(const std::string &url) override
Definition: VFSSvc.cpp:78
std::vector::empty
T empty(T...args)
VFSSvc::m_protocols
std::vector< std::string > m_protocols
Protocols registered.
Definition: VFSSvc.h:51
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:193
MsgStream.h
VFSSvc::initialize
StatusCode initialize() override
Initialize Service.
Definition: VFSSvc.cpp:20
select1st
constexpr struct select1st_t select1st
std
STL namespace.
VFSSvc::m_fallBackProtocol
std::string m_fallBackProtocol
Protocol to use in case there is not a specific tool to handle the URL.
Definition: VFSSvc.h:54
VFSSvc::m_urlHandlers
GaudiUtils::HashMap< std::string, IFileAccess * > m_urlHandlers
Map of the tools handling the known protocols.
Definition: VFSSvc.h:57
merge_files.sc
int sc
Definition: merge_files.py:167
IToolSvc.h
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::string
STL class.
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
std::vector::push_back
T push_back(T...args)
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:118
GaudiUtils::Map::end
iterator end()
Definition: Map.h:132
StatusCode::FAILURE
Definition: StatusCode.h:29
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:149
std::vector::pop_back
T pop_back(T...args)
ana.prot
prot
Definition: ana.py:53
IAlgTool.h
std::vector::clear
T clear(T...args)
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:131
std::pair
std::string::find
T find(T...args)
std::vector< std::string >
std::unique_ptr
STL class.
VFSSvc::finalize
StatusCode finalize() override
Finalize Service.
Definition: VFSSvc.cpp:63
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:63
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
std::vector::back
T back(T...args)
CommonMessaging
Definition: CommonMessaging.h:58
std::string::substr
T substr(T...args)
GaudiUtils::Map::clear
void clear()
Definition: Map.h:178
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:88
VFSSvc
Simple service that allows to read files independently from the storage.
Definition: VFSSvc.h:26
VFSSvc::protocols
const std::vector< std::string > & protocols() const override
Definition: VFSSvc.cpp:106
ana.i
list i
Definition: ana.py:128
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
VFSSvc::m_urlHandlersNames
std::vector< std::string > m_urlHandlersNames
Names of the handlers to use.
Definition: VFSSvc.h:48