mcconnell 2003/01/15 03:57:54
Added: src/java/org/apache/avalon/cornerstone/blocks/source
DefaultDataSourceSelector.java
DefaultDataSourceSelector-schema.xml
Log:
ServiceManager based replacement of DefaultDataSourceSelector
Revision Changes Path
1.1
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/source/DefaultDataSourceSelector.java
Index: DefaultDataSourceSelector.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.cornerstone.blocks.source;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.avalon.cornerstone.services.source.DataSourceSelector;
import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.LogEnabled;
/**
* A default implementation for DataSourceSelector.
* The Configuration is like this:
*
* <pre>
* <myBlock>
* <data-source name="<i>default</i>"
* class="<i>org.apache.avalon.excalibur.datasource.JdbcDataSource</i>">
* <!-- configuration for JdbcDataSource -->
* <pool-controller min="<i>5</i>" max="<i>10</i>"
connection-class="<i>my.overrided.ConnectionClass</i>">
* <keep-alive>select 1</keep-alive>
* </pool-controller>
* <driver><i>com.database.jdbc.JdbcDriver</i></driver>
* <dburl><i>jdbc:driver://host/mydb</i></dburl>
* <user><i>username</i></user>
* <password><i>password</i></password>
* </data-source>
* </myBlock>
* </pre>
*
* @phoenix:block
* @phoenix:service
name="org.apache.avalon.cornerstone.services.source.DataSourceSelector"
*
* @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
*/
public class AvalonDataSourceSelector
extends AbstractLogEnabled
implements DataSourceSelector, Contextualizable, Configurable, Initializable,
Disposable
{
private Configuration m_configuration;
private Map m_dataSources;
private String m_blockName;
public void contextualize( final Context context )
throws ContextException
{
try
{
m_blockName = (String) context.get("block.name");
}
catch( Throwable e )
{
m_blockName = "DataSourceSelector/" + System.identityHashCode( this );
}
}
/**
* @phoenix:configuration-schema type="relax-ng"
*/
public void configure( final Configuration configuration )
{
m_configuration = configuration;
}
public void initialize()
throws Exception
{
m_dataSources = new HashMap();
Configuration[] dataSourceConfs = getDataSourceConfig();
for( int i = 0; i < dataSourceConfs.length; i++ )
{
final Configuration dataSourceConf = dataSourceConfs[ i ];
final String name = dataSourceConf.getAttribute( "name" );
final String clazz = dataSourceConf.getAttribute( "class" );
final String driver = dataSourceConf.getChild( "driver", true
).getValue( "" );
final ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
DataSourceComponent component = null;
if( null == classLoader )
{
if( !"".equals( driver ) )
{
Class.forName( driver, true,
Thread.currentThread().getContextClassLoader() );
}
component = (DataSourceComponent)Class.forName( clazz
).newInstance();
}
else
{
if( !"".equals( driver ) )
{
classLoader.loadClass( driver );
}
component = (DataSourceComponent)classLoader.loadClass( clazz
).newInstance();
}
if( component instanceof LogEnabled )
{
setupLogger( component, name );
}
component.configure( dataSourceConf );
m_dataSources.put( name, component );
if( getLogger().isInfoEnabled() )
{
getLogger().info( "DataSource " + name + " ready" );
}
}
}
private Configuration[] getDataSourceConfig()
{
final Configuration head =
m_configuration.getChild( "data-sources" );
if( 0 != head.getChildren().length )
{
final String message =
"WARNING: Child node <data-sources/> in " +
"configuration of component named " + m_blockName +
" has been deprecated. Please put <data-source/> elements" +
" in root configuration element";
getLogger().warn( message );
System.out.println( message );
return head.getChildren( "data-source" );
}
else
{
return m_configuration.getChildren( "data-source" );
}
}
public void dispose()
{
if( getLogger().isDebugEnabled() )
{
getLogger().debug( "disposal" );
}
final Iterator keys = m_dataSources.keySet().iterator();
while( keys.hasNext() )
{
final DataSourceComponent dsc =
(DataSourceComponent)m_dataSources.get( keys.next() );
if( dsc instanceof Disposable )
{
((Disposable)dsc).dispose();
}
}
}
public boolean isSelectable( final Object hint )
{
return m_dataSources.containsKey( hint );
}
public Object select( final Object hint )
throws ServiceException
{
final Object component = m_dataSources.get( hint );
if( null == component )
{
throw new ServiceException( "Unable to provide DataSourceComponent for "
+ hint );
}
return component;
}
public void release( final Object component )
{
//do nothing
}
}
1.1
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/source/DefaultDataSourceSelector-schema.xml
Index: DefaultDataSourceSelector-schema.xml
===================================================================
<?xml version="1.0"?>
<grammar
xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
xmlns:a="http://jakarta.apache.org/phoenix/schema-annotations/1.0"
>
<start>
<element name="root">
<element name="data-sources">
<oneOrMore>
<ref name="datasource"/>
</oneOrMore>
</element>
<oneOrMore>
<ref name="datasource"/>
</oneOrMore>
</element>
</start>
<define name="datasource" combine="choice">
<a:description>JDBC Data Source</a:description>
<element name="data-source">
<attribute name="name"/>
<attribute
name="class"><value>org.apache.avalon.excalibur.datasource.JdbcDataSource</value></attribute>
<interleave>
<element name="pool-controller">
<attribute name="min">
<data type="integer">
<param name="minInclusive">1</param>
</data>
</attribute>
<attribute name="max">
<data type="integer">
<param name="minInclusive">1</param>
</data>
</attribute>
<optional>
<attribute name="connection-class"/>
<element name="keep-alive"><text/></element>
</optional>
</element>
<element name="auto-commit"><data type="boolean"/></element>
<element name="driver"><text/></element>
<element name="dburl"><text/></element>
<optional>
<element name="user"><text/></element>
<element name="password"><text/></element>
</optional>
</interleave>
</element>
</define>
</grammar>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>