KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > BalancerFactory


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
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 java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import net.sf.hajdbc.Balancer;
27 import net.sf.hajdbc.Messages;
28
29 /**
30  * @author Paul Ferraro
31  * @since 1.1
32  */

33 public final class BalancerFactory
34 {
35     private static Map JavaDoc<String JavaDoc, Class JavaDoc<? extends Balancer>> balancerMap = new HashMap JavaDoc<String JavaDoc, Class JavaDoc<? extends Balancer>>();
36     
37     static
38     {
39         balancerMap.put("simple", SimpleBalancer.class);
40         balancerMap.put("random", RandomBalancer.class);
41         balancerMap.put("round-robin", RoundRobinBalancer.class);
42         balancerMap.put("load", LoadBalancer.class);
43     }
44     
45     /**
46      * Creates a new instance of the Balancer implementation indentified by the specified identifier
47      * @param id an enumerated balancer identifier
48      * @return a new Balancer instance
49      * @throws Exception if specified balancer identifier is invalid
50      */

51     public static Balancer deserialize(String JavaDoc id) throws Exception JavaDoc
52     {
53         Class JavaDoc<? extends Balancer> balancerClass = balancerMap.get(id);
54         
55         if (balancerClass == null)
56         {
57             throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.INVALID_BALANCER, id));
58         }
59         
60         return balancerClass.newInstance();
61     }
62     
63     /**
64      * Return the identifier of the specified Balancer.
65      * @param balancer a Dialect implementation
66      * @return the class name of this dialect
67      */

68     public static String JavaDoc serialize(Balancer balancer)
69     {
70         for (Map.Entry JavaDoc<String JavaDoc, Class JavaDoc<? extends Balancer>> balancerMapEntry: balancerMap.entrySet())
71         {
72             if (balancerMapEntry.getValue().isInstance(balancer))
73             {
74                 return balancerMapEntry.getKey();
75             }
76         }
77         
78         throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.INVALID_BALANCER, balancer.getClass()));
79     }
80     
81     private BalancerFactory()
82     {
83         // Hide constructor
84
}
85 }
86
Popular Tags