如何使用复制器API复制页面及其所有子代? [英] How to replicate a page and all its children using replicator API?

查看:99
本文介绍了如何使用复制器API复制页面及其所有子代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在其中复制页面的代码

Here is the code in which I am replicating the page

            final String pagePath = blogEntryPage.getPath();
            final Resource jcrContent=  blogEntryPage.getContentResource();
            final Node jcrNode = jcrContent.adaptTo(Node.class);
            adminSession = jcrNode.getSession();
            // REPLICATE THE DATE NODE
            replicator.replicate(adminSession, ReplicationActionType.ACTIVATE, pagePath);

这里的问题是只有父页面正在被复制,我也想复制子页面

Here the problem is only the parent page is getting replicated I want to replicate the child pages also

推荐答案

尝试一下,(未经测试)

Try this, (have not tested)

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.Session;
import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import org.osgi.framework.ServiceReference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.LoginException;

public class Activator implements BundleActivator {



    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        AgentManager agentManager = getService(context,AgentManager.class);
        Replicator replicator = getService(context,Replicator.class);
        ResourceResolverFactory resourceFactory = getService(context,ResourceResolverFactory.class);

        ResourceResolver resourceResolver = null;
        Session session = null;
        String path = "/content/geometrixx-gov";
        try {
            resourceResolver = resourceFactory.getAdministrativeResourceResolver(null);
            session = resourceResolver.adaptTo(Session.class);
            for (Map.Entry<String, Agent> e : agentManager.getAgents().entrySet()) {
                if (e.getValue().getConfiguration().getTransportURI().contains("/bin/receive?sling:authRequestLogin=1")) {
                    Agent a = e.getValue();
                    try {
                        ReplicationAction ra = new ReplicationAction(ReplicationActionType.ACTIVATE, path);
                        ReplicationContent rc = a.buildContent(session, ra);
                        a.replicate(ra, rc, new ReplicationOptions());
                        System.out.println("Activator cache flush requested check queue");
                    } catch (ReplicationException ex) {
                        ex.printStackTrace();
                    }
                }
            }

        } catch (LoginException e) {
            e.printStackTrace();
        }
    }

    public <T> T getService(BundleContext bc, Class<T> c)
    {
        ServiceReference sr = bc.getServiceReference(c.getName());
        if (sr != null)
        {
            return (T) bc.getService(sr);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        // TODO add cleanup code
    }

}

这篇关于如何使用复制器API复制页面及其所有子代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