Skip to content

Commit 66a5387

Browse files
Fix licensekey loading from GAC
RND-361
1 parent c57fba8 commit 66a5387

File tree

5 files changed

+148
-42
lines changed

5 files changed

+148
-42
lines changed

src/core/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Reflection;
22
using System.Runtime.CompilerServices;
33
using System.Runtime.InteropServices;
4+
using Versions.Attributes;
45

56
[assembly: AssemblyTitle("iTextSharp")]
67
[assembly: AssemblyDescription("A free PDF library ported from Java iText.")]
@@ -16,5 +17,6 @@
1617

1718
[assembly: ComVisibleAttribute(false)]
1819

20+
[assembly: KeyVersion("3.0.1.0")]
1921
[assembly: AssemblyVersion("5.5.13")]
2022
[assembly: AssemblyInformationalVersion("5.5.13-SNAPSHOT")]

src/core/KeyVersionAttribute.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2018 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using System;
44+
45+
namespace Versions.Attributes {
46+
[AttributeUsage(AttributeTargets.Assembly)]
47+
internal class KeyVersionAttribute : Attribute {
48+
private string keyVersion;
49+
50+
internal string KeyVersion {
51+
get { return keyVersion; }
52+
private set { keyVersion = value; }
53+
}
54+
55+
internal KeyVersionAttribute(string keyVersion) {
56+
this.KeyVersion = keyVersion;
57+
}
58+
}
59+
}

src/core/iTextSharp/text/Version.cs

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.IO;
4445
using System.Reflection;
46+
using iTextSharp.text.log;
47+
using Versions.Attributes;
4548

4649
namespace iTextSharp.text {
4750

@@ -86,8 +89,46 @@ public sealed class Version {
8689
* The license key.
8790
*/
8891
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+
/**
91132
* Gets an instance of the iText version that is currently used.
92133
* Note that iText Group requests that you retain the iText producer line
93134
* in every PDF that is created or manipulated using iText.
@@ -97,46 +138,44 @@ public static Version GetInstance() {
97138
version = new Version();
98139
lock (version) {
99140
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+
}
140179
} catch (Exception) {
141180
version.iTextVersion += AGPL;
142181
}

src/core/itextsharp(VS2010).csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
<Compile Include="AssemblyInfo.cs">
147147
<SubType>Code</SubType>
148148
</Compile>
149+
<Compile Include="KeyVersionAttribute.cs">
150+
<SubType>Code</SubType>
151+
</Compile>
149152
<Compile Include="iTextSharp\awt\geom\AffineTransform.cs" />
150153
<Compile Include="iTextSharp\awt\geom\Point.cs" />
151154
<Compile Include="iTextSharp\awt\geom\Point2D.cs" />

src/core/itextsharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
<Compile Include="AssemblyInfo.cs">
107107
<SubType>Code</SubType>
108108
</Compile>
109+
<Compile Include="KeyVersionAttribute.cs">
110+
<SubType>Code</SubType>
111+
</Compile>
109112
<Compile Include="iTextSharp\text\Anchor.cs">
110113
<SubType>Code</SubType>
111114
</Compile>

0 commit comments

Comments
 (0)