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.jpos.core.Configuration;
022import org.jpos.core.ConfigurationException;
023import org.jpos.q2.QBeanSupport;
024import org.jpos.space.Space;
025import org.jpos.space.SpaceFactory;
026import org.jpos.util.LogEvent;
027import org.jpos.util.Logger;
028import org.jpos.util.NameRegistrar;
029
030/**
031 * QBean that pulls events from a configured space queue and forwards them to
032 * a {@link Logger}, decoupling event production from logging I/O.
033 */
034@SuppressWarnings("unused")
035public class LoggerService extends QBeanSupport implements Runnable {
036    /** Default constructor; no instance state to initialise. */
037    public LoggerService() {}
038    Logger logger;
039    String queueName;
040    Space sp;
041    long timeout = 300000L;
042
043    protected void startService () throws ConfigurationException {
044        NameRegistrar.register(getName(), this);
045        new Thread(this, getName()).start();
046    }
047    protected void stopService() {
048        NameRegistrar.unregister(getName());
049    }
050
051    @SuppressWarnings("unchecked")
052    public void run() {
053        while (running()) {
054            LogEvent evt = (LogEvent) sp.in (queueName, 1000L);
055            if (evt != null) {
056                evt.setSource(getLog());
057                Logger.log(evt);
058            }
059        }
060    }
061
062    @Override
063    public void setConfiguration (Configuration cfg) throws ConfigurationException {
064        super.setConfiguration(cfg);
065        queueName = cfg.get("queue", null);
066        if (queueName == null)
067            throw new ConfigurationException("'queue' property not configured");
068        sp = SpaceFactory.getSpace(cfg.get("space"));
069        timeout = cfg.getLong("timeout", timeout);
070    }
071}