package pl.ibpolsoft.sop.client.service;

import javax.naming.Context;
import javax.naming.NamingException;

/**
 *
 * @author witoldsz
 */
public final class SimpleEjbLookup implements EjbLookup {

    private final Context ctx;

    public SimpleEjbLookup(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public String defaultBeanName(Class remoteInterface) {
        return remoteInterface.getName();
    }

    @Override
    public <T> T getBean(Class<T> remoteInterface) throws NamingException {
        String beanName = defaultBeanName(remoteInterface);
        return getBean(remoteInterface, beanName);
    }

    @Override
    public <T> T getBean(Class<T> remoteInterface, String beanName) throws NamingException {
        @SuppressWarnings("unchecked")
        T result = (T) ctx.lookup(beanName);
        return result;
    }

}