Skip to content

Commit

Permalink
IJMP-1845 Fixed synchronization issues
Browse files Browse the repository at this point in the history
Signed-off-by: Katsiaryna Tsytsenia <[email protected]>
  • Loading branch information
Katsiaryna Tsytsenia authored and Katsiaryna Tsytsenia committed Aug 21, 2024
1 parent 86936fa commit 7f36d12
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 98 deletions.
2 changes: 1 addition & 1 deletion gradle/sonar.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ subprojects {
properties {
property "sonar.sources", "src/main/kotlin"
property "sonar.tests", "src/test/kotlin"
property 'sonar.exclusions', "src/IC-231/**, src/IC-223/**"
property 'sonar.exclusions', "**/src/IC-231/**, **/src/IC-223/**"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,30 @@ private fun createCredentialAttributes(key: String): CredentialAttributes {
*/
class CredentialServiceImpl : CredentialService {

/**
* Get PasswordSafe service
* Required for test purposes
*/
private fun getPasswordSafeService(): PasswordSafe {
return service<PasswordSafe>()
}

/**
* Get user credentials by connection config UUID.
* @param connectionConfigUuid connection configuration universally unique identifier.
* @return user credentials [Credentials] if they are.
*/
private fun getCredentials(connectionConfigUuid: String): Credentials? {
return service<PasswordSafe>().get(createCredentialAttributes(connectionConfigUuid))
val credentialAttributes = createCredentialAttributes(connectionConfigUuid)
var ret = service<PasswordSafe>().get(credentialAttributes)
//Another attempt to read the password was added, since the saving of credentials occurs in a separate thread
//and depends on the operating system. If we saved the credentials and try to read them in another thread,
//but they have not yet actually been saved in storage.
if (ret==null){
Thread.sleep(10)
ret = getPasswordSafeService().get(credentialAttributes)
}
return ret
}

/**
Expand Down Expand Up @@ -67,7 +84,7 @@ class CredentialServiceImpl : CredentialService {
val credentialAttributes = createCredentialAttributes(connectionConfigUuid)
val credentials = Credentials(username, password)
runBackgroundableTask("Setting user credentials") {
service<PasswordSafe>().set(credentialAttributes, credentials)
getPasswordSafeService().set(credentialAttributes, credentials)
sendTopic(CREDENTIALS_CHANGED).onChanged(connectionConfigUuid)
}
}
Expand All @@ -78,7 +95,7 @@ class CredentialServiceImpl : CredentialService {
*/
override fun clearCredentials(connectionConfigUuid: String) {
val credentialAttributes = createCredentialAttributes(connectionConfigUuid)
service<PasswordSafe>().set(credentialAttributes, null)
getPasswordSafeService().set(credentialAttributes, null)
}

}
14 changes: 9 additions & 5 deletions src/main/kotlin/org/zowe/explorer/zowe/ZoweStartupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import org.zowe.explorer.config.connect.ConnectionConfig
import org.zowe.explorer.explorer.EXPLORER_NOTIFICATION_GROUP_ID
import org.zowe.explorer.utils.subscribe
import org.zowe.explorer.zowe.service.*
import org.zowe.explorer.zowe.service.ZoweConfigService.Companion.lock
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import java.util.regex.Pattern
import kotlin.concurrent.write

const val ZOWE_CONFIG_NAME = "zowe.config.json"

Expand Down Expand Up @@ -71,7 +73,7 @@ fun showNotificationForAddUpdateZoweConfigIfNeeded(project: Project, type: ZoweC
fun showDialogForDeleteZoweConfigIfNeeded(project: Project, type: ZoweConfigType) {
val zoweConfigService = project.service<ZoweConfigService>()
val zoweConfigState = zoweConfigService.getZoweConfigState(type = type)
if (zoweConfigState != ZoweConfigState.NEED_TO_ADD || zoweConfigState != ZoweConfigState.NOT_EXISTS) {
if (zoweConfigState != ZoweConfigState.NEED_TO_ADD && zoweConfigState != ZoweConfigState.NOT_EXISTS) {
val choice = Messages.showDialog(
project,
"$type Zowe config file has been deleted.\n" +
Expand All @@ -88,10 +90,12 @@ fun showDialogForDeleteZoweConfigIfNeeded(project: Project, type: ZoweConfigType
zoweConfigService.deleteZoweConfig(type)
}
}
if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig = null
else
zoweConfigService.globalZoweConfig = null
lock.write {
if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig = null
else
zoweConfigService.globalZoweConfig = null
}
zoweConfigService.checkAndRemoveOldZoweConnection(type)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.DumbAwareAction
import org.zowe.explorer.utils.write
import org.zowe.explorer.zowe.service.ZoweConfigService
import org.zowe.explorer.zowe.service.ZoweConfigService.Companion.lock
import org.zowe.explorer.zowe.service.ZoweConfigServiceImpl
import org.zowe.explorer.zowe.service.ZoweConfigState
import org.zowe.explorer.zowe.service.ZoweConfigType
Expand All @@ -29,8 +31,7 @@ import org.zowe.kotlinsdk.zowe.config.parseConfigJson
* @since 2021-02-12
*/
class UpdateZoweConfigAction : DumbAwareAction() {

override fun getActionUpdateThread() = ActionUpdateThread.EDT
override fun getActionUpdateThread() = ActionUpdateThread.BGT

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: let {
Expand Down Expand Up @@ -75,27 +76,26 @@ class UpdateZoweConfigAction : DumbAwareAction() {
type = ZoweConfigType.LOCAL

val zoweConfigService = project.service<ZoweConfigService>()

val prevZoweConfig = if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig
else
zoweConfigService.globalZoweConfig
runCatching {
lock.write {
val prevZoweConfig = if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig
else
zoweConfigService.globalZoweConfig
if (type == ZoweConfigType.LOCAL) {
zoweConfigService.localZoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.localZoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
} else {
zoweConfigService.globalZoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.globalZoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
}
}
val zoweState = zoweConfigService.getZoweConfigState(false, type = type)
e.presentation.isEnabledAndVisible =
zoweState == ZoweConfigState.NEED_TO_UPDATE || zoweState == ZoweConfigState.NEED_TO_ADD
if (type == ZoweConfigType.LOCAL) {
zoweConfigService.localZoweConfig = prevZoweConfig
} else {
zoweConfigService.globalZoweConfig = prevZoweConfig
val zoweState = zoweConfigService.getZoweConfigState(false, type = type)
e.presentation.isEnabledAndVisible =
zoweState == ZoweConfigState.NEED_TO_UPDATE || zoweState == ZoweConfigState.NEED_TO_ADD
if (type == ZoweConfigType.LOCAL) {
zoweConfigService.localZoweConfig = prevZoweConfig
} else {
zoweConfigService.globalZoweConfig = prevZoweConfig
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.util.messages.Topic
import org.zowe.explorer.config.connect.ConnectionConfig
import org.zowe.explorer.config.connect.ui.zosmf.ConnectionDialogState
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import java.util.concurrent.locks.ReentrantReadWriteLock


/**
Expand Down Expand Up @@ -102,6 +103,7 @@ interface ZoweConfigService {

companion object {
fun getInstance(project: Project): ZoweConfigService = project.getService(ZoweConfigService::class.java)
val lock = ReentrantReadWriteLock()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import org.zowe.explorer.dataops.DataOpsManager
import org.zowe.explorer.dataops.operations.InfoOperation
import org.zowe.explorer.dataops.operations.ZOSInfoOperation
import org.zowe.explorer.explorer.EXPLORER_NOTIFICATION_GROUP_ID
import org.zowe.explorer.utils.*
import org.zowe.explorer.utils.crudable.find
import org.zowe.explorer.utils.crudable.getAll
import org.zowe.explorer.utils.runTask
import org.zowe.explorer.utils.sendTopic
import org.zowe.explorer.utils.toMutableList
import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.explorer.zowe.service.ZoweConfigService.Companion.lock
import org.zowe.kotlinsdk.annotations.ZVersion
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
Expand Down Expand Up @@ -145,10 +147,12 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
zoweFile.inputStream.use { zoweFileInputStream ->
parseConfigJson(zoweFileInputStream).also { tmpZoweConfig ->
tmpZoweConfig.extractSecureProperties(zoweFile.path.split("/").toTypedArray())
if(type == ZoweConfigType.LOCAL)
localZoweConfig = tmpZoweConfig
else
globalZoweConfig = tmpZoweConfig
lock.write {
if (type == ZoweConfigType.LOCAL)
localZoweConfig = tmpZoweConfig
else
globalZoweConfig = tmpZoweConfig
}
}
}
} catch (e: Exception) {
Expand Down Expand Up @@ -283,12 +287,9 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
}
}
for (zosmfConnection in zoweConfig.getListOfZosmfConections()) {
val username = zosmfConnection.user
val password = zosmfConnection.password
val zoweConnection = prepareConnection(zosmfConnection, type)
val connectionOpt = configCrudable.addOrUpdate(zoweConnection)
if (!connectionOpt.isEmpty) {
CredentialService.instance.setCredentials(connectionOpt.get().uuid, username, password)
var topic = if (type == ZoweConfigType.LOCAL)
LOCAL_ZOWE_CONFIG_CHANGED
else
Expand Down
Loading

0 comments on commit 7f36d12

Please sign in to comment.