]>
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
.SlcAntConstants
;
9 import org
.argeo
.slc
.ant
.SlcAntException
;
10 import org
.argeo
.slc
.core
.SlcException
;
11 import org
.springframework
.beans
.BeanWrapper
;
12 import org
.springframework
.beans
.BeanWrapperImpl
;
13 import org
.springframework
.beans
.factory
.InitializingBean
;
14 import org
.springframework
.context
.ApplicationContext
;
16 /** Abstract Ant type wrapping a Spring bean. */
17 public class SpringArg
<T
> extends DataType
{
18 private List
<OverrideArg
> overrides
= new Vector
<OverrideArg
>();
21 private String antref
;
23 // cache bean instance to avoid reading it twice if it is a prototype
24 private T beanInstance
= null;
26 /** The <u>name</u> of the underlying bean, as set through the attribute. */
27 public String
getBean() {
31 /** Setter for the bean name. */
32 public void setBean(String bean
) {
33 checkValueAlreadySet();
37 public String
getAntref() {
41 /** Sets a reference to an ant data type. */
42 public void setAntref(String antref
) {
43 checkValueAlreadySet();
48 * Retrieve the instance of the bean, and sets the overridden properties.
49 * <b>The value is cached.</b>
51 public T
getBeanInstance() {
52 if (beanInstance
== null) {
54 beanInstance
= (T
) getContext().getBean(bean
);
55 if (beanInstance
== null)
56 throw new SlcAntException(
57 "No object found for Spring bean " + bean
);
58 } else if (antref
!= null) {
59 beanInstance
= (T
) getProject().getReference(antref
);
60 if (beanInstance
== null)
61 throw new SlcAntException(
62 "No object found for Ant reference " + antref
);
64 throw new SlcAntException(
65 "Don't know how to retrieve bean instance");
68 setOverridenProperties(beanInstance
);
70 // FIXME: why are we doing this? Could not find any object using it
71 if (beanInstance
instanceof InitializingBean
) {
73 ((InitializingBean
) beanInstance
).afterPropertiesSet();
74 } catch (Exception e
) {
75 throw new SlcException("Could not initialize bean", e
);
82 protected void setOverridenProperties(Object obj
) {
83 BeanWrapper wrapper
= new BeanWrapperImpl(obj
);
84 for (OverrideArg override
: overrides
) {
85 if (override
.getName() == null) {
86 throw new SlcAntException(
87 "The name of the property to override has to be set.");
90 // LogFactory.getLog(getClass()).debug(
91 // "Prop " + override.getName());
92 wrapper
.setPropertyValue(override
.getName(), override
.getObject());
97 /** Creates an override subtag. */
98 public OverrideArg
createOverride() {
99 OverrideArg propertyArg
= new OverrideArg();
100 overrides
.add(propertyArg
);
104 /** The related Spring application context. */
105 protected ApplicationContext
getContext() {
106 return (ApplicationContext
) getProject().getReference(
107 SlcAntConstants
.REF_ROOT_CONTEXT
);
110 protected void checkValueAlreadySet() {
111 if (antref
!= null || bean
!= null) {
112 throw new BuildException("Value already set.");