]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBManager.cxx
Improved diagnostic messages. Different access strings for AliCDBGrid. New or renamed...
[u/mrichter/AliRoot.git] / STEER / AliCDBManager.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15 //-------------------------------------------------------------------------
16 //   Implementation of AliCDBManager and AliCDBParam classe
17 //   Author: Alberto Colla 
18 //   e-mail: Alberto.Colla@cern.ch
19 //-------------------------------------------------------------------------
20
21 #include "AliCDBManager.h"
22 #include "AliCDBStorage.h"
23 #include "AliLog.h"
24 #include "AliCDBDump.h"
25 #include "AliCDBLocal.h"
26 #include "AliCDBGrid.h"
27 //#include "AliCDBEntry.h"
28
29 //#include <TObjString.h>
30 #include <TSystem.h>
31
32 ClassImp(AliCDBParam)
33
34 ClassImp(AliCDBManager)
35
36 AliCDBManager* AliCDBManager::fgInstance = 0x0;
37
38 //_____________________________________________________________________________
39 AliCDBManager* AliCDBManager::Instance() {
40 // returns AliCDBManager instance (singleton)
41
42         if (!fgInstance) {
43                 fgInstance = new AliCDBManager();
44                 fgInstance->Init();
45         }
46
47         return fgInstance;
48 }
49
50 //_____________________________________________________________________________
51 void AliCDBManager::Init() {
52 // factory registering
53
54         RegisterFactory(new AliCDBDumpFactory());
55         RegisterFactory(new AliCDBLocalFactory()); 
56         // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root
57         if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes
58                 AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered.");
59                 RegisterFactory(new AliCDBGridFactory());
60         }
61 }
62 //_____________________________________________________________________________
63 void AliCDBManager::Destroy() {
64 // delete ALCDBManager instance and active storages
65
66         if (fgInstance) {
67                 delete fgInstance;
68                 fgInstance = 0x0;
69         }
70 }
71
72 //_____________________________________________________________________________
73 AliCDBManager::AliCDBManager():
74         fDefaultStorage(NULL),
75         fDrainStorage(NULL)
76 {
77 // default constuctor
78         fFactories.SetOwner();
79 }
80
81 //_____________________________________________________________________________
82 AliCDBManager::~AliCDBManager() {
83 // destructor
84         DestroyActiveStorages();
85         fDrainStorage = 0x0;
86         fDefaultStorage = 0x0;
87 }
88
89 //_____________________________________________________________________________
90 AliCDBStorage* AliCDBManager::GetActiveStorage(const AliCDBParam* param) {
91 // get a storage object from the list of active storages 
92
93         return (AliCDBStorage*) fActiveStorages.GetValue(param);
94 }
95
96 //_____________________________________________________________________________
97 void AliCDBManager::PutActiveStorage(AliCDBParam* param, AliCDBStorage* storage){
98 // put a storage object into the list of active storages
99
100         fActiveStorages.Add(param, storage);
101         AliDebug(1, Form("Active storages: %d", fActiveStorages.GetEntries()));
102 }
103
104 //_____________________________________________________________________________
105 void AliCDBManager::RegisterFactory(AliCDBStorageFactory* factory) {
106 // add a storage factory to the list of registerd factories
107  
108         if (!fFactories.Contains(factory)) {
109                 fFactories.Add(factory);
110         }
111 }
112
113 //_____________________________________________________________________________
114 Bool_t AliCDBManager::HasStorage(const char* dbString) const {
115 // check if dbString is a URI valid for one of the registered factories 
116
117         TIter iter(&fFactories);
118
119         AliCDBStorageFactory* factory;
120         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
121
122                 if (factory->Validate(dbString)) {
123                         return kTRUE;
124                 }       
125         }
126
127         return kFALSE;
128 }
129
130 //_____________________________________________________________________________
131 AliCDBParam* AliCDBManager::CreateParameter(const char* dbString) const {
132 // create AliCDBParam object from URI string
133
134         TIter iter(&fFactories);
135
136         AliCDBStorageFactory* factory;
137         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
138
139                 AliCDBParam* param = factory->CreateParameter(dbString);
140                 if (param) {
141                         return param;
142                 }
143         }
144
145         return NULL;
146 }
147
148 //_____________________________________________________________________________
149 AliCDBStorage* AliCDBManager::GetStorage(const char* dbString) {
150 // get storage object from URI string
151         
152         AliCDBParam* param = CreateParameter(dbString);
153         if (!param) {
154                 return NULL;
155         }       
156
157         AliCDBStorage* aStorage = GetStorage(param);
158
159         delete param;
160         
161         return aStorage;
162 }
163
164 //_____________________________________________________________________________
165 AliCDBStorage* AliCDBManager::GetStorage(const AliCDBParam* param) {
166 // get storage object from AliCDBParam object
167
168         // if the list of active storages already contains 
169         // the requested storage, return it
170         AliCDBStorage* aStorage = GetActiveStorage(param);
171         if (aStorage) {
172                 return aStorage;
173         }
174
175         TIter iter(&fFactories);
176
177         AliCDBStorageFactory* factory;
178         
179         // loop on the list of registered factories
180         while ((factory = (AliCDBStorageFactory*) iter.Next())) {
181
182                 // each factory tries to create its storage from the parameter
183                 aStorage = factory->Create(param);
184                 if (aStorage) {
185                         PutActiveStorage(param->CloneParam(), aStorage);
186                         // if default storage is not set, set to this storage
187                         if(!fDefaultStorage){
188                                 fDefaultStorage=aStorage;
189                                 AliInfo(Form("Default storage set to: %s",(param->GetURI()).Data()));
190                         }
191                         return aStorage;
192                 }
193         }
194
195         return NULL;
196 }
197
198 //_____________________________________________________________________________
199 TList* AliCDBManager::GetActiveStorages() {
200 // return list of active storages
201
202         TList* result = new TList();
203
204         TIter iter(fActiveStorages.GetTable()); 
205         TPair* aPair;
206         while ((aPair = (TPair*) iter.Next())) {
207                 result->Add(aPair->Value());
208         }
209
210         return result;
211 }
212
213 //_____________________________________________________________________________
214 void AliCDBManager::SetDrain(const char* dbString) {
215 // set drain storage from URI string
216
217         fDrainStorage = GetStorage(dbString);   
218 }
219
220 //_____________________________________________________________________________
221 void AliCDBManager::SetDrain(const AliCDBParam* param) {
222 // set drain storage from AliCDBParam
223         
224         fDrainStorage = GetStorage(param);
225 }
226
227 //_____________________________________________________________________________
228 void AliCDBManager::SetDrain(AliCDBStorage* storage) {
229 // set drain storage from another active storage
230         
231         fDrainStorage = storage;
232 }
233
234 //_____________________________________________________________________________
235 Bool_t AliCDBManager::Drain(AliCDBEntry *entry) {
236 // drain retrieved object to drain storage
237
238         AliInfo("Draining into drain storage...");
239         return fDrainStorage->Put(entry);
240 }
241
242 //_____________________________________________________________________________
243 void AliCDBManager::SetDefaultStorage(const char* dbString) {
244 // sets default storage from URI string
245
246         fDefaultStorage = GetStorage(dbString); 
247 }
248
249 //_____________________________________________________________________________
250 void AliCDBManager::SetDefaultStorage(const AliCDBParam* param) {
251 // set default storage from AliCDBParam object
252         
253         fDrainStorage = GetStorage(param);
254 }
255
256 //_____________________________________________________________________________
257 void AliCDBManager::SetDefaultStorage(AliCDBStorage* storage) {
258 // set default storage from another active storage
259         
260         fDefaultStorage = storage;
261 }
262
263
264 //_____________________________________________________________________________
265 void AliCDBManager::DestroyActiveStorages() {
266 // delete list of active storages
267
268         fActiveStorages.DeleteAll();
269 }
270
271 //_____________________________________________________________________________
272 void AliCDBManager::DestroyActiveStorage(AliCDBStorage* /*storage*/) {
273 // destroys active storage
274
275 /*
276         TIter iter(fActiveStorages.GetTable()); 
277         TPair* aPair;
278         while ((aPair = (TPair*) iter.Next())) {
279                 if(storage == (AliCDBStorage*) aPair->Value())
280                         delete fActiveStorages.Remove(aPair->Key());
281                         storage->Delete(); storage=0x0;
282         }
283 */      
284
285 }
286
287 ///////////////////////////////////////////////////////////
288 // AliCDBManager Parameter class                         //
289 // interface to specific AliCDBParameter class           //
290 // (AliCDBGridParam, AliCDBLocalParam, AliCDBDumpParam)  //
291 ///////////////////////////////////////////////////////////
292
293 AliCDBParam::AliCDBParam() {
294 // constructor
295
296 }
297
298 //_____________________________________________________________________________
299 AliCDBParam::~AliCDBParam() {
300 // destructor
301
302 }
303