enhance variable substitution (FELIX-1627) to use Ant properties, then System properties, then Environment variables.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@819912 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java b/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java
index 85f9261..8160bd6 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java
@@ -19,7 +19,6 @@
 
 package org.apache.felix.sigil.config;
 
-
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -27,29 +26,29 @@
 import java.util.Map;
 import java.util.Properties;
 
-
 public class BldFactory
 {
     private static Map<URI, BldProject> projects = new HashMap<URI, BldProject>();
 
-
-    public static IBldProject getProject( URI uri ) throws IOException
+    public static IBldProject getProject(URI uri) throws IOException
     {
-        return getProject( uri, false );
+        return load(uri, false, null);
     }
 
-
-    public static IBldProject getProject( URI uri, boolean ignoreCache ) throws IOException
+    public static IBldProject getProject(URI uri, Properties overrides) throws IOException
     {
-        return load( uri, ignoreCache );
+        return load(uri, false, overrides);
     }
 
-
-    public static IRepositoryConfig getConfig( URI uri ) throws IOException
+    public static IBldProject getProject(URI uri, boolean ignoreCache) throws IOException
     {
-        return load( uri, false );
+        return load(uri, ignoreCache, null);
     }
 
+    public static IRepositoryConfig getConfig(URI uri) throws IOException
+    {
+        return load(uri, false, null);
+    }
 
     /**
      * creates a new project file, initialised with defaults.
@@ -58,36 +57,35 @@
      * @return
      * @throws IOException
      */
-    public static IBldProject newProject( URI uri, String defaults ) throws IOException
+    public static IBldProject newProject(URI uri, String defaults) throws IOException
     {
-        BldProject project = new BldProject( uri );
+        BldProject project = new BldProject(uri, null);
         Properties p = new Properties();
-        if ( defaults != null )
-            p.setProperty( BldConfig.S_DEFAULTS, defaults );
-        project.loadDefaults( p );
+        if (defaults != null)
+            p.setProperty(BldConfig.S_DEFAULTS, defaults);
+        project.loadDefaults(p);
         return project;
     }
 
-
-    private static BldProject load( URI uri, boolean ignoreCache ) throws IOException
+    private static BldProject load(URI uri, boolean ignoreCache, Properties overrides) throws IOException
     {
         BldProject p = null;
-        if ( !ignoreCache )
+        if (!ignoreCache)
         {
-            p = projects.get( uri );
+            p = projects.get(uri);
         }
 
-        if ( p == null )
+        if (p == null)
         {
-            p = new BldProject( uri );
+            p = new BldProject(uri, overrides);
             p.load();
-            projects.put( uri, p );
+            projects.put(uri, p);
 
-            if ( Boolean.getBoolean( "org.apache.felix.sigil.config.test" ) )
+            if (Boolean.getBoolean("org.apache.felix.sigil.config.test"))
             {
-                File path = new File( uri.getPath() + ".tmp" );
-                System.out.println( "XXX: config.test writing: " + path );
-                p.saveAs( path );
+                File path = new File(uri.getPath() + ".tmp");
+                System.out.println("XXX: config.test writing: " + path);
+                p.saveAs(path);
             }
         }
         return p;
diff --git a/sigil/common/core/src/org/apache/felix/sigil/config/BldProject.java b/sigil/common/core/src/org/apache/felix/sigil/config/BldProject.java
index ed4d370..426e35b 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/config/BldProject.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/config/BldProject.java
@@ -61,8 +61,9 @@
     private static final int MAX_HEADER = 10240;
     // cache to avoid loading the same default config for each project
     private static Map<URL, BldConfig> defaultsCache = new HashMap<URL, BldConfig>();
-    private static Properties overrides;
+    private static Properties sysOverrides;
 
+    private Properties bldOverrides;
     private List<String> sourcePkgs;
     private BldConfig config;
     private BldConverter convert;
@@ -73,7 +74,7 @@
     private TreeSet<String> packageWildDefaults;
     private long lastModified;
 
-    /* package */BldProject(URI relLoc)
+    /* package */BldProject(URI relLoc, Properties overrides)
     {
         config = new BldConfig();
         convert = new BldConverter(config);
@@ -81,6 +82,7 @@
         File f = new File(loc);
         lastModified = f.lastModified();
         baseDir = f.getParentFile();
+        bldOverrides = overrides;
     }
 
     /* package */void load() throws IOException
@@ -158,7 +160,7 @@
 
         if (defaults != null)
         {
-            defaults = BldUtil.expand(defaults, new BldProperties(base));
+            defaults = BldUtil.expand(defaults, new BldProperties(base, bldOverrides));
         }
         else
         {
@@ -176,7 +178,7 @@
             {
                 File file = new File(base, defaults).getCanonicalFile();
                 URL url = file.toURL();
-                BldProperties bp = new BldProperties(file.getParentFile());
+                BldProperties bp = new BldProperties(file.getParentFile(), bldOverrides);
 
                 if (dflt == null)
                 {
@@ -230,9 +232,9 @@
 
     private static Properties getOverrides()
     {
-        if (overrides == null)
+        if (sysOverrides == null)
         {
-            overrides = new Properties();
+            sysOverrides = new Properties();
             Properties sysProps = System.getProperties();
 
             for (Object okey : sysProps.keySet())
@@ -240,13 +242,13 @@
                 String key = (String) okey;
                 if (key.startsWith(OVERRIDE_PREFIX))
                 {
-                    overrides.setProperty(key.substring(OVERRIDE_PREFIX.length()),
+                    sysOverrides.setProperty(key.substring(OVERRIDE_PREFIX.length()),
                         sysProps.getProperty(key));
                 }
             }
         }
 
-        return overrides;
+        return sysOverrides;
     }
 
     private void readHeader(InputStream in) throws IOException
@@ -621,7 +623,7 @@
     public Map<String, Properties> getRepositoryConfig()
     {
         HashMap<String, Properties> map = new HashMap<String, Properties>();
-        BldProperties bp = new BldProperties(baseDir);
+        BldProperties bp = new BldProperties(baseDir, bldOverrides);
 
         for (String name : config.getList(null, BldConfig.C_REPOSITORIES))
         {
diff --git a/sigil/common/core/src/org/apache/felix/sigil/config/BldProperties.java b/sigil/common/core/src/org/apache/felix/sigil/config/BldProperties.java
index 6b9e376..37400ef 100644
--- a/sigil/common/core/src/org/apache/felix/sigil/config/BldProperties.java
+++ b/sigil/common/core/src/org/apache/felix/sigil/config/BldProperties.java
@@ -26,7 +26,7 @@
 public class BldProperties extends Properties
 {
     private static final long serialVersionUID = 1L;
-    private static final BldProperties global = new BldProperties(null);
+    private static final BldProperties global = new BldProperties(null, null);
     private static final Properties sysEnv;
 
     static
@@ -40,10 +40,15 @@
 
     private final Properties mySysEnv;
     
-    BldProperties(File baseDir)
+    BldProperties(File baseDir, Properties overrides)
     {
         mySysEnv = new Properties(sysEnv);
         
+        if (overrides != null)
+        {
+            mySysEnv.putAll(overrides);
+        }
+        
         try
         {
             if (baseDir != null)
diff --git a/sigil/ivy/resolver/src/org/apache/felix/sigil/ant/BundleTask.java b/sigil/ivy/resolver/src/org/apache/felix/sigil/ant/BundleTask.java
index 6354e1e..7fce3d4 100644
--- a/sigil/ivy/resolver/src/org/apache/felix/sigil/ant/BundleTask.java
+++ b/sigil/ivy/resolver/src/org/apache/felix/sigil/ant/BundleTask.java
@@ -19,7 +19,6 @@
 
 package org.apache.felix.sigil.ant;
 
-
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -36,7 +35,6 @@
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.Path;
 
-
 public class BundleTask extends Task
 {
     private File[] classpath;
@@ -45,66 +43,66 @@
     private String property;
     private String sigilFile;
 
-
     @Override
     public void execute() throws BuildException
     {
-        if ( classpath == null )
-            throw new BuildException( "missing: attribute: classpathref" );
-        if ( destPattern == null )
-            throw new BuildException( "missing attribute: destpattern" );
+        if (classpath == null)
+            throw new BuildException("missing: attribute: classpathref");
+        if (destPattern == null)
+            throw new BuildException("missing attribute: destpattern");
+
+        @SuppressWarnings("unchecked")
+        Hashtable<String, String> projectProperties = getProject().getProperties();
+        Properties antProperties = new Properties();
+        antProperties.putAll(projectProperties);
 
         IBldProject project;
-
         try
         {
-            project = BldFactory.getProject( getSigilFileURI() );
-
+            project = BldFactory.getProject(getSigilFileURI(), antProperties);
         }
-        catch ( IOException e )
+        catch (IOException e)
         {
-            throw new BuildException( "failed to get project file: " + e );
+            throw new BuildException("failed to get project file: " + e);
         }
 
         Properties env = new Properties();
-        @SuppressWarnings("unchecked")
-        Hashtable<String, String> properties = getProject().getProperties();
-        for ( String key : properties.keySet() )
+        for (String key : projectProperties.keySet())
         {
-            if ( key.matches( "^[a-z].*" ) )
+            if (key.matches("^[a-z].*"))
             { // avoid props starting with Uppercase - bnd adds them to manifest
-                env.setProperty( key, properties.get( key ) );
+                env.setProperty(key, projectProperties.get(key));
             }
         }
 
-        BundleBuilder bb = new BundleBuilder( project, classpath, destPattern, env );
+        BundleBuilder bb = new BundleBuilder(project, classpath, destPattern, env);
         boolean anyModified = false;
 
-        for ( IBldBundle bundle : project.getBundles() )
+        for (IBldBundle bundle : project.getBundles())
         {
             String id = bundle.getId();
-            log( "creating bundle: " + id );
+            log("creating bundle: " + id);
             int nWarn = 0;
             int nErr = 0;
             String msg = "";
 
             try
             {
-                boolean modified = ( bb.createBundle( bundle, force, new BundleBuilder.Log()
-                {
-                    public void warn( String msg )
+                boolean modified = (bb.createBundle(bundle, force,
+                    new BundleBuilder.Log()
                     {
-                        log( msg, Project.MSG_WARN );
-                    }
+                        public void warn(String msg)
+                        {
+                            log(msg, Project.MSG_WARN);
+                        }
 
-
-                    public void verbose( String msg )
-                    {
-                        log( msg, Project.MSG_VERBOSE );
-                    }
-                } ) );
+                        public void verbose(String msg)
+                        {
+                            log(msg, Project.MSG_VERBOSE);
+                        }
+                    }));
                 nWarn = bb.warnings().size();
-                if ( modified )
+                if (modified)
                 {
                     anyModified = true;
                 }
@@ -113,86 +111,80 @@
                     msg = " (not modified)";
                 }
             }
-            catch ( Exception e )
+            catch (Exception e)
             {
                 List<String> errors = bb.errors();
-                if ( errors != null )
+                if (errors != null)
                 {
                     nErr = errors.size();
-                    for ( String err : errors )
+                    for (String err : errors)
                     {
-                        log( err, Project.MSG_ERR );
+                        log(err, Project.MSG_ERR);
                     }
                 }
-                throw new BuildException( "failed to create: " + id + ": " + e, e );
+                throw new BuildException("failed to create: " + id + ": " + e, e);
             }
             finally
             {
-                log( id + ": " + count( nErr, "error" ) + ", " + count( nWarn, "warning" ) + msg );
+                log(id + ": " + count(nErr, "error") + ", " + count(nWarn, "warning")
+                    + msg);
             }
         }
 
-        if ( anyModified && property != null )
+        if (anyModified && property != null)
         {
-            getProject().setProperty( property, "true" );
+            getProject().setProperty(property, "true");
         }
     }
 
-
     private URI getSigilFileURI()
     {
-        File file = sigilFile == null ? new File( getProject().getBaseDir(), IBldProject.PROJECT_FILE ) : new File(
-            sigilFile );
-        if ( !file.isFile() )
+        File file = sigilFile == null ? new File(getProject().getBaseDir(),
+            IBldProject.PROJECT_FILE) : new File(sigilFile);
+        if (!file.isFile())
         {
-            throw new BuildException( "File not found " + file.getAbsolutePath() );
+            throw new BuildException("File not found " + file.getAbsolutePath());
         }
         return file.toURI();
     }
 
-
-    private String count( int count, String msg )
+    private String count(int count, String msg)
     {
-        return count + " " + msg + ( count == 1 ? "" : "s" );
+        return count + " " + msg + (count == 1 ? "" : "s");
     }
 
-
-    public void setDestpattern( String pattern )
+    public void setDestpattern(String pattern)
     {
         this.destPattern = pattern;
     }
 
-
-    public void setForce( String force )
+    public void setForce(String force)
     {
-        this.force = Boolean.parseBoolean( force );
+        this.force = Boolean.parseBoolean(force);
     }
 
-
-    public void setProperty( String property )
+    public void setProperty(String property)
     {
         this.property = property;
     }
 
-
-    public void setClasspathref( String value )
+    public void setClasspathref(String value)
     {
-        Path p = ( Path ) getProject().getReference( value );
-        if ( p == null )
+        Path p = (Path) getProject().getReference(value);
+        if (p == null)
         {
-            throw new BuildException( value + "is not a path reference." );
+            throw new BuildException(value + "is not a path reference.");
         }
 
         String[] paths = p.list();
         classpath = new File[paths.length];
-        for ( int i = 0; i < paths.length; ++i )
+        for (int i = 0; i < paths.length; ++i)
         {
-            classpath[i] = new File( paths[i] );
+            classpath[i] = new File(paths[i]);
         }
     }
 
-
-    public void setSigilFile( String sigilFile )
+    public void setSigilFile(String sigilFile)
     {
         this.sigilFile = sigilFile;
     }