How to access LicenseManager from within gui component

I need to get an instance of LicenseManager from within the “Client” project for the new GUI components I am creating. How do I go about doing this. For example, in the Components Example in HelloWorldComponent.java how would I get the LicenseState for that component within it’s code so I can disable features when trial mode is expired. Thanks!

If you’re using the skeleton creator, be sure that “Create Hook Class” is checked for the client scope. Then in ClientHook.java, you’ll should see @Override public void startup(ClientContext context, LicenseState activationState) throws Exception { // TODO write code here! } and you can implement @Override public void notifyActivationStateChanged(LicenseState licenseState) { // some code here } That should give you what you need.

Thanks, I’m closer to getting it to work now that checked that checkbox. But the ClientHook’s startup function doesn’t seem to be getting called.

I’m thinking the problem is in my module.xml file, but I’ve tried many different combinations of values and none seem to work.

[code]<?xml version="1.0" encoding="UTF-8"?>

com.schrolltec.mymodulename MyModuleName My Module Description license.html index.html 1.0.0.0 7.6.0 5 false
	<!-- Load in our jars for each scope -->
	<jar scope="D">time-designer.jar.pack.gz</jar>
	<jar scope="DC">time-client.jar.pack.gz</jar>


	<!-- Tell the Gateway and/or Designer where to find the hooks -->
	<hook scope="C">com.schrolltec.mymodulename.client.ClientHook</hook>
	<hook scope="D">com.schrolltec.mymodulename.designer.DesignerHook</hook>

[/code]

I’ve tried putting print statements in my ClientHook.java file and I’ve also had it try to set values in a singleton class just to be sure it wasn’t the print statements failing for some reason. I also changed the import statements to ones that are not deprecated. This is what my ClientHook.java file looks like now.

[code]package com.schrolltec.mymodulename.client;

import com.inductiveautomation.ignition.client.model.ClientContext;
import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.vision.api.client.AbstractClientModuleHook;

public class ClientHook extends AbstractClientModuleHook {

@Override
public void startup(ClientContext context, LicenseState activationState)
		throws Exception {
	// TODO write code here!
	System.out.println("Client Hook Started.");
	
}
@Override
public void notifyActivationStateChanged(LicenseState activationState){
	System.out.println("Activation State Changed");
}

}[/code]

Thanks again!

Your module.xml file looks fine. How do you know the methods are not being ran?

Hmm, yeah I’m still very stumped.
I’m having the ClientHook.java class pass the LicenseState to a singleton class as follows:

The ClientHook.java file:

[code]package com.mycompanyname.mymodulename.client;

import com.inductiveautomation.ignition.client.model.ClientContext;
import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.vision.api.client.AbstractClientModuleHook;

public class ClientHook extends AbstractClientModuleHook {

@Override
public void startup(ClientContext context, LicenseState activationState)
		throws Exception {
	// TODO write code here!
	MyModuleLicenseManager mmlm = MyModuleLicenseManager.getInstance();
	mmlm.setLicenseState(activationState);
	System.out.println("Client Hook Started.");
	
}
@Override
public void notifyActivationStateChanged(LicenseState activationState){
	MyModuleLicenseManager mmlm = MyModuleLicenseManager.getInstance();
	mmlm.setLicenseState(activationState);
	System.out.println("Activation State Changed");
}

}[/code]

My License Manager Singleton Class:

[code]package com.mycompanyname.mymodulename.client;

import com.inductiveautomation.ignition.common.licensing.LicenseMode;
import com.inductiveautomation.ignition.common.licensing.LicenseState;

//singleton class
public class MyModuleLicenseManager {
//singleton instance
private static MyModuleLicenseManager instance = null;
private LicenseState ls;

protected MyModuleLicenseManager(){
	
}
public static MyModuleLicenseManager getInstance(){
	if(instance == null) {
		instance = new MyModuleLicenseManager();
	}
	return instance;
}
public void setLicenseState(LicenseState ls){
	this.ls = ls;
}

public boolean getLicenseExpired(){
	if(ls.getLicenseMode() == LicenseMode.Trial && ls.isTrialExpired() == true){
		return true;
	} else{
		return false;
	}
}

}[/code]

Now when I call this from one of my components’ java files under my client project, they give me a null pointer exception because the variable “ls” in my singleton class is null. It wouldn’t be null if the Client hook was getting run. None of the print statements in the ClientHook.java file are getting printed to the console either.

Here is the code i’m using in one of my files in my com.mycompanyname.mymodulename.client package:

if (MyModuleLicenseManager.getInstance().getLicenseExpired()) { //trial expired expiredLabel.setText("Trial Expired"); }

You dont want to create your own LicenseManager. Here is the code we commonly use

	protected static GatewayContext context;
	Logger logger = Logger.getLogger(this.getClass());
	private LicenseStateUpdateListener licenseListener;
	private static boolean licenseExpired;

	@Override
	public void setup(final GatewayContext context) {

		GatewayHook.context = context;

		licenseListener = new LicenseStateUpdateListener() {

			@Override
			public void licenseStateUpdated(LicenseStateUpdateEvent e) {
				boolean expired = e.getLicenseState().isTrialExpired();

				if (licenseExpired != expired) {
					licenseExpired = expired;
					if (licenseExpired) {
						logger.warn("Module '" + ScriptingCoreData.MODULE_ID
								+ "' stopped due to trial timeout.");
						licenseExpired = true;
					}
				} else {
					logger.info("License Changed. Starting KSB Scripts Module");
					licenseExpired = false;
				}
			}
		};

		GatewayHook.context.getLicenseManager()
				.addLicenseStateUpdateListener(ScriptingCoreData.MODULE_ID,
						licenseListener);

	}

	@Override
	public void shutdown() {

		GatewayHook.context.getLicenseManager()
				.removeLicenseStateUpdateListener(ScriptingCoreData.MODULE_ID,
						licenseListener);
	}

I think I figured it out, not 100% sure though since now I have to wait another 2 hours for my trial mode to time out. I added the same code to set the activation state in my little singleton class residing in my client project from the DesignerHook.java in the designer project as well. Now I don’t get null pointer exceptions in the designer, and I’m not getting them when I launch a client either. Some reason I thought that the client was running inside of the designer and that it’s ClientHook.java file should be running when just the designer is running. I was wrong; but at least I learned a bit more about the architecture. Thanks all for the responses during a holiday weekend.