/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2008 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.extensions.datasources.olap4j.connections;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Properties;
import org.olap4j.OlapConnection;
import org.olap4j.OlapWrapper;
/**
* Creation-Date: 07.04.2006, 15:04:26
*
* @author Thomas Morgner
*/
public class DriverConnectionProvider implements OlapConnectionProvider
{
private Properties properties;
private String url;
private String driver;
public DriverConnectionProvider()
{
this.properties = new Properties();
}
public String getProperty(final String key)
{
return properties.getProperty(key);
}
public Object setProperty(final String key, final String value)
{
if (value == null)
{
return properties.remove(key);
}
else
{
return properties.setProperty(key, value);
}
}
public String getUrl()
{
return url;
}
public void setUrl(final String url)
{
this.url = url;
}
public String getDriver()
{
return driver;
}
public void setDriver(final String driver)
{
this.driver = driver;
}
public OlapConnection createConnection(final String user, final String password) throws SQLException
{
if (url == null)
{
throw new NullPointerException("URL must not be null when connecting"); //$NON-NLS-1$
}
try
{
if (driver != null)
{
Class.forName(driver);
}
}
catch (Throwable e)
{
throw new SQLException("Unable to load the driver: " + driver, e.getMessage()); //$NON-NLS-1$
}
final Properties p = (Properties) properties.clone();
if (user != null)
{
p.setProperty("user", user);
}
if (password != null)
{
p.setProperty("password", password);
}
final Connection connection = DriverManager.getConnection(url, p);
if (connection instanceof OlapConnection)
{
return (OlapConnection) connection;
}
if (connection instanceof OlapWrapper)
{
final OlapWrapper wrapper = (OlapWrapper) connection;
return wrapper.unwrap(OlapConnection.class);
}
throw new SQLException("Unable to unwrap the connection: " + driver); //$NON-NLS-1$
}
public String[] getPropertyNames()
{
return properties.keySet().toArray(new String[properties.size()]);
}
public Object getConnectionHash()
{
final ArrayList<Object> list = new ArrayList<Object>();
list.add(getClass().getName());
list.add(properties.clone());
list.add(url);
list.add(driver);
return list;
}
public boolean equals(final Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
final DriverConnectionProvider that = (DriverConnectionProvider) o;
if (driver != null ? !driver.equals(that.driver) : that.driver != null)
{
return false;
}
if (properties.equals(that.properties))
{
return false;
}
if (url != null ? !url.equals(that.url) : that.url != null)
{
return false;
}
return true;
}
public int hashCode()
{
int result;
result = (properties != null ? properties.hashCode() : 0);
result = 31 * result + (url != null ? url.hashCode() : 0);
result = 31 * result + (driver != null ? driver.hashCode() : 0);
return result;
}
}