ThirdPartyPluginLicenseStorageManager Example Code

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

Accepted Answer
0 votes

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

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.

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 :)

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?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 21, 2018 in Agile

What's new this quarter in Confluence Server - August 2018

Hello Atlassian Community! My name is Ada , and I'm the Product Marketing Manager for Confluence Server at Atlassian. If you missed   our last post, we're transitioning to quarterly updates&nb...

156 views 0 1
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you