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.ojb.components;
018:
019: import org.apache.avalon.excalibur.datasource.DataSourceComponent;
020: import org.apache.avalon.framework.CascadingRuntimeException;
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.component.Component;
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028:
029: import org.apache.ojb.broker.accesslayer.ConnectionFactory;
030: import org.apache.ojb.broker.accesslayer.LookupException;
031: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
032:
033: import java.sql.Connection;
034: import java.sql.SQLException;
035:
036: /**
037: * OJB ConnectionFactory implemenation to bridge into the Avalon DataSource
038: * connection pooling component defined in the Cocoon configuration.
039: *
040: * <p>This class has two faces to it:
041: * <dl>
042: * <dt>Avalon Component</dt>
043: * <dd>Instance of the class created and managed by Avalon container.
044: * When instance is initialized, it looks up datasource components
045: * service selector.</dd>
046: * <dt>OJB Managed Class</dt>
047: * <dd>Instances of the class are created and managed by OJB, as defined
048: * in the OJB <code>repository.xml</code> file. Each OJB managed instance
049: * of the class will have access to the datasource components service
050: * selector initialized by Avalon managed instance of the class.</dd>
051: * </dl>
052: *
053: * It is important that Avalon component is initialized before any access
054: * to OJB API is made.</p>
055: *
056: * @author giacomo at apache.org
057: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
058: * @version $Id: ConnectionFactoryImpl.java 433543 2006-08-22 06:22:54Z crossley $
059: */
060: public class ConnectionFactoryImpl implements Component, ThreadSafe,
061: Serviceable, Disposable, ConnectionFactory {
062:
063: /** The <code>ServiceManager</code> to be used */
064: private static ServiceManager manager;
065:
066: /** The <code>ServiceSelector</code> to be used */
067: private static ServiceSelector datasources;
068:
069: /** The <code>JdbcConnectionDescriptor</code> */
070: private JdbcConnectionDescriptor conDesc;
071:
072: /**
073: * Default constructor
074: */
075: public ConnectionFactoryImpl() {
076: }
077:
078: /**
079: * OJB 1.1 constructor
080: */
081: public ConnectionFactoryImpl(JdbcConnectionDescriptor conDesc) {
082: this .conDesc = conDesc;
083: }
084:
085: public void service(ServiceManager manager) throws ServiceException {
086: ConnectionFactoryImpl.manager = manager;
087: ConnectionFactoryImpl.datasources = (ServiceSelector) manager
088: .lookup(DataSourceComponent.ROLE + "Selector");
089: }
090:
091: public void dispose() {
092: if (ConnectionFactoryImpl.manager != null) {
093: ConnectionFactoryImpl.manager
094: .release(ConnectionFactoryImpl.datasources);
095: ConnectionFactoryImpl.datasources = null;
096: ConnectionFactoryImpl.manager = null;
097: }
098: }
099:
100: //
101: // OJB 1.1 ConnectionFactory Implementation
102: //
103:
104: public Connection lookupConnection() throws LookupException {
105: return lookupConnection(this .conDesc);
106: }
107:
108: public void releaseConnection(Connection connection) {
109: releaseConnection(this .conDesc, connection);
110: }
111:
112: public int getActiveConnections() {
113: return 0;
114: }
115:
116: public int getIdleConnections() {
117: return 0;
118: }
119:
120: //
121: // OJB 1.0 ConnectionFactory Implementation
122: //
123:
124: public Connection lookupConnection(
125: final JdbcConnectionDescriptor conDesc)
126: throws LookupException {
127: if (ConnectionFactoryImpl.manager == null) {
128: throw new LookupException(
129: "ConnectionFactoryImpl is not initialized! Please check your cocoon.xconf");
130: }
131:
132: try {
133: return ((DataSourceComponent) ConnectionFactoryImpl.datasources
134: .select(conDesc.getJcdAlias())).getConnection();
135: } catch (final ServiceException e) {
136: throw new LookupException("Cannot lookup DataSource "
137: + conDesc.getJcdAlias(), e);
138: } catch (final SQLException e) {
139: throw new LookupException(
140: "Cannot get connection from DataSource "
141: + conDesc.getJcdAlias(), e);
142: }
143: }
144:
145: public void releaseConnection(JdbcConnectionDescriptor conDesc,
146: Connection connection) {
147: try {
148: // The DataSource of this connection will take care of pooling
149: connection.close();
150: } catch (final SQLException e) {
151: // This should not happen, but in case
152: throw new CascadingRuntimeException(
153: "Cannot release SQL Connection to DataSource", e);
154: }
155: }
156:
157: public void releaseAllResources() {
158: // Nothing to do here
159: }
160: }
|