/** * Private constructor providing an external reference counter. * * @param original * @param writeable * @throws MessagingException */ private MimeMessageCopyOnWriteProxy(MimeMessage original, boolean writeable) throws MessagingException { super(Session.getDefaultInstance(System.getProperties(), null)); if (original instanceof MimeMessageCopyOnWriteProxy) { refCount = ((MimeMessageCopyOnWriteProxy) original).refCount; } else { refCount = new MessageReferenceTracker(original); } if (!writeable) { refCount.incrementReferenceCount(); } }
/** * Check the number of references over the MimeMessage and clone it if * needed before returning the reference * * @throws MessagingException * exception */ protected synchronized MimeMessage getWrappedMessageForWriting() throws MessagingException { if (refCount.getReferenceCount() > 1) { refCount.decrementReferenceCount(); refCount = new MessageReferenceTracker(new MimeMessageWrapper(refCount.getWrapped())); } return refCount.getWrapped(); }
/** * Return wrapped mimeMessage * * @return wrapped return the wrapped mimeMessage */ public synchronized MimeMessage getWrappedMessage() { return refCount.getWrapped(); }
/** * @see org.apache.avalon.framework.activity.Disposable#dispose() */ public synchronized void dispose() { if (refCount != null) { refCount.decrementReferenceCount(); refCount = null; } }