KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ojb > components > ConnectionFactoryImpl


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

16 package org.apache.cocoon.ojb.components;
17
18 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
19 import org.apache.avalon.framework.CascadingRuntimeException;
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.ServiceSelector;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27
28 import org.apache.ojb.broker.accesslayer.ConnectionFactory;
29 import org.apache.ojb.broker.accesslayer.LookupException;
30 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
31
32 import java.sql.Connection JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 /**
36  * OJB ConnectionFactory implemenation to bridge into the Avalon DataSource
37  * connection pooling component defined in the Cocoon configuration.
38  *
39  * <p>This class has two faces to it:
40  * <dl>
41  * <dt>Avalon Component</dt>
42  * <dd>Instance of the class created and managed by Avalon container.
43  * When instance is initialized, it looks up datasource components
44  * service selector.</dd>
45  * <dt>OJB Managed Class</dt>
46  * <dd>Instances of the class are created and managed by OJB, as defined
47  * in the OJB <code>repository.xml</code> file. Each OJB managed instance
48  * of the class will have access to the datasource components service
49  * selector initialized by Avalon managed instance of the class.</dd>
50  * </dl>
51  *
52  * It is important that Avalon component is initialized before any access
53  * to OJB API is made.</p>
54  *
55  * @author giacomo at apache.org
56  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
57  * @version $Id: ConnectionFactoryImpl.java 325970 2005-10-17 19:48:08Z vgritsenko $
58  */

59 public class ConnectionFactoryImpl implements Component, ThreadSafe, Serviceable, Disposable,
60                                               ConnectionFactory {
61
62     /** The <code>ServiceManager</code> to be used */
63     private static ServiceManager manager;
64
65     /** The <code>ServiceSelector</code> to be used */
66     private static ServiceSelector datasources;
67
68     /** The <code>JdbcConnectionDescriptor</code> */
69     private JdbcConnectionDescriptor conDesc;
70
71     /**
72      * Default constructor
73      */

74     public ConnectionFactoryImpl() {
75     }
76
77     /**
78      * OJB 1.1 constructor
79      */

80     public ConnectionFactoryImpl(JdbcConnectionDescriptor conDesc) {
81         this.conDesc = conDesc;
82     }
83
84     public void service(ServiceManager manager) throws ServiceException {
85         ConnectionFactoryImpl.manager = manager;
86         ConnectionFactoryImpl.datasources = (ServiceSelector) manager.lookup(DataSourceComponent.ROLE + "Selector");
87     }
88
89     public void dispose() {
90         if (ConnectionFactoryImpl.manager != null) {
91             ConnectionFactoryImpl.manager.release(ConnectionFactoryImpl.datasources);
92             ConnectionFactoryImpl.datasources = null;
93             ConnectionFactoryImpl.manager = null;
94         }
95     }
96
97     //
98
// OJB 1.1 ConnectionFactory Implementation
99
//
100

101     public Connection JavaDoc lookupConnection()
102     throws LookupException {
103         return lookupConnection(this.conDesc);
104     }
105
106     public void releaseConnection(Connection JavaDoc connection) {
107         releaseConnection(this.conDesc, connection);
108     }
109
110     public int getActiveConnections() {
111         return 0;
112     }
113
114     public int getIdleConnections() {
115         return 0;
116     }
117
118     //
119
// OJB 1.0 ConnectionFactory Implementation
120
//
121

122     public Connection JavaDoc lookupConnection(final JdbcConnectionDescriptor conDesc)
123     throws LookupException {
124         if (ConnectionFactoryImpl.manager == null) {
125             throw new LookupException("ConnectionFactoryImpl is not initialized! Please check your cocoon.xconf");
126         }
127
128         try {
129             return ((DataSourceComponent) ConnectionFactoryImpl.datasources.select(conDesc.getJcdAlias())).getConnection();
130         } catch (final ServiceException e) {
131             throw new LookupException("Cannot lookup DataSource " +
132                                       conDesc.getJcdAlias(), e);
133         } catch (final SQLException JavaDoc e) {
134             throw new LookupException("Cannot get connection from DataSource " +
135                                       conDesc.getJcdAlias(), e);
136         }
137     }
138
139     public void releaseConnection(JdbcConnectionDescriptor conDesc, Connection JavaDoc connection) {
140         try {
141             // The DataSource of this connection will take care of pooling
142
connection.close();
143         } catch (final SQLException JavaDoc e) {
144             // This should not happen, but in case
145
throw new CascadingRuntimeException("Cannot release SQL Connection to DataSource", e);
146         }
147     }
148
149     public void releaseAllResources() {
150         // Nothing to do here
151
}
152 }
153
Popular Tags