diff --git a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml index 5936dd14a..2c6e699ce 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/pom.xml +++ b/basyx.aasrepository/basyx.aasrepository-backend/pom.xml @@ -15,6 +15,10 @@ basyx.aasrepository-backend + + org.eclipse.digitaltwin.basyx + basyx.backend + org.eclipse.digitaltwin.basyx basyx.aasrepository-core diff --git a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThreadSafeAasRepository.java b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThreadSafeAasRepository.java index 5700d40a8..1f31a98d0 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThreadSafeAasRepository.java +++ b/basyx.aasrepository/basyx.aasrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/ThreadSafeAasRepository.java @@ -3,12 +3,12 @@ import java.io.File; import java.io.InputStream; import java.util.List; -import java.util.concurrent.locks.ReentrantReadWriteLock; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; import org.eclipse.digitaltwin.aas4j.v3.model.Reference; import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository; +import org.eclipse.digitaltwin.basyx.common.backend.ThreadSafeAccess; import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException; import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException; import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException; @@ -18,7 +18,7 @@ public class ThreadSafeAasRepository implements AasRepository { private final AasRepository decoratedAasRepository; - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private final ThreadSafeAccess access = new ThreadSafeAccess(); public ThreadSafeAasRepository(AasRepository decoratedRepository) { this.decoratedAasRepository = decoratedRepository; @@ -26,131 +26,72 @@ public ThreadSafeAasRepository(AasRepository decoratedRepository) { @Override public CursorResult> getAllAas(PaginationInfo pInfo) { - lock.readLock().lock(); - try { - return decoratedAasRepository.getAllAas(pInfo); - } finally { - lock.readLock().unlock(); - } + return access.read(decoratedAasRepository::getAllAas, pInfo); } @Override public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistException { - lock.readLock().lock(); - try { - return decoratedAasRepository.getAas(aasId); - } finally { - lock.readLock().unlock(); - } + return access.read(decoratedAasRepository::getAas, aasId); } @Override public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException { - lock.writeLock().lock(); - try { - decoratedAasRepository.createAas(aas); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::createAas, aas); } @Override public void deleteAas(String aasId) { - lock.writeLock().lock(); - try { - decoratedAasRepository.deleteAas(aasId); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::deleteAas, aasId); } @Override public void updateAas(String aasId, AssetAdministrationShell aas) { - lock.writeLock().lock(); - try { - decoratedAasRepository.updateAas(aasId, aas); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::updateAas, aasId, aas); } @Override public CursorResult> getSubmodelReferences(String aasId, PaginationInfo pInfo) { - lock.readLock().lock(); - try { - return decoratedAasRepository.getSubmodelReferences(aasId, pInfo); - } finally { - lock.readLock().unlock(); - } + return access.read(decoratedAasRepository::getSubmodelReferences, aasId, pInfo); } @Override public void addSubmodelReference(String aasId, Reference submodelReference) { - lock.writeLock().lock(); - try { - decoratedAasRepository.addSubmodelReference(aasId, submodelReference); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::addSubmodelReference, aasId, submodelReference); } @Override public void removeSubmodelReference(String aasId, String submodelId) { - lock.writeLock().lock(); - try { - decoratedAasRepository.removeSubmodelReference(aasId, submodelId); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::removeSubmodelReference, aasId, submodelId); } @Override public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException { - lock.writeLock().lock(); - try { - decoratedAasRepository.setAssetInformation(aasId, aasInfo); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::setAssetInformation, aasId, aasInfo); } @Override public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException { - lock.readLock().lock(); - try { - return decoratedAasRepository.getAssetInformation(aasId); - } finally { - lock.readLock().unlock(); - } + return access.read(decoratedAasRepository::getAssetInformation, aasId); } @Override public File getThumbnail(String aasId) { - lock.readLock().lock(); - try { - return decoratedAasRepository.getThumbnail(aasId); - } finally { - lock.readLock().unlock(); - } + return access.read(decoratedAasRepository::getThumbnail, aasId); } @Override public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) { - lock.writeLock().lock(); - try { - decoratedAasRepository.setThumbnail(aasId, fileName, contentType, inputStream); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::setThumbnail, aasId, fileName, contentType, inputStream); } @Override public void deleteThumbnail(String aasId) { - lock.writeLock().lock(); - try { - decoratedAasRepository.deleteThumbnail(aasId); - } finally { - lock.writeLock().unlock(); - } + access.write(decoratedAasRepository::deleteThumbnail, aasId); } + + @Override + public String getName() { + return decoratedAasRepository.getName(); + } + } diff --git a/basyx.common/basyx.backend/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/ThreadSafeAccess.java b/basyx.common/basyx.backend/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/ThreadSafeAccess.java index 5383d118f..97764b421 100644 --- a/basyx.common/basyx.backend/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/ThreadSafeAccess.java +++ b/basyx.common/basyx.backend/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/ThreadSafeAccess.java @@ -61,6 +61,10 @@ public void write(TriConsumer consumer, A arg1, B arg2, C arg runWithLock(consumer, arg1, arg2, arg3, writeLock); } + public void write(TetraConsumer consumer, A arg1, B arg2, C arg3, D arg4) { + runWithLock(consumer, arg1, arg2, arg3, arg4, writeLock); + } + public T read(Function func, A arg1) { return runWithLock(func, arg1, readLock); } @@ -123,10 +127,24 @@ private void runWithLock(TriConsumer consumer, A arg1, B arg2 } } + private void runWithLock(TetraConsumer consumer, A arg1, B arg2, C arg3, D arg4, Lock lock) { + try { + lock.lock(); + consumer.accept(arg1, arg2, arg3, arg4); + } finally { + lock.unlock(); + } + } + @FunctionalInterface public static interface TriConsumer { void accept(S s, T t, U u); } + + @FunctionalInterface + public static interface TetraConsumer { + void accept(S s, T t, U u, V v); + } } \ No newline at end of file