@@ -54,26 +54,31 @@ This file is part of the iText (R) project.
54
54
*/
55
55
public final class Version {
56
56
57
+ /**
58
+ * Lock object used for synchronization
59
+ */
60
+ private static final Object staticLock = new Object ();
61
+
57
62
/**
58
63
* String that will indicate if the AGPL version is used.
59
64
*/
60
- private static String AGPL = " (AGPL-version)" ;
65
+ private static final String AGPL = " (AGPL-version)" ;
61
66
62
67
/**
63
68
* The iText version instance.
64
69
*/
65
- private static Version version = null ;
70
+ private static volatile Version version = null ;
66
71
/**
67
72
* This String contains the name of the product.
68
73
* iText is a registered trademark by iText Group NV.
69
74
* Please don't change this constant.
70
75
*/
71
- private static String iTextProductName = "iText\u00ae " ;
76
+ private static final String iTextProductName = "iText\u00ae " ;
72
77
/**
73
78
* This String contains the version number of this iText release.
74
79
* For debugging purposes, we request you NOT to change this constant.
75
80
*/
76
- private static String release = "7.0.7-SNAPSHOT" ;
81
+ private static final String release = "7.0.7-SNAPSHOT" ;
77
82
/**
78
83
* This String contains the iText version as shown in the producer line.
79
84
* iText is a product developed by iText Group NV.
@@ -95,65 +100,66 @@ public final class Version {
95
100
* in every PDF that is created or manipulated using iText.
96
101
*/
97
102
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" ;
135
119
} else {
136
- version . addAGPLPostfix ( null ) ;
120
+ localVersion . key += info [ 5 ] ;
137
121
}
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 ]);
151
128
}
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 );
153
158
}
154
159
}
160
+ localVersion .addAGPLPostfix (e .getCause ());
155
161
}
156
- return version ;
162
+ return atomicSetVersion ( localVersion ) ;
157
163
}
158
164
159
165
/**
@@ -316,4 +322,11 @@ private static boolean isVersionNumeric(String version){
316
322
return false ;
317
323
}
318
324
}
325
+
326
+ private static Version atomicSetVersion (Version newVersion ) {
327
+ synchronized (staticLock ) {
328
+ version = newVersion ;
329
+ return version ;
330
+ }
331
+ }
319
332
}
0 commit comments