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 /** 058 * @deprecated Since 9.2, use {@link #isRegistered()} instead. 059 */ 060 @Deprecated 061 public boolean isRegistred() { 062 return isRegistered(); 063 } 064 065 /** 066 * @since 9.2 067 */ 068 public boolean isRegistered() { 069 // no cache needed 070 return getService().isInstanceRegistered(); 071 } 072 073 public SubscriptionStatusWrapper getStatus() { 074 075 // get status (possibility from cache) 076 SubscriptionStatusWrapper lastStatus = getStatus(false); 077 078 // check freshness 079 Calendar oldestStatusDate = Calendar.getInstance(); 080 oldestStatusDate.add(Calendar.MINUTE, -REFRESH_PERIOD_MINUTES); 081 if (lastStatus == null || lastStatus.refreshDate.before(oldestStatusDate)) { 082 // try to refresh 083 SubscriptionStatusWrapper refreshStatus = getStatus(true); 084 // keep last success status in case of error 085 if ((refreshStatus == null || refreshStatus.isError()) && lastStatus != null && !lastStatus.isError()) { 086 instanceStatus = lastStatus; 087 instanceStatus.refreshDate = Calendar.getInstance(); 088 } 089 } 090 091 return instanceStatus; 092 } 093 094 public void flush() { 095 instanceStatus = null; 096 } 097 098 public SubscriptionStatusWrapper getStatus(boolean forceRefresh) { 099 if (instanceStatus == null || forceRefresh) { 100 if (isRegistered()) { 101 try { 102 instanceStatus = new SubscriptionStatusWrapper(getService().getConnector().getConnectStatus()); 103 } catch (CanNotReachConnectServer e) { 104 log.warn("Cannot reach Nuxeo Online Services", e); 105 instanceStatus = new SubscriptionStatusWrapper("Nuxeo Online Services is not reachable"); 106 instanceStatus.canNotReachConnectServer = true; 107 } catch (ConnectClientVersionMismatchError e) { 108 log.warn( 109 "Nuxeo Connect Client does not have the required version to communicate with Nuxeo Online Services", 110 e); 111 instanceStatus = new SubscriptionStatusWrapper(e.getMessage()); 112 instanceStatus.versionMismatch = true; 113 } catch (ConnectSecurityError e) { 114 log.warn("Cannot authenticate against Nuxeo Online Services", e); 115 instanceStatus = new SubscriptionStatusWrapper(e); 116 } catch (ConnectServerError e) { 117 log.error("Error while calling Nuxeo Online Services", e); 118 instanceStatus = new SubscriptionStatusWrapper(e.getMessage()); 119 } 120 } else { 121 instanceStatus = new UnresgistedSubscriptionStatusWrapper(); 122 } 123 } 124 return instanceStatus; 125 } 126 127}