Skip to content

Commit

Permalink
Move getSize() check to proxy storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jacodg committed Jan 3, 2024
1 parent 399cee0 commit a492676
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022-2023 WeAreFrank!
Copyright 2022-2024 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,7 +63,6 @@ public class DatabaseStorage implements LogStorage, CrudStorage {
protected static final String TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS";
protected @Setter @Getter String name;
protected @Setter String table;
protected @Setter @Getter Boolean checkTableExists;
protected @Setter @Inject @Autowired List<String> metadataNames; // Used as column names in this storage
protected @Setter String storageIdColumn;
protected @Setter List<String> bigValueColumns; // Columns for which to limit the number of retrieved characters to 100
Expand All @@ -85,14 +84,6 @@ public String getTable() {
}
}

public boolean isCheckTableExists() {
if (checkTableExists == null) {
return true;
} else {
return checkTableExists;
}
}

public List<String> getMetadataNames() {
if (getMetadataExtractor().getExtraMetadataFieldExtractors() != null) {
List<String> metadataNames = new ArrayList<String>(this.metadataNames);
Expand Down Expand Up @@ -143,10 +134,6 @@ public long getMaxStorageSize() {

@PostConstruct
public void init() throws StorageException {
if (isCheckTableExists()) {
// Throw exception if table doesn't exist so ProxyStorage (when used) can try an alternative destination
getSize();
}
if (!(getMetadataNames() != null && getMetadataNames().contains(getStorageIdColumn()))) {
throw new StorageException("List metadataNames " + metadataNames
+ " should at least contain storageId column name '" + getStorageIdColumn() + "'");
Expand Down
70 changes: 10 additions & 60 deletions src/main/java/nl/nn/testtool/storage/proxy/ProxyStorage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022-2023 WeAreFrank!
Copyright 2022-2024 WeAreFrank!
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,93 +23,43 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Getter;
import lombok.Setter;
import nl.nn.testtool.MetadataExtractor;
import nl.nn.testtool.Report;
import nl.nn.testtool.storage.CrudStorage;
import nl.nn.testtool.storage.LogStorage;
import nl.nn.testtool.storage.Storage;
import nl.nn.testtool.storage.StorageException;
import nl.nn.testtool.storage.database.DatabaseStorage;
import nl.nn.testtool.storage.proofofmigration.ProofOfMigrationStorage;
import nl.nn.testtool.storage.xml.XmlStorage;

/**
* Storage proxy class than can help Spring XML configuration to become more flexible (e.g. see #{testStorageActive} in
* Frank!Framework's springIbisTestTool.xml) and when a storage cannot be initialized it will, when specified, try
* another storage. This can be used to have the database storage check whether the Ladybug tables are available and if not
* use another storage (see proxy storage usage in Frank!Framework's springIbisTestTool.xml).
* Frank!Framework's springIbisTestTool.xml) and use an alternative storage when getSize() on the preferred storage
* throws an exception. This can for example be used to check whether the Ladybug tables are available and if not use
* file storage instead of database storage (see proxy storage usage in Frank!Framework's springIbisTestTool.xml).
*
* @author Jaco de Groot
*/
public class ProxyStorage implements CrudStorage, LogStorage {
private static Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private @Setter @Getter String name;
private @Setter Storage destination;
private @Setter Storage alternativeDestination;

@Override
public void setName(String name) {
destination.setName(name);
}

@Override
public String getName() {
return destination.getName();
}

public void setMetadataExtractor(MetadataExtractor metadataExtractor) {
if (destination instanceof nl.nn.testtool.storage.file.Storage) {
((nl.nn.testtool.storage.file.Storage)destination).setMetadataExtractor(metadataExtractor);
} else if (destination instanceof nl.nn.testtool.storage.file.TestStorage) {
((nl.nn.testtool.storage.file.TestStorage)destination).setMetadataExtractor(metadataExtractor);
} else if (destination instanceof nl.nn.testtool.storage.memory.Storage) {
((nl.nn.testtool.storage.memory.Storage)destination).setMetadataExtractor(metadataExtractor);
} else if (destination instanceof DatabaseStorage) {
((DatabaseStorage)destination).setMetadataExtractor(metadataExtractor);
} else if (destination instanceof ProofOfMigrationStorage) {
((ProofOfMigrationStorage)destination).setMetadataExtractor(metadataExtractor);
} else if (destination instanceof nl.nn.testtool.storage.zip.Storage) {
((nl.nn.testtool.storage.zip.Storage)destination).setMetadataExtractor(metadataExtractor);
} else {
throw new RuntimeException("Method setMetadataExtractor() not implemented for "
+ destination.getClass().getName());
}
}

@PostConstruct
public void init() throws StorageException {
try {
init(destination);
destination.getSize();
} catch(StorageException e) {
if (alternativeDestination != null) {
log.debug("Could not init " + destination.getName() + " will try " + alternativeDestination.getName()
+ "instead");
init(alternativeDestination);
log.debug("Could not init " + destination.getName() + " will use " + alternativeDestination.getName()
+ " instead");
destination = alternativeDestination;
} else {
throw e;
}
}
}

public void init(Storage destination) throws StorageException {
if (destination instanceof nl.nn.testtool.storage.file.Storage) {
((nl.nn.testtool.storage.file.Storage)destination).init();
} else if (destination instanceof nl.nn.testtool.storage.file.TestStorage) {
((nl.nn.testtool.storage.file.TestStorage)destination).init();
} else if (destination instanceof DatabaseStorage) {
((DatabaseStorage)destination).init();
} else if (destination instanceof ProofOfMigrationStorage) {
((ProofOfMigrationStorage)destination).init();
} else if (destination instanceof nl.nn.testtool.storage.diff.Storage) {
((nl.nn.testtool.storage.diff.Storage)destination).init();
} else if (destination instanceof XmlStorage) {
((XmlStorage)destination).init();
} else {
throw new RuntimeException("Method setMetadataExtractor() not implemented for "
+ destination.getClass().getName());
}
}

@Override
public synchronized void store(Report report) throws StorageException {
((CrudStorage)destination).store(report);
Expand Down

0 comments on commit a492676

Please sign in to comment.