0
|
1 /**
|
|
2 * Palantir - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "PalantirInitialization.h"
|
|
22
|
|
23 #include "../Core/PalantirException.h"
|
|
24 #include "../Core/Toolbox.h"
|
|
25
|
|
26 #include <boost/lexical_cast.hpp>
|
|
27 #include <boost/filesystem.hpp>
|
|
28 #include <curl/curl.h>
|
|
29 #include <boost/thread.hpp>
|
|
30
|
|
31 namespace Palantir
|
|
32 {
|
|
33 static const char* CONFIGURATION_FILE = "Configuration.json";
|
|
34
|
|
35 static boost::mutex globalMutex_;
|
|
36 static std::auto_ptr<Json::Value> configuration_;
|
|
37
|
|
38
|
|
39 static void ReadGlobalConfiguration(const char* configurationFile)
|
|
40 {
|
|
41 configuration_.reset(new Json::Value);
|
|
42
|
|
43 std::string content;
|
|
44
|
|
45 if (configurationFile)
|
|
46 {
|
|
47 Toolbox::ReadFile(content, configurationFile);
|
|
48 }
|
|
49 else
|
|
50 {
|
19
|
51 try
|
|
52 {
|
|
53 #if PALANTIR_STANDALONE == 0
|
|
54 boost::filesystem::path p = PALANTIR_PATH;
|
|
55 p /= "Resources";
|
|
56 p /= CONFIGURATION_FILE;
|
|
57 Toolbox::ReadFile(content, p.string());
|
0
|
58 #else
|
19
|
59 Toolbox::ReadFile(content, CONFIGURATION_FILE);
|
0
|
60 #endif
|
19
|
61 }
|
|
62 catch (PalantirException&)
|
|
63 {
|
|
64 // No configuration file found, give up with empty configuration
|
|
65 return;
|
|
66 }
|
0
|
67 }
|
|
68
|
|
69 Json::Reader reader;
|
|
70 if (!reader.parse(content, *configuration_))
|
|
71 {
|
|
72 throw PalantirException("Unable to read the configuration file");
|
|
73 }
|
|
74 }
|
|
75
|
|
76
|
|
77 void PalantirInitialize(const char* configurationFile)
|
|
78 {
|
|
79 boost::mutex::scoped_lock lock(globalMutex_);
|
|
80 ReadGlobalConfiguration(configurationFile);
|
|
81 curl_global_init(CURL_GLOBAL_ALL);
|
|
82 }
|
|
83
|
|
84
|
|
85
|
|
86 void PalantirFinalize()
|
|
87 {
|
|
88 boost::mutex::scoped_lock lock(globalMutex_);
|
|
89 curl_global_cleanup();
|
|
90 configuration_.reset(NULL);
|
|
91 }
|
|
92
|
|
93
|
|
94
|
|
95 std::string GetGlobalStringParameter(const std::string& parameter,
|
|
96 const std::string& defaultValue)
|
|
97 {
|
|
98 boost::mutex::scoped_lock lock(globalMutex_);
|
|
99
|
|
100 if (configuration_->isMember(parameter))
|
|
101 {
|
|
102 return (*configuration_) [parameter].asString();
|
|
103 }
|
|
104 else
|
|
105 {
|
|
106 return defaultValue;
|
|
107 }
|
|
108 }
|
|
109
|
|
110
|
|
111 int GetGlobalIntegerParameter(const std::string& parameter,
|
|
112 int defaultValue)
|
|
113 {
|
|
114 boost::mutex::scoped_lock lock(globalMutex_);
|
|
115
|
|
116 if (configuration_->isMember(parameter))
|
|
117 {
|
|
118 return (*configuration_) [parameter].asInt();
|
|
119 }
|
|
120 else
|
|
121 {
|
|
122 return defaultValue;
|
|
123 }
|
|
124 }
|
|
125
|
|
126
|
|
127
|
|
128 void GetDicomModality(const std::string& name,
|
|
129 std::string& aet,
|
|
130 std::string& address,
|
|
131 int& port)
|
|
132 {
|
|
133 boost::mutex::scoped_lock lock(globalMutex_);
|
|
134
|
|
135 if (!configuration_->isMember("DicomModalities"))
|
|
136 {
|
|
137 throw PalantirException("");
|
|
138 }
|
|
139
|
|
140 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
|
|
141 if (modalities.type() != Json::objectValue ||
|
|
142 !modalities.isMember(name))
|
|
143 {
|
|
144 throw PalantirException("");
|
|
145 }
|
|
146
|
|
147 try
|
|
148 {
|
|
149 aet = modalities[name].get(0u, "").asString();
|
|
150 address = modalities[name].get(1u, "").asString();
|
|
151 port = modalities[name].get(2u, "").asInt();
|
|
152 }
|
|
153 catch (...)
|
|
154 {
|
|
155 throw PalantirException("Badly formatted DICOM modality");
|
|
156 }
|
|
157 }
|
|
158
|
|
159
|
|
160
|
|
161 void GetListOfDicomModalities(std::set<std::string>& target)
|
|
162 {
|
|
163 boost::mutex::scoped_lock lock(globalMutex_);
|
|
164
|
|
165 target.clear();
|
|
166
|
|
167 if (!configuration_->isMember("DicomModalities"))
|
|
168 {
|
|
169 return;
|
|
170 }
|
|
171
|
|
172 const Json::Value& modalities = (*configuration_) ["DicomModalities"];
|
|
173 if (modalities.type() != Json::objectValue)
|
|
174 {
|
|
175 throw PalantirException("Badly formatted list of DICOM modalities");
|
|
176 }
|
|
177
|
|
178 Json::Value::Members members = modalities.getMemberNames();
|
|
179 for (size_t i = 0; i < members.size(); i++)
|
|
180 {
|
|
181 for (size_t j = 0; j < members[i].size(); j++)
|
|
182 {
|
|
183 if (!isalnum(members[i][j]) && members[i][j] != '-')
|
|
184 {
|
|
185 throw PalantirException("Only alphanumeric and dash characters are allowed in the names of the modalities");
|
|
186 }
|
|
187 }
|
|
188
|
|
189 target.insert(members[i]);
|
|
190 }
|
|
191 }
|
|
192 }
|