001/* 002 * (C) Copyright 2006-2014 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-2.1.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.ArrayList; 021import java.util.List; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026import org.nuxeo.connect.connector.http.ConnectUrlConfig; 027import org.nuxeo.connect.data.DownloadablePackage; 028import org.nuxeo.connect.data.SubscriptionStatusType; 029import org.nuxeo.connect.identity.LogicalInstanceIdentifier; 030import org.nuxeo.connect.identity.LogicalInstanceIdentifier.NoCLID; 031import org.nuxeo.connect.packages.PackageManager; 032import org.nuxeo.connect.packages.dependencies.TargetPlatformFilterHelper; 033import org.nuxeo.connect.update.PackageType; 034import org.nuxeo.ecm.admin.runtime.PlatformVersionHelper; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Store information about registration and possible updates 039 * 040 * @author Tiry (tdelprat@nuxeo.com) 041 */ 042public class ConnectUpdateStatusInfo { 043 044 protected static final String UNREGISTERED = "unregistered"; 045 046 protected static final String ONLINE_REGISTERED = "onlineregistered"; 047 048 protected static final String CONNECT_UNREACHABLE = "unreachable"; 049 050 protected static final String EXPIRED = "expired"; 051 052 protected String type; 053 054 protected String bannerPath; 055 056 protected Integer availableUpdateCount; 057 058 protected String feedUrl; 059 060 protected boolean registered; 061 062 protected static Log log = LogFactory.getLog(ConnectUpdateStatusInfo.class); 063 064 public static ConnectUpdateStatusInfo unregistered() { 065 ConnectUpdateStatusInfo status = new ConnectUpdateStatusInfo(); 066 status.type = UNREGISTERED; 067 status.setBannerPath("clientSideBanner"); 068 status.feedUrl = buildFeedUrl(false); 069 status.availableUpdateCount = 0; 070 status.registered = false; 071 return status; 072 } 073 074 public static ConnectUpdateStatusInfo ok() { 075 ConnectUpdateStatusInfo status = new ConnectUpdateStatusInfo(); 076 status.type = ONLINE_REGISTERED; 077 status.registered = true; 078 return status; 079 } 080 081 public static ConnectUpdateStatusInfo connectServerUnreachable() { 082 ConnectUpdateStatusInfo status = new ConnectUpdateStatusInfo(); 083 status.type = CONNECT_UNREACHABLE; 084 status.setBannerPath("clientSideBanner"); 085 status.feedUrl = buildFeedUrl(true); 086 status.availableUpdateCount = 0; 087 status.registered = true; 088 return status; 089 } 090 091 public static ConnectUpdateStatusInfo notValid() { 092 ConnectUpdateStatusInfo status = new ConnectUpdateStatusInfo(); 093 status.type = EXPIRED; 094 status.setBannerPath("serverSideBanner"); 095 status.registered = true; 096 return status; 097 } 098 099 protected static String buildFeedUrl(boolean registred) { 100 101 StringBuffer sb = new StringBuffer(); 102 103 sb.append(Framework.getProperty("org.nuxeo.connect.client.feedUrl", ConnectUrlConfig.getBaseUrl())); 104 sb.append("connect-gateway/jsonp/"); 105 106 if (registred) { 107 sb.append("registered"); 108 } else { 109 sb.append("unregistered"); 110 } 111 112 sb.append("?product="); 113 sb.append(PlatformVersionHelper.getPlatformFilter()); 114 if (registred) { 115 sb.append("&instance="); 116 try { 117 sb.append(LogicalInstanceIdentifier.instance().getCLID1()); 118 } catch (NoCLID e) { 119 log.error("Error in ConnectUpdateStatusInfo generation : No CLID is defined ..."); 120 } 121 } 122 123 sb.append("&callback=displayConnectUpdateStatus"); 124 return sb.toString(); 125 } 126 127 public String getIdentifier() { 128 try { 129 return LogicalInstanceIdentifier.instance().getCLID1(); 130 } catch (NoCLID e) { 131 return ""; 132 } 133 } 134 135 public String getDistributionLabel() { 136 return PlatformVersionHelper.getDistributionName().toUpperCase() + " " 137 + PlatformVersionHelper.getDistributionVersion(); 138 } 139 140 public String getDistributionName() { 141 return PlatformVersionHelper.getDistributionName().toUpperCase(); 142 } 143 144 public String getDistributionVersion() { 145 return PlatformVersionHelper.getDistributionVersion(); 146 } 147 148 protected int computeAvailableUpdateCount() { 149 if (ConnectStatusHolder.instance().getStatus().isConnectServerUnreachable()) { 150 return 0; 151 } 152 PackageManager pm = Framework.getLocalService(PackageManager.class); 153 String targetPlatform = PlatformVersionHelper.getPlatformFilter(); 154 155 List<DownloadablePackage> pkgs = pm.listUpdatePackages(PackageType.HOT_FIX, targetPlatform); 156 157 List<DownloadablePackage> localHotFixes = pm.listLocalPackages(PackageType.HOT_FIX); 158 159 List<DownloadablePackage> applicablePkgs = new ArrayList<>(); 160 161 for (DownloadablePackage pkg : pkgs) { 162 if (TargetPlatformFilterHelper.isCompatibleWithTargetPlatform(pkg, targetPlatform)) { 163 boolean isInstalled = false; 164 for (DownloadablePackage localPkg : localHotFixes) { 165 if (localPkg.getId().equals(pkg.getId())) { 166 isInstalled = true; 167 break; 168 } 169 } 170 if (!isInstalled) { 171 applicablePkgs.add(pkg); 172 } 173 } 174 } 175 return applicablePkgs.size(); 176 } 177 178 public String getBannerPath() { 179 return bannerPath; 180 } 181 182 protected void setBannerPath(String bannerName) { 183 if (!bannerName.startsWith("/")) { 184 bannerPath = "/incl/" + bannerName; 185 } else { 186 bannerPath = bannerName; 187 } 188 if (!bannerPath.endsWith(".xhtml")) { 189 bannerPath = bannerPath + ".xhtml"; 190 } 191 } 192 193 public int getAvailableUpdateCount() { 194 if (availableUpdateCount == null) { 195 availableUpdateCount = computeAvailableUpdateCount(); 196 } 197 return availableUpdateCount; 198 } 199 200 public String getType() { 201 return type; 202 } 203 204 public String getFeedUrl() { 205 return feedUrl; 206 } 207 208 public boolean isRegistered() { 209 return registered; 210 } 211 212 public boolean isExpired() { 213 return ConnectStatusHolder.instance().getStatus().status() == SubscriptionStatusType.EXPIRED; 214 } 215}