package edu.uw.apl.tupelo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Discovery {

	static final Log log = LogFactory.getLog( Discovery.class );
	
	/*
	  Locate a property value given a property name.  We look in two places
	  for the properties 'file', in this order:

	  1: In a real file name $(HOME)/.tupelo

	  2: In a resource (classpath-based) named /tupelo.prp

	  The first match wins.

	  It is expected that applications can also override this discovery
	  mechanism via e.g. cmd line options.
	*/

	static public String locatePropertyValue( String prpName ) {

		String result = null;
		
		// Search 1: a fixed file = $HOME/.tupelo
		if( result == null ) {
			String userHome = System.getProperty( "user.home" );
			File dir = new File( userHome );
			File f = new File( dir, ".tupelo" );
			if( f.isFile() && f.canRead() ) {
				log.info( "Searching in file " + f + " for property " +
						  prpName );
				try {
					FileInputStream fis = new FileInputStream( f );
					Properties p = new Properties();
					p.load( fis );
					fis.close();
					result = p.getProperty( prpName );
					log.info( "Located " + result );
				} catch( IOException ioe ) {
					log.info( ioe );
				}
			}
		}

		// Search 2: a fixed resource
		if( result == null ) {
			InputStream is = Discovery.class.getResourceAsStream
				( "/tupelo.prp" );
			if( is != null ) {
				try {
					log.info( "Searching in resource for property " +
							  prpName );
					Properties p = new Properties();
					p.load( is );
					result = p.getProperty( prpName );
					log.info( "Located " + result );
					is.close();
				} catch( IOException ioe ) {
					log.info( ioe );
				}
			}
		}
		return result;
	}
}

// eof

	
@@ -252,16 +252,286 @@
 ctory;%0A%0A
+/**%0A * Class for getting application properties. %3Cbr%3E%0A *%0A * It checks the JVM arguments, files ~/.tupelo and /etc/tupelo.prp, and a classpath resource /tupelo.prp file, in that order. %3Cbr%3E%0A *%0A * JVM args are specified by using -Dtupelo.&lt;Property Name%3E=&lt;Value%3E%0A */%0A
 public c
@@ -547,17 +547,16 @@
 overy %7B%0A
-%0A
 %09static 
@@ -614,16 +614,17 @@
 );%0A%09%0A%09/*
+*
 %0A%09  Loca
@@ -682,11 +682,12 @@
  in 
-two
+four
  pla
@@ -738,16 +738,21 @@
 der:
+ %3Cbr%3E
 %0A%0A%09  1: 
 In a
@@ -751,49 +751,204 @@
  1: 
-In a real file name $(HOME)/.tupelo%0A%0A%09  2
+Specified as JVM arguments, prefixed with tupelo.* (Example: -Dtupelo.store=./test-store) %3Cbr%3E%0A%0A%09  2: In a real file name $(HOME)/.tupelo %3Cbr%3E%0A%0A%09  3: In a real file name /etc/tupelo.prp %3Cbr%3E%0A%0A%09  4
 : In
@@ -994,16 +994,21 @@
 pelo.prp
+ %3Cbr%3E
 %0A%0A%09  The
@@ -1025,16 +1025,21 @@
 ch wins.
+ %3Cbr%3E
 %0A%0A%09  It 
@@ -1145,17 +1145,16 @@
 ns.%0A%09*/%0A
-%0A
 %09static 
@@ -1249,16 +1249,98 @@
 earch 1:
+ System property%0A%09%09result = System.getProperty(%22tupelo.%22+prpName);%0A%0A%09%09// Search 2:
  a fixed
@@ -1934,26 +1934,564 @@
  Search 
-2: a fixed
+3: file = /etc/tupelo.prp%0A%09%09if(result == null)%7B%0A%09%09    File f = new File(%22/etc/tupelo.prp%22);%0A%09%09    if(f.isFile() && f.canRead())%7B%0A%09%09        log.info(%22Searching in file %22+f+%22 for property %22+prpName);%0A%09%09        try %7B%0A%09%09            FileInputStream fis = new FileInputStream(f);%0A%09%09            Properties p = new Properties();%0A%09%09            p.load(fis);%0A%09%09            result = p.getProperty(prpName);%0A%09%09            log.info(%22Located %22+result);%0A%09%09        %7D catch(Exception e)%7B%0A%09%09            log.info(e);%0A%09%09        %7D%0A%09%09    %7D%0A%09%09%7D%0A%0A%09%09// Search 4: a classpath
  resourc

Update the Discovery class to cehck/etc/tupelo.prp and JVM args