1 19 20 package org.apache.avalon.excalibur.datasource.cluster; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 25 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 26 import org.apache.avalon.excalibur.datasource.NoValidConnectionException; 27 import org.apache.avalon.framework.activity.Disposable; 28 import org.apache.avalon.framework.activity.Initializable; 29 import org.apache.avalon.framework.configuration.Configurable; 30 import org.apache.avalon.framework.configuration.Configuration; 31 import org.apache.avalon.framework.configuration.ConfigurationException; 32 import org.apache.avalon.framework.logger.AbstractLogEnabled; 33 import org.apache.avalon.framework.service.ServiceException; 34 import org.apache.avalon.framework.service.ServiceManager; 35 import org.apache.avalon.framework.service.ServiceSelector; 36 import org.apache.avalon.framework.service.Serviceable; 37 import org.apache.avalon.framework.thread.ThreadSafe; 38 39 44 public abstract class AbstractDataSourceCluster 45 extends AbstractLogEnabled 46 implements Serviceable, Configurable, Initializable, Disposable, ThreadSafe 47 { 48 protected int m_size; 49 private String [] m_dataSourceNames; 50 private ServiceSelector m_dbSelector; 51 private DataSourceComponent[] m_dataSources; 52 53 56 public AbstractDataSourceCluster() 57 { 58 } 59 60 63 68 public int getClusterSize() 69 { 70 return m_size; 71 } 72 73 84 public Connection getConnectionForIndex( int index ) throws SQLException 85 { 86 if( ( index < 0 ) || ( index >= m_size ) ) 87 { 88 throw new NoValidConnectionException( 89 "index (" + index + ") must be in the range 0 to " + ( m_size - 1 ) ); 90 } 91 return m_dataSources[ index ].getConnection(); 92 } 93 94 97 104 public void service( final ServiceManager manager ) 105 throws ServiceException 106 { 107 m_dbSelector = 108 (ServiceSelector)manager.lookup( DataSourceComponent.ROLE + "Selector" ); 109 } 110 111 114 121 public void configure( Configuration configuration ) throws ConfigurationException 122 { 123 m_size = configuration.getAttributeAsInteger( "size" ); 125 if( m_size < 1 ) 126 { 127 throw new ConfigurationException( 128 "Invalid value (" + m_size + ") for size attribute." ); 129 } 130 131 m_dataSourceNames = new String [ m_size ]; 133 Configuration[] dataSourceConfigs = configuration.getChildren( "dbpool" ); 134 for( int i = 0; i < dataSourceConfigs.length; i++ ) 135 { 136 int index = dataSourceConfigs[ i ].getAttributeAsInteger( "index" ); 137 if( ( index < 0 ) || ( index >= m_size ) ) 138 { 139 throw new ConfigurationException( "The dbpool with index=\"" + index + 140 "\" is invalid. Index must be in the range 0 to " + ( m_size - 1 ) ); 141 } 142 if( m_dataSourceNames[ index ] != null ) 143 { 144 throw new ConfigurationException( "Only one dbpool with index=\"" + index + 145 "\" can be defined." ); 146 } 147 m_dataSourceNames[ index ] = dataSourceConfigs[ i ].getValue(); 148 } 149 150 for( int i = 0; i < m_dataSourceNames.length; i++ ) 152 { 153 if( m_dataSourceNames[ i ] == null ) 154 { 155 throw new ConfigurationException( "Expected a dbpool with index=\"" + i + "\"" ); 156 } 157 } 158 } 159 160 163 168 public void initialize() throws Exception 169 { 170 m_dataSources = new DataSourceComponent[ m_size ]; 172 for( int i = 0; i < m_dataSourceNames.length; i++ ) 173 { 174 m_dataSources[ i ] = (DataSourceComponent)m_dbSelector.select( m_dataSourceNames[ i ] ); 175 } 176 } 177 178 181 184 public void dispose() 185 { 186 if( m_dbSelector != null ) 188 { 189 if( m_dataSources != null ) 190 { 191 for( int i = 0; i < m_dataSources.length; i++ ) 192 { 193 if( m_dataSources[ i ] != null ) 194 { 195 m_dbSelector.release( m_dataSources[ i ] ); 196 } 197 } 198 199 m_dataSources = null; 200 } 201 202 m_dbSelector = null; 203 } 204 } 205 } 206 207 | Popular Tags |