com.alachisoft.ncache.client.internal.caching.LicenseVerification Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.client.internal.caching;
import Alachisoft.NCache.Common.Common;
import com.alachisoft.ncache.client.internal.command.CommandOptions;
import com.alachisoft.ncache.client.internal.command.CommandResponse;
import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.common.protobuf.GetLCCommandProtocol.GetLCCommand;
import com.alachisoft.ncache.common.protobuf.ResponseProtocol;
import com.alachisoft.ncache.licensing.LicenseManager;
import com.alachisoft.ncache.ncactivate.utils.AppUtil;
import com.alachisoft.ncache.runtime.exceptions.LicensingException;
import com.alachisoft.ncache.runtime.exceptions.LocalVerificationFailedException;
import com.google.protobuf.InvalidProtocolBufferException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
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 java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LicenseVerification {
private static String _verifyLicense = "";
private String _server;
private int _port;
private Socket _socket;
private int _requestId;
private int _editionId;
public static void main(String[] args) throws Exception {
LicenseVerification v = new LicenseVerification();
try {
v.verifyLicense();
} catch (LicensingException ex) {
//ex.printStackTrace();
}
}
public LicenseManager.LicenseType verifyLicense() throws LicensingException, Exception {
LicenseManager.LicenseType type = LicenseManager.LicenseType.None;
try {
try {
loadEditionInformation();
} catch (Exception ex) {
loadEditionInfoFromServer();
}
if(LicenseManager.Edition.profCacheServer.equals(LicenseManager.getEditionFromID(_editionId))){
type = getLicenseTypeFromServer();
}
} catch (Exception ex) {
}
return type;
}
private void loadEditionInformation() throws LicensingException {
int editionId = AppUtil.getEditionID();
if (editionId == -1) {
throw new LicensingException("Invalid install code");
}
_editionId = editionId;
}
private void loadEditionInfoFromServer() throws LicensingException{
try {
connectToLocalService();
if (_socket != null)
{
byte[] edId = executeCommand(_socket, 7);
String editionInfo = com.alachisoft.ncache.licensing.License.convertToString(edId);
LicenseManager.setEditionInfo(editionInfo);
_editionId = AppUtil.getEditionIDFromServerValue(editionInfo);
}
}
catch (Exception ex) {
throw new LicensingException(ex.getMessage());
}
finally {
if (_socket != null) {
try {
_socket.close();
} catch (IOException ex) {
Logger.getLogger(LicenseVerification.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private LicenseManager.LicenseType getLicenseTypeFromServer() throws LicensingException {
LicenseManager.LicenseType type = LicenseManager.LicenseType.None;
try {
connectToLocalService();
if (_socket != null) {
byte[] typeResponse = executeCommand(_socket, 8);
int typeValue = com.alachisoft.ncache.licensing.License.Decode(typeResponse);
type = LicenseManager.LicenseType.forValue(typeValue);
}
} catch (LicensingException ex) {
throw ex;
} catch (Exception ex) {
throw new LicensingException(ex.getMessage());
} finally {
if (_socket != null) {
try {
_socket.close();
} catch (IOException ex) {
Logger.getLogger(LicenseVerification.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return type;
}
private LicenseManager.LicenseType verifyLicenseInternally() throws LicensingException, LocalVerificationFailedException {
// Check the type of license the application
com.alachisoft.ncache.licensing.LicenseManager.LicenseType type = LicenseManager.LicenseType.Expired;
try {
type = com.alachisoft.ncache.licensing.LicenseManager.LicenseMode(_editionId, AppUtil.getVersion());
} catch (LicensingException le) {
} catch (SecurityException se) {
} catch (LocalVerificationFailedException lFE) {
} catch (Exception ex) {
}
return type;
}
private LicenseManager.LicenseType verifyLicenseExternally() throws LicensingException {
LicenseManager.LicenseType type = LicenseManager.LicenseType.Expired;
try {
connectToLocalService();
if (_socket != null) {
byte[] edId = executeCommand(_socket, 7);
String editionId = com.alachisoft.ncache.licensing.License.convertToString(edId);
byte[] stubData = executeCommand(_socket, 1);
byte[] cpuData = executeCommand(_socket, 2);
byte[] nicData = executeCommand(_socket, 3);
byte[] phyCoreData = executeCommand(_socket, 4);
byte[] logCoreData = executeCommand(_socket, 5);
byte[] socCountData = executeCommand(_socket, 6);
type = com.alachisoft.ncache.licensing.LicenseManager.LicenseMode(_editionId, AppUtil.getVersion(), stubData, cpuData, nicData, phyCoreData, logCoreData, socCountData);
}
} catch (LicensingException ex) {
throw ex;
} catch (Exception ex) {
throw new LicensingException(ex.getMessage());
} finally {
if (_socket != null) {
try {
_socket.close();
} catch (IOException ex) {
Logger.getLogger(LicenseVerification.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return type;
}
private void connectToLocalService() throws Exception {
try {
readConfiguration();
} catch (Exception ex) {
throw ex;
}
if (_server == null || _server.trim().isEmpty()) {
throw new Exception("Local server ip is not specified in client.ncconf");
}
if (_port <= 0) {
throw new Exception("Local server port is not specified in client.ncconf");
}
try {
_socket = new Socket(_server, _port);
_socket.setTcpNoDelay(false);
} catch (IOException e) {
throw new Exception("Failed to connect with the local cache host");
}
}
private byte[] executeCommand(Socket socket, int opCode) throws Exception {
ResponseProtocol.Response response = null;
if (_socket != null) {
byte[] buffer = serializeCommand(opCode);
sendToServer(buffer);
byte[] lengthBuffer = receiveFromServer(CommandOptions.COMMAND_SIZE);
//parse the length
String s = new String(lengthBuffer, 0, CommandOptions.COMMAND_SIZE);
CommandResponse resultItem = null;
int responseSize = 0;
try {
responseSize = Integer.parseInt(s.trim());
} catch (Exception ex) {
}
//receive the response
buffer = receiveFromServer(responseSize);
try {
///Parse response
response = ResponseProtocol.Response.parseFrom(buffer);
} catch (InvalidProtocolBufferException ex) {
throw ex;
}
}
if (response != null && response.getLcDataResponse() != null && response.getLcDataResponse().getLcData() != null) {
return response.getLcDataResponse().getLcData().toByteArray();
}
return null;
}
private void sendToServer(byte[] buffer) throws IOException {
int dataTobeSent = buffer.length;
OutputStream stream = _socket.getOutputStream();
stream.write(buffer);
}
private byte[] receiveFromServer(int expectedDataLength) throws IOException {
InputStream stream = _socket.getInputStream();
byte[] buffer = new byte[expectedDataLength];
int offset = 0;
while (expectedDataLength > 0) {
int received = stream.read(buffer, offset, expectedDataLength);
expectedDataLength -= received;
offset += received;
if (received <= 0) {
throw new IOException();
}
}
return buffer;
}
private byte[] serializeCommand(int opCode) throws Exception {
byte[] buffer = null;
int requestId = _requestId++;
GetLCCommand.Builder builder = GetLCCommand.newBuilder();
builder.setOpCode(opCode);
builder.setRequestId(requestId);
CommandProtocol.Command.Builder commandBuilder =
CommandProtocol.Command.newBuilder();
commandBuilder = commandBuilder.setGetLCCommand(builder)
.setRequestID(_requestId)
.setType(CommandProtocol.Command.Type.GET_LC_DATA);
buffer = commandBuilder.build().toByteArray();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
///Copy discarding buffer for server
byte[] discardingBuffer = new byte[20];
stream.write(discardingBuffer);
///Copy size of data in 10 bytes
byte[] size = new byte[CommandOptions.COMMAND_SIZE];
byte[] commandSize = String.valueOf(buffer.length).getBytes("UTF-8");
System.arraycopy(commandSize, 0, size, 0, commandSize.length);
stream.write(size);
///Copy actual data
stream.write(buffer);
///Return the packet
buffer = stream.toByteArray();
//return stream.toByteArray();
} finally {
stream.close();
}
return buffer;
}
private void dispose() {
}
private void readConfiguration() throws Exception {
com.alachisoft.ncache.client.internal.util.ClientConfiguration clientConfig = new com.alachisoft.ncache.client.internal.util.ClientConfiguration(null);
//dont load the cache related information from client.conf
clientConfig.setLoadCacheConfiguration(false);
clientConfig.loadLocalServerIP();
_server = clientConfig.getLocalServerIP();
_port = clientConfig.getServerPort();
}
private byte[] getLCData() {
byte[] stubdata = null;
GetLCCommand.Builder builder = GetLCCommand.newBuilder();
int requestId = _requestId++;
builder.setEditionId(_editionId);
builder.setOpCode(1);
builder.setRequestId(requestId);
CommandProtocol.Command.Builder commandBuilder =
CommandProtocol.Command.newBuilder();
commandBuilder = commandBuilder.setGetLCCommand(builder)
.setRequestID(_requestId)
.setType(CommandProtocol.Command.Type.GET_LC_DATA);
return stubdata;
}
public void LoadServerConfiguration() throws SAXException, Exception {
String directoryValue = Common.getNCHome();
String separator = File.separator;
if (directoryValue != null) {
if (!directoryValue.endsWith(separator)) {
directoryValue = directoryValue.concat(separator);
}
directoryValue.replaceFirst(";", "");
directoryValue = directoryValue.concat("config" + separator + "client.ncconf");
}
File hfile = new File(directoryValue);
if (!hfile.exists()) {
}
Document response = null;
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {//ex.printStackTrace();
}
response = builder.parse(hfile);
boolean serverPortFound = false;
NodeList serverPortNL = response.getElementsByTagName("ncache-server");
if (serverPortNL != null) {
Node ncacheServerDetails = serverPortNL.item(0);
if (ncacheServerDetails == null) {
throw new Exception("ncache-server options not found in client.ncconf.");
}
NamedNodeMap attributes = ncacheServerDetails.getAttributes();
try {
Node verifyLicense = attributes.getNamedItem("verify-license");
_verifyLicense = verifyLicense.getTextContent();
} catch (Exception ex) {
_verifyLicense = "";
}
}
}
}