christian-schlichtherle / truelicense

An open source engine for license management on the Java Virtual Machine.
https://truelicense.namespace.global
Apache License 2.0
319 stars 66 forks source link

Unable to get keygen API to work #8

Closed lbruby closed 4 years ago

lbruby commented 4 years ago

Hi,

I need to know how to properly use the current keygen and keymgr Java APIs.

I have tried using the keygen API to generate a license using Java code, and the current code behavior does not seem to match the documentation for using the API that is on the truelicense.net website. Here is what is on the site:

VendorLicenseManager manager = LicenseManager.${edition}; LicenseVendorContext context = manager.context(); License bean = context.license(); Sink sink = context.pathStore(java.nio.file.Paths.get("${license-key-path}")); manager.generate(bean, sink);

Here is what I have found:

LicenseVendorContext does not have an exposed license() method. There is, however, a licenseFactory() method, which returns an object that has a license() method. LicenseVendorContext does not have a pathStore method. VendorLicenseManager does not have a generate() method, but does have a generateFromKey() method.

Here is what I have been able to do so far with the API methods that seem to be available:

VendorLicenseManager manager;

    VendorLicenseManager manager = LicenseManager.standard;
    LicenseManagementContext licMgrContext = manager.context();
    License prelic = licMgrContext.licenseFactory().license();
    try {
        LicenseKeyGenerator keyGenerator = manager.generateKeyFrom(prelic);
        License lic = keyGenerator.license();
           }
           catch( LicenseManagementException exc ) {
                  ...
          }

At this point it seems I have a valid license in the lic object. If I call lic.getSubject() I get what I would expect for how I generated the Maven project and built the code. Now I'm stuck not being able to figure out how to get the license/key file written. LicenseKeyGenerator has a storeTo() method that takes a Sink as an argument. Since there appears to be no pathStore method in LicenseManagementContext I can't figure out how to create the Sink correctly. I tried searching the code for examples, but didn't find anything that would help me. Also looked at the javadocs.

I assume, based on what I see in the code, that similar problems exist with the documentation for the keymgr API.

The code I pulled is from the develop branch.

Need updated API usage instructions.

Thank you.

christian-schlichtherle commented 4 years ago

Hi,

the API has been changed between TrueLicense 3 and 4. The website describes the API of version 3 and thus, is outdated. Updating it is a major undertaking, which is why it hasn't happened yet.

Your best option to understand the new API is to use the TrueLicense Maven Archetype to generate a project for you and inspect the generated code. For the license vendor part, you can look at the Main class in the *-keygen module. This class is using the API to implement the CLI for the license vendor. Taking from that, the updated code should look like this:

VendorLicenseManager manager = LicenseManager.standard;
License license = manager.context().licenseFactory().license();
manager.generateKeyFrom(license).saveTo(global.namespace.fun.io.bios.BIOS.file("license.key"));

Note that the Sink interface parameter for the saveTo method is specified and implemented by the Fun I/O library in TrueLicense 4. In TrueLicense 3, it was specified and implemented by TrueLicense itself, which is why you couldn't find it anymore.

Again, I think the best reference for the API is the code generated by the TrueLicense Maven Archetype. The old documentation web site still serves its purpose for all the other parts (CLI, WSI et al).

lbruby commented 4 years ago

Thank you. I think I see what to do now. Got so lost in the rest of the code, didn't think to go back and look at Main.java. Late for me, will give it a try tomorrow and close this out once I get it working.

lbruby commented 4 years ago

I have it working now. Thank you for the help.