1 17 18 package org.apache.avalon.cornerstone.blocks.datasources; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector; 25 26 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 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.context.Context; 32 import org.apache.avalon.framework.context.ContextException; 33 import org.apache.avalon.framework.context.Contextualizable; 34 import org.apache.avalon.framework.logger.AbstractLogEnabled; 35 import org.apache.avalon.framework.logger.LogEnabled; 36 import org.apache.avalon.framework.service.ServiceException; 37 38 62 public class DefaultDataSourceSelector 63 extends AbstractLogEnabled 64 implements DataSourceSelector, Contextualizable, Configurable, Initializable, Disposable 65 { 66 private Configuration m_configuration; 67 private Map m_dataSources; 68 private String m_blockName; 69 70 71 76 public void contextualize( final Context context ) 77 throws ContextException 78 { 79 try 80 { 81 m_blockName = (String ) context.get( "urn:avalon:name" ); 82 } 83 catch( Throwable e ) 84 { 85 try 87 { 88 m_blockName = (String )context.get( "block.name" ); 89 } 90 catch( Throwable ee ) 91 { 92 m_blockName = "DataSourceSelector/" + System.identityHashCode( this ); 93 } 94 } 95 } 96 97 100 public void configure( final Configuration configuration ) 101 { 102 m_configuration = configuration; 103 } 104 105 public void initialize() 106 throws Exception 107 { 108 m_dataSources = new HashMap (); 109 110 Configuration[] dataSourceConfs = getDataSourceConfig(); 111 112 for( int i = 0; i < dataSourceConfs.length; i++ ) 113 { 114 final Configuration dataSourceConf = dataSourceConfs[ i ]; 115 116 final String name = dataSourceConf.getAttribute( "name" ); 117 final String clazz = dataSourceConf.getAttribute( "class" ); 118 final String driver = dataSourceConf.getChild( "driver", true ).getValue( "" ); 119 120 final ClassLoader classLoader = 121 Thread.currentThread().getContextClassLoader(); 122 123 DataSourceComponent component = null; 124 if( null == classLoader ) 125 { 126 if( !"".equals( driver ) ) 127 { 128 Class.forName( driver, true, Thread.currentThread().getContextClassLoader() ); 129 } 130 131 component = (DataSourceComponent)Class.forName( clazz ).newInstance(); 132 } 133 else 134 { 135 if( !"".equals( driver ) ) 136 { 137 classLoader.loadClass( driver ); 138 } 139 140 component = (DataSourceComponent)classLoader.loadClass( clazz ).newInstance(); 141 } 142 143 if( component instanceof LogEnabled ) 144 { 145 setupLogger( component, name ); 146 } 147 component.configure( dataSourceConf ); 148 m_dataSources.put( name, component ); 149 150 if( getLogger().isInfoEnabled() ) 151 { 152 getLogger().info( "DataSource " + name + " ready" ); 153 } 154 } 155 } 156 157 private Configuration[] getDataSourceConfig() 158 { 159 final Configuration head = 160 m_configuration.getChild( "data-sources" ); 161 if( 0 != head.getChildren().length ) 162 { 163 164 final String message = 165 "WARNING: Child node <data-sources/> in " + 166 "configuration of component named " + m_blockName + 167 " has been deprecated. Please put <data-source/> elements" + 168 " in root configuration element"; 169 getLogger().warn( message ); 170 System.out.println( message ); 171 return head.getChildren( "data-source" ); 172 } 173 else 174 { 175 return m_configuration.getChildren( "data-source" ); 176 } 177 } 178 179 public void dispose() 180 { 181 if( getLogger().isDebugEnabled() ) 182 { 183 getLogger().debug( "disposal" ); 184 } 185 final Iterator keys = m_dataSources.keySet().iterator(); 186 while( keys.hasNext() ) 187 { 188 final DataSourceComponent dsc = 189 (DataSourceComponent)m_dataSources.get( keys.next() ); 190 if( dsc instanceof Disposable ) 191 { 192 ( (Disposable)dsc ).dispose(); 193 } 194 } 195 } 196 197 public boolean isSelectable( final Object hint ) 198 { 199 return m_dataSources.containsKey( hint ); 200 } 201 202 public Object select( final Object hint ) 203 throws ServiceException 204 { 205 final Object component = m_dataSources.get( hint ); 206 207 if( null == component ) 208 { 209 throw new ServiceException( hint.toString(), "Unable to provide DataSourceComponent for " + hint ); 210 } 211 212 return component; 213 } 214 215 public void release( final Object component ) 216 { 217 } 219 } 220 | Popular Tags |