Skip to content

Commit

Permalink
store preferences in file .jorgan/jorgan-<version>.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
svenmeier committed Dec 24, 2011
1 parent 6832aa8 commit 117c256
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 96 deletions.
17 changes: 16 additions & 1 deletion bias/src/main/java/bias/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ public Configuration() {
this.path = "";

this.configurations = new HashMap<String, Configuration>();
configurations.put(this.path, this);
}

private Configuration(String path, Configuration parent) {
this.path = path;
this.parent = parent;

this.configurations = parent.configurations;
configurations.put(path, this);
configurations.put(this.path, this);
}

/**
Expand Down Expand Up @@ -307,6 +308,20 @@ private Property getProperty(Class<?> clazz, String key) {
return property;
}

public void flush() {
for (String path : configurations.keySet()) {
if (path.startsWith(this.path)) {
Configuration configuration = configurations.get(path);

if (configuration.stores != null) {
for (Store store : configuration.stores) {
store.flush();
}
}
}
}
}

private static final Configuration root = new Configuration();

/**
Expand Down
2 changes: 2 additions & 0 deletions bias/src/main/java/bias/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ public interface Store {
public void removeListener(StoreListener listener);

public boolean isReadOnly();

public void flush();
}
6 changes: 6 additions & 0 deletions bias/src/main/java/bias/store/AbstractStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ public static String getPath(String key) {
return key.substring(0, index);
}
}

/**
* Default implementation does nothing.
*/
public void flush() {
}
}
5 changes: 5 additions & 0 deletions bias/src/main/java/bias/store/DefaultingStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ protected Object getValueImpl(String key, Type type) {
protected void setValueImpl(String key, Type type, Object value) {
store.setValue(key, type, value);
}

@Override
public void flush() {
store.flush();
}
}
72 changes: 61 additions & 11 deletions bias/src/main/java/bias/store/PropertiesStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
*/
package bias.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import bias.ConfigurationException;

/**
* A store using {@link java.util.Properties}.
*/
public class PropertiesStore extends ConvertingStore {

private Properties properties;

private File file;

/**
* Create a store on fresh {@link Properties}.
Expand All @@ -38,6 +45,29 @@ public PropertiesStore() {
this(new Properties());
}

public PropertiesStore(File file) {
this();

this.file = file.getAbsoluteFile();

if (this.file.isDirectory()) {
throw new ConfigurationException("file must not be a directory");
}

if (this.file.exists()) {
try {
FileInputStream input = new FileInputStream(file);
try {
properties.load(input);
} finally {
input.close();
}
} catch (IOException ex) {
throw new ConfigurationException(ex);
}
}
}

/**
* Create a store using the given properties.
*
Expand All @@ -51,20 +81,40 @@ public PropertiesStore(Properties properties) {
public PropertiesStore(Class<?> clazz, String name) {
this.properties = new Properties();

InputStream input = clazz.getResourceAsStream(name);
if (input == null) {
throw new IllegalArgumentException("properties '" + name
+ "' not found");
}

try {
properties.load(input);
} catch (IOException ex) {
throw new Error(ex);
} finally {
InputStream input = clazz.getResourceAsStream(name);
if (input == null) {
throw new ConfigurationException("properties '" + name
+ "' not found");
}
try {
properties.load(input);
} finally {
input.close();
} catch (IOException ignore) {
}
} catch (IOException ex) {
throw new ConfigurationException(ex);
}
}

public void flush() {
if (this.file != null) {
File directory = this.file.getParentFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
throw new ConfigurationException("cannot create directory '" + directory +"'");
}
}

try {
FileOutputStream output = new FileOutputStream(file);
try {
properties.store(output, "bias");
} finally {
output.close();
}
} catch (IOException ex) {
throw new ConfigurationException(ex);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions bias/src/test/java/bias/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,8 @@ public void removeListener(StoreListener listener) {
public boolean isReadOnly() {
return false;
}

public void flush() {
}
};
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package jorgan.customizer;


import java.util.ArrayList;
import java.util.List;

import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class CustomizerConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class ExecutorConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package jorgan.exporter;


import java.util.ArrayList;
import java.util.List;

import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class ExporterConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class FluidsynthConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class GUIConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package jorgan.importer;


import java.util.ArrayList;
import java.util.List;

import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class ImporterConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class LanConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package jorgan.midimerger;


import java.util.ArrayList;
import java.util.List;

import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class MidiMergerConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import jorgan.spi.ConfigurationProvider;
import bias.Store;
import bias.store.DefaultingStore;
import bias.store.PreferencesStore;
import bias.store.PropertiesStore;

public class RecorderConfigurationProvider implements ConfigurationProvider {

public List<Store> getStores() {
public List<Store> getStores(Store preferencesStore) {
ArrayList<Store> stores = new ArrayList<Store>();

stores.add(new DefaultingStore(PreferencesStore.user(),
new PropertiesStore(getClass(), "preferences.properties")));
stores.add(new DefaultingStore(preferencesStore, new PropertiesStore(
getClass(), "preferences.properties")));

return stores;
}
Expand Down
Loading

0 comments on commit 117c256

Please sign in to comment.