All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudgraph.bigtable.connect.BigTablePooledConnectionFactory Maven / Gradle / Ivy

Go to download

CloudGraph(tm) is a suite of Service Data Object (SDO) 2.1 services designed for relational and big-table style "cloud" databases, such as HBase and others.

The newest version!
/**
 * Copyright 2017 TerraMeta Software, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cloudgraph.bigtable.connect;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.cloudgraph.common.CloudGraphRuntimeException;
import org.cloudgraph.core.Connection;

import com.google.cloud.bigtable.hbase.BigtableConfiguration;

/**
 * Connection factory which creates pooled connections.
 * 
 * @author Scott Cinnamond
 * @since 2.0.1
 */
public class BigTablePooledConnectionFactory extends BasePooledObjectFactory {
  private static Log log = LogFactory.getLog(BigTablePooledConnectionFactory.class);

  private Configuration config;
  private ObjectPool pool;

  public BigTablePooledConnectionFactory(Configuration config) {
    super();
    this.config = config;
  }

  public void setPool(ObjectPool pool) {
    this.pool = pool;
  }

  @Override
  public Connection create() throws Exception {
    String project = this.config.get(BigTableConnectionManager.CONNECTION_FACTORY_PROJECT);
    if (project == null || project.trim().length() == 0)
      throw new CloudGraphRuntimeException("expected mandatory configuration property '"
          + BigTableConnectionManager.CONNECTION_FACTORY_PROJECT + "'");
    String instance = this.config.get(BigTableConnectionManager.CONNECTION_FACTORY_INSTANCE);
    if (instance == null || instance.trim().length() == 0)
      throw new CloudGraphRuntimeException("expected mandatory configuration property '"
          + BigTableConnectionManager.CONNECTION_FACTORY_INSTANCE + "'");
    String appProfile = this.config.get(BigTableConnectionManager.CONNECTION_FACTORY_APP_PROFILE);
    org.apache.hadoop.hbase.client.Connection con = null;
    if (appProfile != null) {
      con = BigtableConfiguration.connect(project, instance, appProfile);
      if (log.isDebugEnabled())
        log.debug("created new bigtable connection  " + con + " for project/instance/profile: "
            + project + "/" + instance + "/" + appProfile);
    } else {
      con = BigtableConfiguration.connect(project, instance);
      if (log.isDebugEnabled())
        log.debug("created new bigtable connection  " + con + " for project/instance: " + project
            + "/" + instance);
    }

    return new BigTableConnection(con, this.pool, this.config);
  }

  @Override
  public PooledObject wrap(Connection con) {
    return new DefaultPooledObject(con);
  }

  @Override
  public void destroyObject(PooledObject p) throws Exception {
    if (log.isDebugEnabled())
      log.debug("destroying connection " + p.getObject());
    p.getObject().destroy();
    super.destroyObject(p);

  }

  @Override
  public boolean validateObject(PooledObject p) {
    if (log.isDebugEnabled())
      log.debug("validating connection " + p.getObject());
    return super.validateObject(p);
  }

  @Override
  public void activateObject(PooledObject p) throws Exception {
    if (log.isDebugEnabled())
      log.debug("activate connection " + p.getObject());
    super.activateObject(p);
  }

  @Override
  public void passivateObject(PooledObject p) throws Exception {
    if (log.isDebugEnabled())
      log.debug("passivate connection " + p.getObject());
    super.passivateObject(p);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy