Skip to content

Commit

Permalink
Cleanup the code
Browse files Browse the repository at this point in the history
- [x] - Use try-with-resource
- [x] - squash similar exceptions
- [x] - Fix confusing reflection specification in EnvInjectActionRetriever
  • Loading branch information
oleg-nenashev committed Dec 26, 2017
1 parent b8c1386 commit ec607c0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 58 deletions.
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.Maps;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.commons.collections.map.UnmodifiableMap;
import org.jenkinsci.lib.envinject.service.EnvInjectSavable;
Expand Down Expand Up @@ -138,7 +137,7 @@ private Object writeReplace() throws ObjectStreamException {

Map<String, String> toWrite = getEnvMap();
if (toWrite == null) {
toWrite = Collections.<String, String>emptyMap();
toWrite = Collections.emptyMap();
}

if (build == null && rootDir == null) {
Expand Down
Expand Up @@ -12,6 +12,8 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
Expand All @@ -23,15 +25,15 @@ public class EnvInjectActionRetriever {
//Returns the abstract class Action due to a class loading issue
//with EnvInjectAction subclasses. Subclasses cannot be casted from
//all point of Jenkins (classes are not loaded in some points)
public Action getEnvInjectAction(AbstractBuild<?, ?> build) {
public Action getEnvInjectAction(@Nonnull AbstractBuild<?, ?> build) {
List<Action> actions;
if (build == null) {
throw new NullPointerException("A build object must be set.");
}
try {
Class<?> matrixClass = Class.forName("hudson.matrix.MatrixRun");
if (matrixClass.isInstance(build)) {
Method method = matrixClass.getMethod("getParentBuild", null);
Method method = matrixClass.getMethod("getParentBuild");
Object object = method.invoke(build);
if (object instanceof AbstractBuild<?, ?>) {
build = (AbstractBuild<?, ?>) object;
Expand All @@ -41,9 +43,7 @@ public Action getEnvInjectAction(AbstractBuild<?, ?> build) {
LOGGER.log(Level.FINEST, "hudson.matrix.MatrixRun is not installed", e);
} catch (NoSuchMethodException e) {
LOGGER.log(Level.WARNING, "The method getParentBuild does not exist for hudson.matrix.MatrixRun", e);
} catch (IllegalAccessException e) {
LOGGER.log(Level.WARNING, "There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e);
} catch (InvocationTargetException e) {
} catch (IllegalAccessException | InvocationTargetException e) {
LOGGER.log(Level.WARNING, "There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e);
}
actions = build.getActions();
Expand Down
Expand Up @@ -3,7 +3,6 @@
import hudson.Plugin;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Hudson;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down
Expand Up @@ -31,57 +31,36 @@ public Map<String, String> getEnvironment(File envInjectBaseDir) throws EnvInjec
throw new NullPointerException("A base directory of the envinject file must be set.");
}

FileReader fileReader = null;
try {
File f = new File(envInjectBaseDir, ENVINJECT_TXT_FILENAME);
if (!f.exists()) {
return null;
}
fileReader = new FileReader(f);
final Map<String, String> result = new HashMap<String, String>();
File f = new File(envInjectBaseDir, ENVINJECT_TXT_FILENAME);
if (!f.exists()) {
return null;
}

try(FileReader fileReader = new FileReader(f)) {
final Map<String, String> result = new HashMap<>();
fromTxt(fileReader, result);
return result;
} catch (FileNotFoundException fne) {
} catch (IOException fne) {
throw new EnvInjectException(fne);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
}
}

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "Deprecated class")
public void saveEnvironment(@Nonnull File rootDir,@Nonnull Map<String, String> envMap) throws EnvInjectException {
FileWriter fileWriter = null;
try {
File f = new File(rootDir, ENVINJECT_TXT_FILENAME);
fileWriter = new FileWriter(f);
Map<String, String> map2Write = new TreeMap<String, String>();

File f = new File(rootDir, ENVINJECT_TXT_FILENAME);
try(FileWriter fileWriter = new FileWriter(f)) {
Map<String, String> map2Write = new TreeMap<>();
map2Write.putAll(envMap);
toTxt(map2Write, fileWriter);
} catch (FileNotFoundException fne) {
throw new EnvInjectException(fne);
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
}
}

private void fromTxt(FileReader fileReader, Map<String, String> result) throws EnvInjectException {
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
try {
try(BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line, TOKEN);
int tokens = tokenizer.countTokens();
Expand All @@ -91,14 +70,6 @@ private void fromTxt(FileReader fileReader, Map<String, String> result) throws E
}
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
}
}

Expand Down
Expand Up @@ -69,11 +69,7 @@ public Map<String, String> getEnVars(AbstractBuild build) throws EnvInjectExcept
try {
Method method = envInjectAction.getClass().getMethod("getEnvMap");
return (Map<String, String>) method.invoke(envInjectAction);
} catch (NoSuchMethodException e) {
throw new EnvInjectException(e);
} catch (InvocationTargetException e) {
throw new EnvInjectException(e);
} catch (IllegalAccessException e) {
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new EnvInjectException(e);
}
}
Expand Down Expand Up @@ -217,10 +213,8 @@ private Map<String, String> gatherEnvVarsNode(@Nonnull AbstractProject project,

return envVars;

} catch (IOException ioe) {
} catch (IOException | InterruptedException ioe) {
throw new EnvInjectException(ioe);
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
}
}

Expand Down

0 comments on commit ec607c0

Please sign in to comment.