| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 m_urlHandlersNames.push_back("FileReadTool");
14
15 declareProperty("FileAccessTools",m_urlHandlersNames,
16 "List of tools implementing the IFileAccess interface.");
17
18 declareProperty("FallBackProtocol",m_fallBackProtocol = "file",
19 "URL prefix to use if the prefix is not present.");
20 }
21 //------------------------------------------------------------------------------
22 VFSSvc::~VFSSvc(){}
23 //------------------------------------------------------------------------------
24 StatusCode VFSSvc::initialize() {
25 StatusCode sc = Service::initialize();
26 if (sc.isFailure()){
27 return sc;
28 }
29
30 MsgStream log(msgSvc(), name());
31
32 m_toolSvc = serviceLocator()->service("ToolSvc");
33 if (!m_toolSvc.isValid()){
34 log << MSG::ERROR << "Cannot locate ToolSvc" << endmsg;
35 return StatusCode::FAILURE;
36 }
37
38 IAlgTool *tool;
39 IFileAccess *hndlr;
40 std::vector<std::string>::iterator i;
41 for(i = m_urlHandlersNames.begin(); i != m_urlHandlersNames.end(); ++i){
42 // retrieve the tool and the pointer to the interface
43 sc = m_toolSvc->retrieve(*i,IAlgTool::interfaceID(),tool,0,true);
44 if (sc.isFailure()){
45 log << MSG::ERROR << "Cannot get tool " << *i << endmsg;
46 return sc;
47 }
48 m_acquiredTools.push_front(tool); // this is one tool that we will have to release
49 sc = tool->queryInterface(IFileAccess::interfaceID(),pp_cast<void>(&hndlr));
50 if (sc.isFailure()){
51 log << MSG::ERROR << *i << " does not implement IFileAccess" << endmsg;
52 return sc;
53 }
54 // We do not need to increase the reference count for the IFileAccess interface
55 // because we hold the tool by its IAlgTool interface.
56 hndlr->release();
57 // loop over the list of supported protocols and add them to the map (for quick access)
58 for ( std::vector<std::string>::const_iterator prot = hndlr->protocols().begin();
59 prot != hndlr->protocols().end(); ++prot ){
60 m_urlHandlers[*prot] = hndlr;
61 }
62 }
63
64 // Now let's check if we can handle the fallback protocol
65 if ( m_urlHandlers.find(m_fallBackProtocol) == m_urlHandlers.end() ) {
66 log << MSG::ERROR << "No handler specified for fallback protocol prefix "
67 << m_fallBackProtocol << endmsg;
68 return StatusCode::FAILURE;
69 }
70
71 // Note: the list of handled protocols will be filled only if requested
72
73 return sc;
74 }
75 //------------------------------------------------------------------------------
76 StatusCode VFSSvc::finalize() {
77 m_urlHandlers.clear(); // clear the map
78 m_protocols.clear();
79
80 if (m_toolSvc) {
81 // release the acquired tools (from the last acquired one)
82 while ( m_acquiredTools.begin() != m_acquiredTools.end() ){
83 m_toolSvc->releaseTool(*m_acquiredTools.begin()).ignore();
84 m_acquiredTools.pop_front();
85 }
86 m_toolSvc->release(); // release the tool service
87 m_toolSvc = 0;
88 }
89 return Service::finalize();
90 }
91 //------------------------------------------------------------------------------
92 std::auto_ptr<std::istream> VFSSvc::open(const std::string &url){
93
94 // get the url prefix endpos
95 std::string::size_type pos = url.find("://");
96
97 if (std::string::npos == pos) { // no url prefix
98 return m_urlHandlers[m_fallBackProtocol]->open(url);
99 }
100
101 std::string url_prefix(url,0,pos);
102 if ( m_urlHandlers.find(url_prefix) == m_urlHandlers.end() ) {
103 // if we do not have a handler for the URL prefix,
104 // use the fall back one and pass only the path
105 return m_urlHandlers[m_fallBackProtocol]->open(url.substr(pos+3));
106 }
107 return m_urlHandlers[url_prefix]->open(url);
108 }
109 //------------------------------------------------------------------------------
110 namespace {
113 template <class Container>
114 struct append_key
115 {
116 append_key(Container &C):c(C){}
117
118 template <class PAIR>
119 void operator() (const PAIR &x)
120 {
121 c.push_back(x.first);
122 }
123
124 Container &c;
125 };
126 }
127 const std::vector<std::string> &VFSSvc::protocols() const
128 {
129 if (m_protocols.empty()){
130 // prepare the list of handled protocols
131 std::for_each(m_urlHandlers.begin(),m_urlHandlers.end(),
132 append_key<std::vector<std::string> >(const_cast<VFSSvc*>(this)->m_protocols));
133 }
134 return m_protocols;
135 }