]>
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
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.apache
.tools
.ant
.types
.DataType
;
9 import org
.argeo
.slc
.ant
.AntConstants
;
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 final static Log log
= LogFactory
.getLog(SpringArg
.class);
20 private List
<OverrideArg
> overrides
= new Vector
<OverrideArg
>();
23 private String antref
;
25 * Reference to the original object, used to merge overrides. <b>this object
26 * will be modified</b>.
30 // cache bean instance to avoid reading it twice if it is a prototype
31 private T instance
= null;
33 /** The <u>name</u> of the underlying bean, as set through the attribute. */
34 public String
getBean() {
38 /** Setter for the bean name. */
39 public void setBean(String bean
) {
40 checkValueAlreadySet();
44 public String
getAntref() {
48 /** Sets a reference to an ant data type. */
49 public void setAntref(String antref
) {
50 checkValueAlreadySet();
55 * Retrieve the instance of the bean, and sets the overridden properties.
56 * <b>The value is cached.</b>
58 public T
getInstance() {
59 if (instance
== null) {
60 if (log
.isTraceEnabled())
61 log
.trace(this + "\t: Creates instance");
64 instance
= (T
) getContext().getBean(bean
);
66 throw new SlcException("No object found for Spring bean "
68 } else if (antref
!= null) {
69 instance
= (T
) getProject().getReference(antref
);
71 throw new SlcException("No object found for Ant reference "
73 } else if (original
!= null) {
76 throw new SlcException(
77 "Don't know how to retrieve bean instance");
80 setOverridenProperties(instance
);
82 // FIXME: why are we doing this? Could not find any object using it
83 if (instance
instanceof InitializingBean
) {
85 ((InitializingBean
) instance
).afterPropertiesSet();
86 } catch (Exception e
) {
87 throw new SlcException("Could not initialize bean", e
);
91 if (log
.isTraceEnabled())
92 log
.trace(this + "\t: Returns cached instance");
97 protected void setOverridenProperties(Object obj
) {
98 BeanWrapper wrapper
= new BeanWrapperImpl(obj
);
99 for (OverrideArg override
: overrides
) {
100 if (override
.getName() == null) {
101 throw new SlcException(
102 "The name of the property to override has to be set.");
105 if (log
.isTraceEnabled())
106 log
.trace(this + "\t: Overrides property " + override
.getName()
107 + " with " + override
);
109 if (override
.getMerge() == true) {
110 // if override is marked as merged retrieve the value and set is
112 override
.setOriginal(wrapper
.getPropertyValue(override
115 wrapper
.setPropertyValue(override
.getName(), override
.getObject());
120 /** Creates an override subtag. */
121 public OverrideArg
createOverride() {
122 OverrideArg propertyArg
= new OverrideArg();
123 overrides
.add(propertyArg
);
127 /** The related Spring application context. */
128 protected ApplicationContext
getContext() {
129 return (ApplicationContext
) getProject().getReference(
130 AntConstants
.REF_ROOT_CONTEXT
);
133 protected void checkValueAlreadySet() {
134 if (antref
!= null || bean
!= null || original
!= null) {
135 throw new SlcException("Instance value already defined.");
139 public void setOriginal(T original
) {
140 checkValueAlreadySet();
141 this.original
= original
;
144 public T
getOriginal() {
149 public String
toString() {
150 StringBuffer buf
= new StringBuffer(getClass().getSimpleName());
152 buf
.append("#bean=").append(bean
);
153 } else if (antref
!= null) {
154 buf
.append("#antref=").append(antref
);
155 } else if (original
!= null) {
156 buf
.append("#orig=").append(original
.hashCode());
160 buf
.append("#").append(hashCode());
161 return buf
.toString();