001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2002-2005 Julian Hyde
008// Copyright (C) 2005-2010 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.olap;
012
013import mondrian.rolap.RolapConnection;
014import mondrian.rolap.RolapConnectionProperties;
015import mondrian.spi.CatalogLocator;
016
017import javax.sql.DataSource;
018
019/**
020 * The basic service for managing a set of OLAP drivers.
021 *
022 * @author jhyde
023 * @since 15 January, 2002
024 */
025public class DriverManager {
026
027    public DriverManager() {
028    }
029
030    /**
031     * Creates a connection to a Mondrian OLAP Engine
032     * using a connect string
033     * and a catalog locator.
034     *
035     * @param connectString Connect string of the form
036     *   'property=value;property=value;...'.
037     *   See {@link mondrian.olap.Util#parseConnectString} for more details of the format.
038     *   See {@link mondrian.rolap.RolapConnectionProperties} for a list of
039     *   allowed properties.
040     * @param locator Use to locate real catalog url by a customized
041     *   configuration value. If <code>null</code>, leave the catalog url
042     *   unchanged.
043     * @return A {@link Connection}, never null
044     */
045    public static Connection getConnection(
046        String connectString,
047        CatalogLocator locator)
048    {
049        Util.PropertyList properties = Util.parseConnectString(connectString);
050        return getConnection(properties, locator);
051    }
052
053    /**
054     * Creates a connection to a Mondrian OLAP Engine.
055     *
056     * @param properties Collection of properties which define the location
057     *   of the connection.
058     *   See {@link mondrian.rolap.RolapConnection} for a list of allowed properties.
059     * @param locator Use to locate real catalog url by a customized
060     *   configuration value. If <code>null</code>, leave the catalog url
061     *   unchanged.
062     * @return A {@link Connection}, never null
063     */
064    public static Connection getConnection(
065        Util.PropertyList properties,
066        CatalogLocator locator)
067    {
068        return getConnection(properties, locator, null);
069    }
070
071    /**
072     * Creates a connection to a Mondrian OLAP Engine
073     * using a list of connection properties,
074     * a catalog locator,
075     * and a JDBC data source.
076     *
077     * @param properties Collection of properties which define the location
078     *   of the connection.
079     *   See {@link mondrian.rolap.RolapConnection} for a list of allowed properties.
080     * @param locator Use to locate real catalog url by a customized
081     *   configuration value. If <code>null</code>, leave the catalog url
082     *   unchanged.
083     * @param dataSource - if not null an external DataSource to be used
084     *        by Mondrian
085     * @return A {@link Connection}, never null
086     */
087    public static Connection getConnection(
088        Util.PropertyList properties,
089        CatalogLocator locator,
090        DataSource dataSource)
091    {
092        String provider = properties.get("PROVIDER", "mondrian");
093        if (!provider.equalsIgnoreCase("mondrian")) {
094            throw Util.newError("Provider not recognized: " + provider);
095        }
096        final String instance =
097            properties.get(RolapConnectionProperties.Instance.name());
098        MondrianServer server = MondrianServer.forId(instance);
099        if (server == null) {
100            throw Util.newError("Unknown server instance: " + instance);
101        }
102        if (locator == null) {
103            locator = server.getCatalogLocator();
104        }
105        if (locator != null) {
106            String catalog = properties.get(
107                RolapConnectionProperties.Catalog.name());
108            properties.put(
109                RolapConnectionProperties.Catalog.name(),
110                locator.locate(catalog));
111        }
112        final RolapConnection connection =
113            new RolapConnection(server, properties, dataSource);
114        server.addConnection(connection);
115        return connection;
116    }
117}
118
119// End DriverManager.java