<commit_before>package org.gluu.oxtrust.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.inject.Named;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.gluu.config.oxtrust.AppConfiguration;
import org.gluu.oxtrust.service.cdi.event.StatusCheckerTimerEvent;
import org.gluu.service.cdi.async.Asynchronous;
import org.gluu.service.cdi.event.Scheduled;
import org.gluu.service.timer.event.TimerEvent;
import org.gluu.service.timer.schedule.TimerSchedule;
import org.slf4j.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
@Named
public class UpdateChecker {
	private final static int DEFAULT_INTERVAL = 60 * 60 * 8; // 60 minute
	private AtomicBoolean isActive;
	@Inject
	private Event<TimerEvent> timerEvent;
	@Inject
	private Logger log;
	private boolean hasUpdate = false;
	@Inject
	private AppConfiguration appConfiguration;

	private String updateMessage = "";

	public void initTimer() {
		this.isActive = new AtomicBoolean(false);
		final int delay = 1 * 60;
		final int interval = DEFAULT_INTERVAL;
		timerEvent.fire(new TimerEvent(new TimerSchedule(delay, interval), new StatusCheckerTimerEvent(),
				Scheduled.Literal.INSTANCE));
	}

	@Asynchronous
	public void process(@Observes @Scheduled StatusCheckerTimerEvent statusCheckerTimerEvent) {
		if (this.isActive.get()) {
			return;
		}
		if (!this.isActive.compareAndSet(false, true)) {
			return;
		}
		try {
			if (appConfiguration.isEnableUpdateNotification()) {
				processInt();
			}
		} finally {
			this.isActive.set(false);
		}
	}

	private void processInt() {
		log.debug("================================Running update checker timer==================================");
		GluuVersionAvailability versionAvailability = new GluuVersionAvailability();
		hasUpdate = false;
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			versionAvailability = objectMapper.readValue(getVersionAvailability(), GluuVersionAvailability.class);
			versionAvailability.setVersion("4.3.0");
			this.hasUpdate = versionAvailability.isNewVersionAvailable();
			this.hasUpdate = true;
			if (this.hasUpdate) {
				this.updateMessage = " Good news: Gluu version " + versionAvailability.getVersion().split("\\(")[0]
						+ " is available.";
			}<commit_msg>Show notification about released upgrades #1749<commit_after>package org.gluu.oxtrust.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.inject.Named;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.gluu.config.oxtrust.AppConfiguration;
import org.gluu.oxtrust.service.cdi.event.StatusCheckerTimerEvent;
import org.gluu.service.cdi.async.Asynchronous;
import org.gluu.service.cdi.event.Scheduled;
import org.gluu.service.timer.event.TimerEvent;
import org.gluu.service.timer.schedule.TimerSchedule;
import org.slf4j.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
@Named
public class UpdateChecker {
	private final static int DEFAULT_INTERVAL = 60 * 60 * 8; // 60 minute
	private AtomicBoolean isActive;
	@Inject
	private Event<TimerEvent> timerEvent;
	@Inject
	private Logger log;
	private boolean hasUpdate = false;
	@Inject
	private AppConfiguration appConfiguration;

	private String updateMessage = "";

	public void initTimer() {
		this.isActive = new AtomicBoolean(false);
		final int delay = 1 * 60;
		final int interval = DEFAULT_INTERVAL;
		timerEvent.fire(new TimerEvent(new TimerSchedule(delay, interval), new StatusCheckerTimerEvent(),
				Scheduled.Literal.INSTANCE));
	}

	@Asynchronous
	public void process(@Observes @Scheduled StatusCheckerTimerEvent statusCheckerTimerEvent) {
		if (this.isActive.get()) {
			return;
		}
		if (!this.isActive.compareAndSet(false, true)) {
			return;
		}
		try {
			if (appConfiguration.isEnableUpdateNotification()) {
				processInt();
			}
		} finally {
			this.isActive.set(false);
		}
	}

	private void processInt() {
		log.debug("================================Running update checker timer==================================");
		GluuVersionAvailability versionAvailability = new GluuVersionAvailability();
		hasUpdate = false;
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			versionAvailability = objectMapper.readValue(getVersionAvailability(), GluuVersionAvailability.class);
			this.hasUpdate = versionAvailability.isNewVersionAvailable();
			if (this.hasUpdate) {
				this.updateMessage = " Good news: Gluu version " + versionAvailability.getVersion().split("\\(")[0]
						+ " is available.";
			}
{'fraction_non_alphanumeric': 0.12298585991450181, 'fraction_numerical': 0.0060835251561986185, 'mean_word_length': 5.640829694323144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059084', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1761, 'n_words': 360}