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