@@ -41,7 +41,10 @@ source product.
41
41
42
42
*/
43
43
using System ;
44
+ using System . IO ;
44
45
using System . Reflection ;
46
+ using iTextSharp . text . log ;
47
+ using Versions . Attributes ;
45
48
46
49
namespace iTextSharp . text {
47
50
@@ -86,8 +89,46 @@ public sealed class Version {
86
89
* The license key.
87
90
*/
88
91
private String key = null ;
89
-
90
- /**
92
+
93
+ private static Type GetLicenseKeyClass ( ) {
94
+ String licenseKeyClassPartialName = "iText.License.LicenseKey, itext.licensekey" ;
95
+ String licenseKeyClassFullName = null ;
96
+
97
+ object [ ] keyVersionAttrs = typeof ( Version ) . Assembly . GetCustomAttributes ( typeof ( KeyVersionAttribute ) , false ) ;
98
+ object keyVersionAttr = keyVersionAttrs . Length > 0 ? keyVersionAttrs [ 0 ] : null ;
99
+ if ( keyVersionAttr is KeyVersionAttribute ) {
100
+ String keyVersion = ( ( KeyVersionAttribute ) keyVersionAttr ) . KeyVersion ;
101
+ String format = "{0}, Version={1}, Culture=neutral, PublicKeyToken=8354ae6d2174ddca" ;
102
+ licenseKeyClassFullName = String . Format ( format , licenseKeyClassPartialName , keyVersion ) ;
103
+ }
104
+
105
+ Type type = null ;
106
+ if ( licenseKeyClassFullName != null ) {
107
+ String fileLoadExceptionMessage = null ;
108
+ try {
109
+ type = System . Type . GetType ( licenseKeyClassFullName ) ;
110
+ }
111
+ catch ( FileLoadException fileLoadException ) {
112
+ fileLoadExceptionMessage = fileLoadException . Message ;
113
+ }
114
+
115
+ if ( fileLoadExceptionMessage != null ) {
116
+ ILogger logger = LoggerFactory . GetLogger ( typeof ( Version ) ) ;
117
+ try {
118
+ type = System . Type . GetType ( licenseKeyClassPartialName ) ;
119
+ }
120
+ catch {
121
+ // ignore
122
+ }
123
+ if ( type == null ) {
124
+ logger . Error ( fileLoadExceptionMessage ) ;
125
+ }
126
+ }
127
+ }
128
+ return type ;
129
+ }
130
+
131
+ /**
91
132
* Gets an instance of the iText version that is currently used.
92
133
* Note that iText Group requests that you retain the iText producer line
93
134
* in every PDF that is created or manipulated using iText.
@@ -97,46 +138,44 @@ public static Version GetInstance() {
97
138
version = new Version ( ) ;
98
139
lock ( version ) {
99
140
try {
100
- Type type = Type . GetType ( "iText.License.LicenseKey, itext.licensekey" ) ;
101
- if ( type != null ) {
102
- Type [ ] cArg = new Type [ ] { typeof ( String ) } ;
103
- MethodInfo m = type . GetMethod ( "GetLicenseeInfoForVersion" , cArg ) ;
104
- String coreVersion = release ;
105
- Object [ ] args = new Object [ ] { coreVersion } ;
106
- String [ ] info = ( String [ ] ) m . Invoke ( Activator . CreateInstance ( type ) , args ) ;
107
- if ( info [ 3 ] != null && info [ 3 ] . Trim ( ) . Length > 0 ) {
108
- version . key = info [ 3 ] ;
109
- } else {
110
- version . key = "Trial version " ;
111
- if ( info [ 5 ] == null ) {
112
- version . key += "unauthorised" ;
113
- } else {
114
- version . key += info [ 5 ] ;
115
- }
116
- }
117
- if ( info [ 4 ] != null && info [ 4 ] . Trim ( ) . Length > 0 ) {
118
- version . iTextVersion = info [ 4 ] ;
119
- } else if ( info [ 2 ] != null && info [ 2 ] . Trim ( ) . Length > 0 ) {
120
- version . iTextVersion += " (" + info [ 2 ] ;
121
- if ( ! version . key . ToLower ( ) . StartsWith ( "trial" ) ) {
122
- version . iTextVersion += "; licensed version)" ;
123
- } else {
124
- version . iTextVersion += "; " + version . key + ")" ;
125
- }
126
- } else if ( info [ 0 ] != null && info [ 0 ] . Trim ( ) . Length > 0 ) {
127
- // fall back to contact name, if company name is unavailable
128
- version . iTextVersion += " (" + info [ 0 ] ;
129
- if ( ! version . key . ToLower ( ) . StartsWith ( "trial" ) ) {
130
- // we shouldn't have a licensed version without company name,
131
- // but let's account for it anyway
132
- version . iTextVersion += "; licensed version)" ;
133
- } else {
134
- version . iTextVersion += "; " + version . key + ")" ;
135
- }
136
- } else {
137
- throw new Exception ( ) ;
138
- }
139
- }
141
+ Type type = GetLicenseKeyClass ( ) ;
142
+ Type [ ] cArg = new Type [ ] { typeof ( String ) } ;
143
+ MethodInfo m = type . GetMethod ( "GetLicenseeInfoForVersion" , cArg ) ;
144
+ String coreVersion = release ;
145
+ Object [ ] args = new Object [ ] { coreVersion } ;
146
+ String [ ] info = ( String [ ] ) m . Invoke ( Activator . CreateInstance ( type ) , args ) ;
147
+ if ( info [ 3 ] != null && info [ 3 ] . Trim ( ) . Length > 0 ) {
148
+ version . key = info [ 3 ] ;
149
+ } else {
150
+ version . key = "Trial version " ;
151
+ if ( info [ 5 ] == null ) {
152
+ version . key += "unauthorised" ;
153
+ } else {
154
+ version . key += info [ 5 ] ;
155
+ }
156
+ }
157
+ if ( info [ 4 ] != null && info [ 4 ] . Trim ( ) . Length > 0 ) {
158
+ version . iTextVersion = info [ 4 ] ;
159
+ } else if ( info [ 2 ] != null && info [ 2 ] . Trim ( ) . Length > 0 ) {
160
+ version . iTextVersion += " (" + info [ 2 ] ;
161
+ if ( ! version . key . ToLower ( ) . StartsWith ( "trial" ) ) {
162
+ version . iTextVersion += "; licensed version)" ;
163
+ } else {
164
+ version . iTextVersion += "; " + version . key + ")" ;
165
+ }
166
+ } else if ( info [ 0 ] != null && info [ 0 ] . Trim ( ) . Length > 0 ) {
167
+ // fall back to contact name, if company name is unavailable
168
+ version . iTextVersion += " (" + info [ 0 ] ;
169
+ if ( ! version . key . ToLower ( ) . StartsWith ( "trial" ) ) {
170
+ // we shouldn't have a licensed version without company name,
171
+ // but let's account for it anyway
172
+ version . iTextVersion += "; licensed version)" ;
173
+ } else {
174
+ version . iTextVersion += "; " + version . key + ")" ;
175
+ }
176
+ } else {
177
+ throw new Exception ( ) ;
178
+ }
140
179
} catch ( Exception ) {
141
180
version . iTextVersion += AGPL ;
142
181
}
0 commit comments