KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > cluster > AbstractDataSourceCluster


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

19
20 package org.apache.avalon.excalibur.datasource.cluster;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
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 /**
40  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
41  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
42  * @since 4.1
43  */

44 public abstract class AbstractDataSourceCluster
45     extends AbstractLogEnabled
46     implements Serviceable, Configurable, Initializable, Disposable, ThreadSafe
47 {
48     protected int m_size;
49     private String JavaDoc[] m_dataSourceNames;
50     private ServiceSelector m_dbSelector;
51     private DataSourceComponent[] m_dataSources;
52
53     /*---------------------------------------------------------------
54      * Constructors
55      *-------------------------------------------------------------*/

56     public AbstractDataSourceCluster()
57     {
58     }
59
60     /*---------------------------------------------------------------
61      * AbstractDataSourceCluster Methods
62      *-------------------------------------------------------------*/

63     /**
64      * Returns the number of DataSources in the cluster.
65      *
66      * @return size of the cluster.
67      */

68     public int getClusterSize()
69     {
70         return m_size;
71     }
72
73     /**
74      * Gets a Connection to a database given an index.
75      *
76      * @param index Index of the DataSource for which a connection is to be returned.
77      *
78      * @throws NoValidConnectionException when there is no valid Connection wrapper
79      * available in the classloader or when the index is not valid.
80      *
81      * @throws NoValidConnectionException when there are no more available
82      * Connections in the pool.
83      */

84     public Connection JavaDoc getConnectionForIndex( int index ) throws SQLException JavaDoc
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     /*---------------------------------------------------------------
95      * Composable Methods
96      *-------------------------------------------------------------*/

97     /**
98      * Called by the Container to tell the component which ComponentLocator
99      * is controlling it.
100      *
101      * @param manager which curently owns the component.
102      * @avalon.dependency type="org.apache.avalon.excalibur.datasource.DataSourceComponent"
103      */

104     public void service( final ServiceManager manager )
105         throws ServiceException
106     {
107         m_dbSelector =
108             (ServiceSelector)manager.lookup( DataSourceComponent.ROLE + "Selector" );
109     }
110
111     /*---------------------------------------------------------------
112      * Configurable Methods
113      *-------------------------------------------------------------*/

114     /**
115      * Called by the Container to configure the component.
116      *
117      * @param configuration configuration info used to setup the component.
118      *
119      * @throws ConfigurationException if there are any problems with the configuration.
120      */

121     public void configure( Configuration configuration ) throws ConfigurationException
122     {
123         // Get the size
124
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         // Read in the data source names.
132
m_dataSourceNames = new String JavaDoc[ 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         // Make sure that all of the dbpools were defined
151
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     /*---------------------------------------------------------------
161      * Initializable Methods
162      *-------------------------------------------------------------*/

163     /**
164      * Called by the Container to initialize the component.
165      *
166      * @throws Exception if there were any problems durring initialization.
167      */

168     public void initialize() throws Exception JavaDoc
169     {
170         // Get references to a data sources
171
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     /*---------------------------------------------------------------
179      * Disposable Methods
180      *-------------------------------------------------------------*/

181     /**
182      * Called by the Container to dispose the component.
183      */

184     public void dispose()
185     {
186         // Free up the data source
187
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