KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > ids > AbstractDataSourceIdGenerator


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.ids;
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.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 /**
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:17 $
39  * @since 4.1
40  */

41 public abstract class AbstractDataSourceIdGenerator
42     extends AbstractIdGenerator
43     implements Serviceable, Configurable, Initializable, Disposable
44 {
45     private String JavaDoc m_dataSourceName;
46     private ServiceSelector m_dbSelector;
47     protected DataSourceComponent m_dataSource;
48
49     /**
50      * Number of allocated Ids remaining before another block must be allocated.
51      */

52     protected int m_allocated;
53     protected long m_nextId;
54
55     /*---------------------------------------------------------------
56      * Constructors
57      *-------------------------------------------------------------*/

58     public AbstractDataSourceIdGenerator()
59     {
60     }
61
62     /*---------------------------------------------------------------
63      * Methods
64      *-------------------------------------------------------------*/

65     /**
66      * Allocates a connection for the caller. The connection must be closed by the caller
67      * when no longer needed.
68      *
69      * @return an open DB connection.
70      *
71      * @throws SQLException if the connection can not be obtained for any reason.
72      */

73     protected Connection JavaDoc getConnection()
74         throws SQLException JavaDoc
75     {
76         return m_dataSource.getConnection();
77     }
78
79     /*---------------------------------------------------------------
80      * Composable Methods
81      *-------------------------------------------------------------*/

82     /**
83      * Called by the Container to tell the component which ComponentLocator
84      * is controlling it.
85      *
86      * @param manager which curently owns the component.
87      * @avalon.dependency type="org.apache.avalon.excalibur.datasource.DataSourceComponent"
88      */

89     public void service( final ServiceManager manager )
90         throws ServiceException
91     {
92         m_dbSelector = (ServiceSelector)manager.lookup( DataSourceComponent.ROLE + "Selector" );
93     }
94
95     /*---------------------------------------------------------------
96      * Configurable Methods
97      *-------------------------------------------------------------*/

98     /**
99      * Called by the Container to configure the component.
100      *
101      * @param configuration configuration info used to setup the component.
102      *
103      * @throws ConfigurationException if there are any problems with the configuration.
104      */

105     public void configure( Configuration configuration )
106         throws ConfigurationException
107     {
108         // Obtain the big-decimals flag.
109
setUseBigDecimals( configuration.getAttributeAsBoolean( "big-decimals", false ) );
110
111         // Obtain a reference to the configured DataSource
112
m_dataSourceName = configuration.getChild( "dbpool" ).getValue();
113     }
114
115     /*---------------------------------------------------------------
116      * Initializable Methods
117      *-------------------------------------------------------------*/

118     /**
119      * Called by the Container to initialize the component.
120      *
121      * @throws Exception if there were any problems durring initialization.
122      */

123     public void initialize()
124         throws Exception JavaDoc
125     {
126         // Get a reference to a data source
127
m_dataSource = (DataSourceComponent)m_dbSelector.select( m_dataSourceName );
128     }
129
130     /*---------------------------------------------------------------
131      * Disposable Methods
132      *-------------------------------------------------------------*/

133     /**
134      * Called by the Container to dispose the component.
135      */

136     public void dispose()
137     {
138         // Free up the data source
139
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