@Override public void run() { GatewayVersion latestVersion = fetchLatestVersion(); if (latestVersion != null) { updateCheckService.setLatestGatewayVersion(latestVersion); } }
protected void setLatestGatewayVersion(GatewayVersion newlatestVersion) { if (this.latestVersion == null || this.latestVersion.compareTo(newlatestVersion) < 0) { synchronized (this) { this.latestVersion = newlatestVersion; } if (newlatestVersion.compareTo(currentVersion) > 0) { notifyListeners(); } } }
private void notifyListeners() { for (UpdateCheckListener listener : listeners) { if (listener != null) { listener.newVersionAvailable(currentVersion, getLatestGatewayVersion()); } } }
/** * Adds a @UpdateCheckListener who will be notified when the version changes, * @param newListener */ public void addListener(UpdateCheckListener newListener) { GatewayVersion latestGatewayVersion = this.getLatestGatewayVersion(); if (latestGatewayVersion != null && latestGatewayVersion.compareTo(currentVersion) > 0) { newListener.newVersionAvailable(currentVersion, latestGatewayVersion); } newListener.setUpdateCheckService(this); listeners.add(newListener); } }
@Override public void start() throws Exception { // add listeners listeners.clear(); addListener(new UpdateCheckLoggingListener()); Map<String, Object> serviceSpecificObjects = serviceContext.getServiceSpecificObjects(); Object managementListener = serviceSpecificObjects.get(MANAGEMENT_UPDATE_CHECK_LISTENER); if (managementListener != null && managementListener instanceof UpdateCheckListener) { addListener((UpdateCheckListener) managementListener); } for (UpdateCheckListener listener : listeners) { listener.setUpdateCheckService(this); } scheduledTasks = scheduler.scheduleAtFixedRate(new UpdateCheckTask(this, versionServiceUrl, productName), 0, 7, DAYS); }
@Override public boolean equals(Object obj) { boolean result; if (!(obj instanceof GatewayVersion)) { result = false; } else if (obj == this) { result = true; } else { result = this.compareTo((GatewayVersion) obj) == 0; } return result; }
/** * Forces a check for an update and registers the listener if it is not already registered * @param updateCheckListener */ public void checkForUpdate(UpdateCheckListener updateCheckListener) { listeners.add(updateCheckListener); if (scheduler != null) { scheduler.schedule(new UpdateCheckTask(this, versionServiceUrl, productName), 0, SECONDS); } else { // the scheduler won't be provided if the service isn't actually running, // but management may still ask for a check on update new UpdateCheckTask(this, versionServiceUrl, productName).run(); } }
String updateVersionUrl = getVersionServiceUrl(); try { URL url = new URL(updateVersionUrl); JSONObject root = new JSONObject(tokener); String version = root.getString("version"); latestVersion = parseGatewayVersion(version);
@Override public String getAvailableUpdateVersion() { return (this.latestVersion != null) ? this.latestVersion.toString() : ""; }
@Override public Service newService(String serviceType) { assert "update.check".equals(serviceType); return new UpdateCheckService(); } }
@Override public void checkForUpdate() { if (updateCheckService != null) { updateCheckService.checkForUpdate(this); } else { // Requirement to run check even is service isn't running Service service = ServiceFactory.newServiceFactory().newService("update.check"); if (service != null) { updateCheckService = (UpdateCheckService) service; updateCheckService.checkForUpdate(this); } } }
/** * Parses a GatewayVersion from a String * @param version * @return * @throws Exception */ public static GatewayVersion parseGatewayVersion(String version) throws Exception { if ("develop-SNAPSHOT".equals(version)) { return new GatewayVersion(0, 0, 0); } else { String regex = "(?<major>[0-9]+)\\.(?<minor>[0-9]+)\\.(?<patch>[0-9]+)-?(?<rc>[RC0-9{3}]*)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(version); if (matcher.matches()) { int major = Integer.parseInt(matcher.group("major")); int minor = Integer.parseInt(matcher.group("minor")); int patch = Integer.parseInt(matcher.group("patch")); String rc = matcher.group("rc"); return new GatewayVersion(major, minor, patch, rc); } else { throw new IllegalArgumentException(String.format("version String is not of form %s", regex)); } } }
public UpdateCheckService() { ProductInfo productInfo = getProductInfoInstance(); productName = productInfo.getTitle().replaceAll("\\s+", ""); try { String version = productInfo.getMajor() +"."+ productInfo.getMinor() +"."+ productInfo.getPatch(); currentVersion = parseGatewayVersion(version); } catch (Exception e) { throw new RuntimeException("Could not locate a product version associated with the jars on the classpath", e); } final String productEdition = productInfo.getEdition().replaceAll("\\s+", ""); versionServiceUrl = (productEdition.toLowerCase().contains("enterprise")) ? "https://version.kaazing.com" : "https://version.kaazing.org"; }
@Override public void newVersionAvailable(GatewayVersion currentVersion, GatewayVersion latestGatewayVersion) { if (latestVersion == null || latestVersion.compareTo(latestGatewayVersion) < 0) { this.latestVersion = latestGatewayVersion; } }