Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ModuleSettings.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2012-2020 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.coreutils;
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.nio.file.Paths;
27import java.util.HashMap;
28import java.util.Map;
29import java.util.Properties;
30import java.util.Set;
31import java.util.logging.Level;
32
47public class ModuleSettings {
48
49 private final static Logger logger = Logger.getLogger(ModuleSettings.class.getName());
50 private final static String MODULE_DIR_PATH = PlatformUtil.getUserConfigDirectory();
51 private final static String SETTINGS_FILE_EXT = ".properties";
52
53 /*
54 * These SHOULD NOT be public and DO NOT belong in this file. They are being
55 * retained only for the sake of backwards compatibility.
56 */
57 public static final String DEFAULT_CONTEXT = "GeneralContext"; //NON-NLS
58 public static final String MAIN_SETTINGS = "Case"; //NON-NLS
59 public static final String CURRENT_CASE_TYPE = "Current_Case_Type"; //NON-NLS
60
69 public static synchronized boolean makeConfigFile(String moduleName) {
70 if (!configExists(moduleName)) {
71 File propPath = new File(getSettingsFilePath(moduleName));
72 File parent = new File(propPath.getParent());
73 if (!parent.exists()) {
74 parent.mkdirs();
75 }
76
77 Properties props = new Properties();
78 try {
79 propPath.createNewFile();
80 try (FileOutputStream fos = new FileOutputStream(propPath)) {
81 props.store(fos, "Created module settings file");
82 }
83 } catch (IOException ex) {
84 logger.log(Level.SEVERE, String.format("Failed to create module settings file at %s)", propPath), ex); //NON-NLS
85 return false;
86 }
87 return true;
88 }
89 return false;
90 }
91
99 public static synchronized boolean configExists(String moduleName) {
100 return new File(getSettingsFilePath(moduleName)).exists();
101 }
102
113 public static synchronized boolean settingExists(String moduleName, String settingName) {
114 if (!configExists(moduleName)) {
115 return false;
116 }
117
118 try {
119 Properties props = fetchProperties(moduleName);
120 return (props.getProperty(settingName) != null);
121 } catch (IOException ex) {
122 logger.log(Level.SEVERE, String.format("Failed to get %s setting from module settings file at %s)", settingName, getSettingsFilePath(moduleName)), ex); //NON-NLS
123 return false;
124 }
125 }
126
134 static String getSettingsFilePath(String moduleName) {
135 return Paths.get(MODULE_DIR_PATH, moduleName + SETTINGS_FILE_EXT).toString();
136 }
137
149 public static synchronized String getConfigSetting(String moduleName, String settingName) {
150 if (!configExists(moduleName)) {
151 makeConfigFile(moduleName);
152 }
153
154 try {
155 Properties props = fetchProperties(moduleName);
156 return props.getProperty(settingName);
157 } catch (IOException ex) {
158 logger.log(Level.SEVERE, String.format("Failed to get %s setting from module settings file at %s)", settingName, getSettingsFilePath(moduleName)), ex); //NON-NLS
159 return null;
160 }
161 }
162
173 public static synchronized Map<String, String> getConfigSettings(String moduleName) {
174 if (!configExists(moduleName)) {
175 makeConfigFile(moduleName);
176 }
177
178 try {
179 Properties props = fetchProperties(moduleName);
180 Set<String> keys = props.stringPropertyNames();
181 Map<String, String> map = new HashMap<>();
182 for (String s : keys) {
183 map.put(s, props.getProperty(s));
184 }
185 return map;
186 } catch (IOException ex) {
187 logger.log(Level.SEVERE, String.format("Failed to get settings from module settings file at %s)", getSettingsFilePath(moduleName)), ex); //NON-NLS
188 return null;
189 }
190 }
191
201 public static synchronized void setConfigSettings(String moduleName, Map<String, String> settings) {
202 if (!configExists(moduleName)) {
203 makeConfigFile(moduleName);
204 }
205
206 try {
207 Properties props = fetchProperties(moduleName);
208 for (Map.Entry<String, String> kvp : settings.entrySet()) {
209 props.setProperty(kvp.getKey(), kvp.getValue());
210 }
211
212 File path = new File(getSettingsFilePath(moduleName));
213 try (FileOutputStream fos = new FileOutputStream(path)) {
214 props.store(fos, "Set settings (batch)"); //NON-NLS
215 }
216 } catch (IOException ex) {
217 logger.log(Level.SEVERE, String.format("Error writing to module settings file at %s)", getSettingsFilePath(moduleName)), ex); //NON-NLS
218 }
219 }
220
230 public static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal) {
231 if (!configExists(moduleName)) {
232 makeConfigFile(moduleName);
233 }
234 try {
235 Properties props = fetchProperties(moduleName);
236 props.setProperty(settingName, settingVal);
237 File path = new File(getSettingsFilePath(moduleName));
238 try (FileOutputStream fos = new FileOutputStream(path)) {
239 props.store(fos, "Set " + settingName); //NON-NLS
240 }
241 } catch (IOException ex) {
242 logger.log(Level.SEVERE, String.format("Error writing %s setting to module settings file at %s)", settingName, getSettingsFilePath(moduleName)), ex); //NON-NLS
243 }
244 }
245
252 public static synchronized void removeProperty(String moduleName, String settingName) {
253 try {
254 if (getConfigSetting(moduleName, settingName) != null) {
255 Properties props = fetchProperties(moduleName);
256 props.remove(settingName);
257 File path = new File(getSettingsFilePath(moduleName));
258 try (FileOutputStream fos = new FileOutputStream(path)) {
259 props.store(fos, "Removed " + settingName); //NON-NLS
260 }
261 }
262 } catch (IOException ex) {
263 logger.log(Level.SEVERE, String.format("Error removing %s setting from module settings file at %s)", settingName, getSettingsFilePath(moduleName)), ex); //NON-NLS
264 }
265 }
266
276 private static synchronized Properties fetchProperties(String moduleName) throws IOException {
277 Properties props;
278 try (InputStream inputStream = new FileInputStream(getSettingsFilePath(moduleName))) {
279 props = new Properties();
280 props.load(inputStream);
281 }
282 return props;
283 }
284
292 public static synchronized File getPropertyFile(String moduleName) {
293 File configFile = null;
294 if (configExists(moduleName)) {
295 configFile = new File(getSettingsFilePath(moduleName));
296 }
297 return configFile;
298 }
299
303 private ModuleSettings() {
304 }
305
306}
synchronized static Logger getLogger(String name)
Definition Logger.java:124
static synchronized void setConfigSettings(String moduleName, Map< String, String > settings)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static synchronized Map< String, String > getConfigSettings(String moduleName)
static synchronized File getPropertyFile(String moduleName)
static synchronized boolean configExists(String moduleName)
static synchronized String getConfigSetting(String moduleName, String settingName)
static synchronized Properties fetchProperties(String moduleName)
static synchronized void removeProperty(String moduleName, String settingName)
static synchronized boolean makeConfigFile(String moduleName)
static synchronized boolean settingExists(String moduleName, String settingName)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.