com.datastax.driver.mapping.AccessorMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-driver-mapping Show documentation
Show all versions of cassandra-driver-mapping Show documentation
Object mapper for the Java Driver for YugaByte DB.
/*
* Copyright (C) 2012-2015 DataStax 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 com.datastax.driver.mapping;
import com.datastax.driver.core.PreparedStatement;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
class AccessorMapper {
final Class daoClass;
final List methods;
private final Class[] proxyClasses;
private final AccessorInvocationHandler handler;
@SuppressWarnings({"unchecked", "rawtypes"})
AccessorMapper(Class daoClass, List methods) {
this.daoClass = daoClass;
this.methods = methods;
this.proxyClasses = (Class[]) new Class[]{daoClass};
this.handler = new AccessorInvocationHandler(this);
}
@SuppressWarnings("unchecked")
T createProxy() {
try {
return (T) Proxy.newProxyInstance(daoClass.getClassLoader(), proxyClasses, handler);
} catch (Exception e) {
throw new RuntimeException("Cannot create instance for Accessor interface " + daoClass.getName());
}
}
void prepare(MappingManager manager) {
List> statements = new ArrayList>(methods.size());
for (MethodMapper method : methods)
statements.add(manager.getSession().prepareAsync(method.queryString));
try {
List preparedStatements = Futures.allAsList(statements).get();
for (int i = 0; i < methods.size(); i++)
methods.get(i).prepare(manager, preparedStatements.get(i));
} catch (Exception e) {
throw new RuntimeException("Error preparing queries for accessor " + daoClass.getSimpleName(), e);
}
}
}