001/*
002 * (C) Copyright 2006-2013 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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.runtime.datasource;
022
023import javax.naming.NamingException;
024import javax.sql.DataSource;
025import javax.sql.XADataSource;
026
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.jtajca.NuxeoContainer;
029
030/**
031 * Helper class to look up {@link DataSource}s without having to deal with vendor-specific JNDI prefixes.
032 *
033 * @author Thierry Delprat
034 * @author Florent Guillaume
035 */
036public class DataSourceHelper {
037
038    private DataSourceHelper() {
039    }
040
041    /**
042     * Look up a datasource JNDI name given a partial name.
043     * <p>
044     * For a datasource {@code "jdbc/foo"}, then it's sufficient to pass {@code "foo"} to this method.
045     *
046     * @return the datasource JNDI name
047     */
048    public static String getDataSourceJNDIName(String name) {
049        return NuxeoContainer.nameOf(relativize(name));
050    }
051
052    protected static String relativize(String name) {
053        int idx = name.lastIndexOf("/");
054        if (idx > 0) {
055            name = name.substring(idx + 1);
056        }
057        return "jdbc/".concat(name);
058    }
059
060    /**
061     * Look up a datasource given a partial name.
062     * <p>
063     * For a datasource {@code "jdbc/foo"}, then it's sufficient to pass {@code "foo"} to this method.
064     *
065     * @param partialName the partial name
066     * @return the datasource
067     */
068    public static DataSource getDataSource(String partialName) throws NamingException {
069        return getDataSource(partialName, DataSource.class, false);
070    }
071
072    /**
073     * Look up a datasource given a partial name.
074     * <p>
075     * For a datasource {@code "jdbc/foo"}, then it's sufficient to pass {@code "foo"} to this method.
076     *
077     * @param partialName the partial name
078     * @param noSharing {@code true} to request an unshared datasource
079     * @return the datasource
080     */
081    public static DataSource getDataSource(String partialName, boolean noSharing) throws NamingException {
082        return getDataSource(partialName, DataSource.class, noSharing);
083    }
084
085    public static <T> T getDataSource(String name, Class<T> clazz) throws NamingException {
086        return getDataSource(name, clazz, false);
087    }
088
089    public static <T> T getDataSource(String name, Class<T> clazz, boolean noSharing) throws NamingException {
090        PooledDataSourceRegistry pools = Framework.getService(PooledDataSourceRegistry.class);
091        if (pools == null) {
092            throw new NamingException("runtime datasource no installed");
093        }
094        T ds = pools.getDataSource(relativize(name), clazz, noSharing);
095        if (ds == null) {
096            return NuxeoContainer.lookup(name, clazz);
097        }
098        return ds;
099    }
100
101}