/*
 *
 *    OPEN-XCHANGE legal information
 *
 *    All intellectual property rights in the Software are protected by
 *    international copyright laws.
 *
 *
 *    In some countries OX, OX Open-Xchange, open xchange and OXtender
 *    as well as the corresponding Logos OX Open-Xchange and OX are registered
 *    trademarks of the Open-Xchange, Inc. group of companies.
 *    The use of the Logos is not covered by the GNU General Public License.
 *    Instead, you are allowed to use these Logos according to the terms and
 *    conditions of the Creative Commons License, Version 2.5, Attribution,
 *    Non-commercial, ShareAlike, and the interpretation of the term
 *    Non-commercial applicable to the aforementioned license is published
 *    on the web site http://www.open-xchange.com/EN/legal/index.html.
 *
 *    Please make sure that third-party modules and libraries are used
 *    according to their respective licenses.
 *
 *    Any modifications to this package must retain all copyright notices
 *    of the original copyright holder(s) for the original code used.
 *
 *    After any such modifications, the original and derivative code shall remain
 *    under the copyright of the copyright holder(s) and/or original author(s)per
 *    the Attribution and Assignment Agreement that can be located at
 *    http://www.open-xchange.com/EN/developer/. The contributing author shall be
 *    given Attribution for the derivative code and a license granting use.
 *
 *     Copyright (C) 2004-2011 Open-Xchange, Inc.
 *     Mail: info@open-xchange.com
 *
 *
 *     This program is free software; you can redistribute it and/or modify it
 *     under the terms of the GNU General Public License, Version 2 as published
 *     by the Free Software Foundation.
 *
 *     This program 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 General Public License
 *     for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc., 59
 *     Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package com.openexchange.imap.cache;

import java.net.InetSocketAddress;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import javax.mail.MessagingException;
import com.openexchange.imap.IMAPCommandsCollection;
import com.sun.mail.imap.IMAPFolder;

/**
 * {@link MBoxEnabledCache} - A cache to check for MBox feature for a certain IMAP server.
 * 
 * @author <a href="mailto:thorben.betten@open-xchange.com">Thorben Betten</a>
 */
public final class MBoxEnabledCache {

    private static volatile ConcurrentMap<InetSocketAddress, Future<Boolean>> MAP;

    /**
     * Initializes a new {@link MBoxEnabledCache}.
     */
    private MBoxEnabledCache() {
        super();
    }

    /**
     * Initializes this cache.
     */
    public static void init() {
        if (MAP == null) {
            synchronized (MBoxEnabledCache.class) {
                if (MAP == null) {
                    MAP = new ConcurrentHashMap<InetSocketAddress, Future<Boolean>>();
                }
            }
        }
    }

    /**
     * Tear-down for this cache.
     */
    public static void tearDown() {
        if (MAP != null) {
            synchronized (MBoxEnabledCache.class) {
                if (MAP != null) {
                    clear();
                    MAP = null;
                }
            }
        }
    }

    /**
     * Clears this cache.
     */
    public static void clear() {
        MAP.clear();
    }

    /**
     * Checks if MBox feature is enabled for given IMAP server.
     * 
     * @param imapServer The IMAP server's address
     * @param imapFolder The IMAP folder to test with
     * @param prefix The fullname prefix to use
     * @return <code>true</code> if MBox feature is enabled; otherwise <code>false</code>
     * @throws MessagingException If a messaging error occurs
     */
    public static boolean isMBoxEnabled(final InetSocketAddress imapServer, final IMAPFolder imapFolder, final String prefix) throws MessagingException {
        final ConcurrentMap<InetSocketAddress, Future<Boolean>> map = MAP;
        Future<Boolean> f = map.get(imapServer);
        if (null == f) {
            final FutureTask<Boolean> ft = new FutureTask<Boolean>(new MBoxEnabledCallable(imapFolder, prefix));
            f = map.putIfAbsent(imapServer, ft);
            if (null == f) {
                f = ft;
                ft.run();
            }
        }
        try {
            return f.get().booleanValue();
        } catch (final InterruptedException e) {
            // Keep interrupted status
            Thread.currentThread().interrupt();
            throw new MessagingException(e.getMessage());
        } catch (final CancellationException e) {
            throw new MessagingException(e.getMessage());
        } catch (final ExecutionException e) {
            final Throwable cause = e.getCause();
            if (cause instanceof MessagingException) {
                throw ((MessagingException) cause);
            }
            if (cause instanceof RuntimeException) {
                throw new MessagingException(e.getMessage());
            }
            if (cause instanceof Error) {
                throw (Error) cause;
            }
            throw new IllegalStateException("Not unchecked", cause);
        }
    }

    private static final class MBoxEnabledCallable implements Callable<Boolean> {

        private static final int FOLDER_TYPE = (IMAPFolder.HOLDS_MESSAGES | IMAPFolder.HOLDS_FOLDERS);

        private final IMAPFolder imapFolder;

        private final String prefix;

        public MBoxEnabledCallable(final IMAPFolder imapFolder, final String prefix) {
            super();
            this.imapFolder = imapFolder;
            this.prefix = prefix;
        }

        public Boolean call() throws Exception {
            return Boolean.valueOf(!IMAPCommandsCollection.supportsFolderType(imapFolder, FOLDER_TYPE, prefix));
        }

    }

}
