1 16 package org.apache.cocoon.ojb.components; 17 18 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 19 import org.apache.avalon.framework.CascadingRuntimeException; 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.ServiceSelector; 25 import org.apache.avalon.framework.service.Serviceable; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 28 import org.apache.ojb.broker.accesslayer.ConnectionFactory; 29 import org.apache.ojb.broker.accesslayer.LookupException; 30 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 31 32 import java.sql.Connection ; 33 import java.sql.SQLException ; 34 35 59 public class ConnectionFactoryImpl implements Component, ThreadSafe, Serviceable, Disposable, 60 ConnectionFactory { 61 62 63 private static ServiceManager manager; 64 65 66 private static ServiceSelector datasources; 67 68 69 private JdbcConnectionDescriptor conDesc; 70 71 74 public ConnectionFactoryImpl() { 75 } 76 77 80 public ConnectionFactoryImpl(JdbcConnectionDescriptor conDesc) { 81 this.conDesc = conDesc; 82 } 83 84 public void service(ServiceManager manager) throws ServiceException { 85 ConnectionFactoryImpl.manager = manager; 86 ConnectionFactoryImpl.datasources = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE + "Selector"); 87 } 88 89 public void dispose() { 90 if (ConnectionFactoryImpl.manager != null) { 91 ConnectionFactoryImpl.manager.release(ConnectionFactoryImpl.datasources); 92 ConnectionFactoryImpl.datasources = null; 93 ConnectionFactoryImpl.manager = null; 94 } 95 } 96 97 101 public Connection lookupConnection() 102 throws LookupException { 103 return lookupConnection(this.conDesc); 104 } 105 106 public void releaseConnection(Connection connection) { 107 releaseConnection(this.conDesc, connection); 108 } 109 110 public int getActiveConnections() { 111 return 0; 112 } 113 114 public int getIdleConnections() { 115 return 0; 116 } 117 118 122 public Connection lookupConnection(final JdbcConnectionDescriptor conDesc) 123 throws LookupException { 124 if (ConnectionFactoryImpl.manager == null) { 125 throw new LookupException("ConnectionFactoryImpl is not initialized! Please check your cocoon.xconf"); 126 } 127 128 try { 129 return ((DataSourceComponent) ConnectionFactoryImpl.datasources.select(conDesc.getJcdAlias())).getConnection(); 130 } catch (final ServiceException e) { 131 throw new LookupException("Cannot lookup DataSource " + 132 conDesc.getJcdAlias(), e); 133 } catch (final SQLException e) { 134 throw new LookupException("Cannot get connection from DataSource " + 135 conDesc.getJcdAlias(), e); 136 } 137 } 138 139 public void releaseConnection(JdbcConnectionDescriptor conDesc, Connection connection) { 140 try { 141 connection.close(); 143 } catch (final SQLException e) { 144 throw new CascadingRuntimeException("Cannot release SQL Connection to DataSource", e); 146 } 147 } 148 149 public void releaseAllResources() { 150 } 152 } 153 | Popular Tags |