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

com.jk.license.server.LicenseFacade Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jk.license.server;

import java.io.IOException;
import java.util.Date;

import com.jk.license.License;
import com.jk.license.LicenseException;

/**
 * The Class LicenseFacade.
 *
 * @author Jalal Kiswani
 */
public final class LicenseFacade {

	/**
	 * Validate license.
	 *
	 * @param licenseId
	 *            the license id
	 * @param uniqueId
	 *            the unique id
	 * @throws LicenseException
	 *             the license exception
	 * @throws Exception
	 *             the exception
	 */
	protected void validateLicense(final int licenseId, final String uniqueId) throws LicenseException, Exception {
		try {
			final LicenseRepository reposiory = LicenseRepository.getInstance(true);
			final License localLicense = reposiory.findLicenseByUniqueId(uniqueId);
			if (!uniqueId.equals(localLicense.getUserUniqueId())) {
				throw new LicenseException("Invalid License", LicenseConstants.ERROR_INVALID_USER_UNIQUE_ID);
			}
			if (new Date().after(localLicense.getExpiryDate())) {
				final LicenseException licenseException = new LicenseException("License expired",
						LicenseConstants.ERROR_LICENSE_EXPIRED);
				localLicense.setEnabled(false);
				reposiory.saveLicenses();
				throw licenseException;
			}
			if (!localLicense.isEnabled()) {
				throw new LicenseException("licenses is disabled ", LicenseConstants.ERROR_LICENSE_DISABLED);
			}
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy