1 19 20 package org.apache.avalon.excalibur.datasource.ids; 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.framework.activity.Disposable; 27 import org.apache.avalon.framework.activity.Initializable; 28 import org.apache.avalon.framework.configuration.Configurable; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.ServiceSelector; 34 import org.apache.avalon.framework.service.Serviceable; 35 36 41 public abstract class AbstractDataSourceIdGenerator 42 extends AbstractIdGenerator 43 implements Serviceable, Configurable, Initializable, Disposable 44 { 45 private String m_dataSourceName; 46 private ServiceSelector m_dbSelector; 47 protected DataSourceComponent m_dataSource; 48 49 52 protected int m_allocated; 53 protected long m_nextId; 54 55 58 public AbstractDataSourceIdGenerator() 59 { 60 } 61 62 65 73 protected Connection getConnection() 74 throws SQLException 75 { 76 return m_dataSource.getConnection(); 77 } 78 79 82 89 public void service( final ServiceManager manager ) 90 throws ServiceException 91 { 92 m_dbSelector = (ServiceSelector)manager.lookup( DataSourceComponent.ROLE + "Selector" ); 93 } 94 95 98 105 public void configure( Configuration configuration ) 106 throws ConfigurationException 107 { 108 setUseBigDecimals( configuration.getAttributeAsBoolean( "big-decimals", false ) ); 110 111 m_dataSourceName = configuration.getChild( "dbpool" ).getValue(); 113 } 114 115 118 123 public void initialize() 124 throws Exception 125 { 126 m_dataSource = (DataSourceComponent)m_dbSelector.select( m_dataSourceName ); 128 } 129 130 133 136 public void dispose() 137 { 138 if( m_dbSelector != null ) 140 { 141 if( m_dataSource != null ) 142 { 143 m_dbSelector.release( m_dataSource ); 144 145 m_dataSource = null; 146 } 147 148 m_dbSelector = null; 149 } 150 } 151 } 152 153 | Popular Tags |