All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.licensing.config.ConfigHandler Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alachisoft.ncache.licensing.config;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

import static com.alachisoft.ncache.ncactivate.utils.RegUtil.nCacheInfo;

public class ConfigHandler {

    public static NCacheInfo readNCacheInfo(String path) throws IOException {
        //Initialize a list of employees
        NCacheInfo info = null;
        try {
            File file = new File(path);

            if (!file.exists()) {

                throw new FileNotFoundException("Unable to locate the file to read it");

            }
            info = new NCacheInfo();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File(path));
            document.getDocumentElement().normalize();

            info.setProduct(parseProductInfo(document));
            info.setUserInfo(parseUserInfo(document));
            info.setDeploymentInfo(parseDeploymentInfo(document));


        } catch (SAXException | ParserConfigurationException ex) {
        }
        return info;
    }

    private static ProductInfo parseProductInfo(Document document) {
        ProductInfo productInfo = new ProductInfo();
        Node nNode = document.getElementsByTagName("product-info").item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            productInfo.setDotNetInstallMode(eElement.getAttribute("dotnet-install-mode"));
            productInfo.setHttpPort(eElement.getAttribute("http-port"));
            productInfo.setInstallCode(getNodeValue(eElement, "install-code"));
            productInfo.setInstallDir(getNodeValue(eElement, "install-dir"));
            productInfo.setLastReportTime(getNodeValue(eElement, "last-report-time"));
            productInfo.setPlatform(getNodeValue(eElement, "platform"));
            productInfo.setSPVersion(getNodeValue(eElement, "sp-version"));
            productInfo.setTcpPort(getNodeValue(eElement, "tcp-port"));
            productInfo.setJavaHome(getNodeValue(eElement, "java-home"));
            productInfo.setDotNetCoreHome(getNodeValue(eElement, "dotnet-home"));

        }
        return productInfo;
    }

    private static UserInfo parseUserInfo(Document document) {
        UserInfo userInfo = new UserInfo();
        Node nNode = document.getElementsByTagName("user-info").item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            userInfo.setAddress(eElement.getElementsByTagName("address").item(0).getTextContent());
            userInfo.setCity(eElement.getElementsByTagName("city").item(0).getTextContent());
            userInfo.setCompany(eElement.getElementsByTagName("company").item(0).getTextContent());
            userInfo.setCountry(eElement.getElementsByTagName("country").item(0).getTextContent());
            userInfo.setEmail(eElement.getElementsByTagName("email").item(0).getTextContent());
            userInfo.setFirstName(eElement.getElementsByTagName("first-name").item(0).getTextContent());
            userInfo.setLastName(eElement.getElementsByTagName("last-name").item(0).getTextContent());
            userInfo.setPhone(eElement.getElementsByTagName("phone").item(0).getTextContent());
            userInfo.setState(eElement.getElementsByTagName("state").item(0).getTextContent());
            userInfo.setZip(eElement.getElementsByTagName("zip").item(0).getTextContent());
        }
        return userInfo;
    }

    private static DeploymentInfo parseDeploymentInfo(Document document) {
        DeploymentInfo deploymentInfo = new DeploymentInfo();
        Node nNode = document.getElementsByTagName("deployment-info").item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;

            NodeList nodeList= eElement.getElementsByTagName("environment-name");
            if(nodeList !=null && nodeList.item(0)!=null )
                deploymentInfo.setName(nodeList.item(0).getTextContent());

            NodeList nodeList1= eElement.getElementsByTagName("client-cpus");
            if(nodeList1 !=null && nodeList1.item(0)!=null )
                deploymentInfo.setClientCpus(nodeList.item(0).getTextContent());

        }
        return deploymentInfo;
    }

    public static void writeNCacheInfo(String filePath, String xmlString) {

        try {
            if (nCacheInfo == null) {
                return;
            }
            File file = new File(filePath);

            if (!file.exists()) {
                throw new FileNotFoundException("Unable to locate the file to read it");
            }

            try (OutputStream os = new FileOutputStream(filePath)) {
                try (Writer writer = new OutputStreamWriter(os)) {
                    writer.write(xmlString);
                    writer.flush();
                }
            }
        } catch (IOException ex) {
        }
    }

    public static LicenseInfo readLicenseInfo(String LicenseInfoFileOnLinux) throws IOException {
        //Initialize a list of employees
        LicenseInfo info = new LicenseInfo();

        try {
            File file = new File(LicenseInfoFileOnLinux);

            if (!file.exists()) {
                throw new FileNotFoundException("Unable to locate the file to read it");
            }

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File(LicenseInfoFileOnLinux));
            document.getDocumentElement().normalize();

            Node nNode = document.getElementsByTagName("license-config").item(0);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                info.setAuthCode(getNodeValue(eElement, "authcode"));
                info.setLicenseKey(getNodeValue(eElement, "license-key"));
                info.setExtCode(getNodeValue(eElement, "ext-code"));
                info.setDeactCode(getNodeValue(eElement, "deact-code"));
                info.setDeactivationKey(getNodeValue(eElement, "deactivate-key"));
            }

        } catch (SAXException | ParserConfigurationException ex) {
        }
        return info;
    }

    private static String getNodeValue(Element elsement, String tagName) {
        NodeList elementsByTagName = elsement.getElementsByTagName(tagName);
        if (elementsByTagName != null) {
            Node item = elementsByTagName.item(0);
            if (item != null) {
                return item.getTextContent();
            }
        }
        return "";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy