001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2008 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: */
021: package net.sf.hajdbc.sql;
022:
023: import javax.management.DynamicMBean;
024: import javax.management.NotCompliantMBeanException;
025: import javax.management.StandardMBean;
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.sql.CommonDataSource;
030:
031: import net.sf.hajdbc.Messages;
032:
033: /**
034: * A database described by a data source.
035: * @author Paul Ferraro
036: * @param <D> <code>javax.sql</code> data source interface
037: */
038: public abstract class CommonDataSourceDatabase<D extends CommonDataSource>
039: extends AbstractDatabase<D> implements
040: InactiveDataSourceDatabaseMBean {
041: private String name;
042: private Class<D> targetClass;
043:
044: protected CommonDataSourceDatabase(Class<D> targetClass) {
045: this .targetClass = targetClass;
046: }
047:
048: /**
049: * @see net.sf.hajdbc.sql.ActiveDataSourceDatabaseMBean#getName()
050: */
051: @Override
052: public String getName() {
053: return this .name;
054: }
055:
056: /**
057: * @see net.sf.hajdbc.sql.InactiveDataSourceDatabaseMBean#setName(java.lang.String)
058: */
059: @Override
060: public void setName(String name) {
061: this .checkDirty(this .name, name);
062: this .name = name;
063: }
064:
065: /**
066: * @see net.sf.hajdbc.Database#createConnectionFactory()
067: */
068: @Override
069: public D createConnectionFactory() {
070: try {
071: Context context = new InitialContext(this .properties);
072:
073: return this .targetClass.cast(context.lookup(this .name));
074: } catch (ClassCastException e) {
075: throw new IllegalArgumentException(e.toString(), e);
076: } catch (NamingException e) {
077: throw new IllegalArgumentException(Messages.getMessage(
078: Messages.JNDI_LOOKUP_FAILED, this .name), e);
079: }
080: }
081:
082: /**
083: * @see net.sf.hajdbc.Database#getActiveMBean()
084: */
085: @Override
086: public DynamicMBean getActiveMBean() {
087: try {
088: return new StandardMBean(this ,
089: ActiveDataSourceDatabaseMBean.class);
090: } catch (NotCompliantMBeanException e) {
091: throw new IllegalStateException(e);
092: }
093: }
094:
095: /**
096: * @see net.sf.hajdbc.Database#getInactiveMBean()
097: */
098: @Override
099: public DynamicMBean getInactiveMBean() {
100: try {
101: return new StandardMBean(this ,
102: InactiveDataSourceDatabaseMBean.class);
103: } catch (NotCompliantMBeanException e) {
104: throw new IllegalStateException(e);
105: }
106: }
107: }
|