org.xwiki.mail.internal.AbstractMailStatusResult.java Source code

Java tutorial

Introduction

Here is the source code for org.xwiki.mail.internal.AbstractMailStatusResult.java

Source

/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.mail.internal;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper for implementations of {@link UpdateableMailStatusResult}.
 *
 * @version $Id: 2a15a1fac79bea1ed753895fc5c9eb8833dd2360 $
 * @since 7.1M2
 */
public abstract class AbstractMailStatusResult implements UpdateableMailStatusResult {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMailStatusResult.class);

    private long totalSize = -1;

    private long currentSize;

    @Override
    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    @Override
    public void incrementCurrentSize() {
        this.currentSize++;
    }

    @Override
    public long getTotalMailCount() {
        return this.totalSize;
    }

    @Override
    public long getProcessedMailCount() {
        return this.currentSize;
    }

    @Override
    public void waitTillProcessed(long timeout) {
        long startTime = System.currentTimeMillis();
        while (!isProcessed() && System.currentTimeMillis() - startTime < timeout) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                // Ignore but consider that the mail was sent
                LOGGER.warn("Interrupted while waiting for mails to be sent. Reason [{}]",
                        ExceptionUtils.getRootCauseMessage(e));
                break;
            }
        }
    }

    @Override
    public boolean isProcessed() {
        return getTotalMailCount() == getProcessedMailCount();
    }
}