Skip to content

Commit b2f0fa8

Browse files
committed
License scheduled check for kernel
DEVSIX-3475
1 parent 10bd1a9 commit b2f0fa8

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

kernel/src/main/java/com/itextpdf/kernel/Version.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ private Version(VersionInfo info, boolean expired) {
117117
public static Version getInstance() {
118118
synchronized (staticLock) {
119119
if (version != null) {
120+
try {
121+
licenseScheduledCheck();
122+
} catch (Exception e) {
123+
// If any exception occurs during scheduled check of core license,
124+
// then it means that license is not valid yet, so roll back to AGPL.
125+
// The key value is null as it is similar to case
126+
// when an exception has been thrown during initial license loading
127+
atomicSetVersion(initAGPLVersion(e, null));
128+
}
120129
return version;
121130
}
122131
}
@@ -183,7 +192,7 @@ public static Version getInstance() {
183192
* @return returns true if the AGPL version is used.
184193
*/
185194
public static boolean isAGPLVersion() {
186-
return getInstance().getVersion().indexOf(AGPL) > 0;
195+
return getInstance().isAGPL();
187196
}
188197

189198
/**
@@ -246,6 +255,14 @@ public VersionInfo getInfo() {
246255
return info;
247256
}
248257

258+
/**
259+
* Checks if the current object has been initialized with AGPL license.
260+
* @return returns true if the current object has been initialized with AGPL license.
261+
*/
262+
private boolean isAGPL() {
263+
return getVersion().indexOf(AGPL) > 0;
264+
}
265+
249266
private static Version initDefaultLicensedVersion(String ownerName, String key) {
250267
String producer = producerLine + " (" + ownerName;
251268
if (! key.toLowerCase().startsWith("trial")) {
@@ -270,7 +287,11 @@ private static Version initVersion(String producer, String key, boolean expired)
270287

271288
private static Class<?> getLicenseKeyClass() throws ClassNotFoundException {
272289
String licenseKeyClassFullName = "com.itextpdf.licensekey.LicenseKey";
273-
return Class.forName(licenseKeyClassFullName);
290+
return getClassFromLicenseKey(licenseKeyClassFullName);
291+
}
292+
293+
private static Class<?> getClassFromLicenseKey(String classFullName) throws ClassNotFoundException {
294+
return Class.forName(classFullName);
274295
}
275296

276297
private static void checkLicenseVersion(String coreVersionString, String licenseVersionString){
@@ -360,4 +381,23 @@ private static Version atomicSetVersion(Version newVersion) {
360381
return version;
361382
}
362383
}
384+
385+
private static void licenseScheduledCheck() {
386+
if (version.isAGPL()) {
387+
return;
388+
}
389+
390+
String licenseKeyProductFullName = "com.itextpdf.licensekey.LicenseKeyProduct";
391+
String checkLicenseKeyMethodName = "scheduledCheck";
392+
try {
393+
Class<?> licenseKeyClass = getLicenseKeyClass();
394+
Class<?> licenseKeyProductClass = getClassFromLicenseKey(licenseKeyProductFullName);
395+
396+
Class[] cArg = {licenseKeyProductClass};
397+
Method method = licenseKeyClass.getMethod(checkLicenseKeyMethodName, cArg);
398+
method.invoke(null, new Object[]{null});
399+
} catch (Exception e) {
400+
throw new RuntimeException(e.getMessage(), e);
401+
}
402+
}
363403
}

0 commit comments

Comments
 (0)