Autopsy  3.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 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 
20 package org.sleuthkit.autopsy.coreutils;
21 
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.Set;
31 import java.util.logging.Level;
32 
37 public class ModuleSettings {
38 
39  // The directory where the properties file is located
40  private final static String moduleDirPath = PlatformUtil.getUserConfigDirectory();
41  public static final String DEFAULT_CONTEXT = "GeneralContext"; //NON-NLS
42  public static final String MAIN_SETTINGS = "Case"; //NON-NLS
43 
45  private ModuleSettings() {}
46 
52  public static boolean makeConfigFile(String moduleName){
53  if(!configExists(moduleName)){
54  File propPath = new File(moduleDirPath + File.separator + moduleName + ".properties");
55  File parent = new File(propPath.getParent());
56  if(! parent.exists()){
57  parent.mkdirs();
58  }
59  Properties props = new Properties();
60  try{
61  propPath.createNewFile();
62  FileOutputStream fos = new FileOutputStream(propPath);
63  props.store(fos, "");
64  fos.close();
65  }
66  catch(IOException e){
67  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Was not able to create a new properties file.", e); //NON-NLS
68  return false;
69  }
70  return true;
71  }
72  return false;
73  }
74 
75 
81  public static boolean configExists(String moduleName){
82  File f = new File(moduleDirPath + File.separator + moduleName + ".properties");
83  return f.exists();
84  }
85 
86  public static boolean settingExists(String moduleName, String settingName){
87  if(! configExists(moduleName)){
88  return false;
89  }
90  try{
91  Properties props = fetchProperties(moduleName);
92  return (props.getProperty(settingName) != null);
93  }
94  catch(IOException e){
95  return false;
96  }
97 
98  }
99 
105  private static String getPropertyPath(String moduleName){
106  if(configExists(moduleName)){
107  return moduleDirPath + File.separator + moduleName + ".properties"; //NON-NLS
108  }
109 
110  return null;
111  }
112 
120  public static String getConfigSetting(String moduleName, String settingName) {
121  if (!configExists(moduleName)) {
122  makeConfigFile(moduleName);
123  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
124  }
125 
126  try {
127  Properties props = fetchProperties(moduleName);
128 
129  return props.getProperty(settingName);
130  } catch (IOException e) {
131  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not read config file [" + moduleName + "]", e); //NON-NLS
132  return null;
133  }
134 
135  }
136 
137 
144  public static Map< String, String> getConfigSettings(String moduleName) {
145 
146  if (!configExists(moduleName)) {
147  makeConfigFile(moduleName);
148  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
149  }
150  try {
151  Properties props = fetchProperties(moduleName);
152 
153  Set<String> keys = props.stringPropertyNames();
154  Map<String, String> map = new HashMap<String, String>();
155 
156  for (String s : keys) {
157  map.put(s, props.getProperty(s));
158  }
159 
160  return map;
161  } catch (IOException e) {
162  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not read config file [" + moduleName + "]", e); //NON-NLS
163  return null;
164  }
165  }
166 
172  public static synchronized void setConfigSettings(String moduleName, Map<String, String> settings) {
173  if (!configExists(moduleName)) {
174  makeConfigFile(moduleName);
175  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
176  }
177  try {
178  Properties props = fetchProperties(moduleName);
179 
180  for (Map.Entry<String, String> kvp : settings.entrySet()) {
181  props.setProperty(kvp.getKey(), kvp.getValue());
182  }
183 
184  File path = new File(getPropertyPath(moduleName));
185  FileOutputStream fos = new FileOutputStream(path);
186  props.store(fos, "Changed config settings(batch)"); //NON-NLS
187  fos.close();
188  } catch (IOException e) {
189  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
190  }
191  }
192 
199  public static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal) {
200  if (!configExists(moduleName)) {
201  makeConfigFile(moduleName);
202  Logger.getLogger(ModuleSettings.class.getName()).log(Level.INFO, "File did not exist. Created file [" + moduleName + ".properties]"); //NON-NLS NON-NLS
203  }
204 
205  try {
206  Properties props = fetchProperties(moduleName);
207 
208  props.setProperty(settingName, settingVal);
209 
210  File path = new File(getPropertyPath(moduleName));
211  FileOutputStream fos = new FileOutputStream(path);
212  props.store(fos, "Changed config settings(single)"); //NON-NLS
213  fos.close();
214  } catch (IOException e) {
215  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
216  }
217  }
218 
224  public static synchronized void removeProperty(String moduleName, String key){
225  try{
226  if(getConfigSetting(moduleName, key) != null){
227  Properties props = fetchProperties(moduleName);
228 
229  props.remove(key);
230  File path = new File(getPropertyPath(moduleName));
231  FileOutputStream fos = new FileOutputStream(path);
232  props.store(fos, "Removed " + key); //NON-NLS
233  fos.close();
234  }
235  }
236  catch(IOException e ){
237  Logger.getLogger(ModuleSettings.class.getName()).log(Level.WARNING, "Could not remove property from file, file not found", e); //NON-NLS
238  }
239  }
240 
247  private static Properties fetchProperties(String moduleName)throws IOException{
248  InputStream inputStream = new FileInputStream(getPropertyPath(moduleName));
249  Properties props = new Properties();
250  props.load(inputStream);
251  inputStream.close();
252  return props;
253  }
254 
260  public static File getPropertyFile(String moduleName) {
261  String path = getPropertyPath(moduleName);
262  if (path == null){
263  return null;
264  }
265  else{
266  return new File(getPropertyPath(moduleName));
267  }
268  }
269 }
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)
static synchronized void removeProperty(String moduleName, String key)
static boolean settingExists(String moduleName, String settingName)
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.