-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSettingsController.java
More file actions
164 lines (143 loc) · 5.75 KB
/
SettingsController.java
File metadata and controls
164 lines (143 loc) · 5.75 KB
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
package com.codedead.opal.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public final class SettingsController {
private String propertiesFileLocation;
private String propertiesResourceLocation;
private Properties properties;
private final Logger logger;
/**
* Initialize a new SettingsController
*
* @param fileLocation The location of the file that contains the properties
* @param propertiesResourceLocation The location of the default properties file in the resources
* @throws IOException When the properties file could not be loaded
*/
public SettingsController(final String fileLocation,
final String propertiesResourceLocation) throws IOException {
logger = LogManager.getLogger(SettingsController.class);
logger.info("Initializing new SettingsController object");
setPropertiesFileLocation(fileLocation);
setPropertiesResourceLocation(propertiesResourceLocation);
try {
properties = readPropertiesFile();
} catch (final FileNotFoundException ex) {
logger.error("Properties object could not be loaded", ex);
createDefaultProperties();
properties = readPropertiesFile();
}
}
/**
* Create the default properties file from resources
*
* @throws IOException When the default properties file could not be read from the application resources or a pre-existing properties file could not be deleted
*/
public void createDefaultProperties() throws IOException {
logger.info("Attempting to create the default properties file");
final Path propertiesPath = Paths.get(getPropertiesFileLocation());
if (Files.exists(propertiesPath)) {
logger.info("Default properties file already exists, deleting the previous version");
Files.delete(propertiesPath);
}
try (final InputStream is = getClass().getClassLoader().getResourceAsStream(getPropertiesResourceLocation())) {
if (is != null) {
logger.info("Creating default properties file at {}", getPropertiesFileLocation());
try {
final Path p = Paths.get(new File(propertiesPath.toString()).getParent());
Files.createDirectories(p);
} catch (final IOException ex) {
logger.error("Could not create the parent directories for the properties file", ex);
}
Files.copy(is, propertiesPath);
} else {
throw new IOException(String.format("Could not load default properties from application resources (%s)!", getPropertiesResourceLocation()));
}
}
}
/**
* Get the resource location of the default properties file
*
* @return The resource location of the default properties file
*/
public String getPropertiesResourceLocation() {
return propertiesResourceLocation;
}
/**
* Set the resource location of the default properties file
*
* @param propertiesResourceLocation The resource location of the default properties file
*/
public void setPropertiesResourceLocation(final String propertiesResourceLocation) {
if (propertiesResourceLocation == null)
throw new NullPointerException("Properties resource location cannot be null!");
if (propertiesResourceLocation.isBlank())
throw new IllegalArgumentException("Properties resource location cannot be blank!");
this.propertiesResourceLocation = propertiesResourceLocation;
}
/**
* Get the properties file location
*
* @return The properties file location
*/
public String getPropertiesFileLocation() {
return propertiesFileLocation;
}
/**
* Set the properties file location
*
* @param propertiesFileLocation The properties file location
*/
public void setPropertiesFileLocation(final String propertiesFileLocation) {
if (propertiesFileLocation == null)
throw new NullPointerException("Properties file location cannot be null!");
if (propertiesFileLocation.isBlank())
throw new IllegalArgumentException("Properties file location cannot be blank!");
this.propertiesFileLocation = propertiesFileLocation;
}
/**
* Get the Properties object
*
* @return The Properties object
*/
public Properties getProperties() {
return properties;
}
/**
* Set the Properties object
*
* @param properties The properties object
*/
public void setProperties(final Properties properties) {
this.properties = properties;
}
/**
* Save the properties
*
* @throws IOException When the Properties object could not be stored
*/
public void saveProperties() throws IOException {
logger.info("Storing the Properties object");
try (final FileOutputStream fos = new FileOutputStream(getPropertiesFileLocation())) {
properties.store(fos, null);
}
}
/**
* Retrieve the Properties object
*
* @return The Properties object
* @throws IOException When the properties file could not be loaded
*/
public Properties readPropertiesFile() throws IOException {
logger.info("Loading the Properties object");
try (final FileInputStream fis = new FileInputStream(getPropertiesFileLocation())) {
final Properties prop = new Properties();
prop.load(fis);
return prop;
}
}
}