001/*
002 * (C) Copyright 2010-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.connect.client.status;
019
020import java.util.Calendar;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.connect.connector.CanNotReachConnectServer;
025import org.nuxeo.connect.connector.ConnectClientVersionMismatchError;
026import org.nuxeo.connect.connector.ConnectSecurityError;
027import org.nuxeo.connect.connector.ConnectServerError;
028import org.nuxeo.connect.registration.ConnectRegistrationService;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * @author Tiry (tdelprat@nuxeo.com)
033 */
034public class ConnectStatusHolder {
035
036    protected static ConnectStatusHolder instance;
037
038    protected SubscriptionStatusWrapper instanceStatus;
039
040    protected static final Log log = LogFactory.getLog(ConnectStatusHolder.class);
041
042    protected static final int REFRESH_PERIOD_MINUTES = 10;
043
044    public static ConnectStatusHolder instance() {
045        if (instance == null) {
046            instance = new ConnectStatusHolder();
047        }
048        return instance;
049    }
050
051    protected ConnectRegistrationService getService() {
052        return Framework.getLocalService(ConnectRegistrationService.class);
053    }
054
055    public boolean isRegistred() {
056        // no cache needed
057        return getService().isInstanceRegistred();
058    }
059
060    public SubscriptionStatusWrapper getStatus() {
061
062        // get status (possibility from cache)
063        SubscriptionStatusWrapper lastStatus = getStatus(false);
064
065        // check freshness
066        Calendar oldestStatusDate = Calendar.getInstance();
067        oldestStatusDate.add(Calendar.MINUTE, -REFRESH_PERIOD_MINUTES);
068        if (lastStatus == null || lastStatus.refreshDate.before(oldestStatusDate)) {
069            // try to refresh
070            SubscriptionStatusWrapper refreshStatus = getStatus(true);
071            // keep last success status in case of error
072            if ((refreshStatus == null || refreshStatus.isError()) && lastStatus != null && !lastStatus.isError()) {
073                instanceStatus = lastStatus;
074                instanceStatus.refreshDate = Calendar.getInstance();
075            }
076        }
077
078        return instanceStatus;
079    }
080
081    public void flush() {
082        instanceStatus = null;
083    }
084
085    public SubscriptionStatusWrapper getStatus(boolean forceRefresh) {
086        if (instanceStatus == null || forceRefresh) {
087            if (isRegistred()) {
088                try {
089                    instanceStatus = new SubscriptionStatusWrapper(getService().getConnector().getConnectStatus());
090                } catch (CanNotReachConnectServer e) {
091                    log.warn("can not reach Nuxeo Online Services", e);
092                    instanceStatus = new SubscriptionStatusWrapper("Nuxeo Online Services is not reachable");
093                    instanceStatus.canNotReachConnectServer = true;
094                } catch (ConnectClientVersionMismatchError e) {
095                    log.warn(
096                            "Nuxeo Connect Client does not have the required version to communicate with Nuxeo Online Services",
097                            e);
098                    instanceStatus = new SubscriptionStatusWrapper(e.getMessage());
099                    instanceStatus.versionMismatch = true;
100                } catch (ConnectSecurityError e) {
101                    log.warn("Can not authenticated against Nuxeo Online Services", e);
102                    instanceStatus = new SubscriptionStatusWrapper(e);
103                } catch (ConnectServerError e) {
104                    log.error("Error while calling Nuxeo Online Services", e);
105                    instanceStatus = new SubscriptionStatusWrapper(e.getMessage());
106                }
107            } else {
108                instanceStatus = new UnresgistedSubscriptionStatusWrapper();
109            }
110        }
111        return instanceStatus;
112    }
113
114}