001/* 002 * (C) Copyright 2010-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.connect.client.status; 021 022import java.util.Calendar; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.connect.connector.CanNotReachConnectServer; 027import org.nuxeo.connect.connector.ConnectClientVersionMismatchError; 028import org.nuxeo.connect.connector.ConnectSecurityError; 029import org.nuxeo.connect.connector.ConnectServerError; 030import org.nuxeo.connect.registration.ConnectRegistrationService; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * @author Tiry (tdelprat@nuxeo.com) 035 */ 036public class ConnectStatusHolder { 037 038 protected static ConnectStatusHolder instance; 039 040 protected SubscriptionStatusWrapper instanceStatus; 041 042 protected static final Log log = LogFactory.getLog(ConnectStatusHolder.class); 043 044 protected static final int REFRESH_PERIOD_MINUTES = 10; 045 046 public static ConnectStatusHolder instance() { 047 if (instance == null) { 048 instance = new ConnectStatusHolder(); 049 } 050 return instance; 051 } 052 053 protected ConnectRegistrationService getService() { 054 return Framework.getLocalService(ConnectRegistrationService.class); 055 } 056 057 public boolean isRegistred() { 058 // no cache needed 059 return getService().isInstanceRegistred(); 060 } 061 062 public SubscriptionStatusWrapper getStatus() { 063 064 // get status (possibility from cache) 065 SubscriptionStatusWrapper lastStatus = getStatus(false); 066 067 // check freshness 068 Calendar oldestStatusDate = Calendar.getInstance(); 069 oldestStatusDate.add(Calendar.MINUTE, -REFRESH_PERIOD_MINUTES); 070 if (lastStatus == null || lastStatus.refreshDate.before(oldestStatusDate)) { 071 // try to refresh 072 SubscriptionStatusWrapper refreshStatus = getStatus(true); 073 // keep last success status in case of error 074 if ((refreshStatus == null || refreshStatus.isError()) && lastStatus != null && !lastStatus.isError()) { 075 instanceStatus = lastStatus; 076 instanceStatus.refreshDate = Calendar.getInstance(); 077 } 078 } 079 080 return instanceStatus; 081 } 082 083 public void flush() { 084 instanceStatus = null; 085 } 086 087 public SubscriptionStatusWrapper getStatus(boolean forceRefresh) { 088 if (instanceStatus == null || forceRefresh) { 089 if (isRegistred()) { 090 try { 091 instanceStatus = new SubscriptionStatusWrapper(getService().getConnector().getConnectStatus()); 092 } catch (CanNotReachConnectServer e) { 093 log.warn("Cannot reach Nuxeo Online Services", e); 094 instanceStatus = new SubscriptionStatusWrapper("Nuxeo Online Services is not reachable"); 095 instanceStatus.canNotReachConnectServer = true; 096 } catch (ConnectClientVersionMismatchError e) { 097 log.warn( 098 "Nuxeo Connect Client does not have the required version to communicate with Nuxeo Online Services", 099 e); 100 instanceStatus = new SubscriptionStatusWrapper(e.getMessage()); 101 instanceStatus.versionMismatch = true; 102 } catch (ConnectSecurityError e) { 103 log.warn("Cannot authenticate against Nuxeo Online Services", e); 104 instanceStatus = new SubscriptionStatusWrapper(e); 105 } catch (ConnectServerError e) { 106 log.error("Error while calling Nuxeo Online Services", e); 107 instanceStatus = new SubscriptionStatusWrapper(e.getMessage()); 108 } 109 } else { 110 instanceStatus = new UnresgistedSubscriptionStatusWrapper(); 111 } 112 } 113 return instanceStatus; 114 } 115 116}