Skip to content

Commit

Permalink
refactor(antexec): replace some Hudson references with Jenkins and im…
Browse files Browse the repository at this point in the history
…prove XML parsing
  • Loading branch information
svasek committed Jan 21, 2025
1 parent 04d6e2b commit 23bb366
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/main/java/hudson/plugins/antexec/AntExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
import hudson.tasks._ant.AntConsoleAnnotator;
import hudson.util.ArgumentListBuilder;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;

import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -180,7 +183,7 @@ Ant.AntInstallation getAnt() {
}

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder();
String scriptSourceResolved = scriptSource;
String extendedScriptSourceResolved = extendedScriptSource;
Expand Down Expand Up @@ -250,7 +253,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
antLibDir = new FilePath(ws, "antlib");
if (!antLibDir.exists()) {
FilePath antContribJar = new FilePath(antLibDir, "ant-contrib.jar");
FilePath antContribJarOnMaster = new FilePath(Hudson.getInstance().getRootPath(), "plugins/antexec/META-INF/lib/ant-contrib.jar");
FilePath antContribJarOnMaster = new FilePath(Jenkins.get().getRootPath(), "plugins/antexec/META-INF/lib/ant-contrib.jar");
antContribJar.copyFrom(antContribJarOnMaster.toURI().toURL());
}
args.add("-lib", antLibDir.getName());
Expand Down Expand Up @@ -346,14 +349,15 @@ public DescriptorImpl() {

// for compatibility reasons, the persistence is done by Ant.DescriptorImpl
public Ant.AntInstallation[] getInstallations() {
return Hudson.getInstance().getDescriptorByType(Ant.DescriptorImpl.class).getInstallations();
return Jenkins.get().getDescriptorByType(Ant.DescriptorImpl.class).getInstallations();
}

//Check if entered script source is wellformed xml document
public FormValidation doCheckScriptSource(@QueryParameter String value) throws IOException {
public FormValidation doCheckScriptSource(@QueryParameter String value) throws IOException, ParserConfigurationException, SAXException {
String xmlContent = makeBuildFileXml("", value, "test_script");
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.parse(new InputSource(new ByteArrayInputStream(xmlContent.getBytes("UTF-8"))));
return FormValidation.ok();
} catch (SAXException sax) {
Expand All @@ -362,17 +366,20 @@ public FormValidation doCheckScriptSource(@QueryParameter String value) throws I
}

//Check if entered extended script source is wellformed xml document
private FormValidation doCheckExtendedScriptSource(@QueryParameter String value) throws IOException {
@SuppressWarnings("unused")
private FormValidation doCheckExtendedScriptSource(@QueryParameter String value) throws IOException, ParserConfigurationException {
String xmlContent = makeBuildFileXml(value, "", "test_script");
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();

Check warning on line 374 in src/main/java/hudson/plugins/antexec/AntExec.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 256-374 are not covered by tests
reader.parse(new InputSource(new ByteArrayInputStream(xmlContent.getBytes("UTF-8"))));
return FormValidation.ok();
} catch (SAXException sax) {
return FormValidation.error("ERROR: " + sax.getLocalizedMessage());
}
}

@SuppressWarnings("rawtypes")
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// indicates that this builder can be used with all kinds of project types
return true;
Expand Down Expand Up @@ -405,7 +412,7 @@ static String makeBuildFileXml(String scriptSource, String extendedScriptSource,
return sb.toString();
}

static FilePath makeBuildFile(String scriptName, String targetSource, String extendedScriptSource, AbstractBuild build) throws IOException, InterruptedException {
static FilePath makeBuildFile(String scriptName, String targetSource, String extendedScriptSource, AbstractBuild<?, ?> build) throws IOException, InterruptedException {
String myScriptName = buildXml;
if (scriptName != null && scriptName.length() > 0 && !scriptName.equals("")) {
myScriptName = scriptName;
Expand All @@ -419,7 +426,7 @@ static FilePath makeBuildFile(String scriptName, String targetSource, String ext
return buildFile;
}

static FilePath makePropertyFile(String scriptName, AbstractBuild build, Properties buildProperties) throws IOException, InterruptedException {
static FilePath makePropertyFile(String scriptName, AbstractBuild<?, ?> build, Properties buildProperties) throws IOException, InterruptedException {
String myScriptName = buildXml;
if (scriptName != null && scriptName.length() > 0 && !scriptName.equals("")) {
myScriptName = scriptName;
Expand Down

0 comments on commit 23bb366

Please sign in to comment.