001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.databases.ibatis;
018:
019: import java.io.PrintWriter;
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.Map;
023:
024: import javax.sql.DataSource;
025:
026: import org.apache.avalon.excalibur.datasource.DataSourceComponent;
027: import org.apache.avalon.framework.CascadingRuntimeException;
028: import org.apache.avalon.framework.component.ComponentException;
029: import org.apache.avalon.framework.component.ComponentManager;
030: import org.apache.avalon.framework.component.ComponentSelector;
031: import org.apache.cocoon.components.CocoonComponentManager;
032:
033: import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
034:
035: /**
036: * This is an implementation if iBatis DataSourceFactory allowing to use
037: * a reference to an excalibur data source in the iBatis configuration.
038: * The configuration in the iBatis sqlMapConfig looks like this:
039: * <dataSource type="org.apache.cocoon.databases.ibatis.ExcaliburDataSourceFactory">
040: * <property name="connection" value="Name of the Excalibur data source from the cocoon.xconf"/>
041: * </dataSource>
042: *
043: * @version $Id: ExcaliburDataSourceFactory.java 495744 2007-01-12 21:22:51Z anathaniel $
044: * @since 2.1.10
045: */
046: public class ExcaliburDataSourceFactory implements DataSourceFactory {
047:
048: protected DataSourceComponent datasource;
049:
050: public DataSource getDataSource() {
051: return new DataSourceWrapper(this .datasource);
052: }
053:
054: /**
055: * @see com.ibatis.sqlmap.engine.datasource.DataSourceFactory#initialize(java.util.Map)
056: */
057: public void initialize(Map values) {
058: final String connection = (String) values.get("connection");
059: if (connection == null) {
060: throw new RuntimeException(
061: "Connection configuration is missing for "
062: + this .getClass().getName()
063: + "."
064: + " Have a look at the iBatis sqlMapConfig and check the 'connection' property for the data source.");
065: }
066: // get the component manager
067: final ComponentManager manager = CocoonComponentManager
068: .getSitemapComponentManager();
069: if (manager == null) {
070: throw new RuntimeException(
071: "CocoonComponentManager is not available for "
072: + this .getClass().getName()
073: + "."
074: + " Make sure that you're initializing iBatis during an active request and not on startup.");
075: }
076: try {
077: final ComponentSelector selector = (ComponentSelector) manager
078: .lookup(DataSourceComponent.ROLE + "Selector");
079: try {
080: this .datasource = (DataSourceComponent) selector
081: .select(connection);
082: } catch (ComponentException e) {
083: throw new CascadingRuntimeException(
084: "Unable to lookup data source with name "
085: + connection
086: + "."
087: + " Check the cocoon.xconf and the iBatis sqlMapConfig.",
088: e);
089: }
090: } catch (ComponentException e) {
091: throw new CascadingRuntimeException(
092: "Unable to lookup datasource selector.", e);
093: }
094: }
095:
096: protected static final class DataSourceWrapper implements
097: DataSource {
098:
099: protected final DataSourceComponent datasource;
100: protected PrintWriter writer = new PrintWriter(System.out);
101: protected int timeout = 0;
102:
103: public DataSourceWrapper(DataSourceComponent d) {
104: this .datasource = d;
105: }
106:
107: public Connection getConnection() throws SQLException {
108: return this .datasource.getConnection();
109: }
110:
111: public Connection getConnection(String username, String password)
112: throws SQLException {
113: return null;
114: }
115:
116: public int getLoginTimeout() throws SQLException {
117: return this .timeout;
118: }
119:
120: public PrintWriter getLogWriter() throws SQLException {
121: return this .writer;
122: }
123:
124: public void setLoginTimeout(int seconds) throws SQLException {
125: this .timeout = seconds;
126: }
127:
128: public void setLogWriter(PrintWriter out) throws SQLException {
129: this .writer = out;
130: }
131:
132: /**
133: * Required by JDK1.6.
134: */
135: public Object unwrap(Class iface) throws SQLException {
136: return null;
137: }
138:
139: /**
140: * Required by JDK1.6.
141: */
142: public boolean isWrapperFor(Class iface) throws SQLException {
143: return false;
144: }
145: }
146: }
|