Java Code Examples for groovy.lang.Binding#getVariable()

The following examples show how to use groovy.lang.Binding#getVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyScriptBase.java    From java-trader with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProperty(String property) {
    Object result = null;
    Binding binding = getBinding();
    //先判断本地局部变量是否存在
    if ( binding.hasVariable(property) ) {
        result = binding.getVariable(property);
        return result;
    }
    //再判断标准变量是否存在
    result = context.varGet(property);
    //再回头找一遍
    if ( result==null ) {
        result = super.getProperty(property);
    }
    return result;
}
 
Example 2
Source File: OpenShiftGlobalVariable.java    From jenkins-client-plugin with Apache License 2.0 6 votes vote down vote up
@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;

}
 
Example 3
Source File: OpenShiftGlobalVariable.java    From jenkins-client-plugin with Apache License 2.0 6 votes vote down vote up
@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;

}
 
Example 4
Source File: TestScriptletProcessor.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testCorrectScriptExecution() {

    ScriptletProcessor processor = getProcessor(dataFields1, "msg = 'Hello ' + ch.epfl.gsn; def msg1 = 'This is a script internal variable.'");
    StreamElement se = new StreamElement(dataFields1, data1);
    Binding context = processor.updateContext(se);
    context.setVariable("ch.epfl.gsn", new String("Groovy GSN"));
    processor.evaluate(processor.scriptlet, se, true);
    assertNotNull(context.getVariable("msg"));
    assertEquals(context.getVariable("msg"), "Hello Groovy GSN");

    Object o = null;
    try {
        o = context.getVariable("msg1");
    }
    catch (Exception e) {}
    assertNull(o);
}
 
Example 5
Source File: GroovyScriptUtils.java    From genie with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <R> Set<R> getSetVariable(
    final Binding binding,
    final String bindingName,
    final Class<R> expectedType
) {
    if (!binding.hasVariable(bindingName) || !(binding.getVariable(bindingName) instanceof Set<?>)) {
        throw new IllegalArgumentException(
            "Expected " + bindingName + " to be instance of Set<" + expectedType.getName() + ">"
        );
    }
    final Set<?> set = (Set<?>) binding.getVariable(bindingName);
    if (set.isEmpty() || !set.stream().allMatch(expectedType::isInstance)) {
        throw new IllegalArgumentException(
            "Expected " + bindingName + " to be non-empty set of " + expectedType.getName()
        );
    }
    return (Set<R>) set;
}
 
Example 6
Source File: KubernetesDSL.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: CubeDSL.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: TransformActivity.java    From mdw with Apache License 2.0 5 votes vote down vote up
private void executeGPath() throws ActivityException {
    String transform = (String) getAttributeValue(RULE);
    if (StringUtils.isBlank(transform)) {
        getLogger().info("No transform defined for activity: " + getActivityName());
        return;
    }

    GroovyShell shell = new GroovyShell(getClass().getClassLoader());
    Script script = shell.parse(transform);
    Binding binding = new Binding();

    Variable inputVar = getMainProcessDefinition().getVariable(inputDocument);
    if (inputVar == null)
      throw new ActivityException("Input document variable not found: " + inputDocument);
    String inputVarName = inputVar.getName();
    Object inputVarValue = getGPathParamValue(inputVarName, inputVar.getType());
    binding.setVariable(inputVarName, inputVarValue);

    Variable outputVar = getMainProcessDefinition().getVariable(getOutputDocuments()[0]);
    String outputVarName = outputVar.getName();
    Object outputVarValue = getGPathParamValue(outputVarName, outputVar.getType());
    binding.setVariable(outputVarName, outputVarValue);

    script.setBinding(binding);

    script.run();

    Object groovyVarValue = binding.getVariable(outputVarName);
    setGPathParamValue(outputVarName, outputVar.getType(), groovyVarValue);
}
 
Example 9
Source File: GroovyScriptEngineFactory.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static <T> Optional<T> getVariable(final Binding binding, final String name, final Class<T> type) {
  if (binding.hasVariable(name)) {
    Object instance = binding.getVariable(name);
    if(type.isInstance(instance)) {
      return (Optional<T>) Optional.of(instance);
    }
  }
  return Optional.empty();
}
 
Example 10
Source File: DockerDSL.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@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;
}
 
