

package com.adeptj.modules.jaxrs.resteasy.internal;

import org.apache.commons.lang3.StringUtils;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jetbrains.annotations.NotNull;
import org.osgi.framework.ServiceReference;

import javax.servlet.ServletContext;

import static com.adeptj.modules.jaxrs.resteasy.internal.ResteasyConstants.KEY_PROVIDER_NAME;
import static com.adeptj.modules.jaxrs.resteasy.internal.ResteasyConstants.KEY_RESOURCE_NAME;
import static com.adeptj.modules.jaxrs.resteasy.internal.ResteasyConstants.RESTEASY_DEPLOYMENT;

/**
 * Utilities for RESTEasy bootstrap process.
 *
 * @author Rakesh.Kumar, AdeptJ
 */
final class ResteasyUtil {

    private ResteasyUtil() {
    }

    static void addResteasyDeployment(@NotNull ServletContext servletContext, ResteasyDeployment deployment) {
        servletContext.setAttribute(RESTEASY_DEPLOYMENT, deployment);
    }

    static void removeResteasyDeployment(@NotNull ServletContext servletContext) {
        servletContext.removeAttribute(RESTEASY_DEPLOYMENT);
    }

    static <T> String getResourceName(@NotNull ServiceReference<T> reference) {
        return (String) reference.getProperty(KEY_RESOURCE_NAME);
    }

    static <T> String getProviderName(@NotNull ServiceReference<T> reference) {
        return (String) reference.getProperty(KEY_PROVIDER_NAME);
    }

    static <T> boolean isProvider(ServiceReference<T> reference) {
        return StringUtils.isNotEmpty(getProviderName(reference));
    }

    static <T> boolean isResource(ServiceReference<T> reference) {
        return StringUtils.isNotEmpty(getResourceName(reference));
    }
}
