package org.owasp.appsensor.configuration.stax.server;

import javax.inject.Named;

import org.owasp.appsensor.core.configuration.server.ServerConfiguration;
import org.owasp.appsensor.core.configuration.server.ServerConfigurationReader;
import org.owasp.appsensor.core.exceptions.ConfigurationException;
import org.springframework.beans.BeanUtils;

/**
 * Represents the configuration for server-side components. 
 * 
 * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/
 */
@Named
public class StaxServerConfiguration extends ServerConfiguration {

	public StaxServerConfiguration() {
		this(true);
	}
	
	public StaxServerConfiguration(boolean loadConfiguration) {
		if (loadConfiguration) {
			ServerConfiguration configuration = loadConfiguration(new StaxServerConfigurationReader());
			if (configuration != null) {
				BeanUtils.copyProperties(configuration, this);
			}
		}
	}
	
	/**
	 * Bootstrap mechanism that loads the configuration for the server object based 
	 * on the specified configuration reading mechanism. 
	 * 
	 * The reference implementation of the configuration is XML-based, but this interface 
	 * allows for whatever mechanism is desired
	 * 
	 * @param configurationReader desired configuration reader 
	 */
	private ServerConfiguration loadConfiguration(ServerConfigurationReader configurationReader) {
		ServerConfiguration configuration = null;
		
		try {
			configuration = configurationReader.read();
		} catch(ConfigurationException pe) {
			throw new RuntimeException(pe);
		}
		
		return configuration;
	}
}

@@ -49,16 +49,45 @@
 erver;%0A%0A
+import java.io.InputStream;%0A%0A
 import j
@@ -323,24 +323,81 @@
 nException;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -646,16 +646,228 @@
 tion %7B%0A%0A
+%09//This needs to be manually setup (not @Loggable) b/c it's used in constructor%0A%09//so the post-processor won't run before it's used - will get NPE%0A%09private Logger logger = LoggerFactory.getLogger(getClass());%0A%09%09%0A
 %09public 
@@ -1001,16 +1001,26 @@
 tion) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09Serve
@@ -1106,16 +1106,22 @@
 der());%0A
+%09%09%09%09%0A%09
 %09%09%09if (c
@@ -1144,16 +1144,17 @@
 null) %7B%0A
+%09
 %09%09%09%09Bean
@@ -1196,16 +1196,613 @@
  this);%0A
+%09%09%09%09%7D%0A%09%09%09%7D catch (ConfigurationException e) %7B%0A%09%09%09%09InputStream inputStream = this.getClass().getClassLoader()%0A                        .getResourceAsStream(%22appsensor-client-config.xml%22);%0A%09%09%09%09if (inputStream != null) %7B%0A%09%09%09%09%09//report quiet error if we find the server config on the filesystem%0A%09%09%09%09%09logger.warn(%22Could not load appsensor server configuration file. %22%0A%09%09%09%09%09%09%09+ %22This error is fine if you are running a client.%22);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09//report noisy error if we can't find the server config on the filesystem%0A%09%09%09%09%09logger.warn(%22Could not load appsensor server configuration file.%22, e);%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2256,18 +2256,15 @@
 er) 
-%7B%0A%09%09Server
+throws 
 Conf
@@ -2276,46 +2276,29 @@
 tion
- configuration = null;%0A%09%09%0A%09%09try %7B%0A%09%09%09c
+Exception %7B%0A%09%09ServerC
 onfi
@@ -2305,18 +2305,16 @@
 guration
- =
  configu
@@ -2323,100 +2323,38 @@
 tion
-Reader.read();%0A%09%09%7D catch(ConfigurationException pe) %7B%0A%09%09%09throw new RuntimeException(pe);%0A%09%09%7D
+ = configurationReader.read();
 %0A%09%09%0A

make the lack of a server config optional (fixing bug) so that client doesn't need to specify it