Java example
A simple Java application that checks-out a single license
There are only two essential steps in using QuidLM to implement licensing in your application. The security, validity, and license content are managed by QuidLM. To use that functionality, you need to
-
Initialize QuidLM. This is done by creating an
Apiobject from theQlmpackage. You can use this opportunity to provide the user's credentials. They come into play because licenses can be configured as available only to a subset of users. You are free to set the user email and password tonullif you do not intend to use this functionality. If you initialize the license system without specifying user email and password, QuidLM will never give 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 defaultIf the initialization encounters an error, the
Qlm.LicenseInitErrorexception is thrown, and its.Messageproperty explains the issue. -
Grab the license. The
api.CheckoutLicensemethod takes two inputs - the license name and version. The license name is typically just the name of your product. The license version, if provided, should be in themajor.minorformat. The QuidLM will treat this input as the minimal license version that your current application is willing to accept. You normally pass the current version of your application for this parameter. That way, newer licenses enable older applications, but not vice versa, which is typically what you want. If for some reason that is not what you want, you can hard-code that version to "1.0" or some other 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.LicenseCheckoutErrorexception will be thrown, and your application may want to catch it to explain the error to the user and exit. Otherwise, theQlm.Licenseobject contains the useful information about the license, such as its expiration date (license.expiry) and thelicense.propertiesproperty holds a Dictionary of license parameters - a set of key-value pairs, which you, the developer, can use to set the application's behavior. For example, you can enable some features based on the presence of certain parameters: e.g.,trial-license=truemay prompt your application to display the "trial copy" badge somewhere. One other typical use of such a parameter is to limit the application performance, as inmax-threads=10.The license is released when the
licenseobject is destroyed or itsDisposemethod is called.
The above are the necessary parts, but you can take advantage of optional features, such as logging and license revocation. Those 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 grabs the license specified on the command line for the specified number of seconds and prints the licensing information. The comments in the source code explain the standard functionality, such as the initialization and the license checkout mentioned above, and introduce the logging functionality. The example is cross-platform - it works on 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 as available only to a set of users.
// If you initialize the license system without specifying user 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 above code is also built into Qlm.jar package,
which is 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 the above example, Qlm.jar is the Java package that contains both the client library and the example application -
specify the full file path to it if it is not in the current folder. Qlm.ClientExample is the fully qualified
class name that defines the example application, PowerReports XL is the name of the license that the application
will checkout and 1.0 is the license version. Finally, 10 means that after grabbing the license, the application will
idle for 10 seconds before exiting.