if (binding != null && binding.hasVariable(name)) { return binding.getVariable(name);
if (binding != null && binding.hasVariable(name)) { return binding.getVariable(name);
/** * Simple check for whether the binding contains a particular variable or not. * * @param name the name of the variable to check for */ @Override public boolean hasVariable(final String name) { return first.hasVariable(name) && second.hasVariable(name); }
/** * @param name the name of the variable to lookup * @return the variable value */ @Override public Object getVariable(final String name) { return first.hasVariable(name) ? first.getVariable(name) : second.getVariable(name); }
/** * Sets the value of the given variable * * @param name the name of the variable to set * @param value the new value for the given variable */ @Override public void setVariable(final String name, final Object value) { if(first.hasVariable(name)) first.setVariable(name, value); else second.setVariable(name, value); }
@Override public boolean hasProperty(String name) { return binding.hasVariable(name) || scriptObject.hasProperty(name) || dynamicTarget.hasProperty(name); }
@Override public Object getValue(CpsScript script) throws Exception { Binding binding = script.getBinding(); Object docker; if (binding.hasVariable(getName())) { docker = binding.getVariable(getName()); } else { // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation. docker = script.getClass().getClassLoader().loadClass("org.jenkinsci.plugins.docker.workflow.Docker").getConstructor(CpsScript.class).newInstance(script); binding.setVariable(getName(), docker); } return docker; }
public Object getProperty(String property) { Binding b = getBinding(); if(b.hasVariable(property)) { return b.getVariable(property); } return getMetaClass().getProperty(this, property); } }
public Object getProperty(String property) { Binding b = getBinding(); if(b.hasVariable(property)) { return b.getVariable(property); } return getMetaClass().getProperty(this, property); } }
@Nonnull @Override public Object getValue(CpsScript script) throws Exception { Binding binding = script.getBinding(); Object kubernetes; if (binding.hasVariable(getName())) { kubernetes = binding.getVariable(getName()); } else { // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation. kubernetes = script.getClass().getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.Kubernetes").getConstructor(CpsScript.class).newInstance(script); binding.setVariable(getName(), kubernetes); } return kubernetes; }
@Nonnull @Override public Object getValue(CpsScript script) throws Exception { Binding binding = script.getBinding(); Object kubernetes; if (binding.hasVariable(getName())) { kubernetes = binding.getVariable(getName()); } else { // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation. kubernetes = CubeDSL.class.getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.arquillian.cube.kubernetes.Cube").getConstructor(CpsScript.class).newInstance(script); binding.setVariable(getName(), kubernetes); } return kubernetes; }
@Nonnull @Override public Object getValue(CpsScript script) throws Exception { Binding binding = script.getBinding(); Object kubernetes; if (binding.hasVariable(getName())) { kubernetes = binding.getVariable(getName()); } else { // Note that if this were a method rather than a constructor, we would need to mark it @NonCPS lest it throw CpsCallableInvocation. kubernetes = CubeDSL.class.getClassLoader().loadClass("io.fabric8.kubernetes.pipeline.arquillian.cube.kubernetes.Cube").getConstructor(CpsScript.class).newInstance(script); binding.setVariable(getName(), kubernetes); } return kubernetes; }
/** * Execute groovy shell script t. * * @param <T> the type parameter * @param script the script * @param variables the variables * @param clazz the clazz * @return the t */ public static <T> T executeGroovyShellScript(final String script, final Map<String, Object> variables, final Class<T> clazz) { try { val binding = new Binding(); val shell = new GroovyShell(binding); if (variables != null && !variables.isEmpty()) { variables.forEach(binding::setVariable); } if (!binding.hasVariable("logger")) { binding.setVariable("logger", LOGGER); } LOGGER.debug("Executing groovy script [{}] with variables [{}]", script, binding.getVariables()); val result = shell.evaluate(script); return getGroovyScriptExecutionResultOrThrow(clazz, result); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } return null; }
@Nonnull @Override public Object getValue(@Nonnull CpsScript script) throws Exception { Binding binding = script.getBinding(); script.println(); Object openshift; if (binding.hasVariable(getName())) { openshift = binding.getVariable(getName()); } else { // Note that if this were a method rather than a constructor, we // would need to mark it @NonCPS lest it throw // CpsCallableInvocation. openshift = script.getClass().getClassLoader() .loadClass("com.openshift.jenkins.plugins.OpenShiftDSL") .getConstructor(CpsScript.class).newInstance(script); binding.setVariable(getName(), openshift); } return openshift; } }
@SuppressWarnings("rawtypes") public static AsyncChannel open(DeadlockFreeExecutor asyncChannelExecutor, Object key, int queueSize, Policy policy, Closure handler, Closure closer, Object owner, Binding binding, Execution parentStack){ AsyncChannel channel = new AsyncChannel(asyncChannelExecutor, key, queueSize, policy, handler, closer, owner, binding,parentStack); if(key!=null){ Collection<AsyncChannel> channels = ASYNC_CHANNEL_ROUTING.get(key); if(channels==null) { channels = ConcurrentHashMap.newKeySet(); Collection<AsyncChannel> oldChannels = ASYNC_CHANNEL_ROUTING.putIfAbsent(key, channels); if(oldChannels!=null) { channels = oldChannels; } } channels.add(channel); }else{ //update AnonymousChannel MBean when anonymous channel is opened asyncChannelAnonymousManager.incrementNumberOfTotalChannelsOpened(); } if(binding.hasVariable(ASYNC_CHANNEL_OBSERVER_KEY)) { channel.observer = ((AsyncChannelObserver)binding.getVariable(ASYNC_CHANNEL_OBSERVER_KEY)); channel.observer.opened(channel); } return channel; }
@SuppressWarnings("rawtypes") public static AsyncChannel open(DeadlockFreeExecutor asyncChannelExecutor, Object key, int queueSize, Policy policy, Closure handler, Closure closer, Object owner, Binding binding, Execution parentStack){ AsyncChannel channel = new AsyncChannel(asyncChannelExecutor, key, queueSize, policy, handler, closer, owner, binding,parentStack); if(key!=null){ Collection<AsyncChannel> channels = ASYNC_CHANNEL_ROUTING.get(key); if(channels==null) { channels = ConcurrentHashMap.newKeySet(); Collection<AsyncChannel> oldChannels = ASYNC_CHANNEL_ROUTING.putIfAbsent(key, channels); if(oldChannels!=null) { channels = oldChannels; } } channels.add(channel); }else{ //update AnonymousChannel MBean when anonymous channel is opened asyncChannelAnonymousManager.incrementNumberOfTotalChannelsOpened(); } if(binding.hasVariable(ASYNC_CHANNEL_OBSERVER_KEY)) { channel.observer = ((AsyncChannelObserver)binding.getVariable(ASYNC_CHANNEL_OBSERVER_KEY)); channel.observer.opened(channel); } return channel; }
@Nonnull @Override public Object getValue(@Nonnull CpsScript cpsScript) throws Exception { Binding binding = cpsScript.getBinding(); Object artifactory; if (binding.hasVariable(getName())) { artifactory = binding.getVariable(getName()); } else { artifactory = new ArtifactoryPipelineGlobal(cpsScript); binding.setVariable(getName(), artifactory); } return artifactory; } }
@Override public DynamicInvokeResult tryGetProperty(String property) { if (binding.hasVariable(property)) { return DynamicInvokeResult.found(binding.getVariable(property)); } DynamicInvokeResult result = scriptObject.tryGetProperty(property); if (result.isFound()) { return result; } return dynamicTarget.tryGetProperty(property); }
@Nonnull @Override public Object getValue(@Nonnull CpsScript script) throws Exception { Binding binding = script.getBinding(); Object instance; if (binding.hasVariable(getName())) { instance = binding.getVariable(getName()); } else { CpsThread c = CpsThread.current(); if (c==null) throw new IllegalStateException("Expected to be called from CpsThread"); instance = c.getExecution().getShell().getClassLoader().loadClass(getName()).newInstance(); /* We could also skip registration of vars in GroovyShellDecoratorImpl and use: instance = c.getExecution().getShell().parse(source(".groovy")); But then the source will appear in CpsFlowExecution.loadedScripts and be offered up for ReplayAction. We might *want* to support replay of global vars & classes at some point, but to make it actually work we would also need to start calling LoadStepExecution.Replacer. */ binding.setVariable(getName(), instance); } return instance; }
private void setBindings(Binding binding, Map<String, Object> parameters) { for (String key : parameters.keySet()) { binding.setVariable(key, parameters.get(key)); } for (String v : defineIfAbsent) { if (!binding.hasVariable(v)) { binding.setVariable(v, null); } } binding.setVariable("_p", parameters); binding.setVariable("_ns", namespace); binding.setVariable("_mmanager", KLAB.MMANAGER); binding.setVariable("_engine", KLAB.ENGINE); binding.setVariable("_pmanager", KLAB.PMANAGER); binding.setVariable("_kmanager", KLAB.KM); binding.setVariable("_config", KLAB.CONFIG); binding.setVariable("_network", KLAB.ENGINE.getNetwork()); }