package com.github.maven_nar;

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

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.PropertyUtils;

public class NarProperties {
	
	private final static String AOL_PROPERTIES = "aol.properties";
	private Properties properties;
	private static NarProperties instance;
	
	private NarProperties(MavenProject project) throws MojoFailureException {
		
		Properties defaults = PropertyUtils.loadProperties( NarUtil.class.getResourceAsStream( AOL_PROPERTIES ) );
        if ( defaults == null )
        {
            throw new MojoFailureException( "NAR: Could not load default properties file: '"+AOL_PROPERTIES+"'." );
        }
        
        properties = new Properties(defaults);
        FileInputStream fis = null;
        try 
        {
        	if (project != null) {
        		fis = new FileInputStream(project.getBasedir()+File.separator+AOL_PROPERTIES);
        		properties.load( fis );
        	}
		} 
        catch (FileNotFoundException e) 
        {
			// ignore (FIXME)
		} 
        catch (IOException e) 
        {
			// ignore (FIXME)
		}
        finally
        {
            try
            {
                if ( fis != null )
                {
                    fis.close();
                }
            }
            catch ( IOException e )
            {
                // ignore
            }
        }

	}
	
	/**
	 * Retrieve the NarProperties
	 * @param project may be null
	 * @return
	 * @throws MojoFailureException
	 */
	public static NarProperties getInstance(MavenProject project) throws MojoFailureException {
		if (instance == null) {
			instance = new NarProperties(project);
		}
		return instance;
	}
	
	/**
	 * Programmatically inject properties (and possibly overwrite existing properties)
	 * @param project the current maven project
	 * @param properties the properties from input stream
	 */
	public static void inject(MavenProject project, InputStream properties) throws MojoFailureException {
		final Properties defaults = PropertyUtils.loadProperties( properties );
		final NarProperties nar = getInstance(project);
		nar.properties.putAll(defaults);
	}
	
	public String getProperty(String key) {
		return properties.getProperty(key);
	}
}

@@ -443,16 +443,92 @@
 rties%22;%0A
+%09private final static String CUSTOM_AOL_PROPERTY_KEY = %22nar.aolProperties%22;%0A
 %09private
@@ -1040,16 +1040,62 @@
 = null;%0A
+        String customPropertyLocation = null;%0A
         
@@ -1155,208 +1155,862 @@
   %09%09
-fis = new FileInputStream(project.getBasedir()+File.separator+AOL_PROPERTIES);%0A        %09%09properties.load( fis );%0A        %09%7D%0A%09%09%7D %0A        catch (FileNotFoundException e) %0A        %7B%0A%09%09%09// ignore (FIXME)
+customPropertyLocation = project.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);%0A        %09%09if (customPropertyLocation == null) %7B%0A        %09%09    // Try and read from the system property in case it's specified there%0A        %09%09    customPropertyLocation = System.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);%0A        %09%09%7D%0A        %09%09fis = new FileInputStream(customPropertyLocation == null ? %0A        %09%09                          customPropertyLocation : project.getBasedir()+File.separator+AOL_PROPERTIES);%0A        %09%09properties.load( fis );%0A        %09%7D%0A%09%09%7D %0A        catch (FileNotFoundException e) %0A        %7B%0A%09%09%09if (customPropertyLocation != null) %7B%0A%09%09%09    // We tried loading from a custom location - so throw the exception%0A%09%09%09    throw new MojoFailureException( %22NAR: Could not load custom properties file: '%22+customPropertyLocation+%22'.%22 );%0A%09%09%09%7D
 %0A%09%09%7D

Allow specifying a custom aol.properties file