2015-02-24 42 views
1

这是在我复制的页面如何使用复制器API复制页面及其所有子项?

  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); 

这里的问题是只母网页获得的复制我要复制的子页面也

回答

1

如何只迭代子代码页面并复制它们:

Iterator<Page> childPages = blogEntryPage.listChildren(); 
while (childPages.hasNext()) { 
    Page childPage = childPages.next(); 
    replicator.replicate(adminSession, ReplicationActionType.ACTIVATE, childPage.getPath()); 
} 

你甚至可以把它放在方法中并递归地调用它。

1

试试这个,(没有测试)

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 
    } 

}