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=3f3bb3c1d126ebfb4d9878f665b97aed4ffef095;hb=7a8f034133c1112e187e2a2bfd2c56a8d2452345;hp=75b3447c5182e7c7e386f1d89416063801245fa3;hpb=489db3e2297debe1a32e5e98534d5dbf059e1c1d;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 75b3447c5..3f3bb3c1d 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,10 @@ package org.argeo.slc.ant.spring; import java.util.List; import java.util.Vector; -import org.apache.tools.ant.BuildException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.tools.ant.types.DataType; -import org.argeo.slc.ant.SlcAntConstants; +import org.argeo.slc.ant.AntConstants; import org.argeo.slc.core.SlcException; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; @@ -14,13 +15,20 @@ import org.springframework.context.ApplicationContext; /** Abstract Ant type wrapping a Spring bean. */ public class SpringArg extends DataType { + private final static Log log = LogFactory.getLog(SpringArg.class); + private List overrides = new Vector(); private String bean; private String antref; + /** + * Reference to the original object, used to merge overrides. this object + * will be modified. + */ + private T original; // cache bean instance to avoid reading it twice if it is a prototype - private T beanInstance = null; + private T instance = null; /** The name of the underlying bean, as set through the attribute. */ public String getBean() { @@ -47,35 +55,43 @@ public class SpringArg extends DataType { * Retrieve the instance of the bean, and sets the overridden properties. * The value is cached. */ - public T getBeanInstance() { - if (beanInstance == null) { + public T getInstance() { + if (instance == null) { + if (log.isTraceEnabled()) + log.trace(this + "\t: Creates instance"); + if (bean != null) { - beanInstance = (T) getContext().getBean(bean); - if (beanInstance == null) + instance = (T) getContext().getBean(bean); + if (instance == null) throw new SlcException("No object found for Spring bean " + bean); } else if (antref != null) { - beanInstance = (T) getProject().getReference(antref); - if (beanInstance == null) + instance = (T) getProject().getReference(antref); + if (instance == null) throw new SlcException("No object found for Ant reference " + antref); + } else if (original != null) { + instance = original; } else { throw new SlcException( "Don't know how to retrieve bean instance"); } - setOverridenProperties(beanInstance); + setOverridenProperties(instance); // FIXME: why are we doing this? Could not find any object using it - if (beanInstance instanceof InitializingBean) { + if (instance instanceof InitializingBean) { try { - ((InitializingBean) beanInstance).afterPropertiesSet(); + ((InitializingBean) instance).afterPropertiesSet(); } catch (Exception e) { throw new SlcException("Could not initialize bean", e); } } + } else { + if (log.isTraceEnabled()) + log.trace(this + "\t: Returns cached instance"); } - return beanInstance; + return instance; } protected void setOverridenProperties(Object obj) { @@ -86,8 +102,16 @@ public class SpringArg extends DataType { "The name of the property to override has to be set."); } - // LogFactory.getLog(getClass()).debug( - // "Prop " + override.getName()); + if (log.isTraceEnabled()) + log.trace(this + "\t: Overrides property " + override.getName() + + " with " + override); + + if (override.getMerge() == true) { + // if override is marked as merged retrieve the value and set is + // as original + override.setOriginal(wrapper.getPropertyValue(override + .getName())); + } wrapper.setPropertyValue(override.getName(), override.getObject()); } @@ -103,13 +127,37 @@ public class SpringArg extends DataType { /** The related Spring application context. */ protected ApplicationContext getContext() { return (ApplicationContext) getProject().getReference( - SlcAntConstants.REF_ROOT_CONTEXT); + AntConstants.REF_ROOT_CONTEXT); } protected void checkValueAlreadySet() { - if (antref != null || bean != null) { - throw new BuildException("Value already set."); + if (antref != null || bean != null || original != null) { + throw new SlcException("Instance value already defined."); } } + public void setOriginal(T original) { + checkValueAlreadySet(); + this.original = original; + } + + public T getOriginal() { + return original; + } + + @Override + public String toString() { + StringBuffer buf = new StringBuffer(getClass().getSimpleName()); + if (bean != null) { + buf.append("#bean=").append(bean); + } else if (antref != null) { + buf.append("#antref=").append(antref); + } else if (original != null) { + buf.append("#orig=").append(original.hashCode()); + } else { + buf.append("#noid"); + } + buf.append("#").append(hashCode()); + return buf.toString(); + } }