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 java.lang.reflect.InvocationHandler;
024: import java.sql.SQLException;
025: import java.util.Hashtable;
026:
027: import javax.naming.Context;
028: import javax.naming.Name;
029: import javax.naming.RefAddr;
030: import javax.naming.Reference;
031: import javax.naming.spi.ObjectFactory;
032: import javax.sql.CommonDataSource;
033:
034: import net.sf.hajdbc.DatabaseCluster;
035: import net.sf.hajdbc.util.reflect.ProxyFactory;
036:
037: /**
038: * @author Paul Ferraro
039: *
040: * @param <D>
041: */
042: public abstract class CommonDataSourceFactory<D extends CommonDataSource>
043: implements ObjectFactory {
044: private Class<D> targetClass;
045:
046: /**
047: * @param targetClass
048: */
049: protected CommonDataSourceFactory(Class<D> targetClass) {
050: this .targetClass = targetClass;
051: }
052:
053: /**
054: * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
055: */
056: @Override
057: public Object getObjectInstance(Object object, Name name,
058: Context context, Hashtable<?, ?> environment)
059: throws Exception {
060: if ((object == null) || !(object instanceof Reference))
061: return null;
062:
063: Reference reference = (Reference) object;
064:
065: String className = reference.getClassName();
066:
067: if ((className == null)
068: || !className.equals(this .targetClass.getName()))
069: return null;
070:
071: RefAddr idAddr = reference
072: .get(CommonDataSourceReference.CLUSTER);
073:
074: if (idAddr == null)
075: return null;
076:
077: Object idAddrContent = idAddr.getContent();
078:
079: if ((idAddrContent == null)
080: || !(idAddrContent instanceof String))
081: return null;
082:
083: String id = (String) idAddrContent;
084:
085: RefAddr configAddr = reference
086: .get(CommonDataSourceReference.CONFIG);
087:
088: String config = null;
089:
090: if (configAddr != null) {
091: Object configAddrContent = configAddr.getContent();
092:
093: if ((configAddrContent != null)
094: && (configAddrContent instanceof String)) {
095: config = (String) configAddrContent;
096: }
097: }
098:
099: DatabaseCluster<D> cluster = this
100: .getDatabaseCluster(id, config);
101:
102: if (cluster == null)
103: return null;
104:
105: return ProxyFactory.createProxy(this .targetClass, this
106: .getInvocationHandler(cluster));
107: }
108:
109: /**
110: * @param id
111: * @param config
112: * @return the appropriate database cluster
113: * @throws SQLException
114: */
115: protected abstract DatabaseCluster<D> getDatabaseCluster(String id,
116: String config) throws SQLException;
117:
118: /**
119: * @param cluster
120: * @return the appropriate proxy invocation handler for this datasource
121: */
122: protected abstract InvocationHandler getInvocationHandler(
123: DatabaseCluster<D> cluster);
124: }
|