]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java
1 package org
.argeo
.slc
.ant
.spring
;
4 import java
.util
.Vector
;
6 import org
.apache
.tools
.ant
.BuildException
;
7 import org
.apache
.tools
.ant
.types
.DataType
;
8 import org
.argeo
.slc
.ant
.AntConstants
;
9 import org
.argeo
.slc
.core
.SlcException
;
10 import org
.springframework
.beans
.BeanWrapper
;
11 import org
.springframework
.beans
.BeanWrapperImpl
;
12 import org
.springframework
.beans
.factory
.InitializingBean
;
13 import org
.springframework
.context
.ApplicationContext
;
15 /** Abstract Ant type wrapping a Spring bean. */
16 public class SpringArg
<T
> extends DataType
{
17 private List
<OverrideArg
> overrides
= new Vector
<OverrideArg
>();
20 private String antref
;
22 // cache bean instance to avoid reading it twice if it is a prototype
23 private T beanInstance
= null;
25 /** The <u>name</u> of the underlying bean, as set through the attribute. */
26 public String
getBean() {
30 /** Setter for the bean name. */
31 public void setBean(String bean
) {
32 checkValueAlreadySet();
36 public String
getAntref() {
40 /** Sets a reference to an ant data type. */
41 public void setAntref(String antref
) {
42 checkValueAlreadySet();
47 * Retrieve the instance of the bean, and sets the overridden properties.
48 * <b>The value is cached.</b>
50 public T
getBeanInstance() {
51 if (beanInstance
== null) {
53 beanInstance
= (T
) getContext().getBean(bean
);
54 if (beanInstance
== null)
55 throw new SlcException("No object found for Spring bean "
57 } else if (antref
!= null) {
58 beanInstance
= (T
) getProject().getReference(antref
);
59 if (beanInstance
== null)
60 throw new SlcException("No object found for Ant reference "
63 throw new SlcException(
64 "Don't know how to retrieve bean instance");
67 setOverridenProperties(beanInstance
);
69 // FIXME: why are we doing this? Could not find any object using it
70 if (beanInstance
instanceof InitializingBean
) {
72 ((InitializingBean
) beanInstance
).afterPropertiesSet();
73 } catch (Exception e
) {
74 throw new SlcException("Could not initialize bean", e
);
81 protected void setOverridenProperties(Object obj
) {
82 BeanWrapper wrapper
= new BeanWrapperImpl(obj
);
83 for (OverrideArg override
: overrides
) {
84 if (override
.getName() == null) {
85 throw new SlcException(
86 "The name of the property to override has to be set.");
89 // LogFactory.getLog(getClass()).debug(
90 // "Prop " + override.getName());
91 wrapper
.setPropertyValue(override
.getName(), override
.getObject());
96 /** Creates an override subtag. */
97 public OverrideArg
createOverride() {
98 OverrideArg propertyArg
= new OverrideArg();
99 overrides
.add(propertyArg
);
103 /** The related Spring application context. */
104 protected ApplicationContext
getContext() {
105 return (ApplicationContext
) getProject().getReference(
106 AntConstants
.REF_ROOT_CONTEXT
);
109 protected void checkValueAlreadySet() {
110 if (antref
!= null || bean
!= null) {
111 throw new BuildException("Value already set.");