| # 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
RootFileHandler.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IFileMgr.h"
2 #include "TFile.h"
3 #include "TWebFile.h"
4 #include "TROOT.h"
5 #include "TSSLSocket.h"
6
7 #include "RootFileHandler.h"
8 #include "GaudiKernel/MsgStream.h"
9
10
11 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13 RootFileHandler::RootFileHandler( IMessageSvc* msg, const std::string& p,
14 const std::string& c):
15 m_log(msg,"RootFileHandler"), m_userProxy(p), m_certDir(c),
16 m_ssl_setup(false) {
17
18 // Protect against multiple instances of TROOT
19 if ( 0 == gROOT ) {
20 static TROOT root("root","ROOT I/O");
21 }
22 m_level = msg->outputLevel("RootFileHandler");
23
24 }
25
26
27 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29 Io::open_t
30 RootFileHandler::openRootFile(const std::string& n, const Io::IoFlags& f,
31 const std::string& desc, Io::Fd& fd, void*& ptr) {
32
33 m_log.setLevel(m_level);
34
35 if (m_log.level() <= MSG::DEBUG)
36 m_log << MSG::DEBUG << "openRootFile(\"" << n << "\","
37 << f << "," << desc << ")"
38 << endmsg;
39
40 ptr = 0;
41 fd = -1;
42
43 std::string opt;
44
45
46 if (f == Io::READ) {
47 opt = "READ";
48 } else if ( f == (Io::WRITE | Io::CREATE | Io::EXCL) ) {
49 opt = "NEW";
50 } else if ( f == (Io::WRITE | Io::CREATE ) ) {
51 opt = "RECREATE";
52 } else if ( (f | Io::APPEND) != 0 ) {
53 opt = "UPDATE";
54 } else {
55 m_log << MSG::ERROR << "Don't know how to handle IoFlag "
56 << f << endmsg;
57 return 1;
58 }
59
60 TFile *tf(0);
61
62 if (n.find("https://") != std::string::npos ||
63 n.find("HTTPS://") != std::string::npos ||
64 n.find("http://") != std::string::npos ||
65 n.find("HTTP://") != std::string::npos ) {
66
67
68 if ( !f.isRead() ) {
69 m_log << MSG::ERROR << "can only open web files in READ mode. "
70 << "requested mode is: " << f
71 << endmsg;
72 return 1;
73 }
74
75 if (!m_ssl_setup && (n.find("https://") != std::string::npos ||
76 n.find("HTTPS://") != std::string::npos )) {
77
78 if (!setupSSL()) {
79 m_log << MSG::ERROR
80 << "Unable to setup TSSLSocket for ROOT TWebFile access over https"
81 << endmsg;
82 }
83 }
84
85
86 try {
87 tf = new TWebFile(n.c_str());
88 } catch (const std::exception& Exception) {
89 m_log << MSG::ERROR << "exception caught while trying to open root"
90 << " file for reading: " << Exception.what() << std::endl
91 << " -> file probably corrupt." << endmsg;
92 return 1;
93 } catch (...) {
94 m_log << MSG::ERROR << "Problems opening input file \"" << n
95 << "\": probably corrupt" << endmsg;
96 return 1;
97 }
98
99 if (tf != 0 && tf->IsZombie()) {
100 m_log << MSG::ERROR << "Problems opening input file \"" << n
101 << "\": file does not exist or in not accessible" << endmsg;
102 delete tf;
103 tf = 0;
104 return 1;
105 }
106
107
108 } else {
109
110 try {
111 tf = TFile::Open(n.c_str(),opt.c_str());
112 } catch (const std::exception& Exception) {
113 m_log << MSG::ERROR << "exception caught while trying to open root"
114 << " file for reading: " << Exception.what() << std::endl
115 << " -> file probably corrupt." << endmsg;
116 return 1;
117 } catch (...) {
118 m_log << MSG::ERROR << "Problems opening input file \"" << n
119 << "\": probably corrupt" << endmsg;
120 return 1;
121 }
122
123 }
124
125 if (tf == 0 || !tf->IsOpen()) {
126 m_log << MSG::ERROR << "Unable to open ROOT file \"" << n
127 << "\" with options \"" << opt << "\"" << endmsg;
128
129 delete tf;
130 tf = 0;
131 return 1;
132 }
133
134 fd = tf->GetFd();
135
136 ptr = (void*) tf;
137
138 if (m_log.level() <= MSG::DEBUG)
139 m_log << MSG::DEBUG << "opened TFile " << (void*) ptr << " Fd: " << fd
140 << endmsg;
141
142
143 return 0;
144
145 }
146
147 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
148
149 Io::close_t
150 RootFileHandler::closeRootFile(void* ptr) {
151
152 if (m_log.level() <= MSG::DEBUG)
153 m_log << MSG::DEBUG << "closeRootFile(ptr:" << ptr << ")"
154 << endmsg;
155
156 if (ptr == 0) {
157 m_log << MSG::ERROR << "Unable to close file: ptr == 0"
158 << endmsg;
159 return -1;
160 }
161
162 TFile* tf = (TFile*) ptr;
163
164 try {
165 tf->Close();
166 } catch (const std::exception& Exception) {
167 m_log << MSG::ERROR << "exception caught while trying to close root"
168 << " file" << Exception.what()
169 << endmsg;
170 return -1;
171 } catch (...) {
172 m_log << MSG::ERROR << "Problems closing ROOT file \"" << tf->GetName()
173 << "\"" << endmsg;
174 return -1;
175 }
176
177 return 0;
178
179 }
180
181
182 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
183
184 Io::reopen_t
185 RootFileHandler::reopenRootFile(void*, const Io::IoFlags&) {
186
187 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
188
189 return -1;
190
191 }
192
193 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
194
195 bool
196 RootFileHandler::setupSSL() {
197
198 if (m_log.level() <= MSG::DEBUG)
199 m_log << MSG::DEBUG << "setupSSL"
200 << endmsg;
201
202 // don't set anything up
203 if (m_userProxy == "NONE" || m_certDir == "NONE") {
204 m_ssl_setup = true;
205 return true;
206 }
207
208 // get stuff from $X509_USER_PROXY and $X509_CERT_DIR env vars
209 if (m_userProxy == "X509") {
210 if (!System::getEnv("X509_USER_PROXY", m_userProxy)) {
211 m_log << MSG::ERROR << "env var X509_USER_PROXY not set" << endmsg;
212 return false;
213 }
214 }
215
216 if (m_certDir == "X509") {
217 if (!System::getEnv("X509_CERT_DIR", m_certDir)) {
218 m_log << MSG::ERROR << "env var X509_CERT_DIR not set" << endmsg;
219 return false;
220 }
221 }
222
223 if (m_log.level() <= MSG::DEBUG)
224 m_log << MSG::DEBUG << "userProxy: " << m_userProxy
225 << " certDir: " << m_certDir
226 << endmsg;
227
228
229 TSSLSocket::SetUpSSL(m_userProxy.c_str(), m_certDir.c_str(),
230 m_userProxy.c_str(), m_userProxy.c_str());
231
232 m_ssl_setup = true;
233
234 return true;
235
236 }