com.senseidb.gateway.jdbc.JdbcDataProviderBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sensei-gateways Show documentation
Show all versions of sensei-gateways Show documentation
a collection of sensei gateways
/**
* This software is licensed to you under the Apache License, Version 2.0 (the
* "Apache License").
*
* LinkedIn's contributions are made under the Apache License. If you contribute
* to the Software, the contributions will be deemed to have been made under the
* Apache License, unless you expressly indicate otherwise. Please do not make any
* contributions that would be inconsistent with the Apache License.
*
* You may obtain a copy of the Apache License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, this software
* distributed under the Apache License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache
* License for the specific language governing permissions and limitations for the
* software governed under the Apache License.
*
* © 2012 LinkedIn Corp. All Rights Reserved.
*/
package com.senseidb.gateway.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.Set;
import javax.naming.ConfigurationException;
import org.json.JSONObject;
import proj.zoie.api.DataConsumer.DataEvent;
import proj.zoie.dataprovider.jdbc.JDBCConnectionFactory;
import proj.zoie.dataprovider.jdbc.JDBCStreamDataProvider;
import proj.zoie.dataprovider.jdbc.PreparedStatementBuilder;
import proj.zoie.impl.indexing.StreamDataProvider;
import proj.zoie.impl.indexing.ZoieConfig;
import com.senseidb.gateway.SenseiGateway;
import com.senseidb.indexing.DataSourceFilter;
import com.senseidb.indexing.ShardingStrategy;
public class JdbcDataProviderBuilder extends SenseiGateway{
private Comparator _versionComparator;
@Override
public void start() {
_versionComparator = pluginRegistry.getBeanByName("versionComparator", Comparator.class);
if (_versionComparator == null) _versionComparator = ZoieConfig.DEFAULT_VERSION_COMPARATOR;
}
@Override
public StreamDataProvider buildDataProvider(final DataSourceFilter dataFilter,
String oldSinceKey,
ShardingStrategy shardingStrategy,
Set partitions) throws Exception
{
final String url = config.get("jdbc.url");
final String username = config.get("jdbc.username");
final String password = config.get("jdbc.password");
final String driver = config.get("jdbc.driver");
final String adaptor = config.get("jdbc.adaptor");
final SenseiJDBCAdaptor senseiAdaptor =
pluginRegistry.getBeanByName(adaptor, SenseiJDBCAdaptor.class) != null ?
pluginRegistry.getBeanByName(adaptor, SenseiJDBCAdaptor.class) :
pluginRegistry.getBeanByFullPrefix(adaptor, SenseiJDBCAdaptor.class);
if (senseiAdaptor==null){
throw new ConfigurationException("adaptor not found: " + adaptor);
}
JDBCConnectionFactory connFactory = new JDBCConnectionFactory() {
private Connection _conn = null;
@Override
public void showndown() throws SQLException {
if (_conn!=null){
_conn.close();
}
}
@Override
public Connection getConnection() throws SQLException {
if (_conn == null){
try {
Class.forName (driver).newInstance ();
} catch (Exception e) {
throw new SQLException("unable to load driver: "+e.getMessage());
}
_conn = DriverManager.getConnection (url, username, password);
}
return _conn;
}
};
PreparedStatementBuilder stmtBuilder = new PreparedStatementBuilder() {
private final DataSourceFilter filter = dataFilter;
@Override
public PreparedStatement buildStatment(Connection conn,
String fromVersion) throws SQLException {
return senseiAdaptor.buildStatment(conn, fromVersion);
}
@Override
public DataEvent buildDataEvent(ResultSet rs)
throws SQLException {
try{
JSONObject jsonObject = filter.filter(rs);
return new DataEvent(jsonObject, senseiAdaptor.extractVersion(rs));
}
catch(Exception e){
throw new SQLException(e.getMessage(),e);
}
}
};
return new JDBCStreamDataProvider(connFactory, stmtBuilder, _versionComparator);
}
@Override
public Comparator getVersionComparator() {
return _versionComparator;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy