001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.q2.qbean; 020 021import org.jdom2.Element; 022import org.jpos.core.ConfigurationException; 023import org.jpos.q2.QBeanSupport; 024import org.jpos.q2.QFactory; 025import org.jpos.util.*; 026 027import java.io.IOException; 028import java.io.PrintStream; 029 030/** 031 * QBean that materialises a named {@link Logger} from its persist element, 032 * optionally redirecting {@code System.out}/{@code System.err} to it. 033 */ 034public class LoggerAdaptor extends QBeanSupport { 035 /** Default constructor; no instance state to initialise. */ 036 public LoggerAdaptor() {} 037 private Logger logger; 038 private PrintStream originalOut = null; 039 private PrintStream originalErr = null; 040 041 protected void initService () { 042 logger = Logger.getLogger (getName()); 043 } 044 protected void startService () throws ConfigurationException, IOException { 045 logger.removeAllListeners (); 046 for (Element lle : getPersist().getChildren("log-listener")) 047 addListener(lle); 048 049 String redirect = cfg.get("redirect"); 050 long delay = cfg.getLong("delay", 500); 051 052 if (redirect.contains("stdout")) { 053 originalOut = System.out; 054 System.setOut(new PrintStream(new LogEventOutputStream(logger, "stdout", delay))); 055 } 056 if (redirect.contains("stderr")) { 057 originalErr = System.err; 058 System.setErr(new PrintStream(new LogEventOutputStream(logger, "stderr", delay))); 059 } 060 } 061 protected void stopService() { 062 if (originalOut != null) 063 System.setOut(originalOut); 064 if (originalErr != null) 065 System.setErr(originalErr); 066 logger.removeAllListeners (); 067 } 068 protected void destroyService() { 069 // we don't destroy (that would unregister the logger from the 070 // NameRegistrar) because other components might have references 071 // to this logger. 072 // 073 // logger.destroy (); 074 } 075 076 private void addListener (Element e) throws ConfigurationException { 077 QFactory factory = getServer().getFactory(); 078 LogListener listener = factory.newInstance(e); 079 if (listener != null) { 080 attemptToAddWriter(e.getChild("writer"), listener); 081 logger.addListener(listener); 082 } 083 } 084 085 private void attemptToAddWriter (Element e, LogListener listener) throws ConfigurationException { 086 if (e != null) { 087 QFactory factory = getServer().getFactory(); 088 if (QFactory.isEnabled(e)) { 089 String clazz = e.getAttributeValue("class"); 090 LogEventWriter writer = factory.newInstance(clazz); 091 factory.setConfiguration(writer, e); 092 listener.setLogEventWriter (writer); 093 } 094 } 095 } 096}