public LiveStore(BlobStoreInfo config, @Nullable BlobStore store) { Preconditions.checkArgument(config.isEnabled() == (store != null)); this.config = config; this.liveInstance = store; } }
@Override public boolean layerExists(String layerName) { return readFunction( () -> blobStores .values() .stream() .anyMatch( bs -> bs.config.isEnabled() && bs.liveInstance.layerExists(layerName))); }
private void destroy(Map<String, LiveStore> blobStores) { for (LiveStore bs : blobStores.values()) { try { if (bs.config.isEnabled()) { bs.liveInstance.destroy(); } } catch (Exception e) { log.error("Error disposing BlobStore " + bs.config.getName(), e); } } blobStores.clear(); }
@Override public boolean rename(String oldLayerName, String newLayerName) throws StorageException { return readFunctionUnsafe( () -> { for (LiveStore bs : blobStores.values()) { BlobStoreInfo config = bs.config; if (config.isEnabled()) { if (bs.liveInstance.rename(oldLayerName, newLayerName)) { return true; } } } return false; }); }
/** Removes the listener from all the enabled blob stores */ @Override public boolean removeListener(BlobStoreListener listener) { return readFunction( () -> { this.listeners.removeListener(listener); return blobStores .values() .stream() .filter(bs -> bs.config.isEnabled()) .map(bs -> bs.liveInstance.removeListener(listener)) .collect(Collectors.reducing((x, y) -> x || y)) // Don't use anyMatch or // findFirst as we don't want it // to shortcut .orElse(false); }); }
/** Adds the listener to all enabled blob stores */ @Override public void addListener(BlobStoreListener listener) { readAction( () -> { this.listeners.addListener( listener); // save it for later in case setBlobStores is // called for (LiveStore bs : blobStores.values()) { if (bs.config.isEnabled()) { bs.liveInstance.addListener(listener); } } }); }
private BlobStore store(String layerId) throws StorageException { LiveStore store; try { store = forLayer(layerId); } catch (GeoWebCacheException e) { throw new StorageException(e.getMessage(), e); } if (!store.config.isEnabled()) { throw new StorageException( "Attempted to use a blob store that's disabled: " + store.config.getName()); } return store.liveInstance; }
@Test public void testSuitabilityOnAdd() throws Exception { final BlobStoreInfo info = mock(BlobStoreInfo.class); when(info.getName()).thenReturn("testStore"); when(info.isEnabled()).thenReturn(true); BlobStore subStore = mock(BlobStore.class); when(info.createInstance(Mockito.any(), Mockito.any())) .thenAnswer( invocation -> { assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EXISTING)); return subStore; }); store = create(); store.handleAddBlobStore(info); verify(info).createInstance(Mockito.any(), Mockito.any()); }
@Test public void testNonDefaultSuitabilityOnAdd() throws Exception { suitability.setValue(CompositeBlobStore.StoreSuitabilityCheck.EMPTY); final BlobStoreInfo info = mock(BlobStoreInfo.class); when(info.getName()).thenReturn("testStore"); when(info.isEnabled()).thenReturn(true); BlobStore subStore = mock(BlobStore.class); when(info.createInstance(Mockito.any(), Mockito.any())) .thenAnswer( invocation -> { assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EMPTY)); return subStore; }); store = create(); store.handleAddBlobStore(info); verify(info).createInstance(Mockito.any(), Mockito.any()); }
@Test public void testSuitabilityOnStartup() throws Exception { // Default to EXISTING assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EXISTING)); final BlobStoreInfo info = mock(BlobStoreInfo.class); when(info.getName()).thenReturn("testStore"); when(info.isEnabled()).thenReturn(true); BlobStore subStore = mock(BlobStore.class); when(info.createInstance(Mockito.any(), Mockito.any())) .thenAnswer( invocation -> { // When this gets called we should be skipping suitability checks assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.NONE)); return subStore; }); configs.add(info); store = create(); verify(info).createInstance(Mockito.any(), Mockito.any()); // Should be back to default after startup. assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EXISTING)); }
final boolean enabled = config.isEnabled(); LiveStore defaultStore = stores.getOrDefault(CompositeBlobStore.DEFAULT_STORE_DEFAULT_ID, null);
@Test public void testNonDefaultSuitabilityOnStartup() throws Exception { suitability.setValue(CompositeBlobStore.StoreSuitabilityCheck.EMPTY); assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EMPTY)); final BlobStoreInfo info = mock(BlobStoreInfo.class); when(info.getName()).thenReturn("testStore"); when(info.isEnabled()).thenReturn(true); BlobStore subStore = mock(BlobStore.class); when(info.createInstance(Mockito.any(), Mockito.any())) .thenAnswer( invocation -> { // When this gets called we should be skipping suitability checks assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.NONE)); return subStore; }); configs.add(info); store = create(); verify(info).createInstance(Mockito.any(), Mockito.any()); // Should be back to default after startup. assertThat( CompositeBlobStore.getStoreSuitabilityCheck(), equalTo(CompositeBlobStore.StoreSuitabilityCheck.EMPTY)); }
@Test public void testBlobStoreConfigIsLoaded() throws Exception { // get the blobstores from the config (from test resource geowebcache_190.xml) final List<BlobStoreInfo> blobStores = config.getBlobStores(); assertEquals( "Unexpected number of BlobStoreInfo elements configured", 1, blobStores.size()); // get the 1 configured BlobStoreInfo BlobStoreInfo info = blobStores.get(0); assertFalse("Unexpected BlobStoreInfo default setting", info.isDefault()); assertFalse("Unexpected BlobStoreInfo enabled setting", info.isEnabled()); assertTrue( "Unexpected BlobeStoreInfo class type", FileBlobStoreInfo.class.isAssignableFrom(info.getClass())); // cast the info to a FileBlobStoreInfo final FileBlobStoreInfo fileInfo = FileBlobStoreInfo.class.cast(info); assertEquals( "Unexpected FileBlobStoreInfo filesystem block size", 4096, fileInfo.getFileSystemBlockSize()); assertEquals( "Unexpected FileBlobStoreInfo location value", "/tmp/defaultCache", fileInfo.getBaseDirectory()); }