01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.balancer;
22:
23: import net.sf.hajdbc.Balancer;
24: import net.sf.hajdbc.Messages;
25: import net.sf.hajdbc.util.ClassEnum;
26: import net.sf.hajdbc.util.Strings;
27:
28: /**
29: * @author Paul Ferraro
30: * @since 1.1
31: */
32: public enum BalancerClass implements ClassEnum<Balancer<?>> {
33: SIMPLE(SimpleBalancer.class), RANDOM(RandomBalancer.class), ROUND_ROBIN(
34: RoundRobinBalancer.class), LOAD(LoadBalancer.class);
35:
36: @SuppressWarnings("unchecked")
37: private Class<? extends Balancer> balancerClass;
38:
39: @SuppressWarnings("unchecked")
40: private BalancerClass(Class<? extends Balancer> balancerClass) {
41: this .balancerClass = balancerClass;
42: }
43:
44: /**
45: * @see net.sf.hajdbc.util.ClassEnum#isInstance(java.lang.Object)
46: */
47: @Override
48: public boolean isInstance(Balancer<?> balancer) {
49: return this .balancerClass.equals(balancer.getClass());
50: }
51:
52: /**
53: * @see net.sf.hajdbc.util.ClassEnum#newInstance()
54: */
55: @Override
56: public Balancer<?> newInstance() throws Exception {
57: return this .balancerClass.newInstance();
58: }
59:
60: /**
61: * Creates a new instance of the Balancer implementation identified by the specified identifier
62: * @param id an enumerated balancer identifier
63: * @return a new Balancer instance
64: * @throws Exception if specified balancer identifier is invalid
65: */
66: public static Balancer<?> deserialize(String id) throws Exception {
67: try {
68: return BalancerClass.valueOf(
69: id.toUpperCase().replace(Strings.DASH,
70: Strings.UNDERSCORE)).newInstance();
71: } catch (IllegalArgumentException e) {
72: throw new IllegalArgumentException(Messages.getMessage(
73: Messages.INVALID_BALANCER, id));
74: }
75: }
76:
77: /**
78: * Return the identifier of the specified Balancer.
79: * @param balancer a Balancer implementation
80: * @return the class name of this balancer
81: */
82: public static String serialize(Balancer<?> balancer) {
83: for (BalancerClass balancerClass : BalancerClass.values()) {
84: if (balancerClass.isInstance(balancer)) {
85: return balancerClass.name().toLowerCase().replace(
86: Strings.UNDERSCORE, Strings.DASH);
87: }
88: }
89:
90: throw new IllegalArgumentException(Messages.getMessage(
91: Messages.INVALID_BALANCER, balancer.getClass()));
92: }
93: }
|