Skip to content

Commit

Permalink
[JENKINS-7113, JENKINS-10920] Backward compatibility and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vjuranek committed Sep 13, 2011
1 parent 2a0e746 commit 1de7a11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 126 deletions.
117 changes: 40 additions & 77 deletions src/main/java/hudson/plugins/groovy/Groovy.java
Expand Up @@ -15,7 +15,9 @@
import hudson.tasks.Builder;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
Expand Down Expand Up @@ -78,7 +80,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
int result;
try {
Map<String,String> envVars = build.getEnvironment(listener);
GroovyInstallation installation = getGroovy();
hudson.plugins.groovy.GroovyInstallation installation = getGroovy();
if(installation != null) {
envVars.put("GROOVY_HOME", installation.getHome());
}
Expand Down Expand Up @@ -138,14 +140,29 @@ public static final class DescriptorImpl extends AbstractGroovyDescriptor {

private boolean allowMacro;

@CopyOnWrite
@CopyOnWrite
private volatile GroovyInstallation[] installations = new GroovyInstallation[0];

@CopyOnWrite
private volatile List<hudson.plugins.groovy.GroovyInstallation> installations2 = new ArrayList<hudson.plugins.groovy.GroovyInstallation>();

DescriptorImpl() {
super(Groovy.class);
load();
}

public Object readResolve(){
// convert to new installation and drop the old one
if(installations.length > 0){
for(GroovyInstallation inst: installations){
hudson.plugins.groovy.GroovyInstallation inst2 = new hudson.plugins.groovy.GroovyInstallation(inst.getName(),inst.getHome(),null);
installations2.add(inst2);
}
installations = null;
}
return this;
}

public boolean getAllowMacro(){
return allowMacro;
}
Expand All @@ -164,28 +181,13 @@ public String getHelpFile() {
return "/plugin/groovy/project-config.html";
}

public GroovyInstallation[] getInstallations() {
return installations;
}

/*
@Override
public boolean configure(StaplerRequest req, JSONObject formData) {
try {
installations = req.bindJSONToList(GroovyInstallation.class, req.getSubmittedForm().get("groovy")).toArray(new GroovyInstallation[0]);
allowMacro = req.getSubmittedForm().getBoolean("allowMacro");
save();
return true;
} catch (ServletException ex) {
Logger.getLogger(Groovy.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
public hudson.plugins.groovy.GroovyInstallation[] getInstallations() {
hudson.plugins.groovy.GroovyInstallation[] installs = new hudson.plugins.groovy.GroovyInstallation[installations2.size()];
return installations2.toArray(installs);
}
*/

@Override
public Builder newInstance(StaplerRequest req, JSONObject data) throws FormException {

ScriptSource source = getScriptSource(req, data);
String instName = data.getString("groovyName");
String params = data.getString("parameters");
Expand All @@ -194,25 +196,29 @@ public Builder newInstance(StaplerRequest req, JSONObject data) throws FormExcep
String props = data.getString("properties");
String javaOpts = data.getString("javaOpts");
return new Groovy(source, instName, params, scriptParams, props, javaOpts, classPath);

//return (Groovy) req.bindJSON(clazz, data);
}

public static GroovyInstallation getGroovy(String groovyName) {
for( GroovyInstallation i : DESCRIPTOR.getInstallations() ) {
public static hudson.plugins.groovy.GroovyInstallation getGroovy(String groovyName) {
for( hudson.plugins.groovy.GroovyInstallation i : DESCRIPTOR.getInstallations() ) {
if(groovyName!=null && i.getName().equals(groovyName))
return i;
}
return null;
}

public void setInstallations(GroovyInstallation... installations) {
this.installations = installations;
public void setInstallations(hudson.plugins.groovy.GroovyInstallation... installations) {
//this.installations = installations;
this.installations2 = new ArrayList<hudson.plugins.groovy.GroovyInstallation>();
for(hudson.plugins.groovy.GroovyInstallation install: installations){
this.installations2.add(install);
}
save();
}

}
/*


// Keep it for backward compatibility
public static final class GroovyInstallation implements Serializable {

private final String name;
Expand All @@ -224,69 +230,26 @@ public GroovyInstallation(String name, String home) {
this.home = home;
}

*//**
/**
* install directory.
*//*
*/
public String getHome() {
return home;
}

*//**
/**
* Human readable display name.
*//*
*/
public String getName() {
return name;
}

*//**
* Gets the executable path of this groovy installation on the given target system.
*//*
public String getExecutable(VirtualChannel channel) throws IOException, InterruptedException {
return channel.call(new Callable<String, IOException>() {
public String call() throws IOException {
File exe = getExeFile("groovy");
if (exe.exists()) {
return exe.getPath();
} else {
throw new FileNotFoundException(exe.getPath() + " doesn't exist, please check your Groovy installation");
}
}
});
}
private File getExeFile(String execName) {
String groovyHome = Util.replaceMacro(getHome(),EnvVars.masterEnvVars);
File binDir = new File(groovyHome, "bin/");
if (File.separatorChar == '\\') {
if(new File(binDir, execName + ".exe").exists()) {
execName += ".exe";
} else {
execName += ".bat";
}
}
return new File(binDir, execName);
}
*//**
* Returns true if the executable exists.
*//*
public boolean exists() {
try {
return getExecutable(new LocalLauncher(new StreamTaskListener(new NullStream())).getChannel()) != null;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}

private static final long serialVersionUID = 1L;
}


*/
protected GroovyInstallation getGroovy() {
protected hudson.plugins.groovy.GroovyInstallation getGroovy() {
return DescriptorImpl.getGroovy(groovyName);
}

Expand All @@ -300,7 +263,7 @@ protected String[] buildCommandLine(AbstractBuild build, BuildListener listener,

String cmd = "groovy";//last hope in case of missing or not selected installation

GroovyInstallation installation = getGroovy();
hudson.plugins.groovy.GroovyInstallation installation = getGroovy();
if(installation != null) {
EnvVars env = build.getEnvironment(listener);
installation = installation.forNode(Computer.currentComputer().getNode(), listener);
Expand Down
53 changes: 4 additions & 49 deletions src/main/java/hudson/plugins/groovy/GroovyInstallation.java
Expand Up @@ -28,38 +28,11 @@

public class GroovyInstallation extends ToolInstallation implements EnvironmentSpecific<GroovyInstallation>, NodeSpecific<GroovyInstallation> {

private final String name;
private final String home;

/*
public GroovyInstallation(String name, String home) {
super(name,home,new LinkedList());
this.name = name;
this.home = home;
}
*/
@DataBoundConstructor
public GroovyInstallation(String name, String home, List<? extends ToolProperty<?>> properties){
super(name,home,properties);
this.name = name;
this.home = home;

}

/**
* install directory.
*/
public String getHome() {
return home;
}

/**
* Human readable display name.
*/
public String getName() {
return name;
}

/**
* Gets the executable path of this groovy installation on the given target system.
*/
Expand Down Expand Up @@ -88,21 +61,6 @@ private File getExeFile(String execName) {
}
return new File(binDir, execName);
}

/**
* Returns true if the executable exists.
*/
/*
public boolean exists() {
try {
return getExecutable(new LocalLauncher(new StreamTaskListener(new NullStream())).getChannel()) != null;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}
*/

public GroovyInstallation forEnvironment(EnvVars environment) {
return new GroovyInstallation(getName(), environment.expand(getHome()), getProperties().toList());
Expand All @@ -120,16 +78,14 @@ public DescriptorImpl() {

@Override
public String getDisplayName() {
return "Groovy";//Messages.installer_displayName();
return "Groovy";
}

@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new GroovyInstaller(null));
}

// for compatibility reasons, the persistence is done by GradleBuilder.DescriptorImpl

}

@Override
public GroovyInstallation[] getInstallations() {
return Hudson.getInstance().getDescriptorByType(Groovy.DescriptorImpl.class).getInstallations();
Expand All @@ -141,8 +97,7 @@ public void setInstallations(GroovyInstallation... installations) {
}

}



private static final long serialVersionUID = 1L;

}

0 comments on commit 1de7a11

Please sign in to comment.