KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > DataSourceFactory


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.sql;
22
23 import java.util.Hashtable JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.Name JavaDoc;
27 import javax.naming.RefAddr JavaDoc;
28 import javax.naming.Reference JavaDoc;
29 import javax.naming.spi.ObjectFactory JavaDoc;
30
31 import net.sf.hajdbc.DatabaseCluster;
32 import net.sf.hajdbc.DatabaseClusterFactory;
33
34
35 /**
36  * @author Paul Ferraro
37  * @version $Revision: 961 $
38  */

39 public class DataSourceFactory implements ObjectFactory JavaDoc
40 {
41     /**
42      * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
43      */

44     public Object JavaDoc getObjectInstance(Object JavaDoc object, Name JavaDoc name, Context JavaDoc context, Hashtable JavaDoc<?,?> environment) throws Exception JavaDoc
45     {
46         if (object == null) return null;
47         
48         if (!Reference JavaDoc.class.isInstance(object)) return null;
49         
50         Reference JavaDoc reference = Reference JavaDoc.class.cast(object);
51         
52         String JavaDoc className = reference.getClassName();
53         
54         if (className == null) return null;
55         
56         if (!DataSource.class.getName().equals(className)) return null;
57         
58         DataSource dataSource = new DataSource();
59         
60         RefAddr JavaDoc addr = reference.get(DataSource.DATABASE_CLUSTER);
61         
62         if (addr == null) return null;
63         
64         String JavaDoc id = String JavaDoc.class.cast(addr.getContent());
65
66         if (id == null) return null;
67         
68         DatabaseCluster databaseCluster = DatabaseClusterFactory.getInstance().getDatabaseCluster(id);
69         
70         if (databaseCluster == null) return null;
71         
72         dataSource.setCluster(id);
73         dataSource.setConnectionFactory(new ConnectionFactory<javax.sql.DataSource JavaDoc>(databaseCluster, javax.sql.DataSource JavaDoc.class));
74         
75         return dataSource;
76     }
77 }
78
Popular Tags