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 
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  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 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
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
66  log << MSG::ERROR << "No handler specified for fallback protocol prefix "
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 //------------------------------------------------------------------------------
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 }
SmartIF< IToolSvc > m_toolSvc
Handle to the tool service.
Definition: VFSSvc.h:60
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
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:26
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
tuple c
Definition: gaudirun.py:341
virtual StatusCode initialize()
Initialize Service.
Definition: VFSSvc.cpp:24
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:72
virtual ~VFSSvc()
Destructor.
Definition: VFSSvc.cpp:22
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:35
iterator end()
Definition: Map.h:131
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual const std::vector< std::string > & protocols() const
Definition: VFSSvc.cpp:127
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
iterator find(const key_type &key)
Definition: Map.h:148
prot
Definition: ana.py:53
virtual StatusCode finalize()
Finalize Service.
Definition: VFSSvc.cpp:76
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
iterator begin()
Definition: Map.h:130
std::list< IAlgTool * > m_acquiredTools
List of acquired tools (needed to release them).
Definition: VFSSvc.h:63
virtual unsigned long release()=0
Release Interface instance.
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:74
std
AIDA -> ROTO converter.
Definition: GaudiAlgs.py:73
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Abstract interface for a service or tool implementing a read access to files.
Definition: IFileAccess.h:19
Templated class to add the standard messaging functionalities.
void clear()
Definition: Map.h:176
Simple service that allows to read files independently from the storage.
Definition: VFSSvc.h:27
virtual const std::vector< std::string > & protocols() const =0
Protocols supported by the instance.
list i
Definition: ana.py:128
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:171
virtual std::auto_ptr< std::istream > open(const std::string &url)
Definition: VFSSvc.cpp:92
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:199
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
std::vector< std::string > m_urlHandlersNames
Names of the handlers to use.
Definition: VFSSvc.h:48
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.