/**
  * Read last completed process/task instance end time from carbon registry
  *
  * @param resourcePath resource path in the carbon registry
  * @param propertyPath property path in the resource
  * @return the end time of last completed process/task instance
  */
 private String readPublishTimeFromRegistry(String resourcePath, String propertyPath) {
   String time = null;
   try {
     PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
     Registry registry = carbonContext.getRegistry(RegistryType.SYSTEM_GOVERNANCE);
     if (registry != null) {
       if (registry.resourceExists(resourcePath)) {
         if (log.isDebugEnabled()) {
           log.debug("Process instance resource path exists..." + resourcePath);
         }
         Resource resource = registry.get(resourcePath);
         time = resource.getProperty(propertyPath);
       }
     } else {
       if (log.isDebugEnabled()) {
         log.debug("Registry is null...");
       }
       throw new RegistryException("Registry is null");
     }
   } catch (RegistryException e) {
     String errMsg = "Registry error while reading the process instance publish time.";
     log.error(errMsg, e);
   }
   return time;
 }
 /**
  * Write last completed task instance end time to carbon registry
  *
  * @param historicTaskInstanceList List of historic task instances
  */
 private void writeTaskEndTimeToRegistry(List<HistoricTaskInstance> historicTaskInstanceList) {
   if (log.isDebugEnabled()) {
     log.debug("Start writing last completed task instance end time...");
   }
   Date lastTaskInstanceDate =
       historicTaskInstanceList.get(historicTaskInstanceList.size() - 1).getEndTime();
   try {
     PrivilegedCarbonContext privilegedContext =
         PrivilegedCarbonContext.getThreadLocalCarbonContext();
     Registry registry = privilegedContext.getRegistry(RegistryType.SYSTEM_GOVERNANCE);
     Resource resource;
     if (!registry.resourceExists(AnalyticsPublisherConstants.TASK_RESOURCE_PATH)) {
       resource = registry.newResource();
       resource.addProperty(
           AnalyticsPublisherConstants.LAST_TASK_INSTANCE_END_TIME,
           String.valueOf(lastTaskInstanceDate));
       registry.put(AnalyticsPublisherConstants.TASK_RESOURCE_PATH, resource);
     } else {
       resource = registry.get(AnalyticsPublisherConstants.TASK_RESOURCE_PATH);
       resource.setProperty(
           AnalyticsPublisherConstants.LAST_TASK_INSTANCE_END_TIME,
           String.valueOf(lastTaskInstanceDate));
       registry.put(AnalyticsPublisherConstants.TASK_RESOURCE_PATH, resource);
     }
     if (log.isDebugEnabled()) {
       log.debug("End of writing last completed task instance end time...");
     }
   } catch (RegistryException e) {
     String errMsg = "Registry error while writing the task instance end time.";
     log.error(errMsg, e);
   }
 }
 private void loadBAMProfileFromRegistry(
     BAMServerProfile bamServerProfile, Registry registry, String location) {
   try {
     if (registry.resourceExists(location)) {
       Resource resource = registry.get(location);
       String resourceContent = new String((byte[]) resource.getContent());
       OMElement resourceElement =
           new StAXOMBuilder(new ByteArrayInputStream(resourceContent.getBytes()))
               .getDocumentElement();
       processBAMServerProfileName(resourceElement, bamServerProfile);
       processCredentialElement(resourceElement, bamServerProfile);
       processConnectionElement(resourceElement, bamServerProfile);
       processKeyStoreElement(resourceElement, bamServerProfile);
       processStreamsElement(resourceElement, bamServerProfile);
     } else {
       String errMsg = "The resource: " + location + " does not exist.";
       handleError(errMsg);
     }
   } catch (RegistryException e) {
     String errMsg =
         "Error occurred while reading the resource from registry: "
             + location
             + " to build the BAM server profile: "
             + profileLocation;
     handleError(errMsg, e);
   } catch (XMLStreamException e) {
     String errMsg =
         "Error occurred while creating the OMElement out of BAM server "
             + "profile: "
             + profileLocation;
     handleError(errMsg, e);
   } catch (CryptoException e) {
     String errMsg =
         "Error occurred while decrypting password in BAM server profile: " + profileLocation;
     handleError(errMsg, e);
   }
 }