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

Invms.Licence.LicenceRuntime Maven / Gradle / Ivy

The newest version!
package Invms.Licence;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 许可运行时
 */
public class LicenceRuntime {
	/**
	 * 许可文件
	 */
	private static String _licenceFile;
	/**
	 * 许可模型
	 */
	private static LicenceModel _licenceModel;
	/**
	 * 验证通知
	 */
	private static boolean _verifyCancel;
	/**
	 * 无效回调
	 */
	private static IInvalidCallback _invalidCallback;

	/**
	 * 是否是空或空字符串
	 * 
	 * @param value
	 *            字符串
	 * @return 返回是否是空或空字符串
	 */
	private static boolean isNullOrWhiteSpace(String value) {
		if (value == null) {
			return true;
		}
		return "".equals(value.trim());
	}

	/**
	 * 解析时间间隔
	 * 
	 * @param timeSpan
	 *            时间间隔
	 * @return 返回时间间隔
	 */
	private static long parseTimeSpan(String timeSpan) {
		String[] items = timeSpan.split(":");
		long time = 0;
		for (int i = 0, length = items.length; i < length; i++) {
			time += Long.parseLong(items[i]) * Math.pow(60, length - i - 1) * 1000;
		}
		return time;
	}

	/**
	 * 解析时间间隔
	 * 
	 * @param timeSpan
	 *            时间间隔
	 * @return 返回时间间隔
	 */
	private static String parseTimeSpan(long timeSpan) {
		long hour = timeSpan / (60 * 60 * 1000);
		long minute = (timeSpan % (60 * 60 * 1000)) / (60 * 1000);
		long second = ((timeSpan % (60 * 60 * 1000)) % (60 * 1000)) / 1000;
		return String.format("%06d:%02d:%02d", hour, minute, second);
	}

	/**
	 * 验证
	 */
	private static void verify() {
		while (!_verifyCancel) {
			try {
				Thread.sleep(parseTimeSpan(_licenceModel.verifySpan));
			} catch (Exception exception) {
			}
			try {
				verifyStopTime();
				verifyRemainSpan();
			} catch (Exception exception) {
			}
		}
	}

	/**
	 * 无效
	 */
	private static void invalid() {
		if (_invalidCallback == null) {
			System.exit(-1);
		} else {
			_invalidCallback.callback();
		}
	}

	/**
	 * 开始
	 * 
	 * @param licenceFile
	 *            许可文件
	 * @throws Exception
	 *             异常
	 */
	public static void start(String licenceFile) throws Exception {
		_licenceFile = licenceFile;
		_licenceModel = new LicenceModel();
		_licenceModel.load(_licenceFile);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		_licenceModel.startTime = simpleDateFormat.format(new Date());
		verifyStopTime();
		verifyRemainSpan();
		verifyRemainCount();
		if (!isNullOrWhiteSpace(_licenceModel.verifySpan)) {
			long verifySpan = parseTimeSpan(_licenceModel.verifySpan);
			if (verifySpan > 0) {
				_verifyCancel = false;
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						verify();
					}
				});
				thread.start();
			}
		}
	}

	/**
	 * 停止
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void stop() throws Exception {
		_verifyCancel = true;
		if (_licenceModel != null) {
			_licenceModel.save(_licenceFile);
		}
	}

	/**
	 * 验证停止时间
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void verifyStopTime() throws Exception {
		if (!isNullOrWhiteSpace(_licenceModel.stopTime)) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date stopTime = simpleDateFormat.parse(_licenceModel.stopTime);
			if (stopTime.getTime() <= new Date().getTime()) {
				invalid();
			}
		}
	}

	/**
	 * 验证剩余间隔
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void verifyRemainSpan() throws Exception {
		if (!isNullOrWhiteSpace(_licenceModel.remainSpan)) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date startTime = simpleDateFormat.parse(_licenceModel.startTime);
			long remainSpan = parseTimeSpan(_licenceModel.remainSpan);
			remainSpan = remainSpan - (new Date().getTime() - startTime.getTime());
			if (remainSpan <= 0) {
				_licenceModel.remainSpan = parseTimeSpan(0);
				_licenceModel.save(_licenceFile);
				invalid();
			} else {
				_licenceModel.remainSpan = parseTimeSpan(remainSpan);
				_licenceModel.save(_licenceFile);
				_licenceModel.startTime = simpleDateFormat.format(new Date());
			}
		}
	}

	/**
	 * 验证剩余次数
	 * 
	 * @throws Exception
	 *             异常
	 */
	public static void verifyRemainCount() throws Exception {
		if (!isNullOrWhiteSpace(_licenceModel.remainCount)) {
			long remainCount = Long.parseLong(_licenceModel.remainCount);
			if (remainCount <= 0) {
				_licenceModel.remainCount = String.valueOf(0);
				_licenceModel.save(_licenceFile);
				invalid();
			} else {
				_licenceModel.remainCount = String.valueOf(remainCount - 1);
				_licenceModel.save(_licenceFile);
			}
		}
	}

	/**
	 * 设置无效回调
	 * 
	 * @param action
	 *            回调函数
	 */
	public static void SetInvalidCallback(IInvalidCallback action) {
		_invalidCallback = action;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy