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