Refer the post for details on creating the package through Java - https://www.albinsblog.com/2018/01/sling-scheduler-to-auto-create-packages-JcrPackageManager-api-aem.html
Sample Servlet to upload and install the package - Convert the servlet to the scheduler and import/replicate the package based on the scheduled time
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.jackrabbit.vault.fs.io.ImportOptions;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.Replicator;
/**
* Servlet that writes some sample content into the response. It is mounted for
* all resources of a specific Sling resource type. The
* {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
* idempotent. For write operations use the {@link SlingAllMethodsServlet}.
*/
@SuppressWarnings("serial")
@SlingServlet(paths= "/services/packageupload")
public class PackageUploadServlet extends SlingSafeMethodsServlet {
@Reference
private Packaging packaging;
@Reference
private SlingRepository repository;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private SlingSettingsService settingsService;
@Reference
Replicator replicator;
@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {
Session session = null;
if (isRunMode("author") && isMasterRepository()) {//Only run in author - change the servlet to scheduler and import/replicate the package based on the scheduled time
try {
String packagePath = "C:\\Albin\\test12018-01-14-15-00-00-023-1.0.zip";
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "packageService");
ResourceResolver resolver;
resolver = resolverFactory.getServiceResourceResolver(param);
session = resolver.adaptTo(Session.class);
JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);
JcrPackage inputPackage=jcrPackageManager.upload(new File(packagePath), false, true, "test12018-01-14-15-00-00-023-1.0");
ImportOptions importOption=new ImportOptions();//Specify the import configurations
inputPackage.install(importOption);
//Replicate the package to publishers
replicator.replicate(session, ReplicationActionType.ACTIVATE, inputPackage.getDefinition().getNode().getPath().split("/jcr:content")[0]);
resp.getOutputStream().println("This package successfully uploaded and installed to the server....");
} catch (Exception e) {
// TODO Auto-generated catch block
resp.getOutputStream().println("Excpetion while uploading/installing the package...."+e.getMessage());
e.printStackTrace();
}finally {
if(session!=null)
{
session.logout();
}
}
}
}
private Boolean isRunMode(String mode) {
Set<String> runModes = settingsService.getRunModes();
for (String runMode : runModes) {
if (runMode.equalsIgnoreCase(mode)) {
return true;
}
}
return false;
}
public boolean isMasterRepository() {
final String isMaster = repository.getDescriptor("crx.cluster.master");
return isMaster != null && !isMaster.equals("") && Boolean.parseBoolean(isMaster);
}
}
Details on package manager upload method
@Nonnull
JcrPackage upload(@Nonnull
java.io.File file,
boolean isTmpFile,
boolean replace,
@Nullable
java.lang.String nameHint)
throws javax.jcr.RepositoryException,
java.io.IOException
Uploads a package. The location is chosen from the installation path of the package. if the package does not provide such a path, the nameHint is respected and the package is placed below the package root. if the package already exists at that path it is not installed and null is returned unless replace is true.
Parameters:
file - package file to upload
isTmpFile - indicates if the given file is a temp file and can be deleted when the package is closed
replace - if true existing packages are replaced.
nameHint - hint for the name if the package does not provide one
Returns:
the new jcr package
Make sure the service user(packageassempler) is having the access to the filter paths specified in the package and also the create/modify/delete/replicate permission to /etc/packages - Refer the post-https://www.albinsblog.com/2018/01/sling-scheduler-to-auto-create-packages-JcrPackageManager-api-aem.html for the steps on configuring the service user.
The configured package is created with the filter - /etc/cloudservices/search-promote so provided the modify/create/delete access to the service user (the service user should be having the modify/create/delete access for the filter path defined in the package)
Verify the package status in CRXD package manager and CRXDE
The package also successfully replicated to the agent - I could not able to validate the replication through the publisher(not having the local setup) but the replication requests are queued in author agent.
file - package file to upload
isTmpFile - indicates if the given file is a temp file and can be deleted when the package is closed
replace - if true existing packages are replaced.
nameHint - hint for the name if the package does not provide one
Returns:
the new jcr package
Make sure the service user(packageassempler) is having the access to the filter paths specified in the package and also the create/modify/delete/replicate permission to /etc/packages - Refer the post-https://www.albinsblog.com/2018/01/sling-scheduler-to-auto-create-packages-JcrPackageManager-api-aem.html for the steps on configuring the service user.
The configured package is created with the filter - /etc/cloudservices/search-promote so provided the modify/create/delete access to the service user (the service user should be having the modify/create/delete access for the filter path defined in the package)
Verify the package status in CRXD package manager and CRXDE
The package also successfully replicated to the agent - I could not able to validate the replication through the publisher(not having the local setup) but the replication requests are queued in author agent.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.