Skip to content

Commit

Permalink
Just reformatting. (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed Jun 3, 2017
1 parent d0f36fb commit 077dfcd
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,44 @@
@Slf4j
public class MemoryConfiguration {

@Bean
@ConfigurationProperties("kayenta.memory")
MemoryConfigurationProperties memoryConfigurationProperties() {
return new MemoryConfigurationProperties();
}
@Bean
@ConfigurationProperties("kayenta.memory")
MemoryConfigurationProperties memoryConfigurationProperties() {
return new MemoryConfigurationProperties();
}

@Bean
StorageService storageService(MemoryConfigurationProperties memoryConfigurationProperties,
AccountCredentialsRepository accountCredentialsRepository) {
MemoryStorageService.MemoryStorageServiceBuilder memoryStorageServiceBuilder = MemoryStorageService.builder();
@Bean
StorageService storageService(MemoryConfigurationProperties memoryConfigurationProperties,
AccountCredentialsRepository accountCredentialsRepository) {
MemoryStorageService.MemoryStorageServiceBuilder memoryStorageServiceBuilder = MemoryStorageService.builder();

for (MemoryManagedAccount memoryManagedAccount : memoryConfigurationProperties.getAccounts()) {
String name = memoryManagedAccount.getName();
String namespace = memoryManagedAccount.getNamespace();
List<AccountCredentials.Type> supportedTypes = memoryManagedAccount.getSupportedTypes();
for (MemoryManagedAccount memoryManagedAccount : memoryConfigurationProperties.getAccounts()) {
String name = memoryManagedAccount.getName();
String namespace = memoryManagedAccount.getNamespace();
List<AccountCredentials.Type> supportedTypes = memoryManagedAccount.getSupportedTypes();

log.info("Registering Memory account {} with supported types {}.", name, supportedTypes);
log.info("Registering Memory account {} with supported types {}.", name, supportedTypes);

MemoryAccountCredentials memoryAccountCredentials =
MemoryAccountCredentials.builder().build();
MemoryNamedAccountCredentials.MemoryNamedAccountCredentialsBuilder memoryNamedAccountCredentialsBuilder =
MemoryNamedAccountCredentials.builder()
.name(name)
.namespace(namespace)
.credentials(memoryAccountCredentials);
MemoryAccountCredentials memoryAccountCredentials = MemoryAccountCredentials.builder().build();
MemoryNamedAccountCredentials.MemoryNamedAccountCredentialsBuilder memoryNamedAccountCredentialsBuilder =
MemoryNamedAccountCredentials.builder()
.name(name)
.namespace(namespace)
.credentials(memoryAccountCredentials);

if (!CollectionUtils.isEmpty(supportedTypes)) {
memoryNamedAccountCredentialsBuilder.supportedTypes(supportedTypes);
}
if (!CollectionUtils.isEmpty(supportedTypes)) {
memoryNamedAccountCredentialsBuilder.supportedTypes(supportedTypes);
}

MemoryNamedAccountCredentials memoryNamedAccountCredentials = memoryNamedAccountCredentialsBuilder.build();
accountCredentialsRepository.save(name, memoryNamedAccountCredentials);
memoryStorageServiceBuilder.accountName(name);
}
MemoryNamedAccountCredentials memoryNamedAccountCredentials = memoryNamedAccountCredentialsBuilder.build();
accountCredentialsRepository.save(name, memoryNamedAccountCredentials);
memoryStorageServiceBuilder.accountName(name);
}

MemoryStorageService memoryStorageService = memoryStorageServiceBuilder.build();
MemoryStorageService memoryStorageService = memoryStorageServiceBuilder.build();

log.info("Populated MemoryStorageService with {} in-memory accounts.", memoryStorageService.getAccountNames().size());
log.info("Populated MemoryStorageService with {} in-memory accounts.", memoryStorageService.getAccountNames().size());

return memoryStorageService;
}
return memoryStorageService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;

public class MemoryConfigurationProperties {
@Getter
private List<MemoryManagedAccount> accounts = new ArrayList<>();

@Getter
private List<MemoryManagedAccount> accounts = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

@Data
public class MemoryManagedAccount {
@NotNull
private String name;

private String namespace;
@NotNull
private String name;

private List<AccountCredentials.Type> supportedTypes;
private String namespace;

private List<AccountCredentials.Type> supportedTypes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
// TODO: Not sure what kind of credentials or configuration is really required here yet.
public class MemoryAccountCredentials {

private static String applicationVersion =
Optional.ofNullable(MemoryAccountCredentials.class.getPackage().getImplementationVersion()).orElse("Unknown");
private static String applicationVersion =
Optional.ofNullable(MemoryAccountCredentials.class.getPackage().getImplementationVersion()).orElse("Unknown");
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@
@Builder
@Data
public class MemoryNamedAccountCredentials implements AccountCredentials<MemoryAccountCredentials> {
@NotNull
private String name;

@NotNull
@Singular
private List<Type> supportedTypes;
@NotNull
private String name;

@NotNull
private MemoryAccountCredentials credentials;
@NotNull
@Singular
private List<Type> supportedTypes;

@NotNull
private String namespace;
@NotNull
private MemoryAccountCredentials credentials;

@Override
public String getType() {
return "memory";
}
@NotNull
private String namespace;

@Override
public String getType() {
return "memory";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,111 +37,113 @@

@Builder
class ObjectMetadata {
@NotNull
public final String name;

@NotNull
public final String name;
}

@Builder
@Slf4j
public class MemoryStorageService implements StorageService {

public static class MemoryStorageServiceBuilder {
private Map<String, String> entries = new ConcurrentHashMap<String, String>();
}

private final ObjectMapper objectMapper = new ObjectMapper();

@NotNull
@Singular
@Getter
private List<String> accountNames;

@Autowired
AccountCredentialsRepository accountCredentialsRepository;

private Map<String, String> entries;

@Override
public boolean servicesAccount(String accountName) {
return accountNames.contains(accountName);
}

@Override
public <T> T loadObject(String accountName, ObjectType objectType, String objectKey) throws IllegalArgumentException {
String key = makekey(accountName, objectType, objectKey);
log.info("Getting key {}", key);
String json = entries.get(key);
if (json == null)
throw new IllegalArgumentException("No such object named " + key);

try {
return objectMapper.readValue(json, objectType.getTypeReference());
} catch (IOException e) {
log.error("Read failed on path {}: {}", key, e);
throw new IllegalStateException(e);
}
}
public static class MemoryStorageServiceBuilder {
private Map<String, String> entries = new ConcurrentHashMap<String, String>();
}

@Override
public <T> void storeObject(String accountName, ObjectType objectType, String objectKey, T obj) {
String key = makekey(accountName, objectType, objectKey);
log.info("Writing key {}", key);
try {
String json = objectMapper.writeValueAsString(obj);
entries.put(key, json);
} catch (IOException e) {
log.error("Update failed on path {}: {}", key, e);
throw new IllegalStateException(e);
}
}
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public void deleteObject(String accountName, ObjectType objectType, String objectKey) {
String key = makekey(accountName, objectType, objectKey);
log.info("Deleting key {}", key);
String oldValue = entries.remove(key);
if (oldValue == null) {
log.error("Object named {} does not exist", key);
throw new IllegalStateException("Does not exist");
}
}
@NotNull
@Singular
@Getter
private List<String> accountNames;

@Override
public List<Map<String, Object>> listObjectKeys(String accountName, ObjectType objectType) {
String prefix = makeprefix(accountName, objectType);
@Autowired
AccountCredentialsRepository accountCredentialsRepository;

return entries
.entrySet()
.parallelStream()
.filter(e -> e.getKey().startsWith(prefix))
.map(e -> mapFrom(e.getKey()))
.collect(Collectors.toList());
}
private Map<String, String> entries;

private Map<String, Object> mapFrom(String key) {
Map<String, Object> ret = new HashMap<String, Object>();
ret.put(nameFromKey(key), metadataFor(key));
return ret;
}
@Override
public boolean servicesAccount(String accountName) {
return accountNames.contains(accountName);
}

private String nameFromKey(String key) {
return key.split(":", 3)[2];
@Override
public <T> T loadObject(String accountName, ObjectType objectType, String objectKey) throws IllegalArgumentException {
String key = makeKey(accountName, objectType, objectKey);
log.info("Getting key {}", key);
String json = entries.get(key);
if (json == null) {
throw new IllegalArgumentException("No such object named " + key);
}

private ObjectMetadata metadataFor(String key) {
return ObjectMetadata.builder().name(nameFromKey(key)).build();
try {
return objectMapper.readValue(json, objectType.getTypeReference());
} catch (IOException e) {
log.error("Read failed on path {}: {}", key, e);
throw new IllegalStateException(e);
}

private String makeprefix(String accountName, ObjectType objectType) {
MemoryNamedAccountCredentials credentials = (MemoryNamedAccountCredentials)accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
String namespace = credentials.getNamespace();
String typename = objectType.getTypeReference().getType().getTypeName();
return namespace + ":" + typename + ":";
}

@Override
public <T> void storeObject(String accountName, ObjectType objectType, String objectKey, T obj) {
String key = makeKey(accountName, objectType, objectKey);
log.info("Writing key {}", key);
try {
String json = objectMapper.writeValueAsString(obj);
entries.put(key, json);
} catch (IOException e) {
log.error("Update failed on path {}: {}", key, e);
throw new IllegalStateException(e);
}

private String makekey(String accountName, ObjectType objectType, String objectKey) {
return makeprefix(accountName, objectType) + objectKey;
}

@Override
public void deleteObject(String accountName, ObjectType objectType, String objectKey) {
String key = makeKey(accountName, objectType, objectKey);
log.info("Deleting key {}", key);
String oldValue = entries.remove(key);
if (oldValue == null) {
log.error("Object named {} does not exist", key);
throw new IllegalStateException("Does not exist");
}
}

@Override
public List<Map<String, Object>> listObjectKeys(String accountName, ObjectType objectType) {
String prefix = makePrefix(accountName, objectType);

return entries
.entrySet()
.parallelStream()
.filter(e -> e.getKey().startsWith(prefix))
.map(e -> mapFrom(e.getKey()))
.collect(Collectors.toList());
}

private Map<String, Object> mapFrom(String key) {
Map<String, Object> ret = new HashMap<String, Object>();
ret.put(nameFromKey(key), metadataFor(key));
return ret;
}

private String nameFromKey(String key) {
return key.split(":", 3)[2];
}

private ObjectMetadata metadataFor(String key) {
return ObjectMetadata.builder().name(nameFromKey(key)).build();
}

private String makePrefix(String accountName, ObjectType objectType) {
MemoryNamedAccountCredentials credentials = (MemoryNamedAccountCredentials) accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
String namespace = credentials.getNamespace();
String typename = objectType.getTypeReference().getType().getTypeName();
return namespace + ":" + typename + ":";
}

private String makeKey(String accountName, ObjectType objectType, String objectKey) {
return makePrefix(accountName, objectType) + objectKey;
}
}

0 comments on commit 077dfcd

Please sign in to comment.