comparison OrthancServer/OrthancInitialization.cpp @ 2010:4dafe2a0d3ab

Support of SIGHUP signal (restart Orthanc only if the configuration files have changed)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 09 Jun 2016 17:57:47 +0200
parents 6301bbcbcaed
children a0bd8cd55da7
comparison
equal deleted inserted replaced
2009:e2dd40abce72 2010:4dafe2a0d3ab
79 static boost::recursive_mutex globalMutex_; 79 static boost::recursive_mutex globalMutex_;
80 static Json::Value configuration_; 80 static Json::Value configuration_;
81 static boost::filesystem::path defaultDirectory_; 81 static boost::filesystem::path defaultDirectory_;
82 static std::string configurationAbsolutePath_; 82 static std::string configurationAbsolutePath_;
83 static FontRegistry fontRegistry_; 83 static FontRegistry fontRegistry_;
84 static const char* configurationFileArg_ = NULL;
84 85
85 86
86 static std::string GetGlobalStringParameterInternal(const std::string& parameter, 87 static std::string GetGlobalStringParameterInternal(const std::string& parameter,
87 const std::string& defaultValue) 88 const std::string& defaultValue)
88 { 89 {
126 } 127 }
127 } 128 }
128 129
129 130
130 131
131 static void AddFileToConfiguration(const boost::filesystem::path& path) 132 static void AddFileToConfiguration(Json::Value& target,
133 const boost::filesystem::path& path)
132 { 134 {
133 LOG(WARNING) << "Reading the configuration from: " << path; 135 LOG(WARNING) << "Reading the configuration from: " << path;
134 136
135 Json::Value config; 137 Json::Value config;
136 138
148 } 150 }
149 151
150 Toolbox::CopyJsonWithoutComments(config, tmp); 152 Toolbox::CopyJsonWithoutComments(config, tmp);
151 } 153 }
152 154
153 if (configuration_.size() == 0) 155 if (target.size() == 0)
154 { 156 {
155 configuration_ = config; 157 target = config;
156 } 158 }
157 else 159 else
158 { 160 {
159 Json::Value::Members members = config.getMemberNames(); 161 Json::Value::Members members = config.getMemberNames();
160 for (Json::Value::ArrayIndex i = 0; i < members.size(); i++) 162 for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
161 { 163 {
162 if (configuration_.isMember(members[i])) 164 if (target.isMember(members[i]))
163 { 165 {
164 LOG(ERROR) << "The configuration section \"" << members[i] << "\" is defined in 2 different configuration files"; 166 LOG(ERROR) << "The configuration section \"" << members[i] << "\" is defined in 2 different configuration files";
165 throw OrthancException(ErrorCode_BadFileFormat); 167 throw OrthancException(ErrorCode_BadFileFormat);
166 } 168 }
167 else 169 else
168 { 170 {
169 configuration_[members[i]] = config[members[i]]; 171 target[members[i]] = config[members[i]];
170 } 172 }
171 } 173 }
172 } 174 }
173 } 175 }
174 176
175 177
176 static void ScanFolderForConfiguration(const char* folder) 178 static void ScanFolderForConfiguration(Json::Value& target,
179 const char* folder)
177 { 180 {
178 using namespace boost::filesystem; 181 using namespace boost::filesystem;
179 182
180 LOG(WARNING) << "Scanning folder \"" << folder << "\" for configuration files"; 183 LOG(WARNING) << "Scanning folder \"" << folder << "\" for configuration files";
181 184
189 std::string extension = boost::filesystem::extension(it->path()); 192 std::string extension = boost::filesystem::extension(it->path());
190 Toolbox::ToLowerCase(extension); 193 Toolbox::ToLowerCase(extension);
191 194
192 if (extension == ".json") 195 if (extension == ".json")
193 { 196 {
194 AddFileToConfiguration(it->path().string()); 197 AddFileToConfiguration(target, it->path().string());
195 } 198 }
196 } 199 }
197 } 200 }
198 } 201 }
199 202
200 203
201 static void ReadGlobalConfiguration(const char* configurationFile) 204 static void ReadConfiguration(Json::Value& target,
202 { 205 const char* configurationFile)
203 // Prepare the default configuration 206 {
204 defaultDirectory_ = boost::filesystem::current_path(); 207 target = Json::objectValue;
205 configuration_ = Json::objectValue;
206 configurationAbsolutePath_ = "";
207 208
208 if (configurationFile) 209 if (configurationFile)
209 { 210 {
210 if (!boost::filesystem::exists(configurationFile)) 211 if (!boost::filesystem::exists(configurationFile))
211 { 212 {
213 throw OrthancException(ErrorCode_InexistentFile); 214 throw OrthancException(ErrorCode_InexistentFile);
214 } 215 }
215 216
216 if (boost::filesystem::is_directory(configurationFile)) 217 if (boost::filesystem::is_directory(configurationFile))
217 { 218 {
218 defaultDirectory_ = boost::filesystem::path(configurationFile); 219 ScanFolderForConfiguration(target, configurationFile);
219 configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).parent_path().string();
220 ScanFolderForConfiguration(configurationFile);
221 } 220 }
222 else 221 else
223 { 222 {
224 defaultDirectory_ = boost::filesystem::path(configurationFile).parent_path(); 223 AddFileToConfiguration(target, configurationFile);
225 configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).string();
226 AddFileToConfiguration(configurationFile);
227 } 224 }
228 } 225 }
229 else 226 else
230 { 227 {
231 #if ORTHANC_STANDALONE == 1 228 #if ORTHANC_STANDALONE == 1
238 // "Resources/Configuration.json" from the Orthanc source code 235 // "Resources/Configuration.json" from the Orthanc source code
239 236
240 boost::filesystem::path p = ORTHANC_PATH; 237 boost::filesystem::path p = ORTHANC_PATH;
241 p /= "Resources"; 238 p /= "Resources";
242 p /= "Configuration.json"; 239 p /= "Configuration.json";
240
241 AddFileToConfiguration(target, p);
242 #endif
243 }
244 }
245
246
247
248 static void ReadGlobalConfiguration(const char* configurationFile)
249 {
250 // Read the content of the configuration
251 configurationFileArg_ = configurationFile;
252 ReadConfiguration(configuration_, configurationFile);
253
254 // Adapt the paths to the configurations
255 defaultDirectory_ = boost::filesystem::current_path();
256 configurationAbsolutePath_ = "";
257
258 if (configurationFile)
259 {
260 if (boost::filesystem::is_directory(configurationFile))
261 {
262 defaultDirectory_ = boost::filesystem::path(configurationFile);
263 configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).parent_path().string();
264 }
265 else
266 {
267 defaultDirectory_ = boost::filesystem::path(configurationFile).parent_path();
268 configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).string();
269 }
270 }
271 else
272 {
273 #if ORTHANC_STANDALONE != 1
274 // In a non-standalone build, we use the
275 // "Resources/Configuration.json" from the Orthanc source code
276
277 boost::filesystem::path p = ORTHANC_PATH;
278 p /= "Resources";
279 p /= "Configuration.json";
243 configurationAbsolutePath_ = boost::filesystem::absolute(p).string(); 280 configurationAbsolutePath_ = boost::filesystem::absolute(p).string();
244
245 AddFileToConfiguration(p);
246 #endif 281 #endif
247 } 282 }
248 } 283 }
249 284
250 285
1038 std::string s = GetGlobalStringParameter("DefaultEncoding", "Latin1"); 1073 std::string s = GetGlobalStringParameter("DefaultEncoding", "Latin1");
1039 1074
1040 // By default, Latin1 encoding is assumed 1075 // By default, Latin1 encoding is assumed
1041 return s.empty() ? Encoding_Latin1 : StringToEncoding(s.c_str()); 1076 return s.empty() ? Encoding_Latin1 : StringToEncoding(s.c_str());
1042 } 1077 }
1078
1079
1080 bool Configuration::HasConfigurationChanged()
1081 {
1082 Json::Value starting;
1083 GetConfiguration(starting);
1084
1085 Json::Value current;
1086 ReadConfiguration(current, configurationFileArg_);
1087
1088 Json::FastWriter writer;
1089 std::string a = writer.write(starting);
1090 std::string b = writer.write(current);
1091
1092 return a != b;
1093 }
1043 } 1094 }