]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java
Improve Spring/Ant bridge
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / spring / SpringArg.java
index cd85f89c7373b5efcd7220b8d4a34b30efcd9eed..d94384d5ca70410508e2f6ed9c3af889f306e55a 100644 (file)
@@ -3,9 +3,10 @@ package org.argeo.slc.ant.spring;
 import java.util.List;\r
 import java.util.Vector;\r
 \r
+import org.apache.tools.ant.BuildException;\r
 import org.apache.tools.ant.types.DataType;\r
+import org.argeo.slc.ant.SlcAntConstants;\r
 import org.argeo.slc.ant.SlcAntException;\r
-import org.argeo.slc.ant.SlcProjectHelper;\r
 import org.argeo.slc.core.SlcException;\r
 import org.springframework.beans.BeanWrapper;\r
 import org.springframework.beans.BeanWrapperImpl;\r
@@ -17,6 +18,7 @@ public class SpringArg<T> extends DataType {
        private List<OverrideArg> overrides = new Vector<OverrideArg>();\r
 \r
        private String bean;\r
+       private String antref;\r
 \r
        // cache bean instance to avoid reading it twice if it is a prototype\r
        private T beanInstance = null;\r
@@ -28,16 +30,40 @@ public class SpringArg<T> extends DataType {
 \r
        /** Setter for the bean name. */\r
        public void setBean(String bean) {\r
+               checkValueAlreadySet();\r
                this.bean = bean;\r
        }\r
 \r
+       public String getAntref() {\r
+               return antref;\r
+       }\r
+\r
+       /** Sets a reference to an ant data type. */\r
+       public void setAntref(String antref) {\r
+               checkValueAlreadySet();\r
+               this.antref = antref;\r
+       }\r
+\r
        /**\r
-        * Retrieve the instance of the bean, and sets the overriden properties.\r
+        * Retrieve the instance of the bean, and sets the overridden properties.\r
         * <b>The value is cached.</b>\r
         */\r
        public T getBeanInstance() {\r
                if (beanInstance == null) {\r
-                       beanInstance = (T) getContext().getBean(bean);\r
+                       if (bean != null) {\r
+                               beanInstance = (T) getContext().getBean(bean);\r
+                               if (beanInstance == null)\r
+                                       throw new SlcAntException(\r
+                                                       "No object found for Spring bean " + bean);\r
+                       } else if (antref != null) {\r
+                               beanInstance = (T) getProject().getReference(antref);\r
+                               if (beanInstance == null)\r
+                                       throw new SlcAntException(\r
+                                                       "No object found for Ant reference " + antref);\r
+                       } else {\r
+                               throw new SlcAntException(\r
+                                               "Don't know how to retrieve bean instance");\r
+                       }\r
 \r
                        setOverridenProperties(beanInstance);\r
 \r
@@ -78,7 +104,13 @@ public class SpringArg<T> extends DataType {
        /** The related Spring application context. */\r
        protected ApplicationContext getContext() {\r
                return (ApplicationContext) getProject().getReference(\r
-                               SlcProjectHelper.REF_ROOT_CONTEXT);\r
+                               SlcAntConstants.REF_ROOT_CONTEXT);\r
+       }\r
+\r
+       protected void checkValueAlreadySet() {\r
+               if (antref != null || bean != null) {\r
+                       throw new BuildException("Value already set.");\r
+               }\r
        }\r
 \r
 }\r