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 java.io.IOException;
022import java.net.ServerSocket;
023
024import org.jpos.core.Configuration;
025import org.jpos.core.ConfigurationException;
026import org.jpos.q2.QBeanSupport;
027
028public class QSingleInstancePortBasedManager extends QBeanSupport {
029
030    ServerSocket ss;
031
032    int          port;
033
034    /*
035     * (non-Javadoc)
036     *
037     * @see org.jpos.q2.QBeanSupport#initService()
038     */
039    @Override
040    protected void initService() throws Exception {
041
042        try {
043            // attempt to bind, if another instance is already bound, an
044            // exception will get throwm
045            ss = new ServerSocket(port);
046        }
047        catch (IOException e) {
048            getLog().error("An instance of Q2 is already running. Shutting this instance");
049            getServer().shutdown();
050        }
051
052    }
053
054    @Override
055    public void setConfiguration(Configuration cfg) throws ConfigurationException {
056
057        super.setConfiguration(cfg);
058
059        port = cfg.getInt("port", 65000);
060    }
061
062}