Skip to content

Commit cf1a8bc

Browse files
authored
Add caching to ServerKey and SiteKey. (#634)
1 parent aae37fe commit cf1a8bc

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

common/src/main/java/com/genexus/util/IniFile.java

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import java.util.concurrent.ConcurrentHashMap;
88

99
import com.genexus.*;
10-
import com.genexus.util.*;
1110

11+
import com.genexus.diagnostics.core.ILogger;
12+
import com.genexus.diagnostics.core.LogManager;
1213
import json.org.json.*;
1314

1415

1516
public class IniFile {
17+
public static final ILogger logger = LogManager.getLogger(IniFile.class);
1618
private static final int SECTION = 1;
1719
private static final int PROPERTY = 2;
1820
private static final int COMMENT = 3;
@@ -29,8 +31,8 @@ public class IniFile {
2931
private String defaultSiteKey = "7E2E22D26FF2989E2444852A85E57867";
3032
private String defaultServerKey = "7E2E22D26FF2989E2444852A85E57867";
3133

32-
private String realSiteKey;
33-
private String realServerKey;
34+
private String siteKey;
35+
private String serverKey;
3436

3537
private static ConcurrentHashMap<String, String> s_confMapping;
3638
private static String CONFMAPPING_FILE = "confmapping.json";
@@ -57,46 +59,64 @@ public IniFile(String filename) {
5759

5860
public void setEncryptionStream(InputStream is) {
5961
this.encryptionIs = is;
62+
ensureServerKey();
63+
ensureSiteKey();
6064
}
6165

6266
public String getServerKey() {
63-
if (realServerKey != null)
64-
return realServerKey;
67+
if (serverKey != null) {
68+
return serverKey;
69+
}
70+
ensureServerKey();
71+
return serverKey;
72+
}
6573

66-
realServerKey = getFromKeyFile(0);
67-
if (realServerKey != null)
68-
return realServerKey;
74+
private void ensureServerKey() {
75+
serverKey = getFromKeyFile(0);
76+
if (serverKey != null) {
77+
return;
78+
}
6979

70-
if (encryptionIs == null)
71-
return defaultServerKey;
80+
if (encryptionIs == null) {
81+
serverKey = defaultServerKey;
82+
return;
83+
}
7284

7385
try {
7486
IniFile crypto = new IniFile(encryptionIs);
75-
realServerKey = crypto.getProperty("Encryption", "ServerKey", null);
87+
serverKey = crypto.getProperty("Encryption", "ServerKey", null);
7688
} catch (Exception e) {
89+
logger.debug("Could not read ServerKey from InputStream" , e);
7790
}
78-
79-
return realServerKey == null ? defaultServerKey : realServerKey;
91+
serverKey = serverKey == null ? defaultServerKey: serverKey;
8092
}
8193

8294
public String getSiteKey() {
83-
if (realSiteKey != null)
84-
return realSiteKey;
95+
if (siteKey != null) {
96+
return siteKey;
97+
}
98+
ensureSiteKey();
99+
return siteKey;
100+
}
85101

86-
realSiteKey = getFromKeyFile(1);
87-
if (realSiteKey != null)
88-
return realSiteKey;
102+
private void ensureSiteKey() {
103+
siteKey = getFromKeyFile(1);
104+
if (siteKey != null) {
105+
return;
106+
}
89107

90-
if (encryptionIs == null)
91-
return defaultSiteKey;
108+
if (encryptionIs == null) {
109+
siteKey = defaultSiteKey;
110+
return;
111+
}
92112

93113
try {
94114
IniFile crypto = new IniFile(encryptionIs);
95-
realSiteKey = crypto.getProperty("Encryption", "SiteKey", null);
115+
siteKey = crypto.getProperty("Encryption", "SiteKey", null);
96116
} catch (Exception e) {
97117
}
98118

99-
return realSiteKey == null ? defaultSiteKey : realSiteKey;
119+
siteKey = siteKey == null ? defaultSiteKey : siteKey;
100120
}
101121

102122
String getFromKeyFile(int lineNo) {

0 commit comments

Comments
 (0)