Skip to content

Commit 442c5a2

Browse files
committed
Fix synchronization issues during version creation
DEVSIX-1870
1 parent f6a1477 commit 442c5a2

File tree

1 file changed

+70
-57
lines changed

1 file changed

+70
-57
lines changed

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

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,31 @@ This file is part of the iText (R) project.
5454
*/
5555
public final class Version {
5656

57+
/**
58+
* Lock object used for synchronization
59+
*/
60+
private static final Object staticLock = new Object();
61+
5762
/**
5863
* String that will indicate if the AGPL version is used.
5964
*/
60-
private static String AGPL = " (AGPL-version)";
65+
private static final String AGPL = " (AGPL-version)";
6166

6267
/**
6368
* The iText version instance.
6469
*/
65-
private static Version version = null;
70+
private static volatile Version version = null;
6671
/**
6772
* This String contains the name of the product.
6873
* iText is a registered trademark by iText Group NV.
6974
* Please don't change this constant.
7075
*/
71-
private static String iTextProductName = "iText\u00ae";
76+
private static final String iTextProductName = "iText\u00ae";
7277
/**
7378
* This String contains the version number of this iText release.
7479
* For debugging purposes, we request you NOT to change this constant.
7580
*/
76-
private static String release = "7.0.7-SNAPSHOT";
81+
private static final String release = "7.0.7-SNAPSHOT";
7782
/**
7883
* This String contains the iText version as shown in the producer line.
7984
* iText is a product developed by iText Group NV.
@@ -95,65 +100,66 @@ public final class Version {
95100
* in every PDF that is created or manipulated using iText.
96101
*/
97102
public static Version getInstance() {
98-
if (version == null) {
99-
version = new Version();
100-
synchronized (version) {
101-
try {
102-
String coreVersion = release;
103-
String[] info = getLicenseeInfoFromLicenseKey(coreVersion);
104-
if(info != null){
105-
if (info[3] != null && info[3].trim().length() > 0) {
106-
version.key = info[3];
107-
} else {
108-
version.key = "Trial version ";
109-
if (info[5] == null) {
110-
version.key += "unauthorised";
111-
} else {
112-
version.key += info[5];
113-
}
114-
}
115-
116-
if (info.length > 6) {
117-
if (info[6] != null && info[6].trim().length() > 0) {
118-
//Compare versions with this release versions
119-
checkLicenseVersion(coreVersion, info[6]);
120-
}
121-
}
122-
123-
if (info[4] != null && info[4].trim().length() > 0) {
124-
version.producerLine = info[4];
125-
} else if (info[2] != null && info[2].trim().length() > 0) {
126-
version.addLicensedPostfix(info[2]);
127-
} else if (info[0] != null && info[0].trim().length() > 0) {
128-
// fall back to contact name, if company name is unavailable.
129-
// we shouldn't have a licensed version without company name,
130-
// but let's account for it anyway
131-
version.addLicensedPostfix(info[0]);
132-
} else {
133-
version.addAGPLPostfix(null);
134-
}
103+
synchronized (staticLock) {
104+
if (version != null) {
105+
return version;
106+
}
107+
}
108+
Version localVersion = new Version();
109+
try {
110+
String coreVersion = release;
111+
String[] info = getLicenseeInfoFromLicenseKey(coreVersion);
112+
if(info != null){
113+
if (info[3] != null && info[3].trim().length() > 0) {
114+
localVersion.key = info[3];
115+
} else {
116+
localVersion.key = "Trial version ";
117+
if (info[5] == null) {
118+
localVersion.key += "unauthorised";
135119
} else {
136-
version.addAGPLPostfix(null);
120+
localVersion.key += info[5];
137121
}
138-
//Catch the exception
139-
} catch(LicenseVersionException lve) {
140-
//Rethrow license version exceptions
141-
throw lve;
142-
}catch(ClassNotFoundException cnfe){
143-
//License key library not on classpath, switch to AGPL
144-
version.addAGPLPostfix(null);
145-
} catch (Exception e) {
146-
//Check if an iText5 license is loaded
147-
if(e.getCause() != null && e.getCause().getMessage().equals(LicenseVersionException.LICENSE_FILE_NOT_LOADED)) {
148-
if (isiText5licenseLoaded()) {
149-
throw new LicenseVersionException(LicenseVersionException.NO_I_TEXT7_LICENSE_IS_LOADED_BUT_AN_I_TEXT5_LICENSE_IS_LOADED);
150-
}
122+
}
123+
124+
if (info.length > 6) {
125+
if (info[6] != null && info[6].trim().length() > 0) {
126+
//Compare versions with this release versions
127+
checkLicenseVersion(coreVersion, info[6]);
151128
}
152-
version.addAGPLPostfix(e.getCause());
129+
}
130+
131+
if (info[4] != null && info[4].trim().length() > 0) {
132+
localVersion.producerLine = info[4];
133+
} else if (info[2] != null && info[2].trim().length() > 0) {
134+
localVersion.addLicensedPostfix(info[2]);
135+
} else if (info[0] != null && info[0].trim().length() > 0) {
136+
// fall back to contact name, if company name is unavailable.
137+
// we shouldn't have a licensed version without company name,
138+
// but let's account for it anyway
139+
localVersion.addLicensedPostfix(info[0]);
140+
} else {
141+
localVersion.addAGPLPostfix(null);
142+
}
143+
} else {
144+
localVersion.addAGPLPostfix(null);
145+
}
146+
//Catch the exception
147+
} catch(LicenseVersionException lve) {
148+
//Rethrow license version exceptions
149+
throw lve;
150+
}catch(ClassNotFoundException cnfe){
151+
//License key library not on classpath, switch to AGPL
152+
localVersion.addAGPLPostfix(null);
153+
} catch (Exception e) {
154+
//Check if an iText5 license is loaded
155+
if(e.getCause() != null && e.getCause().getMessage().equals(LicenseVersionException.LICENSE_FILE_NOT_LOADED)) {
156+
if (isiText5licenseLoaded()) {
157+
throw new LicenseVersionException(LicenseVersionException.NO_I_TEXT7_LICENSE_IS_LOADED_BUT_AN_I_TEXT5_LICENSE_IS_LOADED);
153158
}
154159
}
160+
localVersion.addAGPLPostfix(e.getCause());
155161
}
156-
return version;
162+
return atomicSetVersion(localVersion);
157163
}
158164

159165
/**
@@ -316,4 +322,11 @@ private static boolean isVersionNumeric(String version){
316322
return false;
317323
}
318324
}
325+
326+
private static Version atomicSetVersion(Version newVersion) {
327+
synchronized (staticLock) {
328+
version = newVersion;
329+
return version;
330+
}
331+
}
319332
}

0 commit comments

Comments
 (0)