001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2022, 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.classic.model.processor; 015 016import ch.qos.logback.classic.model.ReceiverModel; 017import ch.qos.logback.classic.net.ReceiverBase; 018import ch.qos.logback.classic.net.SocketReceiver; 019import ch.qos.logback.core.Context; 020import ch.qos.logback.core.model.Model; 021import ch.qos.logback.core.model.processor.ModelHandlerBase; 022import ch.qos.logback.core.model.processor.ModelHandlerException; 023import ch.qos.logback.core.model.processor.ModelInterpretationContext; 024import ch.qos.logback.core.util.OptionHelper; 025 026/** 027 * A Joran {@link ModelHandler} for a {@link SocketReceiver} configuration. 028 * 029 * @author Carl Harris 030 */ 031public class ReceiverModelHandler extends ModelHandlerBase { 032 033 private ReceiverBase receiver; 034 private boolean inError; 035 036 public ReceiverModelHandler(Context context) { 037 super(context); 038 } 039 040 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 041 return new ReceiverModelHandler(context); 042 } 043 044 @Override 045 protected Class<ReceiverModel> getSupportedModelClass() { 046 return ReceiverModel.class; 047 } 048 049 @Override 050 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 051 ReceiverModel receiverModel = (ReceiverModel) model; 052 String className = receiverModel.getClassName(); 053 054 if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) { 055 addError("Missing class name for receiver. "); 056 inError = true; 057 return; 058 } else { 059 className = mic.getImport(className); 060 } 061 062 try { 063 addInfo("About to instantiate receiver of type [" + className + "]"); 064 065 receiver = (ReceiverBase) OptionHelper.instantiateByClassName(className, ReceiverBase.class, context); 066 receiver.setContext(context); 067 068 mic.pushObject(receiver); 069 } catch (Exception ex) { 070 inError = true; 071 addError("Could not create a receiver of type [" + className + "].", ex); 072 throw new ModelHandlerException(ex); 073 } 074 075 } 076 077 @Override 078 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 079 if (inError) { 080 return; 081 } 082 083 Object o = mic.peekObject(); 084 if (o != receiver) { 085 addWarn("The object at the of the stack is not the receiver pushed earlier."); 086 } else { 087 mic.popObject(); 088 addInfo("Registering receiver with context."); 089 mic.getContext().register(receiver); 090 receiver.start(); 091 092 } 093 } 094}