Skip to content

Commit 84ea24e

Browse files
authored
Merge pull request #20 from cryptlex/borrowing-develop
Borrowing develop
2 parents cde709e + 34d1f09 commit 84ea24e

5 files changed

Lines changed: 268 additions & 7 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<artifactId>jna</artifactId>
4444
<version>5.10.0</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.fasterxml.jackson.core</groupId>
48+
<artifactId>jackson-databind</artifactId>
49+
<version>2.14.2</version>
50+
</dependency>
4651
</dependencies>
4752

4853
<distributionManagement>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.cryptlex.lexfloatclient;
2+
3+
public class HostConfig {
4+
public String maxOfflineLeaseDuration;
5+
}

src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.nio.CharBuffer;
77
import java.io.UnsupportedEncodingException;
88
import com.sun.jna.ptr.IntByReference;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.core.JsonProcessingException;
911
import java.util.ArrayList;
1012
import java.util.List;
1113

@@ -56,6 +58,28 @@ public static void SetHostUrl(String hostUrl) throws LexFloatClientException {
5658
}
5759
}
5860

61+
/**
62+
* Sets the permission flag. This function must be called on every start of your program
63+
* after SetHostProductId() function in case the application allows borrowing of licenses
64+
* or system wide activation.
65+
*
66+
* @param flag depending on your application's requirements, choose one of
67+
* the following values: LF_USER, LF_ALL_USERS.
68+
* <ul>
69+
* <li> LF_USER: This flag indicates that the application does not require admin or root permissions to run.</li>
70+
* <li> LF_ALL_USERS: This flag is specifically designed for Windows and should be used for system-wide activations.</li>
71+
* </ul>
72+
* @throws LexFloatClientException
73+
*/
74+
public static void SetPermissionFlag(int flag) throws LexFloatClientException {
75+
int status;
76+
status = LexFloatClientNative.SetPermissionFlag(flag)
77+
78+
if (LF_OK != status) {
79+
throw new LexFloatClientException(status);
80+
}
81+
}
82+
5983
/**
6084
* Sets the renew license callback function.<br>
6185
* Whenever the license lease is about to expire, a renew request is sent to
@@ -113,6 +137,24 @@ public static void SetFloatingClientMetadata(String key, String value) throws Le
113137
}
114138
}
115139

140+
/**
141+
* Gets the lease expiry date timestamp of the floating client.
142+
*
143+
* @return Returns the timestamp
144+
* @throws LexFloatClientException
145+
*/
146+
public static int GetFloatingClientLeaseExpiryDate() throws LexFloatClientException {
147+
int status;
148+
IntByReference expiryDate = new IntByReference(0);
149+
status = LexFloatClientNative.GetFloatingClientLeaseExpiryDate(expiryDate);
150+
switch (status) {
151+
case LF_OK:
152+
return expiryDate.getValue();
153+
default:
154+
throw new LexFloatClientException(status);
155+
}
156+
}
157+
116158
/**
117159
* Gets the version of this library.
118160
*
@@ -138,6 +180,53 @@ public static String GetFloatingClientLibraryVersion() throws LexFloatClientExce
138180
throw new LexFloatClientException(status);
139181
}
140182

183+
/**
184+
* Gets the host configuration.
185+
* This function sends a network request to LexFloatServer to get the configuration details.
186+
187+
* @return Returns host configuration.
188+
* @throws LexFloatClientException
189+
* @throws UnsupportedEncodingException
190+
*/
191+
public static HostConfig GetHostConfig() throws LexFloatClientException, UnsupportedEncodingException {
192+
int status;
193+
int bufferSize = 1024;
194+
if (Platform.isWindows()) {
195+
CharBuffer buffer = CharBuffer.allocate(bufferSize);
196+
status = LexFloatClientNative.GetHostConfigInternal(buffer, bufferSize);
197+
if (LF_OK == status) {
198+
String hostConfigJson = buffer.toString().trim();
199+
if (!hostConfigJson.isEmpty()) {
200+
HostConfig hostConfig = null;
201+
ObjectMapper objectMapper = new ObjectMapper();
202+
try {
203+
hostConfig = objectMapper.readValue(hostConfigJson, HostConfig.class);
204+
} catch (JsonProcessingException e) {}
205+
return hostConfig;
206+
} else {
207+
return null;
208+
}
209+
}
210+
} else {
211+
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
212+
status = LexFloatClientNative.GetHostConfigInternal(buffer, bufferSize);
213+
if (LF_OK == status) {
214+
String hostConfigJson = new String(buffer.array(), "UTF-8").trim();
215+
if (!hostConfigJson.isEmpty()) {
216+
HostConfig hostConfig = null;
217+
ObjectMapper objectMapper = new ObjectMapper();
218+
try {
219+
hostConfig = objectMapper.readValue(hostConfigJson, HostConfig.class);
220+
} catch (JsonProcessingException e) {}
221+
return hostConfig;
222+
} else {
223+
return null;
224+
}
225+
}
226+
}
227+
throw new LexFloatClientException(status);
228+
}
229+
141230
/**
142231
* Gets the product version name.
143232
*
@@ -246,6 +335,32 @@ public static String GetHostLicenseMetadata(String key) throws LexFloatClientExc
246335
throw new LexFloatClientException(status);
247336
}
248337

338+
/**
339+
* Get the value of the license metadata field associated with the LexFloatServer license.
340+
*
341+
* @param key key of the metadata field whose value you want to get
342+
* @return Returns the metadata key value
343+
* @throws LexFloatClientException
344+
* @throws UnsupportedEncodingException
345+
*/
346+
public static String GetFloatingClientMetadata(String key) throws LexFloatClientException, UnsupportedEncodingException {
347+
int status;
348+
if (Platform.isWindows()) {
349+
CharBuffer buffer = CharBuffer.allocate(256);
350+
status = LexFloatClientNative.GetFloatingClientMetadata(new WString(key), buffer, 256);
351+
if (LF_OK == status) {
352+
return buffer.toString().trim();
353+
}
354+
} else {
355+
ByteBuffer buffer = ByteBuffer.allocate(256);
356+
status = LexFloatClientNative.GetFloatingClientMetadata(key, buffer, 256);
357+
if (LF_OK == status) {
358+
return new String(buffer.array(), "UTF-8");
359+
}
360+
}
361+
throw new LexFloatClientException(status);
362+
}
363+
249364
/**
250365
* Gets the license meter attribute allowed uses and total uses associated
251366
* with the LexFloatServer license.
@@ -293,6 +408,31 @@ public static int GetHostLicenseExpiryDate() throws LexFloatClientException {
293408
}
294409
}
295410

411+
/**
412+
* Gets the mode of the floating license (online or offline).
413+
*
414+
* @return mode - Returns the floating license mode.
415+
* @throws LexFloatClientException
416+
* @throws UnsupportedEncodingException
417+
*/
418+
public static String GetFloatingLicenseMode() throws LexFloatClientException, UnsupportedEncodingException {
419+
int status;
420+
if (Platform.isWindows()) {
421+
CharBuffer buffer = CharBuffer.allocate(256);
422+
status = LexFloatClientNative.GetFloatingLicenseMode(buffer, 256);
423+
if (LF_OK == status) {
424+
return buffer.toString().trim();
425+
}
426+
} else {
427+
ByteBuffer buffer = ByteBuffer.allocate(256);
428+
status = LexFloatClientNative.GetFloatingLicenseMode(buffer, 256);
429+
if (LF_OK == status) {
430+
return new String(buffer.array(), "UTF-8").trim();
431+
}
432+
}
433+
throw new LexFloatClientException(status);
434+
}
435+
296436
/**
297437
* Gets the meter attribute uses consumed by the floating client.
298438
*
@@ -318,6 +458,21 @@ public static int GetFloatingClientMeterAttributeUses(String name) throws LexFlo
318458
throw new LexFloatClientException(status);
319459
}
320460

461+
/**
462+
* Sends the request to lease the license from the LexFloatServer for offline usage.
463+
* The maximum value of lease duration is configured in the config.yml of LexFloatServer.
464+
*
465+
* @param leaseDuration value of the lease duration.
466+
* @throws LexFloatClientException
467+
*/
468+
public static void RequestOfflineFloatingLicense(int leaseDuration) throws LexFloatClientException {
469+
int status;
470+
status = LexFloatClientNative.RequestOfflineFloatingLicense(leaseDuration);
471+
if (LF_OK != status) {
472+
throw new LexFloatClientException(status);
473+
}
474+
}
475+
321476
/**
322477
* Sends the request to lease the license from the LexFloatServer
323478
*

src/main/java/com/cryptlex/lexfloatclient/LexFloatClientException.java

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,32 @@ public static String getErrorMessage(int errorCode) {
7575
message = "No product version is linked with the license.";
7676
break;
7777
case LF_E_FEATURE_FLAG_NOT_FOUND:
78-
message = "The product version feature flag does not exist.";
78+
message = "The product version feature flag does not exist.";
7979
break;
8080
case LF_E_IP:
8181
message = "IP address is not allowed.";
8282
break;
83+
case LF_E_SYSTEM_PERMISSION:
84+
message = "Insufficient system permissions.";
85+
break;
86+
case LF_E_INVALID_PERMISSION_FLAG:
87+
message = "Invalid permission flag.";
88+
break;
89+
case LF_E_OFFLINE_FLOATING_LICENSE_NOT_ALLOWED:
90+
message = "Offline floating license is not allowed for per-instance leasing strategy.";
91+
break;
92+
case LF_E_MAX_OFFLINE_LEASE_DURATION_EXCEEDED:
93+
message = "Maximum offline lease duration exceeded.";
94+
break;
95+
case LF_E_ALLOWED_OFFLINE_FLOATING_CLIENTS_LIMIT_REACHED:
96+
message = "Allowed offline floating clients limit reached.";
97+
break;
98+
case LF_E_WMIC:
99+
message = "Fingerprint couldn't be generated because Windows Management Instrumentation (WMI) service has been disabled.";
100+
break;
101+
case LF_E_MACHINE_FINGERPRINT:
102+
message = "Machine fingerprint has changed since activation.";
103+
break;
83104
case LF_E_CLIENT:
84105
message = "Client error.";
85106
break;
@@ -101,13 +122,14 @@ public static String getErrorMessage(int errorCode) {
101122
case LF_E_SERVER_LICENSE_GRACE_PERIOD_OVER:
102123
message = "The grace period for server license is over.";
103124
break;
125+
case LF_E_PROXY_NOT_TRUSTED:
126+
message = "Request blocked due to untrusted proxy.";
127+
break;
104128
default:
105129
message = "Unknown error!";
106-
107130
}
108131
return message;
109132
}
110-
111133
/*
112134
* CODE: LF_OK
113135
*
@@ -245,25 +267,78 @@ public static String getErrorMessage(int errorCode) {
245267

246268
/*
247269
* CODE: LF_E_PRODUCT_VERSION_NOT_LINKED
248-
*
270+
*
249271
* MESSAGE: No product version is linked with the license.
250272
*/
251273
public static final int LF_E_PRODUCT_VERSION_NOT_LINKED = 57;
252274

253275
/*
254276
* CODE: LF_E_FEATURE_FLAG_NOT_FOUND
255-
*
277+
*
256278
* MESSAGE: The product version feature flag does not exist.
257279
*/
258280
public static final int LF_E_FEATURE_FLAG_NOT_FOUND = 58;
259281

282+
/*
283+
* Insufficient system permissions.
284+
*/
285+
public static final int LF_E_SYSTEM_PERMISSION = 59;
286+
260287
/*
261288
* CODE: LF_E_IP
262289
*
263290
* MESSAGE: IP address is not allowed.
264291
*/
265292
public static final int LF_E_IP = 60;
266293

294+
/*
295+
* CODE: LF_E_INVALID_PERMISSION_FLAG
296+
*
297+
* MESSAGE: Invalid permission flag.
298+
*/
299+
public static final int LF_E_INVALID_PERMISSION_FLAG = 61;
300+
301+
/*
302+
* CODE: LF_E_OFFLINE_FLOATING_LICENSE_NOT_ALLOWED
303+
*
304+
* MESSAGE: Offline floating license is not allowed for per-instance leasing strategy.
305+
*/
306+
public static final int LF_E_OFFLINE_FLOATING_LICENSE_NOT_ALLOWED = 62;
307+
308+
/*
309+
* CODE: LF_E_MAX_OFFLINE_LEASE_DURATION_EXCEEDED
310+
*
311+
* MESSAGE: Maximum offline lease duration exceeded.
312+
*/
313+
public static final int LF_E_MAX_OFFLINE_LEASE_DURATION_EXCEEDED = 63;
314+
315+
/*
316+
* CODE: LF_E_ALLOWED_OFFLINE_FLOATING_CLIENTS_LIMIT_REACHED
317+
*
318+
* MESSAGE: Allowed offline floating clients limit reached.
319+
*/
320+
public static final int LF_E_ALLOWED_OFFLINE_FLOATING_CLIENTS_LIMIT_REACHED = 64;
321+
322+
/*
323+
* CODE: LF_E_WMIC
324+
*
325+
* MESSAGE: Fingerprint couldn't be generated because Windows Management Instrumentation (WMI) service has been disabled.
326+
*/
327+
public static final int LF_E_WMIC = 65;
328+
329+
/*
330+
* CODE: LF_E_MACHINE_FINGERPRINT
331+
*
332+
* MESSAGE: Machine fingerprint has changed since activation.
333+
*/
334+
public static final int LF_E_MACHINE_FINGERPRINT = 66;
335+
/*
336+
* CODE: LF_E_PROXY_NOT_TRUSTED
337+
*
338+
* MESSAGE: Request blocked due to untrusted proxy.
339+
*/
340+
public static final int LF_E_PROXY_NOT_TRUSTED = 67;
341+
267342
/*
268343
* CODE: LF_E_CLIENT
269344
*
@@ -313,4 +388,4 @@ public static String getErrorMessage(int errorCode) {
313388
* MESSAGE: The grace period for server license is over.
314389
*/
315390
public static final int LF_E_SERVER_LICENSE_GRACE_PERIOD_OVER = 76;
316-
}
391+
}

0 commit comments

Comments
 (0)