package org.jenkinsci.plugins.SemanticVersioning;

import hudson.FilePath;
import hudson.remoting.Callable;
import org.jenkinsci.plugins.SemanticVersioning.naming.NamingStrategy;
import org.jenkinsci.plugins.SemanticVersioning.parsing.BuildDefinitionParser;
import org.jenkinsci.remoting.RoleChecker;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SemanticVersioningCallable implements Callable<SemanticVersioningResult,IOException>, Serializable {

	private static final long serialVersionUID = -2239554739563636620L;

	private String env;
	private boolean useBuildNumber;
	
	private FilePath workspace;
	private int buildNumber;
	
	private BuildDefinitionParser parser;
	private NamingStrategy namingStrategy;
	

    
    public SemanticVersioningCallable() {
	}
	
	@Override
	public void checkRoles(RoleChecker arg0) throws SecurityException {
	}

	@Override
	public SemanticVersioningResult call() throws IOException {
		
		SemanticVersioningResult out = new SemanticVersioningResult();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baos,true,"utf-8");
		
		ps.println("executing on "+InetAddress.getLocalHost().getHostName());
		
		try {
	        AppVersion appVersion = AppVersion.EmptyVersion;
	        
	        try {
	            appVersion = getAppVersion(ps);
				
	            ps.println("exporting vars ... ");
	            Map<String, String> vars = new HashMap<String, String>();
	            String v = namingStrategy.exportNames(appVersion, vars, useBuildNumber, buildNumber);

	            out.setVersion(v);
	            
			} catch (Exception e) {
				throw new IOException(e);
			}
	        
	        
	        Map<String,String> vars = new HashMap<String,String>();
	        namingStrategy.exportNames(appVersion,vars,useBuildNumber,buildNumber);
	        
	        out.setVars(vars);
	        
		} catch (Exception e) {
			e.printStackTrace(ps);
		}
		
		ps.flush();
		baos.flush();
		
		List<String> log = new ArrayList<String>();
		for(String l : new String(baos.toByteArray(),"utf-8").split("[\\r\\n]+")) {
			log.add(l);
		}
		
		out.setLog(log);
		
		return out;
	}

    private AppVersion getAppVersion(PrintStream log) throws IOException {
        AppVersion appVersion = AppVersion.EmptyVersion;
        if (this.parser != null) {
            try {
                appVersion = parser.extractAppVersion(this.getWorkspace(),log);
            } catch (Exception e) {
            	throw new IOException(e);
            }
        }
        return appVersion;
    }

	public String getEnv() {
		return env;
	}

	public void setEnv(String env) {
		this.env = env;
	}

	public BuildDefinitionParser getParser() {
		return parser;
	}

	public void setParser(BuildDefinitionParser parser) {
		this.parser = parser;
	}

	public NamingStrategy getNamingStrategy() {
		return namingStrategy;
	}

	public void setNamingStrategy(NamingStrategy namingStrategy) {
		this.namingStrategy = namingStrategy;
	}

	public boolean isUseBuildNumber() {
		return useBuildNumber;
	}

	public void setUseBuildNumber(boolean useBuildNumber) {
		this.useBuildNumber = useBuildNumber;
	}

	public FilePath getWorkspace() {
		return workspace;
	}

	public void setWorkspace(FilePath workspace) {
		this.workspace = workspace;
	}

	public int getBuildNumber() {
		return buildNumber;
	}

	public void setBuildNumber(int buildNumber) {
		this.buildNumber = buildNumber;
	}

}

@@ -79,24 +79,38 @@
 ort 
-hudson.remoting.
+jenkins.security.MasterToSlave
 Call
@@ -268,51 +268,8 @@
 ser;
-%0Aimport org.jenkinsci.remoting.RoleChecker;
 %0A%0Aim
@@ -442,24 +442,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -592,19 +592,29 @@
 ble 
-implement
+extend
 s 
+MasterToSlave
 Call
@@ -643,16 +643,17 @@
 gResult,
+ 
 IOExcept
@@ -656,17 +656,27 @@
 ception%3E
-,
+ implements
  Seriali
@@ -947,22 +947,16 @@
 ategy;%0A%09
-%0A%0A    
 %0A    pub
@@ -996,93 +996,8 @@
 %7B%0A%09%7D
-%0A%09%0A%09@Override%0A%09public void checkRoles(RoleChecker arg0) throws SecurityException %7B%0A%09%7D
 %0A%0A%09@
@@ -2153,53 +2153,46 @@
 ;%0A%09%09
-for(String l : new String(baos.toByteArray(),
+Collections.addAll(log, baos.toString(
 %22utf
@@ -2219,29 +2219,9 @@
 +%22))
- %7B%0A%09%09%09log.add(l);%0A%09%09%7D
+;
 %0A%09%09%0A

Fix type of Callable