ApiEndpointResourceProviderFactoryImpl.java 5.28 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceProvider
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.impl;

import com.adobe.granite.rest.ApiResourceProviderFactory;
import com.adobe.granite.rest.impl.ApiEndpointResourceProvider;
import com.adobe.granite.rest.impl.ApiEndpointResourceProviderFactory;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceProvider;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe Granite REST ApiEndpointResourceProviderFactory")
@Service
@Properties(value={@Property(name="provider.roots", value={"api"})})
public class ApiEndpointResourceProviderFactoryImpl
implements ApiEndpointResourceProviderFactory {
    private static final String DEFAULT_ROOT = "api";
    private final Logger logger;
    private final ReadWriteLock providerFactoriesLock;
    private String root;
    @Reference(referenceInterface=ApiResourceProviderFactory.class, bind="bindApiResourceProviderFactory", unbind="unbindApiResourceProviderFactory", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
    private Map<String, ApiResourceProviderFactory> apiProviderFactories;

    public ApiEndpointResourceProviderFactoryImpl() {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.providerFactoriesLock = new ReentrantReadWriteLock();
        this.apiProviderFactories = new HashMap<String, ApiResourceProviderFactory>();
    }

    public ResourceProvider getResourceProvider(Map<String, Object> authenticationInfo) throws LoginException {
        return new ApiEndpointResourceProvider(this);
    }

    public ResourceProvider getAdministrativeResourceProvider(Map<String, Object> authenticationInfo) throws LoginException {
        return this.getResourceProvider(authenticationInfo);
    }

    @Override
    public String getRootContextPath() {
        String rootContext = this.root;
        if (!rootContext.startsWith("/")) {
            rootContext = "/" + rootContext;
        }
        return rootContext;
    }

    @Override
    public ApiResourceProviderFactory getApiProviderFactory(String type) {
        return this.apiProviderFactories.get(type);
    }

    @Override
    public Collection<ApiResourceProviderFactory> getApiProviderFactories() {
        return this.apiProviderFactories.values();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindApiResourceProviderFactory(ApiResourceProviderFactory apiResourceProviderFactory, Map<String, Object> properties) {
        this.providerFactoriesLock.writeLock().lock();
        try {
            String type = PropertiesUtil.toString((Object)properties.get("provider.type"), (String)null);
            this.apiProviderFactories.put(type, apiResourceProviderFactory);
            this.logger.info("ApiResourceProviderFactory (type={},class={}) bound.", (Object)type, (Object)apiResourceProviderFactory.getClass().getName());
        }
        finally {
            this.providerFactoriesLock.writeLock().unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindApiResourceProviderFactory(ApiResourceProviderFactory apiResourceProviderFactory, Map<String, Object> properties) {
        this.providerFactoriesLock.writeLock().lock();
        try {
            String type = PropertiesUtil.toString((Object)properties.get("provider.type"), (String)null);
            this.apiProviderFactories.remove(type);
            this.logger.info("ApiResourceProviderFactory (type={},class={}) unbound.", (Object)type, (Object)apiResourceProviderFactory.getClass().getName());
        }
        finally {
            this.providerFactoriesLock.writeLock().unlock();
        }
    }

    @Activate
    protected void activate(BundleContext context) {
        this.root = PropertiesUtil.toString((Object)context.getProperty("provider.roots"), (String)"api");
    }
}