Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.verhas.licensor;
 import java.util.Date;
 import java.util.UUID;

ExtendedLicense supports not only String features, but also Integer, Date and URL features. It is also able to check that the license is not expired and can check the revocation state of the license online.

Peter Verhas
 public class ExtendedLicense extends License {
 	final private static String EXPIRATION_DATE = "expiryDate";
 	final private static String DATE_FORMAT = "yyyy-MM-dd";
 	final private static String LICENSE_ID = "licenseId";
 	final private static String REVOCATION_URL = "revocationUrl";

Checks the expiration date of the license and returns true if the license has expired.

The expiration date is encoded in the license with the key expiryDate in the format yyyy-MM-dd. A license is expired if the current date is after the specified expiryDate.

Note that this method does not ensure license validity. You separately have to call License.isVerified() to ensure that the license was successfully verified.

The time is calculated using the default locale, thus licenses expire first in Australia, later in Europe and latest in USA.

true if the license is expired
 	public boolean isExpired() throws ParseException {
 		boolean expired = true;
 		Date expiryDate;
 		try {
 			expiryDate = getFeature(Date.class);
 			final GregorianCalendar today = new GregorianCalendar();
 			today.set(., 0);
 			today.set(., 0);
 			today.set(., 0);
 			today.set(., 0);
 			expired = today.getTime().after(expiryDate);
 		} catch (final Exception e) {
 			expired = true;
 		return expired;

Set the expiration date of the license. Since the date is stored in the format yyyy-MM-dd the actual hours, minutes and so on will be chopped off.

 	public void setExpiry(final Date expiryDate) {

Generates a new license id.

Note that this ID is also stored in the license thus there is no need to call setFeature(java.lang.String,java.util.UUID) separately after the UUID was generated.

Generating UUID can be handy when you want to identify each license individually. For example you want to store revocation information about each license. The url to check the revocation may contain the ${licenseId} place holder that will be replaced by the actual uuid stored in the license.

the generated uuid.
 	public UUID generateLicenseId() {
 		final UUID uuid = UUID.randomUUID();
 		return uuid;

Set the UUID of a license. Note that this UUID can be generated calling the method generateLicenseId(), which method automatically calls this method setting the generated UUID to be the UUID of the license.

licenseId the uuid that was generated somewhere, presumably not by generateLicenseId() because the uuid generated by that method is automatically stored in the license.
	public void setLicenseId(final UUID licenseId) {
	public UUID getLicenseId() {
		UUID licenseId = null;
		try {
			licenseId = getFeature(UUID.class);
catch (final Exception e) {
			licenseId = null;
		return licenseId;

Set an integer feature in the license.

name the name of the feature
i the value of the integer feature
	public void setFeature(final String namefinal Integer i) {

Set a date feature in the license.

name the name of the feature
date the date to be stored for the feature name in the license
	public void setFeature(final String namefinal Date date) {
		final DateFormat formatter = new SimpleDateFormat();

Set a URL feature in the license.

name the name of the feature
url the url to store in the license
	public void setFeature(final String namefinal URL url) {

Set an UUID feature in the license.

name the name of the feature
uuid the uuid to store in the license
	public void setFeature(final String namefinal UUID uuid) {
	public <T> T getFeature(final String namefinal Class<? extends T> klass)
result = null;
		final String resultString = getFeature(name);
		if (Integer.class == klass) {
			result = (T) (Integer) Integer.parseInt(resultString);
else if (Date.class == klass) {
			result = (T) new SimpleDateFormat()
else if (UUID.class == klass) {
			result = (T) UUID.fromString(getFeature(name));
else if (URL.class == klass) {
			result = (T) new URL(getFeature(name));
else {
			throw new IllegalArgumentException("'" + klass.toString()
"' is not handled");
		return result;

Get the revocation URL of the license. This feature is stored in the license under the name revocationUrl. This URL may contain the string ${licenseId} which is replaced by the actual license ID. Thus there is no need to wire into the revocation URL the license ID.

If there is no license id defined in the license then the place holder will not be replaced.

the revocation URL with the license id place holder filled in.
		URL url = null;
		final String revocationURLTemplate = getFeature();
		final String revocationURL;
		if (revocationURLTemplate != null) {
			final UUID licenseId = getLicenseId();
			if (licenseId != null) {
				revocationURL = revocationURLTemplate.replaceAll(
else {
				revocationURL = revocationURLTemplate;
			url = new URL(revocationURL);
		return url;

Set the revocation URL. This method accepts the url as a string that makes it possible to use a string that contains the ${licenseId} place holder.

url the url from where the revocation information can be downloaded
	public void setRevocationURL(final String url) {

Set the revocation URL. Using this method is discouraged in case the URL contains the ${licenseId} place holder. In that case it is recommended to use the setRevocationURL(java.lang.String) method instead.

url the revocation url
	public void setRevocationURL(final URL url) {

Check if the license was revoked or not. For more information see the documentation of the method isRevoked(boolean). Calling this method is equivalent to calling isRevoked(false), meaning that the license is signaled to be revoked if the revocation URL can not be reached.

true if the license was revoked and false if the license was not revoked. It also returns true if the revocation url is unreachable.
	public boolean isRevoked() {
		return isRevoked(false);

Check if the license is revoked or not. To get the revocation information the method tries to issue a http connection (GET) to the url specified in the license feature revocationUrl. If the URL returns anything with http status code 200 then the license is not revoked.

The url string in the feature revocationUrl may contain the place holder ${licenseId}, which is replaced by the feature value licenseId. This feature makes it possible to setup a revocation service and use a constant string in the different licenses.

The method can work in two different ways. One way is to ensure that the license is not revoked and return true only if it is sure that the license is revoked or revocation information is not available.

The other way is to ensure that the license is revoked and return false if the license was not revoked or the revocation information is not available.

The difference is whether to treat the license revoked when the revocation service is not reachable.

forceOnline should be true to treat the license revoked when the revocation service is not reachable. In this case the program using the license manager will treat the license revoked if the revocation service is not reachable. Setting this argument false makes the revocation handling more polite: if the license revocation service is not reachable then the license is treated as not revoked.
true if the license is revoked and false is the license is not revoked.
	public boolean isRevoked(final boolean forceOnline) {
		boolean revoked = true;
		try {
			final URL url = getRevocationURL();
			if (url != null) {
				final URLConnection connection = url.openConnection();
				if (connection instanceof HttpURLConnection) {
					final HttpURLConnection httpUrlConnection = (HttpURLConnectionconnection;
					final int responseCode = httpUrlConnection
					if (responseCode == 200) {
						revoked = false;
else {
				revoked = false;
catch (final IOException exception) {
			revoked = forceOnline;
		return revoked;
New to GrepCode? Check out our FAQ X