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

com.sdklite.sphere.restful.ServiceStubFactory Maven / Gradle / Ivy

The newest version!
package com.sdklite.sphere.restful;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

/**
 * The dynamic proxy factory of {@link ServiceStub} sub interface
 *
 * @author johnsonlee
 * @since 1.0.0
 */
public class ServiceStubFactory {

    private Context context;

    /**
     * Create an instance with the specified context
     *
     * @param context
     *           The android context
     */
    public ServiceStubFactory(final Context context) {
        this.context = context;
    }

    /**
     * Create a proxy instance of the specified interface
     *
     * @param iface
     *           The interface that defined the stub RESTful APIs
     * @param baseUrl
     *           The base url of the RESTful service
     * @return a proxy instance of the specified interface
     */
    public  T create(final Class iface, final String baseUrl) {
        return this.create(iface, baseUrl, Collections.emptyMap());
    }

    /**
     * Create a proxy instance of the specified interface with additional HTTP headers
     *
     * @param iface
     *           The interface that defined the stub RESTful APIs
     * @param baseUrl
     *           The base url of the RESTful service
     * @return a proxy instance of the specified interface
     */
    public  T create(final Class iface, final String baseUrl, final Map headers) {
        final Map invocations = new HashMap();

        for (final Method method : iface.getMethods()) {
            invocations.put(method, new ServiceStubInvocation(this.context, iface, method, baseUrl, headers));
        }

        final ServiceStubProxy proxy = new ServiceStubProxy(iface, invocations);
        return (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[] { iface }, proxy);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy