1 package org.duniter.core.client.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.base.Charsets;
27 import org.duniter.core.exception.TechnicalException;
28 import org.nuiton.config.ApplicationConfig;
29 import org.nuiton.config.ApplicationConfigHelper;
30 import org.nuiton.config.ApplicationConfigProvider;
31 import org.nuiton.config.ArgumentsParserException;
32 import org.nuiton.version.Version;
33 import org.nuiton.version.VersionBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.io.File;
38 import java.net.MalformedURLException;
39 import java.net.URL;
40 import java.util.Locale;
41 import java.util.Set;
42
43 import static org.nuiton.i18n.I18n.t;
44
45
46
47
48
49
50 public class Configuration {
51
52 private static final Logger log = LoggerFactory.getLogger(Configuration.class);
53
54
55
56
57 protected final ApplicationConfig applicationConfig;
58
59 private static Configuration instance;
60
61 public static Configuration instance() {
62 return instance;
63 }
64
65 public static void setInstance(Configuration instance) {
66 Configuration.instance = instance;
67 }
68
69 protected final String[] optionKeyToNotSave;
70
71 protected File configFile;
72
73 public Configuration(ApplicationConfig applicationConfig) {
74 super();
75 this.applicationConfig = applicationConfig;
76 this.optionKeyToNotSave = null;
77
78
79 initVersion(applicationConfig);
80 }
81
82 public Configuration(String file, String... args) {
83 super();
84 this.applicationConfig = new ApplicationConfig();
85 this.applicationConfig.setEncoding(Charsets.UTF_8.name());
86 this.applicationConfig.setConfigFileName(file);
87
88
89 Set<ApplicationConfigProvider> providers =
90 ApplicationConfigHelper.getProviders(null,
91 null,
92 null,
93 true);
94
95
96 ApplicationConfigHelper.loadAllDefaultOption(applicationConfig,
97 providers);
98
99
100 for (ApplicationConfigProvider provider : providers) {
101 applicationConfig.loadActions(provider.getActions());
102 }
103
104
105 addAlias(applicationConfig);
106
107
108 initVersion(applicationConfig);
109
110
111 Set<String> optionToSkip =
112 ApplicationConfigHelper.getTransientOptionKeys(providers);
113
114 if (log.isDebugEnabled()) {
115 log.debug("Option that won't be saved: " + optionToSkip);
116 }
117 optionKeyToNotSave = optionToSkip.toArray(new String[optionToSkip.size()]);
118
119 try {
120 applicationConfig.parse(args);
121
122 } catch (ArgumentsParserException e) {
123 throw new TechnicalException(t("duniter4j.config.parse.error"), e);
124 }
125
126
127 File appBasedir = applicationConfig.getOptionAsFile(
128 ConfigurationOption.BASEDIR.getKey());
129
130 if (appBasedir == null) {
131 appBasedir = new File("");
132 }
133 if (!appBasedir.isAbsolute()) {
134 appBasedir = new File(appBasedir.getAbsolutePath());
135 }
136 if (appBasedir.getName().equals("..")) {
137 appBasedir = appBasedir.getParentFile().getParentFile();
138 }
139 if (appBasedir.getName().equals(".")) {
140 appBasedir = appBasedir.getParentFile();
141 }
142 if (log.isInfoEnabled()) {
143 log.info("Application basedir: " + appBasedir);
144 }
145 applicationConfig.setOption(
146 ConfigurationOption.BASEDIR.getKey(),
147 appBasedir.getAbsolutePath());
148 }
149
150
151
152
153
154 protected void initVersion(ApplicationConfig applicationConfig) {
155
156 String implementationVersion = this.getClass().getPackage().getSpecificationVersion();
157 if (implementationVersion != null) {
158 applicationConfig.setDefaultOption(
159 ConfigurationOption.VERSION.getKey(),
160 implementationVersion);
161 }
162 }
163
164
165
166
167
168
169
170 protected void addAlias(ApplicationConfig applicationConfig) {
171 applicationConfig.addAlias("-h", "--option", ConfigurationOption.NODE_HOST.getKey());
172 applicationConfig.addAlias("--host", "--option", ConfigurationOption.NODE_HOST.getKey());
173 applicationConfig.addAlias("-p", "--option", ConfigurationOption.NODE_PORT.getKey());
174 applicationConfig.addAlias("--port", "--option", ConfigurationOption.NODE_PORT.getKey());
175 applicationConfig.addAlias("-c", "--option", ConfigurationOption.NODE_CURRENCY.getKey());
176 applicationConfig.addAlias("--salt", "--option", ConfigurationOption.USER_SALT.getKey());
177 applicationConfig.addAlias("--passwd", "--option", ConfigurationOption.USER_PASSWD.getKey());
178 }
179
180 public File getConfigFile() {
181 if (configFile == null) {
182 File dir = getBasedir();
183 if (dir == null || !dir.exists()) {
184 dir = new File(applicationConfig.getUserConfigDirectory());
185 }
186 configFile = new File(dir, applicationConfig.getConfigFileName());
187 }
188 return configFile;
189 }
190
191
192 public File getBasedir() {
193 File result = applicationConfig.getOptionAsFile(ConfigurationOption.BASEDIR.getKey());
194 return result;
195 }
196
197
198 public File getDataDirectory() {
199 File result = applicationConfig.getOptionAsFile(ConfigurationOption.DATA_DIRECTORY.getKey());
200 return result;
201 }
202
203 public ApplicationConfig getApplicationConfig() {
204 return applicationConfig;
205 }
206
207 public File getTempDirectory() {
208 return applicationConfig.getOptionAsFile(ConfigurationOption.TMP_DIRECTORY.getKey());
209 }
210
211 public File getCacheDirectory() {
212 return applicationConfig.getOptionAsFile(ConfigurationOption.CACHE_DIRECTORY.getKey());
213 }
214
215 public Version getVersion() {
216 String versionStr = applicationConfig.getOption(ConfigurationOption.VERSION.getKey());
217 return VersionBuilder.create(versionStr).build();
218 }
219
220 public File getI18nDirectory() {
221 return applicationConfig.getOptionAsFile(
222 ConfigurationOption.I18N_DIRECTORY.getKey());
223 }
224
225 public Locale getI18nLocale() {
226 return applicationConfig.getOptionAsLocale(
227 ConfigurationOption.I18N_LOCALE.getKey());
228 }
229
230 public void setI18nLocale(Locale locale) {
231 applicationConfig.setOption(ConfigurationOption.I18N_LOCALE.getKey(), locale.toString());
232 }
233
234 public String getNodeCurrency() {
235 return applicationConfig.getOption(ConfigurationOption.NODE_CURRENCY.getKey());
236 }
237
238 public String getNodeHost() {
239 return applicationConfig.getOption(ConfigurationOption.NODE_HOST.getKey());
240 }
241
242 public int getNodePort() {
243 return applicationConfig.getOptionAsInt(ConfigurationOption.NODE_PORT.getKey());
244 }
245
246 public URL getNodeUrl() {
247 return applicationConfig.getOptionAsURL(ConfigurationOption.NODE_URL.getKey());
248 }
249
250 public int getNetworkTimeout() {
251 return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_TIMEOUT.getKey());
252 }
253
254 public int getNetworkMaxTotalConnections() {
255 return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_MAX_CONNECTIONS.getKey());
256 }
257
258 public int getNetworkMaxConnectionsPerRoute() {
259 return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_MAX_CONNECTIONS_PER_ROUTE.getKey());
260 }
261
262 public int getNetworkCacheTimeInMillis() {
263 return Integer.parseInt(ConfigurationOption.NETWORK_CACHE_TIME_IN_MILLIS.getDefaultValue());
264 }
265
266 public int getPeerUpMaxAge() {
267 return applicationConfig.getOptionAsInt(ConfigurationOption.NETWORK_PEER_UP_MAX_AGE.getKey());
268 }
269
270 public String getNodeElasticSearchHost() {
271 return applicationConfig.getOption(ConfigurationOption.NODE_ELASTICSEARCH_HOST.getKey());
272 }
273 public int getNodeElasticSearchPort() {
274 return applicationConfig.getOptionAsInt(ConfigurationOption.NODE_ELASTICSEARCH_PORT.getKey());
275 }
276
277 public URL getNodeElasticSearchUrl() {
278
279 if (getNodeElasticSearchPort() == 443) {
280 try {
281 return new URL(applicationConfig.getOption(ConfigurationOption.NODE_ELASTICSEARCH_URL.getKey())
282 .replaceAll("http://", "https://"));
283 } catch(MalformedURLException e) {
284 return applicationConfig.getOptionAsURL(ConfigurationOption.NODE_ELASTICSEARCH_URL.getKey());
285 }
286 }
287 else {
288 return applicationConfig.getOptionAsURL(ConfigurationOption.NODE_ELASTICSEARCH_URL.getKey());
289 }
290 }
291 }