/**
   * @see org.apache.james.queue.jms.JMSMailQueue#populateMailMimeMessage(javax.jms.Message,
   *     org.apache.mailet.Mail)
   */
  protected void populateMailMimeMessage(Message message, Mail mail)
      throws MessagingException, JMSException {
    if (message instanceof BlobMessage) {
      try {
        BlobMessage blobMessage = (BlobMessage) message;
        try {
          // store URL and queueName for later usage
          mail.setAttribute(JAMES_BLOB_URL, blobMessage.getURL());
          mail.setAttribute(JAMES_QUEUE_NAME, queueName);
        } catch (MalformedURLException e) {
          // Ignore on error
          logger.debug("Unable to get url from blobmessage for mail " + mail.getName());
        }
        MimeMessageSource source = new MimeMessageBlobMessageSource(blobMessage);
        mail.setMessage(new MimeMessageCopyOnWriteProxy(source));

      } catch (JMSException e) {
        throw new MailQueueException(
            "Unable to populate MimeMessage for mail " + mail.getName(), e);
      }
    } else {
      super.populateMailMimeMessage(message, mail);
    }
  }
  /** Produce the mail to the JMS Queue */
  protected void produceMail(Session session, Map<String, Object> props, int msgPrio, Mail mail)
      throws JMSException, MessagingException, IOException {
    MessageProducer producer = null;
    BlobMessage blobMessage = null;
    boolean reuse = false;

    try {

      // check if we should use a blob message here
      if (useBlob) {
        ActiveMQSession amqSession = getAMQSession(session);

        /*
         * Remove this optimization as it could lead to problems when the same blob content
         * is shared across different messages.
         *
         * I still think it would be a good idea to somehow do this but at the moment it's just
         * safer to disable it.
         *
         * TODO: Re-Enable it again once it works!
         *
         * See JAMES-1240
        if (wrapper instanceof MimeMessageCopyOnWriteProxy) {
            wrapper = ((MimeMessageCopyOnWriteProxy) mm).getWrappedMessage();
        }

        if (wrapper instanceof MimeMessageWrapper) {
            URL blobUrl = (URL) mail.getAttribute(JAMES_BLOB_URL);
            String fromQueue = (String) mail.getAttribute(JAMES_QUEUE_NAME);
            MimeMessageWrapper mwrapper = (MimeMessageWrapper) wrapper;

            if (blobUrl != null && fromQueue != null && mwrapper.isModified() == false) {
                // the message content was not changed so don't need to
                // upload it again and can just point to the url
                blobMessage = amqSession.createBlobMessage(blobUrl);
                reuse = true;
            }

        }*/
        if (blobMessage == null) {
          // just use the MimeMessageInputStream which can read every
          // MimeMessage implementation
          blobMessage = amqSession.createBlobMessage(new MimeMessageInputStream(mail.getMessage()));
        }

        // store the queue name in the props
        props.put(JAMES_QUEUE_NAME, queueName);

        Queue queue = session.createQueue(queueName);

        producer = session.createProducer(queue);
        for (Map.Entry<String, Object> entry : props.entrySet()) {
          blobMessage.setObjectProperty(entry.getKey(), entry.getValue());
        }
        producer.send(
            blobMessage, Message.DEFAULT_DELIVERY_MODE, msgPrio, Message.DEFAULT_TIME_TO_LIVE);

      } else {
        super.produceMail(session, props, msgPrio, mail);
      }
    } catch (JMSException e) {
      if (!reuse && blobMessage != null && blobMessage instanceof ActiveMQBlobMessage) {
        ((ActiveMQBlobMessage) blobMessage).deleteFile();
      }
      throw e;
    } finally {

      try {
        if (producer != null) producer.close();
      } catch (JMSException e) {
        // ignore here
      }
    }
  }