001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.balancer;
022
023import net.sf.hajdbc.Balancer;
024import net.sf.hajdbc.Messages;
025import net.sf.hajdbc.util.ClassEnum;
026import net.sf.hajdbc.util.Enums;
027
028/**
029 * @author  Paul Ferraro
030 * @since   1.1
031 */
032public enum BalancerClass implements ClassEnum<Balancer<?>>
033{
034        SIMPLE(SimpleBalancer.class),
035        RANDOM(RandomBalancer.class),
036        ROUND_ROBIN(RoundRobinBalancer.class),
037        LOAD(LoadBalancer.class);
038
039        @SuppressWarnings("unchecked")
040        private Class<? extends Balancer> balancerClass;
041        
042        @SuppressWarnings("unchecked")
043        private BalancerClass(Class<? extends Balancer> balancerClass)
044        {
045                this.balancerClass = balancerClass;
046        }
047        
048        /**
049         * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object)
050         */
051        @Override
052        public boolean isInstance(Balancer<?> balancer)
053        {
054                return this.balancerClass.equals(balancer.getClass());
055        }
056        
057        /**
058         * @see net.sf.hajdbc.util.ClassEnum#newInstance()
059         */
060        @Override
061        public Balancer<?> newInstance() throws Exception
062        {
063                return this.balancerClass.newInstance();
064        }
065        
066        /**
067         * Creates a new instance of the Balancer implementation identified by the specified identifier
068         * @param id an enumerated balancer identifier
069         * @return a new Balancer instance
070         * @throws Exception if specified balancer identifier is invalid
071         */
072        public static Balancer<?> deserialize(String id) throws Exception
073        {
074                try
075                {
076                        return Enums.valueOf(BalancerClass.class, id).newInstance();
077                }
078                catch (IllegalArgumentException e)
079                {
080                        throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_BALANCER, id));
081                }
082        }
083        
084        /**
085         * Return the identifier of the specified Balancer.
086         * @param balancer a Balancer implementation
087         * @return the class name of this balancer
088         */
089        public static String serialize(Balancer<?> balancer)
090        {
091                for (BalancerClass balancerClass: BalancerClass.values())
092                {
093                        if (balancerClass.isInstance(balancer))
094                        {
095                                return Enums.id(balancerClass);
096                        }
097                }
098                
099                throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_BALANCER, balancer.getClass()));
100        }
101}