ReplicationStatusAdapterFactory.java 3.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  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.Service
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.impl.ReplicationStatusImpl;
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.Service;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Properties(value={@Property(name="service.description", value={"Replication Status Adapter"}), @Property(name="adapter.condition", value={"If the resource has a child resource named jcr:content"})})
@Service(value={AdapterFactory.class})
public class ReplicationStatusAdapterFactory
implements AdapterFactory {
    private final Logger logger;
    private static final Class<Resource> RESOURCE_CLASS = Resource.class;
    private static final Class<ReplicationStatus> REPLICATION_STATUS_CLASS = ReplicationStatus.class;
    @Property(name="adapters")
    protected static final String[] ADAPTER_CLASSES = new String[]{REPLICATION_STATUS_CLASS.getName()};
    @Property(name="adaptables")
    protected static final String[] ADAPTABLE_CLASSES = new String[]{RESOURCE_CLASS.getName()};
    @Reference
    private AgentManager agentManager;

    public ReplicationStatusAdapterFactory() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        this.logger.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (type == REPLICATION_STATUS_CLASS) {
            Resource contentResource = resource.getResourceResolver().getResource(resource.getPath() + "/jcr:content");
            if (contentResource == null) {
                contentResource = resource;
            }
            return (AdapterType)new ReplicationStatusImpl(this.agentManager, resource.getPath(), contentResource);
        }
        this.logger.debug("Unable to adapt resource to type {}", (Object)type.getName());
        return null;
    }

    protected void bindAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    protected void unbindAgentManager(AgentManager agentManager) {
        if (this.agentManager == agentManager) {
            this.agentManager = null;
        }
    }
}