-
Notifications
You must be signed in to change notification settings - Fork 240
/
SystemProperties.java
383 lines (331 loc) · 10.8 KB
/
SystemProperties.java
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.properties;
import io.github.dsheirer.gui.SDRTrunk;
import io.github.dsheirer.util.ThreadPool;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.Manifest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SystemProperties - provides an isolated instance of properties for the application
*/
public class SystemProperties
{
private final static Logger mLog = LoggerFactory.getLogger(SystemProperties.class);
private static String DEFAULT_APP_ROOT = "SDRTrunk";
private static String PROPERTIES_FILENAME = "SDRTrunk.properties";
private static String MANIFEST_VERSION = "Implementation-Version";
private static String BUILD_TIMESTAMP = "Build-Timestamp";
private static SystemProperties INSTANCE;
private static Properties mProperties;
private Path mPropertiesPath;
private String mApplicationName;
private AtomicBoolean mSavePending = new AtomicBoolean();
private SystemProperties()
{
mProperties = new Properties();
}
/**
* Returns a SINGLETON instance of the application properties set
*
* @return
*/
public static SystemProperties getInstance()
{
if(INSTANCE == null)
{
INSTANCE = new SystemProperties();
}
return INSTANCE;
}
/**
* Saves any currently changed settings to the application properties file
*/
private void save()
{
if(mSavePending.compareAndSet(false, true))
{
ThreadPool.SCHEDULED.schedule(new SavePropertiesTask(), 2, TimeUnit.SECONDS);
}
}
/**
* Get the Manifest for the jar that contains the specified class
* @param clz contained in a jar
* @return manifest from the containing jar
*/
public static Manifest getManifest(Class<?> clz)
{
String resource = "/" + clz.getName().replace(".", "/") + ".class";
String fullPath = clz.getResource(resource).toString();
String archivePath = fullPath.substring(0, fullPath.length() - resource.length());
if (archivePath.endsWith("\\WEB-INF\\classes") || archivePath.endsWith("/WEB-INF/classes")) {
archivePath = archivePath.substring(0, archivePath.length() - "/WEB-INF/classes".length()); // Required for wars
}
try (InputStream input = new URL(archivePath + "/META-INF/MANIFEST.MF").openStream())
{
return new Manifest(input);
}
catch (Exception e)
{
mLog.error("Unable to load jar manifest - we're probably not running from a release jar");
}
return null;
}
/**
* Application root directory. Normally returns "SDRTRunk" from the user's
* home directory, unless that has been changed to another location on the
* file system by the user.
*/
public Path getApplicationRootPath()
{
Path retVal = null;
String root = get("root.directory", DEFAULT_APP_ROOT);
if(root.equalsIgnoreCase(DEFAULT_APP_ROOT))
{
retVal = Paths.get(
System.getProperty("user.home"), DEFAULT_APP_ROOT);
}
else
{
retVal = Paths.get(root);
}
return retVal;
}
public Path getApplicationFolder(String folder)
{
Path retVal = getApplicationRootPath().resolve(folder);
if(!Files.exists(retVal))
{
try
{
Files.createDirectory(retVal);
}
catch(IOException e)
{
mLog.error("SystemProperties - exception while creating app folder [" + folder + "]", e);
}
}
return retVal;
}
public void logCurrentSettings()
{
if(mPropertiesPath == null)
{
mLog.info("SystemProperties - no properties file loaded - using defaults");
}
else
{
mLog.info("SystemProperties - application properties loaded [" + mPropertiesPath.toString() + "]");
}
}
/**
* Loads the properties file into this properties set
*/
public void load(Path propertiesPath)
{
if(propertiesPath != null)
{
mPropertiesPath = propertiesPath;
if(Files.exists(mPropertiesPath))
{
try(InputStream in = new FileInputStream(propertiesPath.toString()))
{
mProperties.load(in);
}
catch(IOException ioe)
{
mLog.error("Error loading system properties file", ioe.getMessage());
}
}
}
mLog.info("SystemProperties - loaded [" + propertiesPath.toString() + "]");
}
/**
* Creates the application name, suitable for display in the title bar of the gui
*/
public String getApplicationName()
{
if(mApplicationName == null)
{
StringBuilder sb = new StringBuilder();
sb.append("sdrtrunk");
Manifest manifest = getManifest(SDRTrunk.class);
if(manifest != null)
{
String version = manifest.getMainAttributes().getValue(MANIFEST_VERSION);
String timestamp = manifest.getMainAttributes().getValue(BUILD_TIMESTAMP);
if(version != null)
{
if(version.contains("nightly") && timestamp != null)
{
sb.append( " nightly - ").append(timestamp);
}
else
{
sb.append(" v");
sb.append(version);
}
}
}
mApplicationName = sb.toString();
}
return mApplicationName;
}
/**
* Returns the value of the property, or null if the property doesn't exist
*/
private String get(String key)
{
return mProperties.getProperty(key);
}
/**
* Returns the value of the property, or the defaultValue if the
* property doesn't exist
*/
public String get(String key, String defaultValue)
{
String value = get(key);
if(value != null)
{
return value;
}
set(key, defaultValue);
return defaultValue;
}
/**
* Returns the value of the property, or the defaultValue if the
* property doesn't exist
*/
public boolean get(String key, boolean defaultValue)
{
String value = get(key);
if(value != null)
{
try
{
boolean stored = Boolean.parseBoolean(value);
return stored;
}
catch(Exception e)
{
//Do nothing, we couldn't parse the stored value
}
}
set(key, String.valueOf(defaultValue));
return defaultValue;
}
/**
* Returns the value of the property, or the defaultValue if the
* property doesn't exist
*/
public int get(String key, int defaultValue)
{
String value = get(key);
if(value != null)
{
try
{
int stored = Integer.parseInt(value);
return stored;
}
catch(Exception e)
{
//Do nothing, we couldn't parse the stored value
}
}
set(key, String.valueOf(defaultValue));
return defaultValue;
}
/**
* Sets (overrides) the property key with the new value
*/
public void set(String key, String value)
{
mProperties.setProperty(key, value);
save();
}
/**
* Sets (overrides) the property key with the new boolean value
*/
public void set(String key, boolean value)
{
set(key, String.valueOf(value));
}
/**
* Sets (overrides) the property key with the new integer value
*/
public void set(String key, int value)
{
set(key, String.valueOf(value));
}
/**
* Gets the named color property, storing the default value in properties file if it doesn't exist.
*
* @param key identifying the color property
* @param defaultColor to use for creating the property if it doesn't exist
* @return named color
*/
public Color get(String key, Color defaultColor)
{
return new Color(get(key, defaultColor.getRGB()), true);
}
/**
* Creates a new color instance with the RGB values from color argument and translucency (alpha) argument
* @param color containing RGB values
* @param alpha desired translucency/alpha value
* @return new color with adjusted alpha value
*/
public static Color getTranslucent(Color color, int alpha)
{
return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
}
public class SavePropertiesTask implements Runnable
{
@Override
public void run()
{
if(mSavePending.compareAndSet(true, false))
{
Path propsPath = getApplicationRootPath().resolve(PROPERTIES_FILENAME);
try(OutputStream out = new FileOutputStream(propsPath.toString()))
{
String comments = "SDRTrunk - SDR Trunking Decoder Application Settings";
mProperties.store(out, comments);
}
catch(IOException ioe)
{
mLog.error("Error saving system properties file [" + propsPath.toString() + "]", ioe);
}
}
}
}
}