Aem 如何使用replicator API复制页面及其所有子级?
下面是我复制页面的代码Aem 如何使用replicator API复制页面及其所有子级?,aem,Aem,下面是我复制页面的代码 final String pagePath = blogEntryPage.getPath(); final Resource jcrContent= blogEntryPage.getContentResource(); final Node jcrNode = jcrContent.adaptTo(Node.class); adminSession = jcrNode.g
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);
这里的问题是,只有父页面被复制了。我还想复制子页面如何在子页面上迭代并复制它们:
Iterator<Page> childPages = blogEntryPage.listChildren();
while (childPages.hasNext()) {
Page childPage = childPages.next();
replicator.replicate(adminSession, ReplicationActionType.ACTIVATE, childPage.getPath());
}
Iterator childPages=blogEntryPage.listChildren();
while(childPages.hasNext()){
Page childPage=childPages.next();
replicator.replicate(adminSession、ReplicationActionType.ACTIVATE、childPage.getPath());
}
您甚至可以将其放入一个方法中,并递归调用它。试试这个,(尚未测试)
import org.osgi.framework.BundleActivator;
导入org.osgi.framework.BundleContext;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入javax.jcr.Session;
导入com.day.cq.replication.Agent;
导入com.day.cq.replication.AgentManager;
导入com.day.cq.replication.ReplicationAction;
导入com.day.cq.replication.ReplicationActionType;
导入com.day.cq.replication.ReplicationContent;
导入com.day.cq.replication.ReplicationException;
导入com.day.cq.replication.ReplicationOptions;
导入com.day.cq.replication.Replicator;
导入com.day.cq.wcm.api.WCMException;
导入com.day.cq.wcm.msm.api.LiveRelationship;
导入com.day.cq.wcm.msm.api.LiveRelationshipManager;
导入com.day.cq.wcm.msm.api.RolloutManager;
导入org.osgi.framework.ServiceReference;
导入org.apache.sling.api.resource.ResourceResolver;
导入org.apache.sling.api.resource.ResourceResolverFactory;
导入org.apache.sling.api.resource.LoginException;
公共类激活器实现BundleActivator{
/*
*(非Javadoc)
*@see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext)引发异常{
AgentManager AgentManager=getService(上下文,AgentManager.class);
Replicator Replicator=getService(上下文,Replicator.class);
ResourceResolverFactory resourceFactory=getService(上下文,ResourceResolverFactory.class);
ResourceResolver ResourceResolver=null;
会话=空;
字符串路径=“/content/geometryxx gov”;
试一试{
resourceResolver=resourceFactory.getAdministrativeResourceResolver(null);
session=resourcesolver.adapto(session.class);
对于(Map.Entry e:agentManager.getAgents().entrySet()){
如果(例如getValue().getConfiguration().getTransportURI()包含(“/bin/receive?sling:authRequestLogin=1”)){
代理a=e.getValue();
试一试{
ReplicationAction ra=新的ReplicationAction(ReplicationActionType.ACTIVATE,路径);
ReplicationContent rc=a.buildContent(会话,ra);
a、 复制(ra、rc、新复制选项());
System.out.println(“激活器缓存刷新请求的检查队列”);
}捕获(ReplicationException ex){
例如printStackTrace();
}
}
}
}捕获(LoginException e){
e、 printStackTrace();
}
}
公共T getService(BundleContext bc,c类)
{
ServiceReference sr=bc.getServiceReference(c.getName());
如果(sr!=null)
{
返回(T)bc.getService(sr);
}
返回null;
}
/*
*(非Javadoc)
*@see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
公共void stop(BundleContext上下文)引发异常{
//TODO添加清理代码
}
}
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
}
}