/**
  * 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 setupEmailTemplates() throws EmailSenderConfigurationFailedException {
   File templateDir =
       new File(
           CarbonUtils.getCarbonHome()
               + File.separator
               + "repository"
               + File.separator
               + "resources"
               + File.separator
               + "email-templates");
   if (!templateDir.exists()) {
     if (log.isDebugEnabled()) {
       log.debug(
           "The directory that is expected to use as the container for all email templates is not "
               + "available. Therefore, no template is uploaded to the registry");
     }
   }
   if (templateDir.canRead()) {
     File[] templates =
         templateDir.listFiles(
             new FilenameFilter() {
               @Override
               public boolean accept(File dir, String name) {
                 name = name.toLowerCase();
                 return name.endsWith(".vm");
               }
             });
     try {
       Registry registry =
           EmailSenderDataHolder.getInstance().getRegistryService().getConfigSystemRegistry();
       if (!registry.resourceExists(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH)) {
         Collection collection = registry.newCollection();
         registry.put(EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH, collection);
         for (File template : templates) {
           Resource resource = registry.newResource();
           String contents = FileUtils.readFileToString(template);
           resource.setContent(contents.getBytes());
           registry.put(
               EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource);
         }
       } else {
         /* Existence of a given resource is not checked consciously, before performing registry.put() below.
          *  The rationale is that, the only less expensive way that one can check if a resource exists is
          *  that through registry.resourceExists(), which only checks if 'some' resource exists at the given
          *  registry path. However, this does not capture scenarios where there can be updated contents to
          *  the same resource of which the path hasn't changed after it has been initialized for the first
          *  time. Therefore, whenever the server starts-up, all email templates are updated just to avoid
          *  the aforementioned problem */
         for (File template : templates) {
           Resource resource = registry.newResource();
           String contents = FileUtils.readFileToString(template);
           resource.setContent(contents.getBytes());
           registry.put(
               EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH + "/" + template.getName(), resource);
         }
       }
     } catch (RegistryException e) {
       throw new EmailSenderConfigurationFailedException(
           "Error occurred while setting up email templates", e);
     } catch (FileNotFoundException e) {
       throw new EmailSenderConfigurationFailedException(
           "Error occurred while writing template file "
               + "contents as an input stream of a resource",
           e);
     } catch (IOException e) {
       throw new EmailSenderConfigurationFailedException(
           "Error occurred while serializing file " + "contents to a string", e);
     }
   }
 }