/*
 * Copyright (c) 2010 Célio Cidral Junior
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.tomighty.config;

import java.io.File;
import java.util.Properties;

import org.tomighty.ioc.Initializable;
import org.tomighty.ioc.Inject;

public class Configuration implements Initializable {
	
	@Inject private Directories directories;
	@Inject private PropertyStore propertyStore;
	private Properties properties;
	private File configFile;
	
	@Override
	public void initialize() {
		configFile = new File(directories.configuration(), "tomighty.conf");
		properties = propertyStore.load(configFile);
	}

	public int asInt(String name, int defaultValue) {
		String value = properties.getProperty(name);
		return value == null ? defaultValue : Integer.parseInt(value);
	}
	
	public boolean asBoolean(String name, boolean defaultValue) {
		String value = properties.getProperty(name);
		return value == null ? defaultValue : Boolean.parseBoolean(value);
	}

	public void set(String name, boolean value) {
		set(name, String.valueOf(value));
	}

	public void set(String name, int value) {
		set(name, String.valueOf(value));
	}

	private void set(String name, String value) {
		properties.setProperty(name, value);
		saveConfiguration();
	}

	private void saveConfiguration() {
		propertyStore.store(properties, configFile);
	}
	
}

@@ -729,24 +729,60 @@
 perties;%0D%0A%0D%0A
+import org.tomighty.ioc.Container;%0D%0A
 import org.t
@@ -805,24 +805,24 @@
 ializable;%0D%0A
-
 import org.t
@@ -992,16 +992,55 @@
 Store;%0D%0A
+%09@Inject private Container container;%0D%0A
 %09private
@@ -1611,32 +1611,261 @@
 ean(value);%0D%0A%09%7D%0D
+%0A%09%0D%0A%09public %3CT%3E T asObject(String name, Class%3CT%3E defaultClass) %7B%0D%0A%09%09String className = properties.getProperty(name);%0D%0A%09%09if(className == null) %7B%0D%0A%09%09%09return container.get(defaultClass);%0D%0A%09%09%7D%0D%0A%09%09return container.get(className);%0D%0A%09%7D%0D
 %0A%0D%0A%09public void 
@@ -1978,32 +1978,32 @@
 , int value) %7B%0D%0A
-
 %09%09set(name, Stri
@@ -2021,32 +2021,118 @@
 value));%0D%0A%09%7D%0D%0A%0D%0A
+%09public void set(String name, Class%3C?%3E value) %7B%0D%0A%09%09set(name, value.getName());%0D%0A%09%7D%0D%0A%0D%0A
 %09private void se

Add methods to get/set configuration settings as objects