Example 11
Source File: GroovyScriptUtils.java    From genie with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <R> R getObjectVariable(
    final Binding binding,
    final String variableName,
    final Class<R> expectedType
) {
    if (!binding.hasVariable(variableName)
        || !(binding.getVariable(variableName).getClass().isAssignableFrom(expectedType))) {
        throw new IllegalArgumentException(variableName + " argument not instance of " + expectedType.getName());
    }
    return (R) binding.getVariable(variableName);
}
 
Example 12
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * This method overrides property retrieval in the scope of the
 * {@code GroovyBeanDefinitionReader}. A property retrieval will either:
 * <ul>
 * <li>Retrieve a variable from the bean builder's binding if it exists
 * <li>Retrieve a RuntimeBeanReference for a specific bean if it exists
 * <li>Otherwise just delegate to MetaClass.getProperty which will resolve
 * properties from the {@code GroovyBeanDefinitionReader} itself
 * </ul>
 */
public Object getProperty(String name) {
	Binding binding = getBinding();
	if (binding != null && binding.hasVariable(name)) {
		return binding.getVariable(name);
	}
	else {
		if (this.namespaces.containsKey(name)) {
			return createDynamicElementReader(name);
		}
		if (getRegistry().containsBeanDefinition(name)) {
			GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
					getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
			if (beanDefinition != null) {
				return new GroovyRuntimeBeanReference(name, beanDefinition, false);
			}
			else {
				return new RuntimeBeanReference(name, false);
			}
		}
		// This is to deal with the case where the property setter is the last
		// statement in a closure (hence the return value)
		else if (this.currentBeanDefinition != null) {
			MutablePropertyValues pvs = this.currentBeanDefinition.getBeanDefinition().getPropertyValues();
			if (pvs.contains(name)) {
				return pvs.get(name);
			}
			else {
				DeferredProperty dp = this.deferredProperties.get(this.currentBeanDefinition.getBeanName() + name);
				if (dp != null) {
					return dp.value;
				}
				else {
					return getMetaClass().getProperty(this, name);
				}
			}
		}
		else {
			return getMetaClass().getProperty(this, name);
		}
	}
}
 
Example 13
Source File: GroovyBeanDefinitionReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * This method overrides property retrieval in the scope of the
 * {@code GroovyBeanDefinitionReader}. A property retrieval will either:
 * <ul>
 * <li>Retrieve a variable from the bean builder's binding if it exists
 * <li>Retrieve a RuntimeBeanReference for a specific bean if it exists
 * <li>Otherwise just delegate to MetaClass.getProperty which will resolve
 * properties from the {@code GroovyBeanDefinitionReader} itself
 * </ul>
 */
public Object getProperty(String name) {
	Binding binding = getBinding();
	if (binding != null && binding.hasVariable(name)) {
		return binding.getVariable(name);
	}
	else {
		if (this.namespaces.containsKey(name)) {
			return createDynamicElementReader(name);
		}
		if (getRegistry().containsBeanDefinition(name)) {
			GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
					getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
			if (beanDefinition != null) {
				return new GroovyRuntimeBeanReference(name, beanDefinition, false);
			}
			else {
				return new RuntimeBeanReference(name, false);
			}
		}
		// This is to deal with the case where the property setter is the last
		// statement in a closure (hence the return value)
		else if (this.currentBeanDefinition != null) {
			MutablePropertyValues pvs = this.currentBeanDefinition.getBeanDefinition().getPropertyValues();
			if (pvs.contains(name)) {
				return pvs.get(name);
			}
			else {
				DeferredProperty dp = this.deferredProperties.get(this.currentBeanDefinition.getBeanName() + name);
				if (dp != null) {
					return dp.value;
				}
				else {
					return getMetaClass().getProperty(this, name);
				}
			}
		}
		else {
			return getMetaClass().getProperty(this, name);
		}
	}
}
 
Example 14
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This method overrides property retrieval in the scope of the
 * {@code GroovyBeanDefinitionReader} to either:
 * <ul>
 * <li>Retrieve a variable from the bean builder's binding if it exists
 * <li>Retrieve a RuntimeBeanReference for a specific bean if it exists
 * <li>Otherwise just delegate to MetaClass.getProperty which will resolve
 * properties from the {@code GroovyBeanDefinitionReader} itself
 * </ul>
 */
