Last active
December 30, 2017 05:39
-
-
Save yupadhyay/48a62cf34eaf762a72fa to your computer and use it in GitHub Desktop.
ReplicationUtilServiceImpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wemblog.utility.impl; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.jcr.Session; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.resource.NonExistingResource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.day.cq.replication.Agent; | |
import com.day.cq.replication.AgentManager; | |
import com.day.cq.replication.ReplicationActionType; | |
import com.day.cq.replication.Replicator; | |
import com.wemblog.ReplicationHelper; | |
import com.wemblog.ReplicationUtilService; | |
/** | |
* | |
* @author Yogesh Upadhyay | |
* @since 1.0.12 | |
* | |
*/ | |
@Service | |
@Component | |
public class ReplicationUtilServiceImpl implements ReplicationUtilService { | |
private Logger logger = LoggerFactory.getLogger(ReplicationUtilServiceImpl.class); | |
@Reference | |
private Replicator replicator; | |
@Reference | |
private ResourceResolverFactory resourceResolverFactory; | |
@Reference | |
private AgentManager agentManager; | |
private ResourceResolver resourceResolver = null; | |
private Session session; | |
/** | |
* Tree Replicate based on path | |
*/ | |
@Override | |
public void treeReplicate(final String parent_path, final ResourceResolver resolver) { | |
treeReplicate(parent_path, ReplicationActionType.ACTIVATE, resolver); | |
} | |
@Override | |
public void treeReplicate(final String parent_path, | |
final ReplicationActionType actionType,final ResourceResolver resolver) { | |
try { | |
if (null == resolver) { | |
logger.error("Resolver is null can not tree activate " + parent_path); | |
return; | |
} | |
if (resolver.resolve(parent_path) instanceof NonExistingResource) { | |
logger.error("Parent path does not exist can not activate " | |
+ parent_path); | |
return; | |
} | |
this.resourceResolver = resolver; | |
ReplicationHelper replicationHelper = new ReplicationHelper(replicator, | |
resourceResolver, agentManager); | |
replicationHelper.setReplicationActionType(actionType); | |
replicationHelper.process(parent_path); | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} finally { | |
// we do not close resource resolver here because it's passed as reference | |
} | |
} | |
@Override | |
public void treeReplicate(final String parent_path, final boolean onlyModified, | |
final ResourceResolver resolver) { | |
treeReplicate(parent_path, onlyModified, false, resolver); | |
} | |
@Override | |
public void treeReplicate(final String parent_path, final boolean onlyModified, | |
final boolean ignoreDeactivated, final ResourceResolver resolver) { | |
try { | |
if (null == resolver) { | |
logger.error("Resolver is null can not tree activate " + parent_path); | |
return; | |
} | |
if (resolver.resolve(parent_path) instanceof NonExistingResource) { | |
logger.error("Parent path does not exist can not activate " | |
+ parent_path); | |
return; | |
} | |
resourceResolver = resolver; | |
ReplicationHelper replicationHelper = new ReplicationHelper(replicator, | |
resourceResolver, agentManager); | |
replicationHelper.setOnlyModified(onlyModified); | |
replicationHelper.setIgnoreDeactivated(ignoreDeactivated); | |
replicationHelper.process(parent_path); | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} finally { | |
// we do not close resource resolver here because it's passed as reference | |
} | |
} | |
@Override | |
public List<Agent> getAllActiveAgents() { | |
List<Agent> all_active_agent = new ArrayList<Agent>(); | |
for (Agent agent : agentManager.getAgents().values()) { | |
if (agent.isEnabled()) { | |
all_active_agent.add(agent); | |
} | |
} | |
return all_active_agent; | |
} | |
@Override | |
public List<Agent> getAllPublishAgent() { | |
List<Agent> all_active_agent = new ArrayList<Agent>(); | |
for (Agent agent : agentManager.getAgents().values()) { | |
if (agent.isEnabled() && !agent.isCacheInvalidator()) { | |
all_active_agent.add(agent); | |
} | |
} | |
return all_active_agent; | |
} | |
@Override | |
public List<Agent> getAllDispatcherAgent() { | |
List<Agent> all_active_agent = new ArrayList<Agent>(); | |
for (Agent agent : agentManager.getAgents().values()) { | |
if (agent.isEnabled() && agent.isCacheInvalidator()) { | |
all_active_agent.add(agent); | |
} | |
} | |
return all_active_agent; | |
} | |
@Override | |
public void activate(final String resource_path, final ResourceResolver resolver) { | |
try { | |
resourceResolver = resolver; | |
if (resolver == null) { | |
logger.error("Resolver is null can not activate " + resource_path); | |
return; | |
} | |
this.session = resourceResolver.adaptTo(Session.class); | |
if (!(resourceResolver.resolve(resource_path) instanceof NonExistingResource)) { | |
replicator.replicate(this.session, ReplicationActionType.ACTIVATE, | |
resource_path); | |
} else { | |
logger.error("Resource you are trying to replicate does not exist " | |
+ resource_path); | |
} | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} finally { | |
// we do not close resource resolver here because it's passed as reference | |
} | |
} | |
@Override | |
public void deactivate(final String resource_path, final ResourceResolver resolver) { | |
try { | |
if (resolver == null) { | |
logger.error("Resolver is null can not activate " + resource_path); | |
return; | |
} | |
this.resourceResolver = resolver; | |
this.session = resourceResolver.adaptTo(Session.class); | |
if (!(resourceResolver.resolve(resource_path) instanceof NonExistingResource)) { | |
replicator.replicate(this.session, ReplicationActionType.DEACTIVATE, | |
resource_path); | |
} else { | |
logger.error("Resource you are trying to replicate does not exist " | |
+ resource_path); | |
} | |
} catch (Exception e) { | |
logger.error(e.getMessage()); | |
} finally { | |
// we do not close resource resolver here because it's passed as reference | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment