Skip to content

Commit

Permalink
Use latest opencsv version and replace with builder (#62)
Browse files Browse the repository at this point in the history
* use latest opencsv version and replace with builder

* use jenkins.get() and remove null checks
  • Loading branch information
StefanSpieker authored Dec 4, 2022
1 parent 07ff86c commit 2248f50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.6</version>
<version>5.7.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -39,6 +41,9 @@

import javax.servlet.ServletException;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReaderBuilder;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.codec.digest.DigestUtils;
Expand Down Expand Up @@ -137,10 +142,7 @@ public FormValidation doCheckPropertyFile(@QueryParameter final String propertyF
}
property.execute();
}
catch(MalformedURLException e) {
return FormValidation.warning(Messages.ExtendedChoiceParameterDefinition_PropertyFileDoesntExist(), propertyFile);
}
catch(BuildException e) {
catch(MalformedURLException | BuildException e) {
return FormValidation.warning(Messages.ExtendedChoiceParameterDefinition_PropertyFileDoesntExist(), propertyFile);
}

Expand Down Expand Up @@ -762,22 +764,20 @@ private Binding getGroovyBinding() {

private synchronized GroovyShell getGroovyShell(String groovyClasspath) {
if(groovyShell == null) {
Jenkins jenkins = Jenkins.getInstance();
if(jenkins != null) {
ClassLoader cl = jenkins.getPluginManager().uberClassLoader;
Jenkins jenkins = Jenkins.get();
ClassLoader cl = jenkins.getPluginManager().uberClassLoader;

if(cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}

CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if(!StringUtils.isBlank(groovyClasspath)) {
compilerConfiguration.setClasspath(groovyClasspath);
}
if(cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}

Binding groovyBinding = getGroovyBinding();
groovyShell = new GroovyShell(cl, groovyBinding, compilerConfiguration);
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if(!StringUtils.isBlank(groovyClasspath)) {
compilerConfiguration.setClasspath(groovyClasspath);
}

Binding groovyBinding = getGroovyBinding();
groovyShell = new GroovyShell(cl, groovyBinding, compilerConfiguration);
}
else {
if(!StringUtils.isBlank(groovyClasspath)) {
Expand Down Expand Up @@ -837,10 +837,10 @@ private void setBindings(GroovyShell shell, String bindings) throws IOException
}
}

Jenkins instance = Jenkins.getInstance();
Jenkins instance = Jenkins.get();
shell.setProperty("jenkins", instance);

if(projectName != null && instance != null) {
if(projectName != null) {
AbstractProject<?, ?> project = (AbstractProject<?, ?>)instance.getItem(projectName);
shell.setProperty("currentProject", project);
}
Expand Down Expand Up @@ -939,10 +939,11 @@ Map<String, Set<String>> calculateChoicesByDropdownId() throws Exception {
String resolvedPropertyFile = expandVariables(propertyFile);
File file = new File(resolvedPropertyFile);
List<String[]> fileLines = Collections.emptyList();
CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();
if(file.isFile()) {
CSVReader csvReader = null;
try {
csvReader = new CSVReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), '\t');
csvReader = new CSVReaderBuilder(new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8)).withCSVParser(csvParser).build();
fileLines = csvReader.readAll();
}
finally {
Expand All @@ -953,7 +954,7 @@ Map<String, Set<String>> calculateChoicesByDropdownId() throws Exception {
URL propertyFileUrl = new URL(resolvedPropertyFile);
CSVReader csvReader = null;
try {
csvReader = new CSVReader(new InputStreamReader(propertyFileUrl.openStream(), "UTF-8"), '\t');
csvReader = new CSVReaderBuilder(new InputStreamReader(propertyFileUrl.openStream(), StandardCharsets.UTF_8)).withCSVParser(csvParser).build();
fileLines = csvReader.readAll();
}
finally {
Expand Down

0 comments on commit 2248f50

Please sign in to comment.