001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.lucene.engine.store.jdbc;
018:
019: import java.sql.Connection;
020: import java.sql.SQLException;
021: import javax.sql.DataSource;
022:
023: import org.apache.lucene.store.jdbc.datasource.AbstractDataSource;
024: import org.compass.core.CompassException;
025: import org.compass.core.config.CompassSettings;
026:
027: /**
028: * A data source provider that can use an externally configured data source. In order to set the external
029: * DataSource to be used, the {@link #setDataSource(javax.sql.DataSource)} static method needs to be called
030: * before the Compass instance is created.
031: *
032: * @author kimchy
033: */
034: public class ExternalDataSourceProvider extends
035: AbstractDataSourceProvider {
036:
037: private static class UsernamePasswordDataSourceWrapper extends
038: AbstractDataSource {
039:
040: private DataSource dataSource;
041:
042: private String username;
043:
044: private String password;
045:
046: private boolean autoCommit;
047:
048: private boolean externalAutoCommit;
049:
050: public UsernamePasswordDataSourceWrapper(DataSource dataSource,
051: String username, String password, boolean autoCommit,
052: boolean externalAutoCommit) {
053: this .dataSource = dataSource;
054: this .username = username;
055: this .password = password;
056: this .autoCommit = autoCommit;
057: this .externalAutoCommit = externalAutoCommit;
058: }
059:
060: public Connection getConnection() throws SQLException {
061: return getConnection(username, password);
062: }
063:
064: public Connection getConnection(String username, String password)
065: throws SQLException {
066: Connection conn;
067: if (username == null) {
068: conn = dataSource.getConnection();
069: } else {
070: conn = dataSource.getConnection(username, password);
071: }
072: if (!externalAutoCommit) {
073: if (conn.getAutoCommit() != autoCommit) {
074: conn.setAutoCommit(autoCommit);
075: }
076: }
077: return conn;
078: }
079: }
080:
081: private static ThreadLocal dataSourceHolder = new ThreadLocal();
082:
083: private static String dataSourceKey = ExternalDataSourceProvider.class
084: .getName();
085:
086: private CompassSettings settings;
087:
088: /**
089: * Sets the external data source to be used. Must be set before creating the compass instance.
090: */
091: public static void setDataSource(DataSource dataSource) {
092: dataSourceHolder.set(dataSource);
093: }
094:
095: protected DataSource doCreateDataSource(String url,
096: CompassSettings settings) throws CompassException {
097: this .settings = settings;
098: DataSource dataSource = (DataSource) dataSourceHolder.get();
099: if (dataSource == null) {
100: dataSource = (DataSource) settings
101: .getRegistry(dataSourceKey);
102: }
103: if (dataSource == null) {
104: throw new CompassException(
105: "Failed to find data source, have you set the static set data source?");
106: } else {
107: settings.setRegistry(dataSourceKey, dataSource);
108: dataSourceHolder.set(null);
109: }
110: return new UsernamePasswordDataSourceWrapper(dataSource,
111: username, password, autoCommit, externalAutoCommit);
112: }
113:
114: public void closeDataSource() {
115: dataSourceHolder.set(null);
116: settings.removeRegistry(dataSourceKey);
117: }
118: }
|