001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2002, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.model.processor; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.joran.action.ImcplicitActionDataForBasicProperty; 018import ch.qos.logback.core.joran.action.ImplicitModelData; 019import ch.qos.logback.core.joran.action.ImplicitModelDataForComplexProperty; 020import ch.qos.logback.core.joran.spi.NoAutoStartUtil; 021import ch.qos.logback.core.joran.util.PropertySetter; 022import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache; 023import ch.qos.logback.core.model.ComponentModel; 024import ch.qos.logback.core.model.ImplicitModel; 025import ch.qos.logback.core.model.Model; 026import ch.qos.logback.core.model.ModelConstants; 027import ch.qos.logback.core.spi.ContextAware; 028import ch.qos.logback.core.spi.LifeCycle; 029import ch.qos.logback.core.util.AggregationType; 030import ch.qos.logback.core.util.Loader; 031import ch.qos.logback.core.util.OptionHelper; 032 033public class ImplicitModelHandler extends ModelHandlerBase { 034 035 private final BeanDescriptionCache beanDescriptionCache; 036 private ImplicitModelData implicitModelData; 037 038 static public final String IGNORING_UNKNOWN_PROP = "Ignoring unknown property"; 039 040 boolean inError = false; 041 042 public ImplicitModelHandler(Context context, BeanDescriptionCache beanDescriptionCache) { 043 super(context); 044 this.beanDescriptionCache = beanDescriptionCache; 045 } 046 047 protected Class<? extends ImplicitModel> getSupportedModelClass() { 048 return ImplicitModel.class; 049 } 050 051 static public ImplicitModelHandler makeInstance(Context context, ModelInterpretationContext mic) { 052 BeanDescriptionCache beanDescriptionCache = mic.getBeanDescriptionCache(); 053 return new ImplicitModelHandler(context, beanDescriptionCache); 054 } 055 056 @Override 057 public void handle(ModelInterpretationContext mic, Model model) { 058 059 ImplicitModel implicitModel = (ImplicitModel) model; 060 061 // calling intercon.peekObject with an empty stack will throw an exception 062 if (mic.isObjectStackEmpty()) { 063 inError = true; 064 return; 065 } 066 String nestedElementTagName = implicitModel.getTag(); 067 068 Object o = mic.peekObject(); 069 PropertySetter parentBean = new PropertySetter(beanDescriptionCache, o); 070 parentBean.setContext(context); 071 072 AggregationType aggregationType = parentBean.computeAggregationType(nestedElementTagName); 073 074 switch (aggregationType) { 075 case NOT_FOUND: 076 addWarn(IGNORING_UNKNOWN_PROP+" [" + nestedElementTagName + "] in [" + o.getClass().getName() + "]"); 077 inError = true; 078 // no point in processing submodels 079 implicitModel.markAsSkipped(); 080 return; 081 case AS_BASIC_PROPERTY: 082 case AS_BASIC_PROPERTY_COLLECTION: 083 ImcplicitActionDataForBasicProperty adBasicProperty = new ImcplicitActionDataForBasicProperty(parentBean, 084 aggregationType, nestedElementTagName); 085 implicitModelData = adBasicProperty; 086 doBasicProperty(mic, model, adBasicProperty); 087 return; 088 // we only push action data if NestComponentIA is applicable 089 case AS_COMPLEX_PROPERTY_COLLECTION: 090 case AS_COMPLEX_PROPERTY: 091 ImplicitModelDataForComplexProperty adComplex = new ImplicitModelDataForComplexProperty(parentBean, 092 aggregationType, nestedElementTagName); 093 implicitModelData = adComplex; 094 doComplex(mic, implicitModel, adComplex); 095 return; 096 default: 097 addError("PropertySetter.computeAggregationType returned " + aggregationType); 098 return; 099 } 100 101 } 102 103 void doBasicProperty(ModelInterpretationContext interpretationContext, Model model, 104 ImcplicitActionDataForBasicProperty actionData) { 105 String finalBody = interpretationContext.subst(model.getBodyText()); 106 // get the action data object pushed in isApplicable() method call 107 // IADataForBasicProperty actionData = (IADataForBasicProperty) 108 // actionDataStack.peek(); 109 switch (actionData.aggregationType) { 110 case AS_BASIC_PROPERTY: 111 actionData.parentBean.setProperty(actionData.propertyName, finalBody); 112 break; 113 case AS_BASIC_PROPERTY_COLLECTION: 114 actionData.parentBean.addBasicProperty(actionData.propertyName, finalBody); 115 break; 116 default: 117 addError("Unexpected aggregationType " + actionData.aggregationType); 118 } 119 } 120 121 public void doComplex(ModelInterpretationContext interpretationContext, ComponentModel componentModel, 122 ImplicitModelDataForComplexProperty actionData) { 123 124 String className = componentModel.getClassName(); 125 // perform variable name substitution 126 String substClassName = interpretationContext.subst(className); 127 128 String fqcn = interpretationContext.getImport(substClassName); 129 130 Class<?> componentClass = null; 131 try { 132 133 if (!OptionHelper.isNullOrEmptyOrAllSpaces(fqcn)) { 134 componentClass = Loader.loadClass(fqcn, context); 135 } else { 136 // guess class name via implicit rules 137 PropertySetter parentBean = actionData.parentBean; 138 componentClass = parentBean.getClassNameViaImplicitRules(actionData.propertyName, 139 actionData.getAggregationType(), interpretationContext.getDefaultNestedComponentRegistry()); 140 } 141 142 if (componentClass == null) { 143 actionData.inError = true; 144 String errMsg = "Could not find an appropriate class for property [" + componentModel.getTag() + "]"; 145 addError(errMsg); 146 return; 147 } 148 149 if (OptionHelper.isNullOrEmptyOrAllSpaces(fqcn)) { 150 addInfo("Assuming default type [" + componentClass.getName() + "] for [" + componentModel.getTag() 151 + "] property"); 152 } 153 154 actionData.setNestedComplexProperty(componentClass.getConstructor().newInstance()); 155 156 // pass along the repository 157 if (actionData.getNestedComplexProperty() instanceof ContextAware) { 158 ((ContextAware) actionData.getNestedComplexProperty()).setContext(this.context); 159 } 160 // addInfo("Pushing component [" + localName 161 // + "] on top of the object stack."); 162 interpretationContext.pushObject(actionData.getNestedComplexProperty()); 163 164 } catch (Exception oops) { 165 actionData.inError = true; 166 String msg = "Could not create component [" + componentModel.getTag() + "] of type [" + fqcn + "]"; 167 addError(msg, oops); 168 } 169 } 170 171 @Override 172 public void postHandle(ModelInterpretationContext intercon, Model model) { 173 if (inError) { 174 return; 175 } 176 177 if(implicitModelData == null) 178 return; 179 180 // the action data can in an incorrect state, in which case we need to 181 // disengage 182 if(implicitModelData.inError) { 183 return; 184 } 185 if (implicitModelData instanceof ImplicitModelDataForComplexProperty) { 186 postHandleComplex(intercon, model, (ImplicitModelDataForComplexProperty) implicitModelData); 187 } 188 189 } 190 191 private void postHandleComplex(ModelInterpretationContext mic, Model model, 192 ImplicitModelDataForComplexProperty imdComplex) { 193 194 PropertySetter nestedBean = new PropertySetter(beanDescriptionCache, 195 imdComplex.getNestedComplexProperty()); 196 nestedBean.setContext(context); 197 198 // have the nested element point to its parent if possible 199 if (nestedBean.computeAggregationType(ModelConstants.PARENT_PROPPERTY_KEY) == AggregationType.AS_COMPLEX_PROPERTY) { 200 nestedBean.setComplexProperty(ModelConstants.PARENT_PROPPERTY_KEY, imdComplex.parentBean.getObj()); 201 } 202 203 // start the nested complex property if it implements LifeCycle and is not 204 // marked with a @NoAutoStart annotation 205 Object nestedComplexProperty = imdComplex.getNestedComplexProperty(); 206 if (NoAutoStartUtil.shouldBeStarted(nestedComplexProperty)) { 207 ((LifeCycle) nestedComplexProperty).start(); 208 } 209 210 Object o = mic.peekObject(); 211 212 if (o != imdComplex.getNestedComplexProperty()) { 213 addError("The object on the top the of the stack is not the component pushed earlier."); 214 } else { 215 mic.popObject(); 216 // Now let us attach the component 217 switch (imdComplex.aggregationType) { 218 case AS_COMPLEX_PROPERTY: 219 imdComplex.parentBean.setComplexProperty(model.getTag(), imdComplex.getNestedComplexProperty()); 220 221 break; 222 case AS_COMPLEX_PROPERTY_COLLECTION: 223 imdComplex.parentBean.addComplexProperty(model.getTag(), imdComplex.getNestedComplexProperty()); 224 break; 225 default: 226 addError("Unexpected aggregationType " + imdComplex.aggregationType); 227 } 228 } 229 } 230 231}