001/*
002 * (C) Copyright 2006-20011 Nuxeo SAS (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.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 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.reporting.datasource;
020
021import java.net.MalformedURLException;
022import java.net.URL;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * Helper class to find JDBC jar from the database type. Since BIRT used ODA to wrap JDBC we must provide the JDBC JAR.
030 * This class directly finds the target Class in the server ClassLoader and extract the associated Jar.
031 *
032 * @author Tiry (tdelprat@nuxeo.com)
033 */
034public class SupportedDBHelper {
035
036    protected static Map<String, String> driverMapping;
037
038    public static final String H2 = "h2";
039
040    public static final String PGSQL = "postgresql";
041
042    public static final String MSSQL = "mssql";
043
044    public static final String MYSQL = "mysql";
045
046    public static final String ORACLE = "oracle";
047
048    public static Map<String, String> getMapping() {
049        if (driverMapping == null) {
050            driverMapping = new HashMap<String, String>();
051            driverMapping.put(H2, "org.h2.Driver");
052            driverMapping.put(PGSQL, "org.postgresql.Driver");
053            driverMapping.put(MSSQL, "net.sourceforge.jtds.jdbc.Driver");
054            driverMapping.put(MYSQL, "com.mysql.jdbc.Driver");
055            driverMapping.put(ORACLE, "oracle.jdbc.OracleDriver");
056        }
057        return driverMapping;
058    }
059
060    public static String getDriver(String name) {
061        return getMapping().get(name);
062    }
063
064    public static URL getDriverJar(String name) throws MalformedURLException {
065        String javaName = getDriver(name);
066        String classPath = javaName.replace(".", "/");
067        classPath = classPath + ".class";
068
069        ClassLoader cl = Thread.currentThread().getContextClassLoader();
070        URL url = cl.getResource(classPath);
071        if (url == null) {
072            return null;
073        }
074        String protocol = url.getProtocol();
075        String file = url.getFile();
076        if ("vfszip".equals(protocol)) {
077            return new URL("vfszip:" + file.substring(0, file.length() - classPath.length() - 1));
078        } else if ("jar".equals(protocol)) {
079            return new URL(file.substring(0, file.length() - classPath.length() - 2));
080        } else {
081            throw new Error("Cannot loate jar location of '" + name + "' JDBC Driver, unsupported protocol '"
082                    + protocol + "'");
083        }
084    }
085
086    public static List<URL> getDriverJars() throws MalformedURLException {
087        List<URL> jars = new ArrayList<URL>();
088        for (String name : getMapping().keySet()) {
089            URL jar = getDriverJar(name);
090            if (jar != null) {
091                jars.add(jar);
092            }
093        }
094        return jars;
095    }
096
097}