Autopsy  4.6.0
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 2011 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  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.Set;
30 import java.util.logging.Level;
31 
36 public class ModuleSettings {
37 
38  // The directory where the properties file is located
39  private final static String moduleDirPath = PlatformUtil.getUserConfigDirectory();
40  public static final String DEFAULT_CONTEXT = "GeneralContext"; //NON-NLS
41  public static final String MAIN_SETTINGS = "Case"; //NON-NLS
42  public static final String CURRENT_CASE_TYPE = "Current_Case_Type"; //NON-NLS
43 
47  private ModuleSettings() {
48  }
49 
59  public static boolean makeConfigFile(String moduleName) {
60  if (!configExists(moduleName)) {
61  File propPath = new File(moduleDirPath + File.separator + moduleName + ".properties");
62  File parent = new File(propPath.getParent());
63  if (!parent.exists()) {
64  parent.mkdirs();
65  }
66  Properties props = new Properties();
67  try {
68  propPath.createNewFile();
69  FileOutputStream fos = new FileOutputStream(propPath);
70  props.store(fos, "");
71  fos.close();
72  } catch (IOException e) {
73  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Was not able to create a new properties file.", e); //NON-NLS
74  return false;
75  }
76  return true;
77  }
78  return false;
79  }
80 
88  public static boolean configExists(String moduleName) {
89  File f = new File(moduleDirPath + File.separator + moduleName + ".properties");
90  return f.exists();
91  }
92 
93  public static boolean settingExists(String moduleName, String settingName) {
94  if (!configExists(moduleName)) {
95  return false;
96  }
97  try {
98  Properties props = fetchProperties(moduleName);
99  return (props.getProperty(settingName) != null);
100  } catch (IOException e) {
101  return false;
102  }
103 
104  }
105 
114  private static String getPropertyPath(String moduleName) {
115  if (configExists(moduleName)) {
116  return moduleDirPath + File.separator + moduleName + ".properties"; //NON-NLS
117  }
118 
119  return null;
120  }
121 
132  public static String getConfigSetting(String moduleName, String settingName) {
133  if (!configExists(moduleName)) {
134  makeConfigFile(moduleName);
135  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
136  }
137 
138  try {
139  Properties props = fetchProperties(moduleName);
140 
141  return props.getProperty(settingName);
142  } catch (IOException e) {
143  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not read config file [" + moduleName + "]", e); //NON-NLS
144  return null;
145  }
146 
147  }
148 
159  public static Map< String, String> getConfigSettings(String moduleName) {
160 
161  if (!configExists(moduleName)) {
162  makeConfigFile(moduleName);
163  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
164  }
165  try {
166  Properties props = fetchProperties(moduleName);
167 
168  Set<String> keys = props.stringPropertyNames();
169  Map<String, String> map = new HashMap<String, String>();
170 
171  for (String s : keys) {
172  map.put(s, props.getProperty(s));
173  }
174 
175  return map;
176  } catch (IOException e) {
177  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not read config file [" + moduleName + "]", e); //NON-NLS
178  return null;
179  }
180  }
181 
189  public static synchronized void setConfigSettings(String moduleName, Map<String, String> settings) {
190  if (!configExists(moduleName)) {
191  makeConfigFile(moduleName);
192  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
193  }
194  try {
195  Properties props = fetchProperties(moduleName);
196 
197  for (Map.Entry<String, String> kvp : settings.entrySet()) {
198  props.setProperty(kvp.getKey(), kvp.getValue());
199  }
200 
201  File path = new File(getPropertyPath(moduleName));
202  FileOutputStream fos = new FileOutputStream(path);
203  props.store(fos, "Changed config settings(batch)"); //NON-NLS
204  fos.close();
205  } catch (IOException e) {
206  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Property file exists for [" + moduleName + "] at [" + getPropertyPath(moduleName) + "] but could not be loaded.", e); //NON-NLS NON-NLS NON-NLS
207  }
208  }
209 
217  public static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal) {
218  if (!configExists(moduleName)) {
219  makeConfigFile(moduleName);
220  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
221  }
222 
223  try {
224  Properties props = fetchProperties(moduleName);
225 
226  props.setProperty(settingName, settingVal);
227 
228  File path = new File(getPropertyPath(moduleName));
229  FileOutputStream fos = new FileOutputStream(path);
230  props.store(fos, "Changed config settings(single)"); //NON-NLS
231  fos.close();
232  } catch (IOException e) {
233  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Property file exists for [" + moduleName + "] at [" + getPropertyPath(moduleName) + "] but could not be loaded.", e); //NON-NLS NON-NLS NON-NLS
234  }
235  }
236 
243  public static synchronized void removeProperty(String moduleName, String key) {
244  try {
245  if (getConfigSetting(moduleName, key) != null) {
246  Properties props = fetchProperties(moduleName);
247 
248  props.remove(key);
249  File path = new File(getPropertyPath(moduleName));
250  FileOutputStream fos = new FileOutputStream(path);
251  props.store(fos, "Removed " + key); //NON-NLS
252  fos.close();
253  }
254  } catch (IOException e) {
255  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not remove property from file, file not found", e); //NON-NLS
256  }
257  }
258 
268  private static Properties fetchProperties(String moduleName) throws IOException {
269  InputStream inputStream = new FileInputStream(getPropertyPath(moduleName));
270  Properties props = new Properties();
271  props.load(inputStream);
272  inputStream.close();
273  return props;
274  }
275 
283  public static File getPropertyFile(String moduleName) {
284  String path = getPropertyPath(moduleName);
285  if (path == null) {
286  return null;
287  } else {
288  return new File(getPropertyPath(moduleName));
289  }
290  }
291 }
static Map< String, String > getConfigSettings(String moduleName)
static Properties fetchProperties(String moduleName)
static String getPropertyPath(String moduleName)
static boolean makeConfigFile(String moduleName)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static File getPropertyFile(String moduleName)
static synchronized void setConfigSettings(String moduleName, Map< String, String > settings)
static String getConfigSetting(String moduleName, String settingName)
static boolean configExists(String moduleName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized void removeProperty(String moduleName, String key)
static boolean settingExists(String moduleName, String settingName)

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.