X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.agent%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2Fspring%2FSpringArg.java;h=c84c96567d80d0df8e7e9a9558e09277db91fca5;hb=b7f351768f4577c0799ab3c5df116ce30270af49;hp=cd85f89c7373b5efcd7220b8d4a34b30efcd9eed;hpb=f2781535c07af51281b59fd0e3b6a362f29c5e10;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java b/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java index cd85f89c7..c84c96567 100644 --- a/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java +++ b/org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java @@ -3,9 +3,9 @@ package org.argeo.slc.ant.spring; import java.util.List; import java.util.Vector; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.DataType; -import org.argeo.slc.ant.SlcAntException; -import org.argeo.slc.ant.SlcProjectHelper; +import org.argeo.slc.ant.AntConstants; import org.argeo.slc.core.SlcException; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; @@ -17,6 +17,7 @@ public class SpringArg extends DataType { private List overrides = new Vector(); private String bean; + private String antref; // cache bean instance to avoid reading it twice if it is a prototype private T beanInstance = null; @@ -28,16 +29,40 @@ public class SpringArg extends DataType { /** Setter for the bean name. */ public void setBean(String bean) { + checkValueAlreadySet(); this.bean = bean; } + public String getAntref() { + return antref; + } + + /** Sets a reference to an ant data type. */ + public void setAntref(String antref) { + checkValueAlreadySet(); + this.antref = antref; + } + /** - * Retrieve the instance of the bean, and sets the overriden properties. + * Retrieve the instance of the bean, and sets the overridden properties. * The value is cached. */ public T getBeanInstance() { if (beanInstance == null) { - beanInstance = (T) getContext().getBean(bean); + if (bean != null) { + beanInstance = (T) getContext().getBean(bean); + if (beanInstance == null) + throw new SlcException("No object found for Spring bean " + + bean); + } else if (antref != null) { + beanInstance = (T) getProject().getReference(antref); + if (beanInstance == null) + throw new SlcException("No object found for Ant reference " + + antref); + } else { + throw new SlcException( + "Don't know how to retrieve bean instance"); + } setOverridenProperties(beanInstance); @@ -57,7 +82,7 @@ public class SpringArg extends DataType { BeanWrapper wrapper = new BeanWrapperImpl(obj); for (OverrideArg override : overrides) { if (override.getName() == null) { - throw new SlcAntException( + throw new SlcException( "The name of the property to override has to be set."); } @@ -78,7 +103,13 @@ public class SpringArg extends DataType { /** The related Spring application context. */ protected ApplicationContext getContext() { return (ApplicationContext) getProject().getReference( - SlcProjectHelper.REF_ROOT_CONTEXT); + AntConstants.REF_ROOT_CONTEXT); + } + + protected void checkValueAlreadySet() { + if (antref != null || bean != null) { + throw new BuildException("Value already set."); + } } }