📄 mimemessagecopyonwriteproxy.java
字号:
/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you under the Apache License, Version 2.0 (the * * "License"); you may not use this file except in compliance * * with the License. You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.apache.james.core;import org.apache.avalon.framework.activity.Disposable;import org.apache.avalon.framework.container.ContainerUtil;import javax.activation.DataHandler;import javax.mail.Address;import javax.mail.Flags;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Flags.Flag;import javax.mail.internet.MimeMessage;import javax.mail.search.SearchTerm;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import java.util.Enumeration;/** * This object wraps a "possibly shared" MimeMessage tracking copies and * automatically cloning it (if shared) when a write operation is invoked. */public class MimeMessageCopyOnWriteProxy extends MimeMessage implements Disposable { /** * Used internally to track the reference count * It is important that this is static otherwise it will keep a reference to * the parent object. */ protected static class MessageReferenceTracker { /** * reference counter */ private int referenceCount = 1; /** * The mime message in memory */ private MimeMessage wrapped = null; public MessageReferenceTracker(MimeMessage ref) { wrapped = ref; } protected synchronized void incrementReferenceCount() { referenceCount++; } protected synchronized void decrementReferenceCount() { referenceCount--; if (referenceCount<=0) { ContainerUtil.dispose(wrapped); wrapped = null; } } protected synchronized int getReferenceCount() { return referenceCount; } public MimeMessage getWrapped() { return wrapped; } } protected MessageReferenceTracker refCount; /** * @param original * MimeMessageWrapper * @throws MessagingException */ public MimeMessageCopyOnWriteProxy(MimeMessage original) throws MessagingException { this(original, false); } /** * @param original * MimeMessageSource * @throws MessagingException */ public MimeMessageCopyOnWriteProxy(MimeMessageSource original) throws MessagingException { this(new MimeMessageWrapper(original), true); } /** * Private constructor providing an external reference counter. * * @param original * @param refCount * @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 MimeMessage getWrappedMessageForWriting() throws MessagingException { synchronized (refCount) { if (refCount.getReferenceCount() > 1) { refCount.decrementReferenceCount(); refCount = new MessageReferenceTracker(new MimeMessageWrapper(refCount.getWrapped())); } } return refCount.getWrapped(); } /** * @return */ public MimeMessage getWrappedMessage() { return refCount.getWrapped(); } /** * @see org.apache.avalon.framework.activity.Disposable#dispose() */ public synchronized void dispose() { if (refCount != null) { refCount.decrementReferenceCount(); refCount = null; } } /** * Rewritten for optimization purposes */ public void writeTo(OutputStream os) throws IOException, MessagingException { getWrappedMessage().writeTo(os); } /** * Rewritten for optimization purposes */ public void writeTo(OutputStream os, String[] ignoreList) throws IOException, MessagingException { getWrappedMessage().writeTo(os, ignoreList); } /** * Various reader methods */ /** * @see javax.mail.Message#getFrom() */ public Address[] getFrom() throws MessagingException { return getWrappedMessage().getFrom(); } /** * @see javax.mail.Message#getRecipients(javax.mail.Message.RecipientType) */ public Address[] getRecipients(Message.RecipientType type) throws MessagingException { return getWrappedMessage().getRecipients(type); } /** * @see javax.mail.Message#getAllRecipients() */ public Address[] getAllRecipients() throws MessagingException { return getWrappedMessage().getAllRecipients(); } /** * @see javax.mail.Message#getReplyTo() */ public Address[] getReplyTo() throws MessagingException { return getWrappedMessage().getReplyTo(); } /** * @see javax.mail.Message#getSubject() */ public String getSubject() throws MessagingException { return getWrappedMessage().getSubject(); } /** * @see javax.mail.Message#getSentDate() */ public Date getSentDate() throws MessagingException { return getWrappedMessage().getSentDate(); } /** * @see javax.mail.Message#getReceivedDate() */ public Date getReceivedDate() throws MessagingException { return getWrappedMessage().getReceivedDate(); } /** * @see javax.mail.Part#getSize() */ public int getSize() throws MessagingException { return getWrappedMessage().getSize(); } /** * @see javax.mail.Part#getLineCount() */ public int getLineCount() throws MessagingException { return getWrappedMessage().getLineCount(); } /** * @see javax.mail.Part#getContentType() */ public String getContentType() throws MessagingException { return getWrappedMessage().getContentType(); } /** * @see javax.mail.Part#isMimeType(java.lang.String) */ public boolean isMimeType(String mimeType) throws MessagingException { return getWrappedMessage().isMimeType(mimeType); } /** * @see javax.mail.Part#getDisposition() */ public String getDisposition() throws MessagingException { return getWrappedMessage().getDisposition(); } /** * @see javax.mail.internet.MimePart#getEncoding() */ public String getEncoding() throws MessagingException { return getWrappedMessage().getEncoding(); } /** * @see javax.mail.internet.MimePart#getContentID() */ public String getContentID() throws MessagingException { return getWrappedMessage().getContentID(); } /** * @see javax.mail.internet.MimePart#getContentMD5() */ public String getContentMD5() throws MessagingException { return getWrappedMessage().getContentMD5(); } /** * @see javax.mail.Part#getDescription() */ public String getDescription() throws MessagingException { return getWrappedMessage().getDescription(); } /** * @see javax.mail.internet.MimePart#getContentLanguage() */ public String[] getContentLanguage() throws MessagingException { return getWrappedMessage().getContentLanguage(); } /** * @see javax.mail.internet.MimeMessage#getMessageID() */ public String getMessageID() throws MessagingException { return getWrappedMessage().getMessageID(); } /** * @see javax.mail.Part#getFileName() */ public String getFileName() throws MessagingException { return getWrappedMessage().getFileName(); } /** * @see javax.mail.Part#getInputStream() */ public InputStream getInputStream() throws IOException, MessagingException { return getWrappedMessage().getInputStream(); } /** * @see javax.mail.Part#getDataHandler() */ public DataHandler getDataHandler() throws MessagingException { return getWrappedMessage().getDataHandler(); } /** * @see javax.mail.Part#getContent() */ public Object getContent() throws IOException, MessagingException { return getWrappedMessage().getContent(); } /** * @see javax.mail.Part#getHeader(java.lang.String) */ public String[] getHeader(String name) throws MessagingException { return getWrappedMessage().getHeader(name); } /** * @see javax.mail.internet.MimePart#getHeader(java.lang.String, java.lang.String) */ public String getHeader(String name, String delimiter) throws MessagingException { return getWrappedMessage().getHeader(name, delimiter); } /** * @see javax.mail.Part#getAllHeaders() */ public Enumeration getAllHeaders() throws MessagingException { return getWrappedMessage().getAllHeaders(); } /** * @see javax.mail.Part#getMatchingHeaders(java.lang.String[]) */ public Enumeration getMatchingHeaders(String[] names) throws MessagingException { return getWrappedMessage().getMatchingHeaders(names); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -