Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary use of reflection #214

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Computer;
Expand All @@ -21,6 +20,8 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.model.Jenkins;
Expand All @@ -31,7 +32,7 @@
@Extension
public class EnvInjectComputerListener extends ComputerListener implements Serializable {

private EnvVars getNewMasterEnvironmentVariables(@NonNull Computer c,
private NavigableMap<String, String> getNewMasterEnvironmentVariables(@NonNull Computer c,
@NonNull FilePath nodePath, @NonNull TaskListener listener) throws EnvInjectException, IOException, InterruptedException {

//Get env vars for the current node
Expand Down Expand Up @@ -67,7 +68,7 @@ private EnvVars getNewMasterEnvironmentVariables(@NonNull Computer c,
//Resolve against node env vars
envInjectEnvVarsService.resolveVars(globalPropertiesEnvVars, nodeEnvVars);

EnvVars envVars2Set = new EnvVars();
NavigableMap<String, String> envVars2Set = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
}
Expand All @@ -76,7 +77,7 @@ private EnvVars getNewMasterEnvironmentVariables(@NonNull Computer c,
return envVars2Set;
}

private EnvVars getNewSlaveEnvironmentVariables(@NonNull Computer c,
private NavigableMap<String, String> getNewSlaveEnvironmentVariables(@NonNull Computer c,
@NonNull FilePath nodePath, @NonNull TaskListener listener) throws EnvInjectException, IOException, InterruptedException {

Map<String, String> currentEnvVars = new HashMap<>();
Expand Down Expand Up @@ -114,7 +115,7 @@ private EnvVars getNewSlaveEnvironmentVariables(@NonNull Computer c,
//Resolve against node env vars
envInjectEnvVarsService.resolveVars(currentEnvVars, nodeEnvVars);

EnvVars envVars2Set = new EnvVars();
NavigableMap<String, String> envVars2Set = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
if (!unsetSystemVariables) {
envVars2Set.putAll(nodeEnvVars);
}
Expand All @@ -140,7 +141,7 @@ public void onOnline(Computer c, TaskListener listener) throws IOException, Inte
if (isActiveSlave(c)) {

try {
EnvVars envVars2Set = getNewSlaveEnvironmentVariables(c, nodePath, listener);
NavigableMap<String, String> envVars2Set = getNewSlaveEnvironmentVariables(c, nodePath, listener);
nodePath.act(new EnvInjectMasterEnvVarsSetter(envVars2Set));
} catch (EnvInjectException e) {
throw new IOException(e);
Expand All @@ -151,7 +152,7 @@ public void onOnline(Computer c, TaskListener listener) throws IOException, Inte
//use case : it is only on master
else if (isGlobalEnvInjectActivatedOnMaster()) {
try {
EnvVars envVars2Set = getNewMasterEnvironmentVariables(c, nodePath, listener);
NavigableMap<String, String> envVars2Set = getNewMasterEnvironmentVariables(c, nodePath, listener);
nodePath.act(new EnvInjectMasterEnvVarsSetter(envVars2Set));
} catch (EnvInjectException e) {
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,26 @@
package org.jenkinsci.plugins.envinject.service;

import hudson.EnvVars;
import hudson.Main;
import hudson.Platform;
import java.util.NavigableMap;
import jenkins.security.MasterToSlaveCallable;
import org.jenkinsci.lib.envinject.EnvInjectException;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* @author Gregory Boissinot
*/
public class EnvInjectMasterEnvVarsSetter extends MasterToSlaveCallable<Void, EnvInjectException> {

private @NonNull EnvVars enVars;
private @NonNull NavigableMap<String, String> enVars;

public EnvInjectMasterEnvVarsSetter(@NonNull EnvVars enVars) {
public EnvInjectMasterEnvVarsSetter(@NonNull NavigableMap<String, String> enVars) {
this.enVars = enVars;
}

private Field getModifiers() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
Field modifiers = null;
for (Field each : fields) {
if ("modifiers".equals(each.getName())) {
modifiers = each;
break;
}
}
if (modifiers == null) {
throw new NoSuchFieldException();
}
return modifiers;
}

@Override
public Void call() throws EnvInjectException {
try {
Field platformField = EnvVars.class.getDeclaredField("platform");
platformField.setAccessible(true);
platformField.set(enVars, Platform.current());
if (Main.isUnitTest || Main.isDevelopmentMode) {
enVars.remove("MAVEN_OPTS");
}
Field masterEnvVarsFiled = EnvVars.class.getDeclaredField("masterEnvVars");
masterEnvVarsFiled.setAccessible(true);
Field modifiersField = getModifiers();
modifiersField.setAccessible(true);
modifiersField.setInt(masterEnvVarsFiled, masterEnvVarsFiled.getModifiers() & ~Modifier.FINAL);
masterEnvVarsFiled.set(null, enVars);
} catch (IllegalAccessException | NoSuchFieldException iae) {
throw new EnvInjectException(iae);
} catch (InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}

public Void call() {
EnvVars.masterEnvVars.putAll(enVars);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this would work with multiple calls to EnvInjectMasterEnvVarsSetter#call. This very well may be incorrect. 😕

return null;
}

}