Java example

A simple Java application that checks-out a single license

QuidLM discovers, validates, and parses licenses. To take advantage of that functionality in your application, you need to

  1. Initialize QuidLM. This is done by creating an Api object from the Qlm package. You can use this opportunity to provide the user's credentials. They come into play because licenses can be configured to be available only to a subset of users. You may set the user email and password to null if you do not intend to use this functionality. If you initialize the license system without specifying the user's email and password, QuidLM will not provide those licenses to your application.

     String user_email = null, password = null;
     var api = new Qlm.Api(user_email, password);
     // you can also omit the arguments - they are null by default
    

    If initialization encounters an error, the Qlm.LicenseInitError exception is thrown, and its .Message property explains the issue.

  2. Grab the license. The api.CheckoutLicense method takes two inputs - the license name and the version. The license name is typically just the name of your product. The license version, if provided, should be in the major.minor format. QuidLM treats this input as the minimum license version that your application is willing to accept. Typically, you pass your application’s current version for this parameter. This ensures that newer licenses enable older applications, but not vice versa — which is usually the desired behavior. If that is not what you want, you can hard‑code the version to "1.0" or another constant.

     string product_name = "MyProduct", min_version = "3.1";
     Qlm.License license = api.CheckoutLicense(product_name, min_version);
    

    If the requested license is not available, Qlm.LicenseCheckoutError exception is thrown, and your application may want to catch it to explain the error to the user and exit. Otherwise, the Qlm.Licenseobject contains useful information about the license, such as its expiration date (license.expiry) and its custom parameters, available via license.properties, which holds a Map of key-value pairs you can use to configure application behavior. For example, you can enable some features based on the presence of certain parameters: e.g., trial-license=true may prompt your application to display the "trial copy" badge. One other typical use of such a parameter is to limit the application performance, as in max-threads=10.

    The license is released when the license object is destroyed or its Dispose method is called.

The above are the required parts, but you can also take advantage of optional features, such as logging and license revocation. These additional features are demonstrated in a sample client application shipped with QuidLM. The application whose full source code is shown below is a full-fledged example, which can also be used for testing. It is a command line application that checks out the license specified on the command line for the specified number of seconds and prints licensing information. The comments in the source code explain standard functionality, such as initialization and license checkout, and introduce logging. The example is cross-platform and works on both Windows and UNIX systems.

/// Java client application: grab the specified license for
/// the specified length of time and print the licensing information.

public class ClientExample {
    static public void main(String[] args) {
        if (args.length < 1) {
            System.out.println(
                    "Expected some inputs on the command line.\n\n"
                            + "  product_name: the product that you need the license for                  [required]\n"
                            + "  min_version:  the minimum version of the product, in the major.minor format [1.0]\n"
                            + "  duration:     exit application after this many seconds                       [2]\n");
            System.exit(1);
        }

        String product_name = args[0];
        String minimum_product_version = args.length < 2 ? "1.0" : args[1];
        int duration = args.length < 3 ? 2 : Integer.parseInt(args[2]);

        System.err.println(
            "Using " + product_name + ":" + minimum_product_version + " for " + String.valueOf(duration) + "s");

        // log licensing errors
        // Qlm.Api.SetLogLevel(Qlm.LogLevel.err);
        // alternatively, set the environment variable: QLM_LOG_LEVEL=err

        // Licenses can be configured to be available only to a set of users.
        // If you initialize the license system without specifying the user's email and
        // password, you will never receive those licenses.
        String user_email = null, password = null;
        try {
            var api = new Qlm.Api(user_email, password);
            try (var license = api.CheckoutLicense(product_name, minimum_product_version)) {
                System.out.println("Got license for product version " + license.product_version + ".");
                System.out.println("Expiring " + license.expiry.toString() + ".\nFloating: "
                        + (license.is_floating ? "yes" : "no") + ".\nLicense properties:");
                license.properties.forEach((k, v) -> System.out.println("  " + k + ": " + v));

                Thread.sleep(duration * 1000);
                System.out.println("Returning the license");
                // the license is released when its owner object is destroyed or its .close()
                // method is called
            }
        } catch (Qlm.LicenseCheckoutError | LicenseInitError | InterruptedException exc) {
            System.out.println("Exception: " + exc.toString());
            System.exit(2);
        }
        System.exit(0);
    }
}

The compiled version of the example code is also included in the Qlm.jar package, which serves as both the client library and a test application. You can run the application from the command line as follows:

java -cp Qlm.jar Qlm.ClientExample "PowerReports XL" 1.0 10

In this example:

Your application may request several licenses. For example, instead of enabling optional features or modules using license parameters (e.g., feature1=true), the application may enable them based on the successful checkout of a relevant license (qlm_checkout_license("Feature1-MyProduct", "1.0")). This allows the main license and additional features to be purchased separately, at different times. Also, when separate features correspond to separate licenses, they may each have different properties — some anonymous, some assigned to named users; some node‑locked, others floating.