]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/SpringArg.java
Simplify new runtime
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / spring / SpringArg.java
1 package org.argeo.slc.ant.spring;
2
3 import java.util.List;
4 import java.util.Vector;
5
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;
15
16 /** Abstract Ant type wrapping a Spring bean. */
17 public class SpringArg<T> extends DataType {
18 private List<OverrideArg> overrides = new Vector<OverrideArg>();
19
20 private String bean;
21 private String antref;
22
23 // cache bean instance to avoid reading it twice if it is a prototype
24 private T beanInstance = null;
25
26 /** The <u>name</u> of the underlying bean, as set through the attribute. */
27 public String getBean() {
28 return bean;
29 }
30
31 /** Setter for the bean name. */
32 public void setBean(String bean) {
33 checkValueAlreadySet();
34 this.bean = bean;
35 }
36
37 public String getAntref() {
38 return antref;
39 }
40
41 /** Sets a reference to an ant data type. */
42 public void setAntref(String antref) {
43 checkValueAlreadySet();
44 this.antref = antref;
45 }
46
47 /**
48 * Retrieve the instance of the bean, and sets the overridden properties.
49 * <b>The value is cached.</b>
50 */
51 public T getBeanInstance() {
52 if (beanInstance == null) {
53 if (bean != 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);
63 } else {
64 throw new SlcAntException(
65 "Don't know how to retrieve bean instance");
66 }
67
68 setOverridenProperties(beanInstance);
69
70 // FIXME: why are we doing this? Could not find any object using it
71 if (beanInstance instanceof InitializingBean) {
72 try {
73 ((InitializingBean) beanInstance).afterPropertiesSet();
74 } catch (Exception e) {
75 throw new SlcException("Could not initialize bean", e);
76 }
77 }
78 }
79 return beanInstance;
80 }
81
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.");
88 }
89
90 // LogFactory.getLog(getClass()).debug(
91 // "Prop " + override.getName());
92 wrapper.setPropertyValue(override.getName(), override.getObject());
93 }
94
95 }
96
97 /** Creates an override subtag. */
98 public OverrideArg createOverride() {
99 OverrideArg propertyArg = new OverrideArg();
100 overrides.add(propertyArg);
101 return propertyArg;
102 }
103
104 /** The related Spring application context. */
105 protected ApplicationContext getContext() {
106 return (ApplicationContext) getProject().getReference(
107 SlcAntConstants.REF_ROOT_CONTEXT);
108 }
109
110 protected void checkValueAlreadySet() {
111 if (antref != null || bean != null) {
112 throw new BuildException("Value already set.");
113 }
114 }
115
116 }