1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

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 /**
33  * This action is responsible for tying together a parent object with a child
34  * element for which there is no explicit rule.
35  *
36  * @author Ceki Gülcü
37  */

38 public class NestedComplexPropertyIA extends ImplicitAction {
39
40     // actionDataStack contains ActionData instances
41     // We use a stack of ActionData objects in order to support nested
42     // elements which are handled by the same NestedComplexPropertyIA instance.
43     // We push a ActionData instance in the isApplicable method (if the
44     // action is applicable) and pop it in the end() method.
45     // The XML well-formedness property will guarantee that a push will eventually
46     // be followed by a corresponding pop.
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         // calling ic.peekObject with an empty stack will throw an exception
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             // we only push action data if NestComponentIA is applicable
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         // LogLog.debug("in NestComponentIA begin method");
91         // get the action data object pushed in isApplicable() method call
92         IADataForComplexProperty actionData = (IADataForComplexProperty) actionDataStack.peek();
93
94         String className = attributes.getValue(CLASS_ATTRIBUTE);
95         // perform variable name substitution
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                 // guess class name via implicit rules
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             // pass along the repository
124             if (actionData.getNestedComplexProperty() instanceof ContextAware) {
125                 ((ContextAware) actionData.getNestedComplexProperty()).setContext(this.context);
126             }
127             // addInfo("Pushing component [" + localName
128             // + "] on top of the object stack.");
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         // pop the action data object pushed in isApplicable() method call
142         // we assume that each this begin
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         // have the nested element point to its parent if possible
153         if (nestedBean.computeAggregationType("parent") == AggregationType.AS_COMPLEX_PROPERTY) {
154             nestedBean.setComplexProperty("parent", actionData.parentBean.getObj());
155         }
156
157         // start the nested complex property if it implements LifeCycle and is not
158         // marked with a @NoAutoStart annotation
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             // Now let us attach the component
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