001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.core.scheduler;
018
019import java.sql.Connection;
020import java.sql.SQLException;
021
022import org.nuxeo.runtime.datasource.ConnectionHelper;
023import org.quartz.utils.ConnectionProvider;
024
025/**
026 * Quartz Connection Provider delegating to the Nuxeo datasource framework.
027 *
028 * @since 7.1
029 */
030public class NuxeoQuartzConnectionProvider implements ConnectionProvider {
031
032    protected String dataSourceName;
033
034    protected Connection connection;
035
036    /**
037     * Called by reflection from StdSchedulerFactory.setBeanProps.
038     *
039     * @param jndiURL the JNDI URL from the Quartz configuration
040     */
041    public void setJndiURL(String jndiURL) {
042        int i = jndiURL.lastIndexOf('/');
043        if (i != -1) {
044            dataSourceName = jndiURL.substring(i + 1);
045        } else {
046            dataSourceName = jndiURL;
047        }
048    }
049
050    @Override
051    public Connection getConnection() throws SQLException {
052        connection = ConnectionHelper.getConnection(dataSourceName);
053        return connection;
054    }
055
056    @Override
057    public void shutdown() throws SQLException {
058        connection.close();
059    }
060
061    /**
062     * @since 7.10
063     */
064    @Override
065    public void initialize() throws SQLException {
066        // do nothing
067    }
068
069}