All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.licensing.LicenseMonitor Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alachisoft.ncache.licensing;


import Alachisoft.NCache.Common.Enum.EventType;
import Alachisoft.NCache.Common.IDisposable;
import Alachisoft.NCache.Common.Logger.EventLogger;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author usama_tariq
 */
public class LicenseMonitor implements IDisposable {

    private static LicenseMonitor _monitor = null;
    private Calendar _lastNotificationDate;
    private ExpirationStatus _lastExpirationStatus;
    private final TimerTask OnNotifyUser = new TimerTask() {

        @Override
        public synchronized void run() {

            ExpirationInfo expirationInfo = License.GetExpirationInfo();
            if (DoNotify(expirationInfo)) {
                switch (expirationInfo.getExpirationStatus()) {
                    case Expired:
                        EventLogger.LogEvent("NCache has expired on this machine.", EventType.ERROR);
                        LicenseManager.onLicenseExpiration();
                        break;
                    case InGracePeriod:
                        EventLogger.LogEvent(String.format("NCache has expired on this machine. Grace period of %d days will expire on %s.", expirationInfo.getGracePeriod(), LicenseHelper.getString(expirationInfo.getHardExpirationDate())), EventType.WARNING);
                        break;
                    case Licensed:
                        EventLogger.LogEvent(String.format("NCache will expire on this machine on %s.", LicenseHelper.getString(expirationInfo.getHardExpirationDate())), EventType.WARNING);
                        break;
                    case InEvaluation:
                        break;
                }
            }
        }
    };
    private Timer _notificationTimer;

    private LicenseMonitor() {

        _lastNotificationDate = LicenseHelper.getUTCDate();
        _lastNotificationDate.add(Calendar.DAY_OF_MONTH, -365);
        _lastExpirationStatus = ExpirationStatus.Licensed;
        long licenseCheckInterval = 24 * 60 * 60 * 1000;
        String interval = Alachisoft.NCache.Common.AppUtil.GetAppSetting("CacheServer.LicenseCheckInterval");
        try {
            licenseCheckInterval = Long.parseLong(interval);
        } catch (Exception e) {
        }
        _notificationTimer = new Timer();
        _notificationTimer.scheduleAtFixedRate(OnNotifyUser, new Date(), licenseCheckInterval);
    }

    public static LicenseMonitor getInstance() {
        return _monitor;
    }

    public static synchronized boolean VerifyLicenseMonitoring() {
        if (_monitor == null)
            _monitor = new LicenseMonitor();
        if (_monitor._notificationTimer == null)
            return false;
        return true;
    }

    private boolean DoNotify(ExpirationInfo expirationInfo) {
        boolean statusChanged = false;
        if (expirationInfo.getExpirationStatus() != _lastExpirationStatus) {
            _lastExpirationStatus = expirationInfo.getExpirationStatus();
            statusChanged = true;
        }

        switch (expirationInfo.getExpirationStatus()) {
            case Expired:
            case InGracePeriod:
                if (_lastNotificationDate.compareTo(LicenseHelper.getUTCDate()) == 0 && !statusChanged)
                    return false;
                return true;
            case InEvaluation:
                return false;
            case Licensed:
                long daysToExpire = 0;
                daysToExpire = LicenseHelper.DifferenceInDays(expirationInfo.getExpirationDate(), LicenseHelper.getUTCDate());
                if (daysToExpire > 60)
                    return false;
                else if (daysToExpire > 10) {
                    if (LicenseHelper.DifferenceInDays(LicenseHelper.getUTCDate(), _lastNotificationDate) > 7)
                        return true;
                    return false;
                } else {
                    if (_lastNotificationDate.compareTo(LicenseHelper.getUTCDate()) == 0)
                        return false;
                    return true;
                }
            default:
                return true;
        }
    }

    @Override
    public void dispose() {
        try {
            _lastNotificationDate = null;
            _lastExpirationStatus = null;
            if (_notificationTimer != null)
                _notificationTimer.cancel();
            _monitor = null;
        } catch (Exception ex) {

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy