KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > cron > DataSourceComponentConnectionProvider


1 /*
2  * Copyright 1999-2004 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.components.cron;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.ServiceSelector;
26 import org.quartz.utils.ConnectionProvider;
27
28 /**
29  * Quartz database connection provider that uses the
30  * Excalibur DataSourceComponent service.
31  */

32 public class DataSourceComponentConnectionProvider implements ConnectionProvider {
33
34     private ServiceManager m_manager;
35     private ServiceSelector m_datasources;
36     private DataSourceComponent m_ds;
37
38     public DataSourceComponentConnectionProvider(String JavaDoc dsName, ServiceManager manager) throws ConfigurationException {
39         m_manager = manager;
40         try {
41              m_datasources = (ServiceSelector) m_manager.lookup(DataSourceComponent.ROLE + "Selector");
42              m_ds = (DataSourceComponent) m_datasources.select(dsName);
43         }
44         catch (ServiceException e) {
45             throw new ConfigurationException("No datasource available by that name: " + dsName);
46         }
47     }
48
49     /*
50      * @see org.quartz.utils.ConnectionProvider#getConnection()
51      */

52     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
53         return m_ds.getConnection();
54     }
55
56     /*
57      * @see org.quartz.utils.ConnectionProvider#shutdown()
58      */

59     public void shutdown() throws SQLException JavaDoc {
60         if (m_ds != null) {
61             m_datasources.release(m_ds);
62         }
63         if (m_datasources != null) {
64             m_manager.release(m_datasources);
65         }
66         m_ds = null;
67         m_datasources = null;
68         m_manager = null;
69     }
70
71 }
72
Popular Tags