forked from tnb-software/TNB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Accounts] Add support for composite accounts
- Loading branch information
Showing
48 changed files
with
480 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
system-x/common/src/main/java/software/tnb/common/account/CredentialsLoader.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
system-x/common/src/main/java/software/tnb/common/account/loader/CredentialsLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package software.tnb.common.account.loader; | ||
|
||
import software.tnb.common.account.Account; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.List; | ||
|
||
public abstract class CredentialsLoader { | ||
private static final Logger LOG = LoggerFactory.getLogger(CredentialsLoader.class); | ||
|
||
protected final ObjectMapper mapper; | ||
|
||
public abstract Object loadCredentials(String credentialsId); | ||
|
||
public abstract String toJson(Object credentials); | ||
|
||
public CredentialsLoader() { | ||
mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
} | ||
|
||
public <T extends Account> T get(List<String> credentialsIds, Class<T> accountClass) { | ||
T account = null; | ||
try { | ||
for (String id : credentialsIds) { | ||
Object credentials = loadCredentials(id); | ||
if (credentials != null) { | ||
if (account == null) { | ||
LOG.trace("Creating {} instance from credentials {}", accountClass.getSimpleName(), id); | ||
account = mapper.readValue(toJson(credentials), accountClass); | ||
} else { | ||
LOG.trace("Updating {} instance with credentials {}", accountClass.getSimpleName(), id); | ||
mapper.readerForUpdating(account).readValue(toJson(credentials)); | ||
} | ||
} else { | ||
LOG.trace("Account with id {} not found in credentials", id); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("Couldnt get credentials from ids: " + String.join(",", credentialsIds), e); | ||
} | ||
|
||
if (account == null) { | ||
throw new IllegalArgumentException(String.format("Unable to create %s instance from credentials [%s]" | ||
+ " - no credentials with given ids found", accountClass.getSimpleName(), String.join(",", credentialsIds))); | ||
} | ||
|
||
return account; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 13 additions & 11 deletions
24
...common/account/YamlCredentialsLoader.java → ...account/loader/YamlCredentialsLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,38 @@ | ||
package software.tnb.common.account; | ||
package software.tnb.common.account.loader; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
import java.io.FileInputStream; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
|
||
public class YamlCredentialsLoader implements CredentialsLoader { | ||
public class YamlCredentialsLoader extends CredentialsLoader { | ||
private static final Logger LOG = LoggerFactory.getLogger(YamlCredentialsLoader.class); | ||
|
||
private final Map<String, Map<String, Object>> credentials; | ||
private final ObjectMapper mapper; | ||
|
||
public YamlCredentialsLoader(String credentialsFile) throws Exception { | ||
try (FileInputStream fs = new FileInputStream(Paths.get(credentialsFile).toAbsolutePath().toString())) { | ||
LOG.info("Loading credentials file from {}", Paths.get(credentialsFile).toAbsolutePath()); | ||
credentials = (Map) ((Map) new Yaml().load(fs)).get("services"); | ||
mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
} | ||
} | ||
|
||
@Override | ||
public <T extends Account> T get(String credentialsId, Class<T> accountClass) { | ||
if (!credentials.containsKey(credentialsId)) { | ||
return null; | ||
public Object loadCredentials(String credentialsId) { | ||
return credentials.getOrDefault(credentialsId, Map.of()).getOrDefault("credentials", null); | ||
} | ||
|
||
@Override | ||
public String toJson(Object credentials) { | ||
try { | ||
return mapper.writeValueAsString(credentials); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException("Unable to convert credentials to json", e); | ||
} | ||
return accountClass.cast(mapper.convertValue(credentials.get(credentialsId).get("credentials"), accountClass)); | ||
} | ||
} |
Oops, something went wrong.