KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29
30 import net.sf.hajdbc.ActiveDatabaseMBean;
31 import net.sf.hajdbc.InactiveDatabaseMBean;
32 import net.sf.hajdbc.Messages;
33
34 /**
35  * @author Paul Ferraro
36  * @version $Revision: 1190 $
37  * @since 1.0
38  */

39 public class DataSourceDatabase extends AbstractDatabase<DataSource> implements InactiveDataSourceDatabaseMBean
40 {
41     protected String JavaDoc name;
42     
43     /**
44      * @see net.sf.hajdbc.sql.ActiveDataSourceDatabaseMBean#getName()
45      */

46     public String JavaDoc getName()
47     {
48         return this.name;
49     }
50     
51     /**
52      * Sets the JNDI name of this DataSource
53      * @param name a JNDI name
54      */

55     public void setName(String JavaDoc name)
56     {
57         this.checkDirty(this.name, name);
58         this.name = name;
59     }
60     
61     /**
62      * @param dataSource A DataSource
63      * @return a database connection
64      * @throws java.sql.SQLException if a database connection could not be made
65      * @see net.sf.hajdbc.Database#connect(Object)
66      */

67     public Connection connect(DataSource dataSource) throws java.sql.SQLException JavaDoc
68     {
69         return (this.user != null) ? dataSource.getConnection(this.user, this.password) : dataSource.getConnection();
70     }
71
72     /**
73      * @see net.sf.hajdbc.Database#createConnectionFactory()
74      */

75     public DataSource createConnectionFactory()
76     {
77         try
78         {
79             Context JavaDoc context = new InitialContext JavaDoc(this.properties);
80     
81             Object JavaDoc object = context.lookup(this.name);
82             
83             return DataSource.class.cast(object);
84         }
85         catch (ClassCastException JavaDoc e)
86         {
87             throw new IllegalArgumentException JavaDoc(e.toString(), e);
88         }
89         catch (NamingException JavaDoc e)
90         {
91             throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.JNDI_LOOKUP_FAILED, this.name), e);
92         }
93     }
94
95     /**
96      * @see net.sf.hajdbc.Database#getConnectionFactoryClass()
97      */

98     public Class JavaDoc<DataSource> getConnectionFactoryClass()
99     {
100         return DataSource.class;
101     }
102
103     /**
104      * @see net.sf.hajdbc.Database#getActiveMBeanClass()
105      */

106     public Class JavaDoc<? extends ActiveDatabaseMBean> getActiveMBeanClass()
107     {
108         return ActiveDataSourceDatabaseMBean.class;
109     }
110
111     /**
112      * @see net.sf.hajdbc.Database#getInactiveMBeanClass()
113      */

114     public Class JavaDoc<? extends InactiveDatabaseMBean> getInactiveMBeanClass()
115     {
116         return InactiveDataSourceDatabaseMBean.class;
117     }
118 }
119
Popular Tags