public Object getProperty(String name) {
	Binding binding = getBinding();
	if (binding != null && binding.hasVariable(name)) {
		return binding.getVariable(name);
	}
	else {
		if (this.namespaces.containsKey(name)) {
			return createDynamicElementReader(name);
		}
		if (getRegistry().containsBeanDefinition(name)) {
			GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
					getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
			if (beanDefinition != null) {
				return new GroovyRuntimeBeanReference(name, beanDefinition, false);
			}
			else {
				return new RuntimeBeanReference(name, false);
			}
		}
		// This is to deal with the case where the property setter is the last
		// statement in a closure (hence the return value)
		else if (this.currentBeanDefinition != null) {
			MutablePropertyValues pvs = this.currentBeanDefinition.getBeanDefinition().getPropertyValues();
			if (pvs.contains(name)) {
				return pvs.get(name);
			}
			else {
				DeferredProperty dp = this.deferredProperties.get(this.currentBeanDefinition.getBeanName() + name);
				if (dp != null) {
					return dp.value;
				}
				else {
					return getMetaClass().getProperty(this, name);
				}
			}
		}
		else {
			return getMetaClass().getProperty(this, name);
		}
	}
}
 
Example 15
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
	 * This method overrides property retrieval in the scope of the
	 * GroovyBeanDefinitionReader to either:
	 * <ul>
	 * <li>Retrieve a variable from the bean builder's binding if it exists
	 * <li>Retrieve a RuntimeBeanReference for a specific bean if it exists
	 * <li>Otherwise just delegate to MetaClass.getProperty which will resolve
	 * properties from the GroovyBeanDefinitionReader itself
	 * </ul>
	 */
	public Object getProperty(String name) {
		Binding binding = getBinding();
		if (binding != null && binding.hasVariable(name)) {
			return binding.getVariable(name);
		}
		else {
			if (this.namespaces.containsKey(name)) {
//				return createDynamicElementReader(name);
				return null;
			}
			if (getRegistry().containsBeanDefinition(name)) {
				GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
						getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
				if (beanDefinition != null) {
					return new GroovyRuntimeBeanReference(name, beanDefinition, false);
				}
				else {
					return new RuntimeBeanReference(name, false);
				}
			}
			// This is to deal with the case where the property setter is the last
			// statement in a closure (hence the return value)
			else if (this.currentBeanDefinition != null) {
				MutablePropertyValues pvs = this.currentBeanDefinition.getBeanDefinition().getPropertyValues();
				if (pvs.contains(name)) {
					return pvs.get(name);
				}
				else {
					DeferredProperty dp = this.deferredProperties.get(this.currentBeanDefinition.getBeanName() + name);
					if (dp != null) {
						return dp.value;
					}
					else {
						return getMetaClass().getProperty(this, name);
					}
				}
			}
			else {
				return getMetaClass().getProperty(this, name);
			}
		}
	}
 
Example 16
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * This method overrides property retrieval in the scope of the
 * {@code GroovyBeanDefinitionReader} to either:
 * <ul>
 * <li>Retrieve a variable from the bean builder's binding if it exists
 * <li>Retrieve a RuntimeBeanReference for a specific bean if it exists
 * <li>Otherwise just delegate to MetaClass.getProperty which will resolve
 * properties from the {@code GroovyBeanDefinitionReader} itself
 * </ul>
 */
public Object getProperty(String name) {
	Binding binding = getBinding();
	if (binding != null && binding.hasVariable(name)) {
		return binding.getVariable(name);
	}
	else {
		if (this.namespaces.containsKey(name)) {
			return createDynamicElementReader(name);
		}
		if (getRegistry().containsBeanDefinition(name)) {
			GroovyBeanDefinitionWrapper beanDefinition = (GroovyBeanDefinitionWrapper)
					getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName());
			if (beanDefinition != null) {
				return new GroovyRuntimeBeanReference(name, beanDefinition, false);
			}
			else {
				return new RuntimeBeanReference(name, false);
			}
		}
		// This is to deal with the case where the property setter is the last
		// statement in a closure (hence the return value)
		else if (this.currentBeanDefinition != null) {
			MutablePropertyValues pvs = this.currentBeanDefinition.getBeanDefinition().getPropertyValues();
			if (pvs.contains(name)) {
				return pvs.get(name);
			}
			else {
				DeferredProperty dp = this.deferredProperties.get(this.currentBeanDefinition.getBeanName() + name);
				if (dp != null) {
					return dp.value;
				}
				else {
					return getMetaClass().getProperty(this, name);
				}
			}
		}
		else {
			return getMetaClass().getProperty(this, name);
		}
	}
}