001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jdbc.local;
023:
024: import java.util.Hashtable;
025: import javax.naming.InitialContext;
026: import javax.naming.Name;
027: import javax.naming.NamingException;
028: import javax.resource.ResourceException;
029: import javax.resource.spi.ConnectionManager;
030: import javax.resource.spi.ConnectionRequestInfo;
031: import javax.resource.spi.ManagedConnectionFactory;
032: import javax.transaction.TransactionManager;
033: import org.jboss.deployment.DeploymentException;
034: import org.jboss.logging.Logger;
035: import org.jboss.resource.connectionmanager.CachedConnectionManager;
036: import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
037: import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
038: import org.jboss.resource.connectionmanager.TxConnectionManager;
039: import org.jboss.resource.connectionmanager.CachedConnectionManagerReference;
040: import org.jboss.util.naming.NonSerializableFactory;
041: import org.jboss.util.naming.Util;
042:
043: /**
044: * This is a pojo that instantiates a Local tx connection pool.
045: * It provides same functionality as ds.xml files
046: *
047: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
048: * @version $Revision: 57189 $
049: */
050: public class LocalTxDataSource {
051: protected static Logger log = Logger
052: .getLogger(LocalTxDataSource.class.getName());
053:
054: public LocalTxDataSource() {
055: }
056:
057: private CachedConnectionManager cachedConnectionManager;
058: private TransactionManager transactionManager;
059: private String jndiName;
060:
061: private InternalManagedConnectionPool.PoolParams poolParams = new InternalManagedConnectionPool.PoolParams();
062: private LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
063:
064: private JBossManagedConnectionPool.OnePool pool = new JBossManagedConnectionPool.OnePool(
065: mcf, poolParams, false, log);
066: private TxConnectionManager connectionManager;
067: private Object datasource;
068: protected Hashtable initialContextProperties;
069: protected InitialContext initialContext;
070:
071: public class ConnectionManagerDelegate implements ConnectionManager {
072: private static final long serialVersionUID = 1L;
073:
074: public Object allocateConnection(ManagedConnectionFactory mcf,
075: ConnectionRequestInfo cxRequestInfo)
076: throws ResourceException {
077: return connectionManager.allocateConnection(mcf,
078: cxRequestInfo);
079: }
080: }
081:
082: public void setInitialContextProperties(
083: Hashtable initialContextProperties) {
084: this .initialContextProperties = initialContextProperties;
085: }
086:
087: public void start() throws Exception {
088: if (initialContextProperties == null)
089: initialContext = new InitialContext();
090: else
091: initialContext = new InitialContext(
092: initialContextProperties);
093:
094: connectionManager = new TxConnectionManager(
095: cachedConnectionManager, pool, transactionManager);
096: connectionManager.setLocalTransactions(true);
097: connectionManager.setTrackConnectionByTx(true);
098: pool.setConnectionListenerFactory(connectionManager);
099: datasource = connectionManager.getPoolingStrategy()
100: .getManagedConnectionFactory().createConnectionFactory(
101: new ConnectionManagerDelegate());
102: bindConnectionFactory();
103: }
104:
105: /**
106: * Bind the connection factory into jndi
107: */
108: protected void bindConnectionFactory() throws Exception {
109: InitialContext ctx = initialContext;
110: try {
111: Name name = ctx.getNameParser("").parse(jndiName);
112: String key = name.toString();
113: if (true == true && name.size() > 1) {
114: int size = name.size() - 1;
115: Util.createSubcontext(initialContext, name
116: .getPrefix(size));
117: }
118: NonSerializableFactory.rebind(initialContext, key,
119: datasource);
120: log
121: .info("Bound datasource to JNDI name '" + jndiName
122: + "'");
123: } catch (NamingException ne) {
124: throw new DeploymentException(
125: "Could not bind ConnectionFactory into jndi: "
126: + jndiName, ne);
127: } finally {
128: ctx.close();
129: }
130: }
131:
132: protected void unbindConnectionFactory() throws Exception {
133: InitialContext ctx = initialContext;
134: try {
135: ctx.unbind(jndiName);
136: NonSerializableFactory.unbind(jndiName);
137: log.info("Unbound datasource for JNDI name '" + jndiName
138: + "'");
139: } catch (NamingException ne) {
140: log.error("Could not unbind datasource from jndi: "
141: + jndiName, ne);
142: } finally {
143: ctx.close();
144: }
145: }
146:
147: public Object getDatasource() {
148: return datasource;
149: }
150:
151: public void setCachedConnectionManager(
152: CachedConnectionManagerReference cachedConnectionManager) {
153: this .cachedConnectionManager = cachedConnectionManager
154: .getCachedConnectionManager();
155: }
156:
157: public TransactionManager getTransactionManager() {
158: return transactionManager;
159: }
160:
161: public void setTransactionManager(
162: TransactionManager transactionManager) {
163: this .transactionManager = transactionManager;
164: }
165:
166: public String getJndiName() {
167: return jndiName;
168: }
169:
170: public void setJndiName(String jndiName) {
171: this .jndiName = jndiName;
172: }
173:
174: public int getMinSize() {
175: return poolParams.minSize;
176: }
177:
178: public void setMinSize(int minSize) {
179: poolParams.minSize = minSize;
180: }
181:
182: public int getMaxSize() {
183: return poolParams.maxSize;
184: }
185:
186: public void setMaxSize(int maxSize) {
187: poolParams.maxSize = maxSize;
188: }
189:
190: public int getBlockingTimeout() {
191: return poolParams.blockingTimeout;
192: }
193:
194: public void setBlockingTimeout(int blockingTimeout) {
195: poolParams.blockingTimeout = blockingTimeout;
196: }
197:
198: public long getIdleTimeout() {
199: return poolParams.idleTimeout;
200: }
201:
202: public void setIdleTimeout(long idleTimeout) {
203: poolParams.idleTimeout = idleTimeout;
204: }
205:
206: public String getDriverClass() {
207: return mcf.getDriverClass();
208: }
209:
210: public void setDriverClass(final String driverClass) {
211: mcf.setDriverClass(driverClass);
212: }
213:
214: public String getConnectionURL() {
215: return mcf.getConnectionURL();
216: }
217:
218: public void setConnectionURL(final String connectionURL) {
219: mcf.setConnectionURL(connectionURL);
220: }
221:
222: public void setUserName(final String userName) {
223: mcf.setUserName(userName);
224: }
225:
226: public void setPassword(final String password) {
227: mcf.setPassword(password);
228: }
229:
230: public void setPreparedStatementCacheSize(int size) {
231: mcf.setPreparedStatementCacheSize(size);
232: }
233:
234: public int getPreparedStatementCacheSize() {
235: return mcf.getPreparedStatementCacheSize();
236: }
237:
238: public boolean getSharePreparedStatements() {
239: return mcf.getSharePreparedStatements();
240: }
241:
242: public void setSharePreparedStatements(boolean sharePS) {
243: mcf.setSharePreparedStatements(sharePS);
244: }
245:
246: public boolean getTxQueryTimeout() {
247: return mcf.isTransactionQueryTimeout();
248: }
249:
250: public void setTxQueryTimeout(boolean qt) {
251: mcf.setTransactionQueryTimeout(qt);
252: }
253:
254: public String getTransactionIsolation() {
255: return mcf.getTransactionIsolation();
256: }
257:
258: public void setTransactionIsolation(String transactionIsolation) {
259: mcf.setTransactionIsolation(transactionIsolation);
260: }
261:
262: public String getNewConnectionSQL() {
263: return mcf.getNewConnectionSQL();
264: }
265:
266: public void setNewConnectionSQL(String newConnectionSQL) {
267: mcf.setNewConnectionSQL(newConnectionSQL);
268: }
269:
270: public String getCheckValidConnectionSQL() {
271: return mcf.getCheckValidConnectionSQL();
272: }
273:
274: public void setCheckValidConnectionSQL(
275: String checkValidConnectionSQL) {
276: mcf.setCheckValidConnectionSQL(checkValidConnectionSQL);
277: }
278:
279: public String getTrackStatements() {
280: return mcf.getTrackStatements();
281: }
282:
283: public void setTrackStatements(String value) {
284: mcf.setTrackStatements(value);
285: }
286:
287: public String getExceptionSorterClassName() {
288: return mcf.getExceptionSorterClassName();
289: }
290:
291: public void setExceptionSorterClassName(
292: String exceptionSorterClassName) {
293: mcf.setExceptionSorterClassName(exceptionSorterClassName);
294: }
295:
296: public String getValidConnectionCheckerClassName() {
297: return mcf.getValidConnectionCheckerClassName();
298: }
299:
300: public void setValidConnectionCheckerClassName(String value) {
301: mcf.setValidConnectionCheckerClassName(value);
302: }
303:
304: }
|