001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the 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 General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix;
021    
022    import java.util.*;
023    
024    /**
025     * Manages protocols. Protocol instances are obtained by calling the
026     * getProtocol() method. The ProtocolManager is in charge to serve the
027     * same unique instance of the specified protocol.
028     *
029     * @author Emmanuel Bourg
030     * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
031     */
032    public class ProtocolManager
033    {
034        private static ProtocolManager instance = new ProtocolManager();
035        private Map<Class<? extends Protocol>, Protocol> protocols;
036    
037        private ProtocolManager()
038        {
039            protocols = new HashMap<Class<? extends Protocol>, Protocol>();
040        }
041    
042        /**
043         * Returns the instance of the ProtocolManager.
044         */
045        public static ProtocolManager getInstance()
046        {
047            return instance;
048        }
049    
050        /**
051         * Returns a protocol of the specified class. If a protocol of this class
052         * has already been created, the same instance is returned.
053         *
054         * @param cls Class of the protocol to return
055         *
056         * @return Protocol of the specified class.
057         */
058        public synchronized <P extends Protocol> P  getProtocol(Class<P> cls)
059        {
060            // is there an entry for this class in the hashtable ?
061            Object obj = protocols.get(cls);
062            if (obj != null)
063            {
064                return (P) obj;
065            }
066    
067            P protocol = null;
068    
069            try
070            {
071                // constructing a new protocol
072                protocol = cls.newInstance();
073    
074                // adding the protocol to the hashtable
075                protocols.put(cls, protocol);
076            }
077            catch (Exception e)
078            {
079                IllegalArgumentException iae = new IllegalArgumentException();
080                iae.initCause(e);
081                throw iae;
082            }
083    
084            return protocol;
085        }
086    
087    }