Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ConfigProperties.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018 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.core;
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.Map.Entry;
27 import java.util.Properties;
28 import java.util.logging.Level;
29 import java.util.prefs.AbstractPreferences;
30 import java.util.prefs.BackingStoreException;
31 
36 class ConfigProperties extends AbstractPreferences {
37 
38  // use java util logger;
39  // autopsy core logger relies on UserPreferences which relies on this class
40  private static java.util.logging.Logger logger = null;
41 
49  private static java.util.logging.Logger getLogger() {
50  if (logger == null) {
51  logger = org.sleuthkit.autopsy.coreutils.Logger.getLogger(ConfigProperties.class.getName());
52  }
53  return logger;
54  }
55 
56  private final Properties inMemoryProperties = new Properties();
57  private final String configPath;
58 
65  public ConfigProperties(String configPath) {
66  super(null, "");
67  this.configPath = configPath;
68  }
69 
75  public synchronized void load() throws IOException {
76  Properties loaded = loadSavedProperties(this.configPath);
77  this.inMemoryProperties.clear();
78  mergeProperties(loaded, this.inMemoryProperties, true);
79  }
80 
81  @Override
82  protected void putSpi(String key, String value) {
83  inMemoryProperties.put(key, value);
84  tryFlush();
85  }
86 
87  @Override
88  protected String getSpi(String key) {
89  Object val = inMemoryProperties.get(key);
90  return val == null
91  ? null
92  : val.toString();
93  }
94 
95  @Override
96  protected void removeSpi(String key) {
97  inMemoryProperties.remove(key);
98  tryFlush();
99  }
100 
101  @Override
102  protected void removeNodeSpi() throws BackingStoreException {
103  inMemoryProperties.clear();
104  tryFlush();
105  }
106 
107  @Override
108  protected String[] keysSpi() throws BackingStoreException {
109  return inMemoryProperties.keySet().toArray(new String[inMemoryProperties.size()]);
110  }
111 
112  @Override
113  protected String[] childrenNamesSpi() throws BackingStoreException {
114  return new String[0];
115  }
116 
117  @Override
118  protected AbstractPreferences childSpi(String name) {
119  throw new IllegalArgumentException("Cannot create new child nodes");
120  }
121 
122  @Override
123  protected void syncSpi() throws BackingStoreException {
124  try {
125  Properties onDiskProps = loadSavedProperties(this.configPath);
126  mergeProperties(onDiskProps, this.inMemoryProperties, false);
127  flushSpi();
128  } catch (IOException ex) {
129  throw new BackingStoreException(new IOException("An error occurred while saving to: " + this.configPath, ex));
130  }
131  }
132 
136  private void tryFlush() {
137  try {
138  flushSpi();
139  } catch (BackingStoreException ex) {
140  getLogger().log(Level.SEVERE, "An error occurred when writing to disk at: " + this.configPath, ex);
141  }
142  }
143 
144  @Override
145  protected void flushSpi() throws BackingStoreException {
146  try (FileOutputStream fos = new FileOutputStream(this.configPath)) {
147  this.inMemoryProperties.store(fos, "Set settings (batch)");
148  } catch (IOException ex) {
149  throw new BackingStoreException(new IOException("An error occurred while saving to: " + this.configPath, ex));
150 
151  }
152  }
153 
164  private static Properties loadSavedProperties(String path) throws IOException {
165  Properties props = new Properties();
166 
167  File propFile = new File(path);
168  if (propFile.exists()) {
169  try (InputStream inputStream = new FileInputStream(propFile)) {
170  props.load(inputStream);
171  }
172  }
173 
174  return props;
175  }
176 
185  private static void mergeProperties(Properties src, Properties dest, boolean overwrite) {
186  if (overwrite) {
187  dest.putAll(src);
188  } else {
189  for (Entry<Object, Object> entry : dest.entrySet()) {
190  dest.putIfAbsent(entry.getKey(), entry.getValue());
191  }
192  }
193  }
194 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.