ReverseReplicator.java 6.19 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Credentials
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.SimpleCredentials
 *  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.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationLog;
import com.day.cq.replication.impl.ReverseReplicationHandler;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import javax.jcr.Credentials;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
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.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1)
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.period", longValue={30}, label="%reverse.scheduler.period.name", description="%reverse.scheduler.period.description"), @Property(name="scheduler.concurrent", boolValue={0}, propertyPrivate=1)})
public class ReverseReplicator
implements Runnable {
    private final Logger logger;
    @Reference
    private SlingRepository repository;
    @Reference
    private AgentManager agentMgr;
    @Reference
    private ReverseReplicationHandler agentReverseReplicationHandler;

    public ReverseReplicator() {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.repository = null;
        this.agentMgr = null;
    }

    @Override
    public void run() {
        try {
            if (this.isMaster()) {
                this.logger.debug("Running reverse replication, since this is the cluster master...");
                this.poll();
            } else {
                this.logger.debug("Skipping reverse replication, since this is a cluster slave.");
            }
        }
        catch (ReplicationException e) {
            String msg = "Error during poll.";
            this.logger.warn(msg, (Throwable)e);
        }
    }

    private boolean isMaster() {
        String value = this.repository.getDescriptor("crx.cluster.master");
        return value == null || "true".equals(value);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void poll() throws ReplicationException {
        LinkedList<Agent> agents = new LinkedList<Agent>();
        for (Agent agent : this.agentMgr.getAgents().values()) {
            if (!agent.isValid() || !agent.isEnabled() || !agent.getConfiguration().usedForReverseReplication()) continue;
            agents.add(agent);
        }
        if (agents.isEmpty()) {
            return;
        }
        ReplicationAction action = new ReplicationAction(ReplicationActionType.INTERNAL_POLL, "", 0, "", null);
        Session session = null;
        try {
            session = this.repository.loginService("replicationService", null);
            for (Agent agent2 : agents) {
                Session agentSession = session;
                ReplicationLog log = agent2.getLog();
                String userId = agent2.getConfiguration().getAgentUserId();
                if (userId != null) {
                    try {
                        agentSession = this.repository.impersonateFromService("replicationService", (Credentials)new SimpleCredentials(userId, new char[0]), null);
                        log.info("Using user %s for building content.", userId);
                    }
                    catch (RepositoryException e) {
                        log.error("Error while impersonating to user '%s'. using system session.", userId);
                    }
                }
                try {
                    this.agentReverseReplicationHandler.poll(agent2, agentSession, action);
                    continue;
                }
                catch (Exception e) {
                    log.error("Error while polling agent %s: %s", agent2.getId(), e.toString());
                    this.logger.error("Error while poling agent " + agent2.getId(), (Throwable)e);
                    continue;
                }
                finally {
                    if (agentSession == session) continue;
                    agentSession.logout();
                    continue;
                }
            }
        }
        catch (RepositoryException e) {
            throw new ReplicationException("Error while accessing repository", (Exception)e);
        }
        finally {
            if (session != null) {
                session.logout();
            }
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

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

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

    protected void bindAgentReverseReplicationHandler(ReverseReplicationHandler reverseReplicationHandler) {
        this.agentReverseReplicationHandler = reverseReplicationHandler;
    }

    protected void unbindAgentReverseReplicationHandler(ReverseReplicationHandler reverseReplicationHandler) {
        if (this.agentReverseReplicationHandler == reverseReplicationHandler) {
            this.agentReverseReplicationHandler = null;
        }
    }
}