de.zalando.sprocwrapper.proxy.SProcProxyBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zalando-sprocwrapper Show documentation
Show all versions of zalando-sprocwrapper Show documentation
Library to make PostgreSQL stored procedures available through simple Java "*SProcService" interfaces
including automatic object serialization and deserialization (using typemapper and
convention-over-configuration). Supports sharding, advisory locking, statement timeouts and PostgreSQL types
such as enums and hstore.
package de.zalando.sprocwrapper.proxy;
import java.lang.reflect.Method;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.zalando.sprocwrapper.dsprovider.DataSourceProvider;
/**
* @author jmussler
*/
public class SProcProxyBuilder {
private static SProcServiceAnnotationHandler sProcServiceAnnotationHandler = new SProcServiceAnnotationHandler();
private static SProcCallHandler sProcCallHandler = new SProcCallHandler();
private static final Logger LOG = LoggerFactory.getLogger(SProcProxyBuilder.class);
private SProcProxyBuilder() {
// utility class: private constructor
}
@SuppressWarnings("unchecked")
public static T build(final DataSourceProvider d, final Class c) {
final SProcProxy proxy = new SProcProxy(d, c.getName());
try {
SProcServiceAnnotationHandler.HandlerResult handlerResult = sProcServiceAnnotationHandler.handle(c);
Map spMap = sProcCallHandler.handle(c, handlerResult);
for (Method method : spMap.keySet()) {
StoredProcedure storedProcedure = spMap.get(method);
LOG.debug("{} registering {}", c.getSimpleName(), storedProcedure);
proxy.addStoredProcedure(method, storedProcedure);
}
} catch (IllegalArgumentException ex) {
return null;
}
return (T) java.lang.reflect.Proxy.newProxyInstance(c.getClassLoader(), new Class[]{c}, proxy);
}
}