001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.scheduler;
020
021import java.sql.Connection;
022import java.sql.SQLException;
023
024import org.nuxeo.runtime.datasource.ConnectionHelper;
025import org.quartz.utils.ConnectionProvider;
026
027/**
028 * Quartz Connection Provider delegating to the Nuxeo datasource framework.
029 *
030 * @since 7.1
031 */
032public class NuxeoQuartzConnectionProvider implements ConnectionProvider {
033
034    protected String dataSourceName;
035
036    protected Connection connection;
037
038    /**
039     * Called by reflection from StdSchedulerFactory.setBeanProps.
040     *
041     * @param jndiURL the JNDI URL from the Quartz configuration
042     */
043    public void setJndiURL(String jndiURL) {
044        int i = jndiURL.lastIndexOf('/');
045        if (i != -1) {
046            dataSourceName = jndiURL.substring(i + 1);
047        } else {
048            dataSourceName = jndiURL;
049        }
050    }
051
052    @Override
053    public Connection getConnection() throws SQLException {
054        connection = ConnectionHelper.getConnection(dataSourceName);
055        return connection;
056    }
057
058    @Override
059    public void shutdown() throws SQLException {
060        connection.close();
061    }
062
063    /**
064     * @since 7.10
065     */
066    @Override
067    public void initialize() throws SQLException {
068        // do nothing
069    }
070
071}