KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > datasources > DefaultDataSourceSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.blocks.datasources;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
39  * A default implementation for DataSourceSelector.
40  * The Configuration is like this:
41  *
42  * <pre>
43  * &lt;myBlock&gt;
44  * &lt;data-source name="<i>default</i>"
45  * class="<i>org.apache.avalon.excalibur.datasource.JdbcDataSource</i>"&gt;
46  * &lt;!-- configuration for JdbcDataSource --&gt;
47  * &lt;pool-controller min="<i>5</i>" max="<i>10</i>" connection-class="<i>my.overrided.ConnectionClass</i>"&gt;
48  * &lt;keep-alive&gt;select 1&lt;/keep-alive&gt;
49  * &lt;/pool-controller&gt;
50  * &lt;driver&gt;<i>com.database.jdbc.JdbcDriver</i>&lt;/driver&gt;
51  * &lt;dburl&gt;<i>jdbc:driver://host/mydb</i>&lt;/dburl&gt;
52  * &lt;user&gt;<i>username</i>&lt;/user&gt;
53  * &lt;password&gt;<i>password</i>&lt;/password&gt;
54  * &lt;/data-source&gt;
55  * &lt;/myBlock&gt;
56  * </pre>
57  *
58  * @author <a HREF="mailto:avalon-dev@jakarta.apache.org">Avalon Development Team</a>
59  * @avalon.component name="data-source-selector" lifestyle="singleton"
60  * @avalon.service type="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector"
61  */

62 public class DefaultDataSourceSelector
63     extends AbstractLogEnabled
64     implements DataSourceSelector, Contextualizable, Configurable, Initializable, Disposable
65 {
66     private Configuration m_configuration;
67     private Map JavaDoc m_dataSources;
68     private String JavaDoc m_blockName;
69
70
71    /**
72     * Contextualization of the component by the container.
73     * @param context the supplied context object
74     * @avalon.entry key="urn:avalon:name" alias="block.name"
75     */

76     public void contextualize( final Context context )
77         throws ContextException
78     {
79         try
80         {
81             m_blockName = (String JavaDoc) context.get( "urn:avalon:name" );
82         }
83         catch( Throwable JavaDoc e )
84         {
85             // handle legacy scenario
86
try
87             {
88                 m_blockName = (String JavaDoc)context.get( "block.name" );
89             }
90             catch( Throwable JavaDoc ee )
91             {
92                 m_blockName = "DataSourceSelector/" + System.identityHashCode( this );
93             }
94         }
95     }
96
97     /**
98      * @avalon.configuration schema="relax-ng"
99      */

100     public void configure( final Configuration configuration )
101     {
102         m_configuration = configuration;
103     }
104
105     public void initialize()
106         throws Exception JavaDoc
107     {
108         m_dataSources = new HashMap JavaDoc();
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 JavaDoc name = dataSourceConf.getAttribute( "name" );
117             final String JavaDoc clazz = dataSourceConf.getAttribute( "class" );
118             final String JavaDoc driver = dataSourceConf.getChild( "driver", true ).getValue( "" );
119
120             final ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc hint )
198     {
199         return m_dataSources.containsKey( hint );
200     }
201
202     public Object JavaDoc select( final Object JavaDoc hint )
203         throws ServiceException
204     {
205         final Object JavaDoc 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 JavaDoc component )
216     {
217         //do nothing
218
}
219 }
220
Popular Tags