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

de.zalando.sprocwrapper.proxy.SProcProxy Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.0.0
Show newest version
package de.zalando.sprocwrapper.proxy;

import java.lang.reflect.Method;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Objects;
import com.google.common.reflect.AbstractInvocationHandler;

import de.zalando.sprocwrapper.dsprovider.DataSourceProvider;

/**
 * @author  jmussler
 */
class SProcProxy extends AbstractInvocationHandler {

    private final HashMap sprocs = new HashMap();
    private final DataSourceProvider dataSourceProvider;

    private static final Logger LOG = LoggerFactory.getLogger(SProcProxy.class);
    private final String description;

    public boolean addStoredProcedure(final Method method, final StoredProcedure p) {
        if (sprocs.containsKey(method)) {
            return false;
        }

        sprocs.put(method, p);
        return true;
    }

    public SProcProxy(final DataSourceProvider provider, final String description) {
        if (provider == null) {
            throw new IllegalArgumentException("DataSourceProvider cannot be null");
        }

        dataSourceProvider = provider;
        this.description = description;
    }

    @Override
    protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) {
        final StoredProcedure p = sprocs.get(method);

        if (p == null) {
            LOG.warn("no StoredProcedure found for method {}", method);
            return null;
        }

        return p.execute(dataSourceProvider, new InvocationContext(proxy, method, args));
    }

    @Override
    public String toString() {
        return
            Objects.toStringHelper(this)                          //
                   .addValue(description)                         //
                   .add("dataSourceProvider", dataSourceProvider) //
                   .toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy