org.gridgain.grid.tools.license.GridLicenseGeneratorV1.java Source code

Java tutorial

Introduction

Here is the source code for org.gridgain.grid.tools.license.GridLicenseGeneratorV1.java

Source

// Copyright (C) GridGain Systems Licensed under GPLv3, http://www.gnu.org/licenses/gpl.html

/*  _________        _____ __________________        _____
 *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
 *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
 *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
 *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
 */

package org.gridgain.grid.tools.license;

import org.apache.commons.beanutils.*;
import org.apache.commons.beanutils.converters.*;
import org.gridgain.grid.*;
import org.gridgain.grid.kernal.controllers.license.*;
import org.gridgain.grid.kernal.controllers.license.impl.*;
import org.gridgain.grid.typedef.*;
import org.gridgain.grid.typedef.internal.*;
import javax.xml.bind.*;
import java.io.*;
import java.security.*;
import java.util.*;

/**
 * GridGain license generator.
 *
 * @author 2012 Copyright (C) GridGain Systems
 * @version 4.0.2c.12042012
 */
public class GridLicenseGeneratorV1 {
    /** Property key-value separator. */
    private static final char SEPARATOR = '=';

    /** Default license version. */
    private static final String LIC_VER = "1.0";

    /** Default issue organization. */
    private static final String ISSUE_ORG = "GridGain Systems";

    /** Available parameters with mapping to license properties. */
    private static final Map<String, String> params = new HashMap<String, String>();

    /** Key store. */
    private static final String KEY_STORE = "gglicense.store";

    /** Key store password. */
    private static final String KEY_STORE_PWD = "gr1dga1n";

    /** Key alias. */
    private static final String KEY_ALIAS = "gglicense";

    /** Default license file name. */
    private static final String DFLT_LIC_FILE_NAME = "gridgain-license.xml";

    /** */
    private static final String FS = File.separator;

    /**
     * Converter registration and parameters initialization.
     */
    static {
        DateTimeConverter c = new DateConverter();

        c.setPatterns(new String[] { "MM/dd/yyyy" });

        ConvertUtils.register(c, Date.class);

        ConvertUtils.register(new AbstractConverter() {
            @SuppressWarnings({ "unchecked" })
            @Override
            protected Object convertToType(Class cls, Object o) throws Throwable {
                assert GridLicenseType.class.isAssignableFrom(cls);
                assert o instanceof String;

                return Enum.valueOf(cls, (String) o);
            }

            @Override
            protected Class getDefaultType() {
                return GridLicenseType.class;
            }
        }, GridLicenseType.class);

        params.put("user-org", "userOrganization");
        params.put("user-www", "userWww");
        params.put("user-email", "userEmail");
        params.put("type", "type");
        params.put("expire-date", "expireDate");
        params.put("issue-date", "issueDate");
        params.put("max-nodes", "maxNodes");
        params.put("max-cpus", "maxCpus");
        params.put("file", null);
    }

    /**
     * Generate license by program parameters.
     * Example set of parameters: "user-org=User Organization" "user-www=www.user.org"
     * "user-email=info@user-org.org" "type=ENT" "issue-date=08/25/2011" "expire-date=08/26/2011" "max-cpus=40"
     * "max-nodes=40"
     *
     * @param args Program parameters.
     * @throws GridException If any exception occurs while license generating.
     */
    public static void main(String[] args) throws GridException {
        Map<String, String> argMap = getArgumentsMap(args);

        if (argMap.isEmpty())
            throw new GridException("User arguments cannot be empty.");

        GridLicenseV1 lic = buildLicense(argMap);

        saveLicense(lic, argMap);
    }

    /**
     * Ensures singleton.
     */
    private GridLicenseGeneratorV1() {
        // No-op.
    }

    /**
     * Generates and saves license file.
     *
     * @param lic License.
     * @param args User arguments.
     * @throws GridException If any exception occurs while license generation or saving.
     */
    private static void saveLicense(GridLicenseV1 lic, Map<String, String> args) throws GridException {
        assert lic != null;
        assert args != null;

        String path = args.get("file");

        path = U.getGridGainHome() + FS + "keystore" + FS + (F.isEmpty(path) ? DFLT_LIC_FILE_NAME : path);

        try {
            Marshaller marsh = JAXBContext.newInstance(GridLicenseV1Adapter.class).createMarshaller();

            marsh.setProperty("jaxb.encoding", "UTF-8");
            marsh.setProperty("jaxb.formatted.output", true);

            marsh.marshal(lic, new File(path));

            System.out.println("License was generated: " + path);
        } catch (Exception e) {
            throw new GridException("Failed to generate and store license in file: " + path, e);
        }
    }

