ThirdPartyPluginLicenseStorageManager Example Code

Bahar Çağlar October 29, 2012

I am trying to implement a license validiy in a jira plugin. I need this check in a velocity file.

I have a class for this. I cannot find any example about it. How can I make licence validity check in velocity file?

import com.atlassian.upm.api.license.entity.PluginLicense;
import com.atlassian.upm.license.storage.lib.PluginLicenseStoragePluginUnresolvedException;
import com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManager;

public class License {
	
	private final ThirdPartyPluginLicenseStorageManager licenseManager;
	
	public License(ThirdPartyPluginLicenseStorageManager licenseManager){
		this.licenseManager = licenseManager;
	}

	public boolean isLicenseValid(){
		try
        {
            //Check and see if a license is currently stored.
            //This accessor method can be used whether or not a licensing-aware UPM is present.
            if (licenseManager.getLicense().isDefined())
            {
                PluginLicense pluginLicense = licenseManager.getLicense().get();
                //Check and see if the stored license has an error. If not, it is currently valid.
                if (pluginLicense.getError().isDefined())
                {
                    //A license is currently stored, however, it is invalid (e.g. expired or user count mismatch)
                    return false;
                }
                else
                {
                    //A license is currently stored and it is valid.
                   return true;
                }
            }
            else
            {
                //No license (valid or invalid) is stored.
            	return false;
                
            }
        }
        catch (PluginLicenseStoragePluginUnresolvedException e)
        {
            //The current license status cannot be retrieved because the Plugin License Storage plugin is unavailable.
        	return false;
        }
	}
}

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
carlos.fernandez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 30, 2012

Hi,

Which element loads the velocity file? Servlet, JiraWebActionSuport...

In this controller Class, in constructor you inject the ThirdPartyPluginLicenseStorageManager component as licenseManager and then pass it as parameter when create License object:

public boolean hasLicense(){
     License license = new License(licenseManager);
     return license.isLicenseValid();
}

In the velocity file you could do this:

#if($action.hasLicense())

    //Error license

#else

    //...

or you could pass the result of the method as velocityparams and then check it in the velocity.

Regards

Bahar Çağlar October 30, 2012

Thank you Carlos. I am using JiraWebActionSupport. I created the method hasLicence(), but how can I create the licenceManager object? I looked the ThirdPartyPluginLicenseStorageManager API. However, it is an interface, so it has no constructor.

Bahar Çağlar October 30, 2012

Ok, I got it. I must use ThirdPartyPluginLicenseStorageManager in a servlet or jirawebaction. Pass parameters in constructor. Because it is an abstract class, and has no constructor. I used the code in this example https://developer.atlassian.com/display/UPM/Tutorial%3A+Adding+Licensing+Support+to+Your+Add-on I just added ThirdPartyPluginLicenseStorageManager to the constructor of my action class, and a method hasLicense(), its content is also copied from LicenseHelloServlet. It is totally works :)

Bahar Çağlar November 1, 2012

When license is invalid, settings page will not be displayed great :). However, plugin has custom field modules. When license has expired, the custom field is still on the custom field list, and the custom fields added before the expiration of the lisence are usable in related screens. How can I solve it? Any ideas?

TAGS
AUG Leaders

Atlassian Community Events