bes  Updated for version 3.17.0
TheBESKeys.cc
1 // TheBESKeys.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <unistd.h>
36 
37 #include "TheBESKeys.h"
38 #include "BESInternalFatalError.h"
39 
40 BESKeys *TheBESKeys::_instance = 0;
41 string TheBESKeys::ConfigFile = "";
42 
44 {
45 #if 1
46  if (_instance)
47  return _instance;
48 
49  if (!TheBESKeys::ConfigFile.empty()) {
50  _instance = new TheBESKeys(TheBESKeys::ConfigFile);
51  return _instance;
52  }
53 
54  // _instance is a nullptr and TheBESKeys::ConfigFile is ""
55  // so lets try some obvious places...
56 
57  string try_ini = "/usr/local/etc/bes/bes.conf";
58  if (access(try_ini.c_str(), R_OK) == 0) {
59  TheBESKeys::ConfigFile = try_ini;
60  _instance = new TheBESKeys(TheBESKeys::ConfigFile);
61  return _instance;
62  }
63 
64  try_ini = "/etc/bes/bes.conf";
65  if (access(try_ini.c_str(), R_OK) == 0) {
66  TheBESKeys::ConfigFile = try_ini;
67  _instance = new TheBESKeys(TheBESKeys::ConfigFile);
68  return _instance;
69  }
70 
71  try_ini = "/usr/etc/bes/bes.conf";
72  if (access(try_ini.c_str(), R_OK) == 0) {
73  TheBESKeys::ConfigFile = try_ini;
74  _instance = new TheBESKeys(TheBESKeys::ConfigFile);
75  return _instance;
76  }
77 
78  throw BESInternalFatalError("Unable to find a conf file or module version mismatch.", __FILE__, __LINE__);
79 
80 #else
81 
82  if (_instance == 0) {
83  string use_ini = TheBESKeys::ConfigFile;
84  if (use_ini == "") {
85  string try_ini = "/usr/local/etc/bes/bes.conf";
86  struct stat buf;
87  int statret = stat(try_ini.c_str(), &buf);
88  if (statret == -1 || !S_ISREG(buf.st_mode)) {
89  try_ini = "/etc/bes/bes.conf";
90  int statret = stat(try_ini.c_str(), &buf);
91  if (statret == -1 || !S_ISREG(buf.st_mode)) {
92  try_ini = "/usr/etc/bes/bes.conf";
93  int statret = stat(try_ini.c_str(), &buf);
94  if (statret == -1 || !S_ISREG(buf.st_mode)) {
95  throw BESInternalFatalError("Unable to find a conf file or module version mismatch.", __FILE__,
96  __LINE__);
97  }
98  else {
99  use_ini = try_ini;
100  }
101  }
102  else {
103  use_ini = try_ini;
104  }
105  }
106  else {
107  use_ini = try_ini;
108  }
109  }
110  _instance = new TheBESKeys(use_ini);
111  }
112 
113  return _instance;
114 
115 #endif
116 }
exception thrown if an internal error is found and is fatal to the BES
mapping of key/value pairs defining different behaviors of an application.
Definition: BESKeys.h:84
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:43
static string ConfigFile
Definition: TheBESKeys.h:53