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.q2.QBeanSupport;
023import org.jpos.util.NameRegistrar;
024
025/**
026 * QBean that publishes its {@link Configuration} into {@link NameRegistrar}
027 * so other beans can resolve it by name.
028 */
029@SuppressWarnings("unused")
030public class QConfig extends QBeanSupport {
031    /** Default constructor; no instance state to initialise. */
032    public QConfig() {}
033    /** Prefix used when registering the configuration in {@link NameRegistrar}. */
034    public static final String PREFIX = "config.";
035
036    @Override
037    protected void initService() {
038        NameRegistrar.register(PREFIX + getName(), cfg);
039    }
040
041    @Override
042    protected void destroyService() {
043        NameRegistrar.unregister (PREFIX + getName());
044    }
045    /**
046     * Looks up the {@link Configuration} registered under {@code name}.
047     *
048     * @param name configuration name (without the {@link #PREFIX} prefix)
049     * @return the registered configuration
050     * @throws NameRegistrar.NotFoundException if no configuration is registered under that name
051     */
052    public static Configuration getConfiguration (String name)
053            throws NameRegistrar.NotFoundException
054    {
055        return (Configuration) NameRegistrar.get(PREFIX + name);
056    }
057
058    /**
059     * Looks up the {@link Configuration} registered under {@code name}, waiting up to {@code timeout}.
060     *
061     * @param name configuration name
062     * @param timeout in millis
063     * @return Configuration object or null
064     */
065    public static Configuration getConfiguration (String name, long timeout) {
066        return (Configuration) NameRegistrar.get(PREFIX + name, timeout);
067    }
068}