ReplicationInventoryPrinter.java 5.63 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.inventory.Format
 *  org.apache.felix.inventory.InventoryPrinter
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  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.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.replication.impl.inventory;

import com.day.cq.replication.impl.inventory.AgentPrinter;
import com.day.cq.replication.impl.inventory.BaseAgentPrinter;
import com.day.cq.replication.impl.inventory.DefaultAgentPrinter;
import com.day.cq.replication.impl.inventory.StaticAgentPrinter;
import com.day.cq.replication.impl.inventory.TriggeredAgentPrinter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.inventory.Format;
import org.apache.felix.inventory.InventoryPrinter;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;

@Component
@Service(value={InventoryPrinter.class})
@Properties(value={@Property(name="felix.inventory.printer.name", value={"replication"}), @Property(name="felix.inventory.printer.title", value={"Granite Replication"})})
public class ReplicationInventoryPrinter
implements InventoryPrinter {
    @Reference
    private ResourceResolverFactory resolverFacoty;
    private final Map<String, AgentPrinter> printers = new HashMap<String, AgentPrinter>();
    private final AgentPrinter defaultPrinter = new DefaultAgentPrinter();

    @Activate
    protected void activate() throws Exception {
        this.register(new TriggeredAgentPrinter(), "publish", "flush", "test_and_target", "outbox");
        this.register(new BaseAgentPrinter(), "publish_reverse");
        this.register(new StaticAgentPrinter(), "static");
    }

    private /* varargs */ void register(AgentPrinter printerDelegate, String ... printersName) {
        for (String printerName : printersName) {
            this.printers.put(printerName, printerDelegate);
        }
    }

    @Deactivate
    protected void deactivate() throws Exception {
        this.printers.clear();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void print(PrintWriter pw, Format fmt, boolean isZip) {
        ResourceResolverFactory rrf = this.resolverFacoty;
        if (rrf != null) {
            pw.println("Granite Replication");
            pw.println("-------------------");
            pw.println();
            ResourceResolver resolver = null;
            try {
                resolver = rrf.getServiceResourceResolver((Map)new HashMap<String, Object>(){});
                Resource parentRsrc = resolver.getResource("/etc/replication");
                if (parentRsrc == null) {
                    pw.println("Unable to read any configured agents. /etc/replication does not exist.");
                    return;
                }
                for (Resource agentsRsrc : parentRsrc.getChildren()) {
                    Resource content;
                    if (agentsRsrc.isResourceType("sling:Folder") || (content = agentsRsrc.getChild("jcr:content")) == null) continue;
                    String category = (String)((ValueMap)content.adaptTo(ValueMap.class)).get("jcr:title", (Object)agentsRsrc.getName());
                    pw.println(category);
                    pw.println("----------------------------------------------------");
                    for (Resource agentRsrc : agentsRsrc.getChildren()) {
                        if (agentRsrc.isResourceType("nt:unstructured") || (content = agentRsrc.getChild("jcr:content")) == null) continue;
                        AgentPrinter printer = this.printers.get(agentRsrc.getName());
                        if (printer == null) {
                            printer = this.defaultPrinter;
                        }
                        String id = agentRsrc.getName();
                        ValueMap vm = (ValueMap)content.adaptTo(ValueMap.class);
                        printer.print(id, vm, pw);
                        pw.println();
                        pw.println("----------------------------------------------------");
                    }
                    pw.println();
                }
            }
            catch (LoginException parentRsrc) {}
            finally {
                if (resolver != null) {
                    resolver.close();
                }
            }
        }
    }

    protected void bindResolverFacoty(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFacoty = resourceResolverFactory;
    }

    protected void unbindResolverFacoty(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFacoty == resourceResolverFactory) {
            this.resolverFacoty = null;
        }
    }

}