1
14 package ch.qos.logback.core.joran.action;
15
16 import java.util.Stack;
17
18 import ch.qos.logback.core.joran.spi.ElementPath;
19
20 import org.xml.sax.Attributes;
21
22 import ch.qos.logback.core.joran.spi.InterpretationContext;
23 import ch.qos.logback.core.joran.spi.NoAutoStartUtil;
24 import ch.qos.logback.core.joran.util.PropertySetter;
25 import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
26 import ch.qos.logback.core.spi.ContextAware;
27 import ch.qos.logback.core.spi.LifeCycle;
28 import ch.qos.logback.core.util.AggregationType;
29 import ch.qos.logback.core.util.Loader;
30 import ch.qos.logback.core.util.OptionHelper;
31
32
38 public class NestedComplexPropertyIA extends ImplicitAction {
39
40
41
42
43
44
45
46
47 Stack<IADataForComplexProperty> actionDataStack = new Stack<IADataForComplexProperty>();
48
49 private final BeanDescriptionCache beanDescriptionCache;
50
51 public NestedComplexPropertyIA(BeanDescriptionCache beanDescriptionCache) {
52 this.beanDescriptionCache = beanDescriptionCache;
53 }
54
55 public boolean isApplicable(ElementPath elementPath, Attributes attributes, InterpretationContext ic) {
56
57 String nestedElementTagName = elementPath.peekLast();
58
59
60 if (ic.isEmpty()) {
61 return false;
62 }
63
64 Object o = ic.peekObject();
65 PropertySetter parentBean = new PropertySetter(beanDescriptionCache, o);
66 parentBean.setContext(context);
67
68 AggregationType aggregationType = parentBean.computeAggregationType(nestedElementTagName);
69
70 switch (aggregationType) {
71 case NOT_FOUND:
72 case AS_BASIC_PROPERTY:
73 case AS_BASIC_PROPERTY_COLLECTION:
74 return false;
75
76
77 case AS_COMPLEX_PROPERTY_COLLECTION:
78 case AS_COMPLEX_PROPERTY:
79 IADataForComplexProperty ad = new IADataForComplexProperty(parentBean, aggregationType, nestedElementTagName);
80 actionDataStack.push(ad);
81
82 return true;
83 default:
84 addError("PropertySetter.computeAggregationType returned " + aggregationType);
85 return false;
86 }
87 }
88
89 public void begin(InterpretationContext ec, String localName, Attributes attributes) {
90
91
92 IADataForComplexProperty actionData = (IADataForComplexProperty) actionDataStack.peek();
93
94 String className = attributes.getValue(CLASS_ATTRIBUTE);
95
96 className = ec.subst(className);
97
98 Class<?> componentClass = null;
99 try {
100
101 if (!OptionHelper.isEmpty(className)) {
102 componentClass = Loader.loadClass(className, context);
103 } else {
104
105 PropertySetter parentBean = actionData.parentBean;
106 componentClass = parentBean.getClassNameViaImplicitRules(actionData.getComplexPropertyName(), actionData.getAggregationType(),
107 ec.getDefaultNestedComponentRegistry());
108 }
109
110 if (componentClass == null) {
111 actionData.inError = true;
112 String errMsg = "Could not find an appropriate class for property [" + localName + "]";
113 addError(errMsg);
114 return;
115 }
116
117 if (OptionHelper.isEmpty(className)) {
118 addInfo("Assuming default type [" + componentClass.getName() + "] for [" + localName + "] property");
119 }
120
121 actionData.setNestedComplexProperty(componentClass.newInstance());
122
123
124 if (actionData.getNestedComplexProperty() instanceof ContextAware) {
125 ((ContextAware) actionData.getNestedComplexProperty()).setContext(this.context);
126 }
127
128
129 ec.pushObject(actionData.getNestedComplexProperty());
130
131 } catch (Exception oops) {
132 actionData.inError = true;
133 String msg = "Could not create component [" + localName + "] of type [" + className + "]";
134 addError(msg, oops);
135 }
136
137 }
138
139 public void end(InterpretationContext ec, String tagName) {
140
141
142
143 IADataForComplexProperty actionData = (IADataForComplexProperty) actionDataStack.pop();
144
145 if (actionData.inError) {
146 return;
147 }
148
149 PropertySetter nestedBean = new PropertySetter(beanDescriptionCache, actionData.getNestedComplexProperty());
150 nestedBean.setContext(context);
151
152
153 if (nestedBean.computeAggregationType("parent") == AggregationType.AS_COMPLEX_PROPERTY) {
154 nestedBean.setComplexProperty("parent", actionData.parentBean.getObj());
155 }
156
157
158
159 Object nestedComplexProperty = actionData.getNestedComplexProperty();
160 if (nestedComplexProperty instanceof LifeCycle && NoAutoStartUtil.notMarkedWithNoAutoStart(nestedComplexProperty)) {
161 ((LifeCycle) nestedComplexProperty).start();
162 }
163
164 Object o = ec.peekObject();
165
166 if (o != actionData.getNestedComplexProperty()) {
167 addError("The object on the top the of the stack is not the component pushed earlier.");
168 } else {
169 ec.popObject();
170
171 switch (actionData.aggregationType) {
172 case AS_COMPLEX_PROPERTY:
173 actionData.parentBean.setComplexProperty(tagName, actionData.getNestedComplexProperty());
174
175 break;
176 case AS_COMPLEX_PROPERTY_COLLECTION:
177 actionData.parentBean.addComplexProperty(tagName, actionData.getNestedComplexProperty());
178 break;
179 default:
180 addError("Unexpected aggregationType " + actionData.aggregationType);
181 }
182 }
183 }
184
185 }
186