package org.jenkinsci.plugins.radargun.util;

import hudson.EnvVars;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.util.LogTaskListener;
import hudson.util.VariableResolver;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Convenient class for resolving/expanding various variabales.
 * 
 * @author vjuranek
 * 
 */
public class Resolver {
    
    private static AbstractBuild<?, ?> build;
    
    public static void init(final AbstractBuild<?, ?> build) {
        Resolver.build = build;
    }
    
    public static String doResolve(final String toResolve) throws IllegalStateException {
        if(build == null)
            throw new IllegalStateException("Variable resolver was not initialized!");
        
        return buildVar(build, toResolve);
    }

    public static String buildVar(final AbstractBuild<?, ?> build,final String toResolve) {
        if(toResolve == null)
            return null;
        
        VariableResolver<String> vr = build.getBuildVariableResolver();
        String resolved = Util.replaceMacro(toResolve, vr);
        try {
            EnvVars env = build.getEnvironment(new LogTaskListener(LOG, Level.INFO));
            resolved = env.expand(resolved);
        } catch (Exception e) {
            //TODO no-op?
        }
        return resolved;
    }
    
    private static final Logger LOG = Logger.getLogger(Resolver.class.getName());
}

@@ -39,16 +39,159 @@
 .util;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Properties;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 import h
@@ -205,16 +205,16 @@
 nvVars;%0A
-
 import h
@@ -338,74 +338,8 @@
 r;%0A%0A
-import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 /**%0A
@@ -505,24 +505,66 @@
 , ?%3E build;%0A
+    private static EnvVars propertiesEnv;%0A
     %0A    pub
@@ -647,24 +647,294 @@
 ld = build;%0A
+        Properties props = System.getProperties();%0A        Map%3CString, String%3E propMap = new HashMap%3C%3E(props.size());%0A        props.entrySet().forEach(entry -%3E propMap.put((String)entry.getKey(), (String)entry.getValue()));%0A        propertiesEnv = new EnvVars(propMap);%0A
     %7D%0A    %0A 
@@ -1159,16 +1159,26 @@
  return 
+javaProps(
 buildVar
@@ -1195,16 +1195,17 @@
 Resolve)
+)
 ;%0A    %7D%0A
@@ -1589,24 +1589,24 @@
 vel.INFO));%0A
-
             
@@ -1738,24 +1738,354 @@
 %0A    %7D%0A    %0A
+    public static String javaProps(String toResolve) %7B%0A        if(toResolve == null)%0A            return null;%0A        String resolved = toResolve;%0A        %0A        try %7B%0A            resolved = propertiesEnv.expand(toResolve);%0A        %7D catch (Exception e) %7B%0A            //TODO no-op?%0A        %7D%0A        return resolved;%0A    %7D%0A    %0A
     private 

Resolve also java properties