Skip to content
Permalink
Browse files

Define a new extension point GlobalVariableSet to support dynamically…

… changing list of GlobalVariables.

To make the common case easy, GlobalVariable is still left as a separate extension point. To reduce the error prone-ness of listing all variables,
I added GlobalVariable.ALL.

Originally-Committed-As: e92efa79f9061eaf1516fd6979e7c0d6b541e0ff
  • Loading branch information...
kohsuke committed May 22, 2015
1 parent 9344430 commit 0c8bbb16c903947a3b02461e99676778fff0bbc6
@@ -100,7 +100,7 @@ public final Object invokeMethod(String name, Object args) {

@Override
public Object getProperty(String property) {
for (GlobalVariable v : ExtensionList.lookup(GlobalVariable.class)) {
for (GlobalVariable v : GlobalVariable.ALL) {
if (v.getName().equals(property)) {
try {
return v.getValue(this);
@@ -2,6 +2,7 @@

import hudson.ExtensionList;
import org.codehaus.groovy.runtime.GStringImpl;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.AbstractWhitelist;
@@ -39,7 +40,7 @@ public boolean permitsMethod(Method method, Object receiver, Object[] args) {
return true;
}
if (name.equals("getProperty") && args.length == 1 && args[0] instanceof String) {
for (GlobalVariable v : ExtensionList.lookup(GlobalVariable.class)) {
for (GlobalVariable v : GlobalVariable.ALL) {
if (v.getName().equals(args[0])) {
return true;
}
@@ -25,15 +25,23 @@
package org.jenkinsci.plugins.workflow.cps;

import groovy.lang.GroovyObject;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import javax.annotation.Nonnull;
import hudson.util.Iterators.FlattenIterator;
import jenkins.model.RunAction2;

import javax.annotation.Nonnull;
import java.util.Iterator;

/**
* Defines a provider of a global variable offered to flows.
* Within a given flow execution, the variable is assumed to be a singleton.
* It is created on demand if the script refers to it by name.
* <p>Should have a view named {@code help} offering usage.
*
* <p>
* Should have a view named {@code help} offering usage.
*
* @see GlobalVariableSet
*/
public abstract class GlobalVariable implements ExtensionPoint {

@@ -54,4 +62,18 @@
*/
public abstract @Nonnull Object getValue(@Nonnull CpsScript script) throws Exception;

/**
* Returns all the registered {@link GlobalVariable}s.
*/
public static final Iterable<GlobalVariable> ALL = new Iterable<GlobalVariable>() {
@Override
public Iterator<GlobalVariable> iterator() {
return new FlattenIterator<GlobalVariable,GlobalVariableSet>(ExtensionList.lookup(GlobalVariableSet.class).iterator()) {
@Override
protected Iterator<GlobalVariable> expand(GlobalVariableSet vs) {
return vs.iterator();
}
};
}
};
}
@@ -0,0 +1,31 @@
package org.jenkinsci.plugins.workflow.cps;

import hudson.Extension;
import hudson.ExtensionPoint;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import java.util.Iterator;

/**
* Extension point that defines a collection of global variables.
*
* @since 1.7
* @author Kohsuke Kawaguchi
* @see GlobalVariable#ALL
*/
public abstract class GlobalVariableSet implements ExtensionPoint, Iterable<GlobalVariable> {

/**
* Allow {@link GlobalVariable}s to be defined with {@link Extension}, and make them discoverable
* via {@link GlobalVariableSet}. This simplifies the registration of single global variable.
*/
@Restricted(NoExternalUse.class)
public static class GlobalVariableProvider extends GlobalVariableSet {
@Override
public Iterator<GlobalVariable> iterator() {
return Jenkins.getInstance().getExtensionList(GlobalVariable.class).iterator();
}
}
}
@@ -184,10 +184,10 @@ private static void render(StringBuilder b, Object value) {
return ds;
}

@Restricted(DoNotUse.class)
public Collection<GlobalVariable> getGlobalVariables() {
@Restricted(DoNotUse.class) // for stapler
public Iterable<GlobalVariable> getGlobalVariables() {
// TODO order TBD. Alphabetical? Extension.ordinal?
return ExtensionList.lookup(GlobalVariable.class);
return GlobalVariable.ALL;
}

@Restricted(NoExternalUse.class)

0 comments on commit 0c8bbb1

Please sign in to comment.
You can’t perform that action at this time.