Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Nov 3, 2022
1 parent cd25df1 commit e9fe621
Showing 1 changed file with 73 additions and 63 deletions.
136 changes: 73 additions & 63 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
* lock instance for "this"
**/
private final Lock lock = new ReentrantLock();

/**
* static lock instance for the class
**/
private static final Lock LOCK = new ReentrantLock();
private static final Lock sLock = new ReentrantLock();

/**
* Return an existing cached SharedTimer associated with this Connection or create a new one.
Expand Down Expand Up @@ -940,47 +941,48 @@ public static void registerColumnEncryptionKeyStoreProviders(
loggerExternal.entering(loggingClassNameBase, "registerColumnEncryptionKeyStoreProviders",
"Registering Column Encryption Key Store Providers");

LOCK.lock();
sLock.lock();
try {
if (null == clientKeyStoreProviders) {
throw new SQLServerException(null, SQLServerException.getErrString("R_CustomKeyStoreProviderMapNull"),
null, 0, false);
}

if (null != globalCustomColumnEncryptionKeyStoreProviders
&& !globalCustomColumnEncryptionKeyStoreProviders.isEmpty()) {
throw new SQLServerException(null, SQLServerException.getErrString("R_CustomKeyStoreProviderSetOnce"), null,
0, false);
}

globalCustomColumnEncryptionKeyStoreProviders = new HashMap<>();

for (Map.Entry<String, SQLServerColumnEncryptionKeyStoreProvider> entry : clientKeyStoreProviders.entrySet()) {
String providerName = entry.getKey();
if (null == providerName || 0 == providerName.trim().length()) {
throw new SQLServerException(null, SQLServerException.getErrString("R_EmptyCustomKeyStoreProviderName"),
if (null != globalCustomColumnEncryptionKeyStoreProviders
&& !globalCustomColumnEncryptionKeyStoreProviders.isEmpty()) {
throw new SQLServerException(null, SQLServerException.getErrString("R_CustomKeyStoreProviderSetOnce"),
null, 0, false);
}
if ((providerName.substring(0, 6).equalsIgnoreCase(RESERVED_PROVIDER_NAME_PREFIX))) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_InvalidCustomKeyStoreProviderName"));
Object[] msgArgs = {providerName, RESERVED_PROVIDER_NAME_PREFIX};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}

SQLServerColumnEncryptionKeyStoreProvider provider = entry.getValue();
if (null == provider) {
throw new SQLServerException(null, String
.format(SQLServerException.getErrString("R_CustomKeyStoreProviderValueNull"), providerName),
null, 0, false);
}
globalCustomColumnEncryptionKeyStoreProviders = new HashMap<>();

for (Map.Entry<String, SQLServerColumnEncryptionKeyStoreProvider> entry : clientKeyStoreProviders
.entrySet()) {
String providerName = entry.getKey();
if (null == providerName || 0 == providerName.trim().length()) {
throw new SQLServerException(null,
SQLServerException.getErrString("R_EmptyCustomKeyStoreProviderName"), null, 0, false);
}
if ((providerName.substring(0, 6).equalsIgnoreCase(RESERVED_PROVIDER_NAME_PREFIX))) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_InvalidCustomKeyStoreProviderName"));
Object[] msgArgs = {providerName, RESERVED_PROVIDER_NAME_PREFIX};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}

SQLServerColumnEncryptionKeyStoreProvider provider = entry.getValue();
if (null == provider) {
throw new SQLServerException(null, String
.format(SQLServerException.getErrString("R_CustomKeyStoreProviderValueNull"), providerName),
null, 0, false);
}

// Global providers should not use their own CEK caches.
provider.setColumnEncryptionCacheTtl(Duration.ZERO);
globalCustomColumnEncryptionKeyStoreProviders.put(providerName, provider);
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "registerColumnEncryptionKeyStoreProviders",
Expand All @@ -996,14 +998,14 @@ public static void unregisterColumnEncryptionKeyStoreProviders() {
loggerExternal.entering(loggingClassNameBase, "unregisterColumnEncryptionKeyStoreProviders",
"Removing Column Encryption Key Store Provider");

LOCK.lock();
sLock.lock();
try {
if (null != globalCustomColumnEncryptionKeyStoreProviders) {
globalCustomColumnEncryptionKeyStoreProviders.clear();
globalCustomColumnEncryptionKeyStoreProviders = null;
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "unregisterColumnEncryptionKeyStoreProviders",
Expand All @@ -1013,8 +1015,11 @@ public static void unregisterColumnEncryptionKeyStoreProviders() {
SQLServerColumnEncryptionKeyStoreProvider getGlobalSystemColumnEncryptionKeyStoreProvider(String providerName) {
lock.lock();
try {
return (null != globalSystemColumnEncryptionKeyStoreProviders && globalSystemColumnEncryptionKeyStoreProviders.containsKey(
providerName)) ? globalSystemColumnEncryptionKeyStoreProviders.get(providerName) : null;
return (null != globalSystemColumnEncryptionKeyStoreProviders
&& globalSystemColumnEncryptionKeyStoreProviders.containsKey(providerName))
? globalSystemColumnEncryptionKeyStoreProviders
.get(providerName)
: null;
} finally {
lock.unlock();
}
Expand All @@ -1023,8 +1028,8 @@ SQLServerColumnEncryptionKeyStoreProvider getGlobalSystemColumnEncryptionKeyStor
String getAllGlobalCustomSystemColumnEncryptionKeyStoreProviders() {
lock.lock();
try {
return (null != globalCustomColumnEncryptionKeyStoreProviders)
? globalCustomColumnEncryptionKeyStoreProviders.keySet().toString() : null;
return (null != globalCustomColumnEncryptionKeyStoreProviders) ? globalCustomColumnEncryptionKeyStoreProviders
.keySet().toString() : null;
} finally {
lock.unlock();
}
Expand All @@ -1038,8 +1043,8 @@ String getAllSystemColumnEncryptionKeyStoreProviders() {
keyStores = systemColumnEncryptionKeyStoreProvider.keySet().toString();
}
if (0 != SQLServerConnection.globalSystemColumnEncryptionKeyStoreProviders.size()) {
keyStores += "," + SQLServerConnection.globalSystemColumnEncryptionKeyStoreProviders.keySet()
.toString();
keyStores += ","
+ SQLServerConnection.globalSystemColumnEncryptionKeyStoreProviders.keySet().toString();
}
return keyStores;
} finally {
Expand All @@ -1050,8 +1055,11 @@ String getAllSystemColumnEncryptionKeyStoreProviders() {
SQLServerColumnEncryptionKeyStoreProvider getGlobalCustomColumnEncryptionKeyStoreProvider(String providerName) {
lock.lock();
try {
return (null != globalCustomColumnEncryptionKeyStoreProviders && globalCustomColumnEncryptionKeyStoreProviders.containsKey(
providerName)) ? globalCustomColumnEncryptionKeyStoreProviders.get(providerName) : null;
return (null != globalCustomColumnEncryptionKeyStoreProviders
&& globalCustomColumnEncryptionKeyStoreProviders.containsKey(providerName))
? globalCustomColumnEncryptionKeyStoreProviders
.get(providerName)
: null;
} finally {
lock.unlock();
}
Expand All @@ -1060,8 +1068,8 @@ SQLServerColumnEncryptionKeyStoreProvider getGlobalCustomColumnEncryptionKeyStor
SQLServerColumnEncryptionKeyStoreProvider getSystemColumnEncryptionKeyStoreProvider(String providerName) {
lock.lock();
try {
return (null != systemColumnEncryptionKeyStoreProvider && systemColumnEncryptionKeyStoreProvider.containsKey(
providerName)) ? systemColumnEncryptionKeyStoreProvider.get(providerName) : null;
return (null != systemColumnEncryptionKeyStoreProvider && systemColumnEncryptionKeyStoreProvider
.containsKey(providerName)) ? systemColumnEncryptionKeyStoreProvider.get(providerName) : null;
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -1103,7 +1111,8 @@ SQLServerColumnEncryptionKeyStoreProvider getSystemOrGlobalColumnEncryptionKeySt
boolean hasConnectionColumnEncryptionKeyStoreProvidersRegistered() {
lock.lock();
try {
return null != connectionColumnEncryptionKeyStoreProvider && connectionColumnEncryptionKeyStoreProvider.size() > 0;
return null != connectionColumnEncryptionKeyStoreProvider
&& connectionColumnEncryptionKeyStoreProvider.size() > 0;
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -1154,7 +1163,7 @@ SQLServerColumnEncryptionKeyStoreProvider getColumnEncryptionKeyStoreProviderOnC
* Registers connection-level key store providers, replacing all existing providers.
*
* @param clientKeyStoreProviders
* a map containing the store providers information.
* a map containing the store providers information.
* @throws SQLServerException
* when an error occurs
*/
Expand All @@ -1171,7 +1180,8 @@ public void registerColumnEncryptionKeyStoreProvidersOnConnection(

connectionColumnEncryptionKeyStoreProvider.clear();

for (Map.Entry<String, SQLServerColumnEncryptionKeyStoreProvider> entry : clientKeyStoreProviders.entrySet()) {
for (Map.Entry<String, SQLServerColumnEncryptionKeyStoreProvider> entry : clientKeyStoreProviders
.entrySet()) {
String providerName = entry.getKey();
if (null == providerName || 0 == providerName.trim().length()) {
throw new SQLServerException(null,
Expand All @@ -1187,9 +1197,9 @@ public void registerColumnEncryptionKeyStoreProvidersOnConnection(
}

if (null == entry.getValue()) {
throw new SQLServerException(null,
String.format(SQLServerException.getErrString("R_CustomKeyStoreProviderValueNull"),
providerName), null, 0, false);
throw new SQLServerException(null, String
.format(SQLServerException.getErrString("R_CustomKeyStoreProviderValueNull"), providerName),
null, 0, false);
}

connectionColumnEncryptionKeyStoreProvider.put(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -1217,15 +1227,15 @@ public static void setColumnEncryptionTrustedMasterKeyPaths(Map<String, List<Str
loggerExternal.entering(loggingClassNameBase, "setColumnEncryptionTrustedMasterKeyPaths",
"Setting Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.clear();
for (Map.Entry<String, List<String>> entry : trustedKeyPaths.entrySet()) {
columnEncryptionTrustedMasterKeyPaths.put(entry.getKey().toUpperCase(), entry.getValue());
}
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "setColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1244,12 +1254,12 @@ public static void updateColumnEncryptionTrustedMasterKeyPaths(String server, Li
loggerExternal.entering(loggingClassNameBase, "updateColumnEncryptionTrustedMasterKeyPaths",
"Updating Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.put(server.toUpperCase(), trustedKeyPaths);
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "updateColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1266,12 +1276,12 @@ public static void removeColumnEncryptionTrustedMasterKeyPaths(String server) {
loggerExternal.entering(loggingClassNameBase, "removeColumnEncryptionTrustedMasterKeyPaths",
"Removing Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
// Use upper case for server and instance names.
columnEncryptionTrustedMasterKeyPaths.remove(server.toUpperCase());
} finally {
LOCK.unlock();
sLock.unlock();
}

loggerExternal.exiting(loggingClassNameBase, "removeColumnEncryptionTrustedMasterKeyPaths",
Expand All @@ -1287,7 +1297,7 @@ public static Map<String, List<String>> getColumnEncryptionTrustedMasterKeyPaths
loggerExternal.entering(loggingClassNameBase, "getColumnEncryptionTrustedMasterKeyPaths",
"Getting Trusted Master Key Paths");

LOCK.lock();
sLock.lock();
try {
Map<String, List<String>> masterKeyPathCopy = new HashMap<>();

Expand All @@ -1300,12 +1310,12 @@ public static Map<String, List<String>> getColumnEncryptionTrustedMasterKeyPaths

return masterKeyPathCopy;
} finally {
LOCK.unlock();
sLock.unlock();
}
}

static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Boolean[] hasEntry) {
LOCK.lock();
sLock.lock();
try {
if (columnEncryptionTrustedMasterKeyPaths.containsKey(server)) {
hasEntry[0] = true;
Expand All @@ -1315,7 +1325,7 @@ static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Bool
return null;
}
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand All @@ -1324,11 +1334,11 @@ static List<String> getColumnEncryptionTrustedMasterKeyPaths(String server, Bool
* request to acquire an access token.
*/
public static void clearUserTokenCache() {
LOCK.lock();
sLock.lock();
try {
PersistentTokenCacheAccessAspect.clearUserTokenCache();
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand Down Expand Up @@ -7430,26 +7440,26 @@ void doSecurityCheck() {
*/
public static void setColumnEncryptionKeyCacheTtl(int columnEncryptionKeyCacheTTL,
TimeUnit unit) throws SQLServerException {
LOCK.lock();
sLock.lock();
try {
if (columnEncryptionKeyCacheTTL < 0 || unit.equals(TimeUnit.MILLISECONDS) || unit.equals(
TimeUnit.MICROSECONDS) || unit.equals(TimeUnit.NANOSECONDS)) {
if (columnEncryptionKeyCacheTTL < 0 || unit.equals(TimeUnit.MILLISECONDS)
|| unit.equals(TimeUnit.MICROSECONDS) || unit.equals(TimeUnit.NANOSECONDS)) {
throw new SQLServerException(null, SQLServerException.getErrString("R_invalidCEKCacheTtl"), null, 0,
false);
}

columnEncryptionKeyCacheTtl = TimeUnit.SECONDS.convert(columnEncryptionKeyCacheTTL, unit);
} finally {
LOCK.unlock();
sLock.unlock();
}
}

static long getColumnEncryptionKeyCacheTtl() {
LOCK.lock();
sLock.lock();
try {
return columnEncryptionKeyCacheTtl;
} finally {
LOCK.unlock();
sLock.unlock();
}
}

Expand Down

0 comments on commit e9fe621

Please sign in to comment.