    /**
     * Build license.
     *
     * @param args User arguments.
     * @return License.
     * @throws GridException If any exception occurs while license building.
     */
    private static GridLicenseV1 buildLicense(Map<String, String> args) throws GridException {
        assert args != null;

        GridLicenseV1Adapter lic = new GridLicenseV1Adapter();

        setUserArguments(lic, args);
        setSystemProperties(lic);

        return lic;
    }

    /**
     * Sets license user properties.
     *
     * @param lic License.
     * @param args User arguments.
     * @throws GridException If property set failed.
     */
    private static void setUserArguments(GridLicenseV1Adapter lic, Map<String, String> args) throws GridException {
        assert lic != null;
        assert args != null;

        if (!F.isEmpty(args)) {
            for (Map.Entry<String, String> e : args.entrySet()) {
                String prop = params.get(e.getKey());

                if (F.isEmpty(prop))
                    continue;

                String val = e.getValue();

                assert !F.isEmpty(val);

                try {
                    BeanUtils.setProperty(lic, prop, val);
                } catch (Exception ex) {
                    throw new GridException("Failed to set property value [prop=" + prop + ", val=" + val + ']',
                            ex);
                }
            }
        }
    }

    /**
     * Sets license system properties.
     *
     * @param lic License.
     * @throws GridException If property set failed.
     */
    private static void setSystemProperties(GridLicenseV1Adapter lic) throws GridException {
        assert lic != null;

        lic.setId(UUID.randomUUID());
        lic.setVersion(LIC_VER);
        lic.setIssueOrganization(ISSUE_ORG);

        if (lic.getIssueDate() == null)
            lic.setIssueDate(new Date());

        try {
            lic.setSignature(GridLicenseUtil.createSignatureV1(getKey(), lic));
        } catch (GeneralSecurityException e) {
            throw new GridException("Failed to create signature for license:" + lic, e);
        }
    }

    /**
     * Load private key from key store.
     *
     * @return Private key.
     * @throws GridException If key loading failed.
     */
    private static PrivateKey getKey() throws GridException {
        KeyStore ks;

        try {
            ks = KeyStore.getInstance(KeyStore.getDefaultType());
        } catch (Exception e) {
            throw new GridException("Failed to get key store instance.", e);
        }

        File ksFile = new File(U.getGridGainHome() + FS + "keystore", KEY_STORE);

        if (!ksFile.exists() || !ksFile.canRead())
            throw new GridException("Key store file doesn't exist or is not readable: " + ksFile.getAbsolutePath());

        InputStream in;

        try {
            in = new FileInputStream(ksFile);
        } catch (IOException e) {
            throw new GridException("Failed to open key store file: " + ksFile.getAbsolutePath(), e);
        }

        try {
            ks.load(in, KEY_STORE_PWD.toCharArray());

            return (PrivateKey) ks.getKey(KEY_ALIAS, KEY_STORE_PWD.toCharArray());
        } catch (Exception e) {
            throw new GridException("Failed to get private key from key store [keystore=" + KEY_STORE + ", alias="
                    + KEY_ALIAS + ']', e);
        } finally {
            U.close(in, null);
        }
    }

    /**
     * Gets arguments map from program arguments.
     *
     * @param args Program arguments.
     * @return Arguments map.
     * @throws GridException If any program argument is invalid.
     */
    private static Map<String, String> getArgumentsMap(String[] args) throws GridException {
        if (F.isEmpty(args))
            return Collections.emptyMap();

        Map<String, String> map = new HashMap<String, String>();

        for (String s : args) {
            int idx = s.indexOf(SEPARATOR);

            if (idx <= 0)
                throw new GridException("Invalid key-value argument in: " + s);

            String key = s.substring(0, idx);

            if (!params.containsKey(key))
                throw new GridException("Unknown argument: " + key);

            if (map.containsKey(key))
                throw new GridException("Duplicate argument: " + key);

            String val = s.substring(idx + 1);

            if (val.isEmpty())
                throw new GridException("Argument value can't be empty in: " + s);

            map.put(key, val);
        }

        return map;
    }
}