01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.lucene.engine.store.jdbc;
18:
19: import java.sql.Connection;
20: import java.sql.SQLException;
21: import javax.naming.NamingException;
22: import javax.sql.DataSource;
23:
24: import org.apache.lucene.store.jdbc.datasource.AbstractDataSource;
25: import org.compass.core.CompassException;
26: import org.compass.core.config.CompassSettings;
27: import org.compass.core.jndi.NamingHelper;
28:
29: /**
30: * A JNDI based data source provider. Uses any additional jndi settings to get the JNDI
31: * initial context. The data source jndi name is the url set (without the jdbc:// prefix).
32: *
33: * @author kimchy
34: */
35: public class JndiDataSourceProvider extends AbstractDataSourceProvider {
36:
37: private static class UsernamePasswordDataSourceWrapper extends
38: AbstractDataSource {
39:
40: private DataSource dataSource;
41:
42: private String username;
43:
44: private String password;
45:
46: private boolean autoCommit;
47:
48: private boolean externalAutoCommit;
49:
50: public UsernamePasswordDataSourceWrapper(DataSource dataSource,
51: String username, String password, boolean autoCommit,
52: boolean externalAutoCommit) {
53: this .dataSource = dataSource;
54: this .username = username;
55: this .password = password;
56: this .autoCommit = autoCommit;
57: this .externalAutoCommit = externalAutoCommit;
58: }
59:
60: public Connection getConnection() throws SQLException {
61: return getConnection(username, password);
62: }
63:
64: public Connection getConnection(String username, String password)
65: throws SQLException {
66: Connection conn;
67: if (username == null) {
68: conn = dataSource.getConnection();
69: } else {
70: conn = dataSource.getConnection(username, password);
71: }
72: if (!externalAutoCommit) {
73: if (conn.getAutoCommit() != autoCommit) {
74: conn.setAutoCommit(autoCommit);
75: }
76: }
77: return conn;
78: }
79: }
80:
81: protected DataSource doCreateDataSource(String url,
82: CompassSettings settings) throws CompassException {
83: try {
84: DataSource dataSource = (DataSource) NamingHelper
85: .getInitialContext(settings).lookup(url);
86: return new UsernamePasswordDataSourceWrapper(dataSource,
87: username, password, autoCommit, externalAutoCommit);
88: } catch (NamingException e) {
89: throw new CompassException(
90: "Failed to lookup data source from jndi under ["
91: + url + "]", e);
92: }
93: }
94: }
|