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 com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
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 final static String USERINFO_PATH = "SOFTWARE\\Alachisoft\\NCache\\UserInfo";
public final static String NCACHEINFO_PATH = "SOFTWARE\\Alachisoft\\NCache";
public final static String AUTHCODE_KEY = "AuthCode";
public final static String EXTCODE_KEY = "ExtCode";
public final static String LICENSE_KEY = "licenseKey";
public final static String DEACTIVATECODE_KEY = "DeactivateCode";
private static final String CITY_KEY = "city";
private static final String ADDRESS_KEY = "address";
private static final String COMPANY_KEY = "company";
private static final String EMAIL_KEY = "email";
private static final String COUNTRY_KEY = "country";
private static final String ZIP_KEY = "zip";
private static final String FIRSTNAME_KEY = "firstname";
private static final String PHONE_KEY = "phone";
private static final String STATE_KEY = "state";
private static final String LASTNAME_KEY = "lastname";
private static final String INSTALLCODE_KEY = "InstallCode";
private static final String DOTNETCOREINSTALLMODE_KEY = "DotNetInstallMode";
private static final String HTTPPORT_KEY = "Http.Port";
private static final String INSTALLDIR_KEY = "InstallDir";
private static final String LASTREPORTTIME_KEY = "LastReportTime";
private static final String NCACHETCPPORT_KEY = "NCacheTcp.Port";
private static final String PLATFORM_KEY = "Platform";
private static final String INSTALLTYPE_KEY = "InstallType";
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(nodeList1.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 "";
}
private static String readUserInfoRegistry(String key) {
try {
return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, USERINFO_PATH, key);
} catch (Exception e) {
return "";
}
}
private static String readNCacheRegistry(String key) {
try {
return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, NCACHEINFO_PATH, key);
} catch (Exception e) {
return "";
}
}
public static NCacheInfo readNCInfoFromRegistry() {
return new NCacheInfo.Builder()
// UserInfo
.setCity(readUserInfoRegistry(CITY_KEY))
.setAddress(readUserInfoRegistry(ADDRESS_KEY))
.setCompany(readUserInfoRegistry(COMPANY_KEY))
.setEmail(readUserInfoRegistry(EMAIL_KEY))
.setCountry(readUserInfoRegistry(COUNTRY_KEY))
.setExtCode(readUserInfoRegistry(EXTCODE_KEY))
.setZip(readUserInfoRegistry(ZIP_KEY))
.setFirstName(readUserInfoRegistry(FIRSTNAME_KEY))
.setPhone(readUserInfoRegistry(PHONE_KEY))
.setState(readUserInfoRegistry(STATE_KEY))
.setLastName(readUserInfoRegistry(LASTNAME_KEY))
// ProductInfo
.setDotNetInstallMode(readNCacheRegistry(DOTNETCOREINSTALLMODE_KEY))
.setHttpPort(readNCacheRegistry(HTTPPORT_KEY))
.setInstallCode(readNCacheRegistry(INSTALLCODE_KEY))
.setInstallDir(readNCacheRegistry(INSTALLDIR_KEY))
.setInstallType(readNCacheRegistry(INSTALLTYPE_KEY))
.setLastReportTime(readNCacheRegistry(LASTREPORTTIME_KEY))
.setTcpPort(readNCacheRegistry(NCACHETCPPORT_KEY))
.setPlatform(readNCacheRegistry(PLATFORM_KEY))
.build();
}
public static LicenseInfo readLicenseInfoFromRegistry() {
return new LicenseInfo.Builder()
.setAuthCode(readUserInfoRegistry(AUTHCODE_KEY))
.setDeactCode(readUserInfoRegistry(DEACTIVATECODE_KEY))
.setLicenseKey(readUserInfoRegistry(LICENSE_KEY))
.setExtCode(readUserInfoRegistry(EXTCODE_KEY))
.build();
}
}