public Collection<Message> produce(final Execution execution) {
    final Map<String, Object> vars = ((ExecutionImpl) execution).getVariables();
    Locale locale = (Locale) vars.get("locale");

    if (templateKey != null) {
      MailTemplate template = null;
      MailTemplateRegistry templateRegistry =
          ((ProcessEngine) SpringContextSingleton.getBean("processEngine"))
              .get(MailTemplateRegistry.class);
      if (locale != null) {
        template = (templateRegistry.getTemplate(templateKey + "." + locale.toString()));
        if (template == null) {
          template = (templateRegistry.getTemplate(templateKey + "." + locale.getLanguage()));
        }
      }
      if (template == null) {
        template = templateRegistry.getTemplate(templateKey);
      }
      setTemplate(template);
    }

    if (ServicesRegistry.getInstance().getMailService().isEnabled() && getTemplate() != null) {
      try {
        return JCRTemplate.getInstance()
            .doExecuteWithSystemSession(
                null,
                "default",
                locale,
                new JCRCallback<Collection<Message>>() {
                  public Collection<Message> doInJCR(JCRSessionWrapper session)
                      throws RepositoryException {
                    try {
                      scriptEngine =
                          ScriptEngineUtils.getInstance()
                              .getEngineByName(getTemplate().getLanguage());
                      bindings = null;
                      Message email = instantiateEmail();
                      fillFrom(email, execution, session);
                      fillRecipients(email, execution, session);
                      fillSubject(email, execution, session);
                      fillContent(email, execution, session);
                      Address[] addresses = email.getRecipients(Message.RecipientType.TO);
                      if (addresses != null && addresses.length > 0) {
                        return Collections.singleton(email);
                      } else {
                        return Collections.emptyList();
                      }
                    } catch (MessagingException e) {
                      logger.error(e.getMessage(), e);
                    } catch (ScriptException e) {
                      logger.error(e.getMessage(), e);
                    }
                    return Collections.emptyList();
                  }
                });
      } catch (RepositoryException e) {
        logger.error(e.getMessage(), e);
      }
    }
    return Collections.emptyList();
  }
 @BeforeClass
 public static void oneTimeSetUp() throws Exception {
   service =
       (HtmlExternalizationService) SpringContextSingleton.getBean("HtmlExternalizationService");
   assertNotNull("HtmlExternalizationService cannot be retrieved", service);
 }