Snippets: Getting License Information from the Confluence API

May 6th, 2010 by

Sometimes one needs to look up license details of a running Confluence system .. perhaps for creating a commercial plugin or to display recommendations dependant from the license used. For this reason there are a few possibilities for receiving some license information from the Confluence API or the velocity context.

Note: This article is outdated since the Atlassian Marketplace was launched and a shiny new licensing API was added. Until this article is updated I strongly recommend to take a closer look at the detailed information that Atlassian is providing in the Developer Documentation.

 

Available Velocity/Sitemesh Objects

Parsing through the list of available velocity objects I found some useful objects/methods for handling license stuff:

  • GeneralUtil – available as $generalUtil in velocity
    static String checkPartnerDetails(com.atlassian.license.License license, String buildPartnerName)
    static String getVersionNumber()
    static boolean hasTooManyUsers()
    static boolean isLicenseExpired()
  • ConfluenceActionSupport – available as $action in velocity
    static String LICENSE_EXPIRED
    static String LICENSE_USERS_EXCEEDED
    boolean isDevMode()

Confluence API

The entry point for getting license information is the LicenseManager – there is also a LicenseCalculator but I haven’t tested it yet. I’ve build a simple Velocity Context Module Plugin sample to display some information from the API in a velocity template – it’s just a simple class and some xml in the plugin descriptor:

  • The pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
     
    <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     
     <parent>
     <groupId>com.atlassian.confluence.plugin.base</groupId>
     <artifactId>confluence-plugin-base</artifactId>
     <version>25</version>
     </parent>
     
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.hascode.confluence.demo</groupId>
     <artifactId>license-snippets</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     
     <name>hasCode.com - Confluence License Snippets</name>
     <packaging>atlassian-plugin</packaging>
     
     <properties>
     <atlassian.plugin.key>com.hascode.confluence.demo.license-snippets</atlassian.plugin.key>
     
     <!-- Confluence version -->
     <atlassian.product.version>3.1</atlassian.product.version>
     <!-- Confluence plugin functional test library version -->
     <atlassian.product.test-lib.version>1.4.1</atlassian.product.test-lib.version>
     <!-- Confluence data version -->
     <atlassian.product.data.version>3.1</atlassian.product.data.version>
     </properties>
     
     <description>hasCode.com - Confluence License Snippets</description>
     <url>https://www.hascode.com</url>
    </project>
  • The atlassian-plugin.xml
    <atlassian-plugin key="${atlassian.plugin.key}" name="license-snippets" pluginsVersion="2">
     <plugin-info>
     <description>hasCode.com - License Demo Plugin</description>
     <version>${project.version}</version>
     <vendor name="hasCode.com" url="https://www.hascode.com"/>
     </plugin-info>
     <velocity-context-item
     key="licenseHelper"
     name="License Demo Helper"
     context-key="licenseHelper"
     class="com.hascode.confluence.demo.VeloHelper"
     />
    </atlassian-plugin>
  • The class VeloHelper in the package com.hascode.confluence.demo
    package com.hascode.confluence.demo;
     
    import com.atlassian.confluence.setup.ConfluenceBootstrapConstants;
    import com.atlassian.license.License;
    import com.atlassian.license.LicenseManager;
    import com.atlassian.license.LicenseType;
    import com.atlassian.license.applications.confluence.ConfluenceLicenseTypeStore;
     
    public class VeloHelper {
    	private LicenseManager licenseManager;
    	private License confluenceLicense;
     
    	public VeloHelper() {
    		licenseManager = LicenseManager.getInstance();
    		confluenceLicense = licenseManager.getLicense(ConfluenceBootstrapConstants.DEFAULT_LICENSE_REGISTRY_KEY);
    		// or: confluenceLicense = licenseManager.getLicense("CONF");
    	}
     
    	public LicenseManager getLicenseManager() {
    		return licenseManager;
    	}
     
    	public LicenseType getConfluenceLicenseType() {
    		return confluenceLicense.getLicenseType();
    	}
     
    	public long getConfluenceLicenseDuration() {
    		return confluenceLicense.getLicenseDuration();
    	}
     
    	public double getNumberofAllowedUsers() {
    		// return the number of allowed users for academic version
    		// for unlimited licenses License.getUsers() will return a value greater
    		// than 9999, typically 10000
    		// source: http://forums.atlassian.com/thread.jspa?messageID=257285668
    		return confluenceLicense.getUsers();
    	}
     
    	public boolean isAcademicLicense() {
    		if (confluenceLicense.getLicenseType().equals(ConfluenceLicenseTypeStore.ACADEMIC)) {
    			return true;
    		}
    		return false;
    	}
     
    	public boolean isEvaluationLicense() {
    		if (confluenceLicense.getLicenseType().equals(ConfluenceLicenseTypeStore.EVALUATION)) {
    			return true;
    		}
    		return false;
    	}
     
    	// [..]
    }
  • Run the stuff
    mvn -Pplugin-debug

Available License Types

To evaluate which license is currently used simply compare the received license type to the types available in ConfluenceLicenseTypeStore as they are:

ConfluenceLicenseTypeStore.ACADEMIC
ConfluenceLicenseTypeStore.COMMUNITY
ConfluenceLicenseTypeStore.DEMONSTRATION
ConfluenceLicenseTypeStore.DEVELOPER
ConfluenceLicenseTypeStore.EVALUATION
ConfluenceLicenseTypeStore.FULL_LICENSE
ConfluenceLicenseTypeStore.NON_PROFIT
ConfluenceLicenseTypeStore.OPEN_SOURCE

Third party software

The nice guys from Customware have created License Support Libraries and posted some helpful information about licensing a plugin – in addition you’re able to check out the libs from https://svn.atlassian.com/svn/public/contrib/common/net.customware.license/trunk/

I haven’t tested these tools – perhaps it is worth a look .. there is also an ongoing discussion about this topic in the Confluence Developer Forum from Atlassian..

Quick Testing the Examples above

If you want a quick test of the examples above using the existing- or created velocity objects just go to the Confluence Administration Area > Look & Feel > Layouts and change an existing decorator e.g. the page decorator and use the velocity objects.If you do so – be sure to have the standard Confluence theme activated..

Here some screenshots using the created velocity helper object in the page decorator and the output of the amount of allowed users in the page view

Links

Tags: , , , , , , , ,

Search
Tags
Categories