Package org.pentaho.reporting.engine.classic.extensions.datasources.xquery

Source Code of org.pentaho.reporting.engine.classic.extensions.datasources.xquery.DriverXQConnectionProvider

/*
* 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) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
*/

package org.pentaho.reporting.engine.classic.extensions.datasources.xquery;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import java.util.Properties;

/**
* @author Thomas Morgner
* @author Cedric Pronzato
*/
public class DriverXQConnectionProvider implements XQConnectionProvider
{
  private static final Log logger = LogFactory.getLog(DriverXQConnectionProvider.class);

  private Properties properties;
  private String url;
  private String driver;
  private String xqdatasource;

  public DriverXQConnectionProvider()
  {
    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;
  }

  /**
   * Returns the XQJ datasource implementation class name which must extends {@link javax.xml.xquery.XQDataSource}.
   *
   * @return The class name.
   */
  public String getXqdatasource()
  {
    return xqdatasource;
  }

  /**
   * Sets the XQJ datasource implementation class name which must extends {@link javax.xml.xquery.XQDataSource}.
   *
   * @param xqdatasource The XQJ implementation class name.
   */
  public void setXqdatasource(String xqdatasource)
  {
    this.xqdatasource = xqdatasource;
  }

  public XQConnection getConnection() throws XQException
  {
    /*if (url == null)
    {
      throw new NullPointerException("URL must not be null when connecting"); //$NON-NLS-1$
    }

    try
    {
      if (driver != null)
      {
        Class.forName(driver);
      }
    }
    catch (Exception e)
    {
      logger.error("Unable to load JDBC driver", e);  //$NON-NLS-1$
      return null;
    } */
    if (driver != null)
    {
      throw new IllegalArgumentException("The jdbc connection is not yet supported");
    }

    if (xqdatasource != null)
    {
      final XQDataSource datasource = (XQDataSource) ObjectUtilities.loadAndInstantiate(xqdatasource, DriverXQConnectionProvider.class, XQDataSource.class);
      if (datasource == null)
      {
        throw new IllegalArgumentException("Unable to load XQJ datasource driver: " + xqdatasource);
      }
      else
      {
        datasource.setProperties(properties);
        final XQConnection xqConnection = datasource.getConnection();
        return xqConnection;
        /*try
        {
          final Connection sqlConnection = DriverManager.getConnection(url, properties);
          datasource.setProperties(properties);
          return datasource.getConnection(sqlConnection);
        }
        catch (SQLException e)
        {
          logger.error("Unable to establish the jdbc connection", e); //$NON-NLS-1$
          return null;
        } */
      }

    }
    else
    {
      throw new IllegalArgumentException("The xqdatasource must not be null");
    }

  }

  public String[] getPropertyNames()
  {
    return (String[]) properties.keySet().toArray(new String[properties.size()]);
  }

  public Properties getProperties()
  {
    return properties;
  }

  public void setProperties(Properties properties)
  {
    this.properties = properties;
  }

  public boolean equals(final Object o)
  {
    if (this == o)
    {
      return true;
    }
    if (o == null || getClass() != o.getClass())
    {
      return false;
    }

    final DriverXQConnectionProvider that = (DriverXQConnectionProvider) o;

    if (xqdatasource != null ? !xqdatasource.equals(that.xqdatasource) : that.xqdatasource != null)
    {
      return false;
    }
    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;
  }
}
TOP

Related Classes of org.pentaho.reporting.engine.classic.extensions.datasources.xquery.DriverXQConnectionProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.