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@SuppressWarnings("unused")
031public class LoggerService extends QBeanSupport implements Runnable {
032    Logger logger;
033    String queueName;
034    Space sp;
035    long timeout = 300000L;
036
037    protected void startService () throws ConfigurationException {
038        NameRegistrar.register(getName(), this);
039        new Thread(this, getName()).start();
040    }
041    protected void stopService() {
042        NameRegistrar.unregister(getName());
043    }
044
045    @SuppressWarnings("unchecked")
046    public void run() {
047        while (running()) {
048            LogEvent evt = (LogEvent) sp.in (queueName, 1000L);
049            if (evt != null) {
050                evt.setSource(getLog());
051                Logger.log(evt);
052            }
053        }
054    }
055
056    @Override
057    public void setConfiguration (Configuration cfg) throws ConfigurationException {
058        super.setConfiguration(cfg);
059        queueName = cfg.get("queue", null);
060        if (queueName == null)
061            throw new ConfigurationException("'queue' property not configured");
062        sp = SpaceFactory.getSpace(cfg.get("space"));
063        timeout = cfg.getLong("timeout", timeout);
064    }
065}