Skip to content

Commit

Permalink
[GEOS-8739] CatalogRepository cannot find a store by name, if the sto…
Browse files Browse the repository at this point in the history
…re has just been added
  • Loading branch information
aaime committed May 25, 2018
1 parent 85ac010 commit ea6e887
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,7 @@ public void sync( CatalogImpl other ) {
if ( resourcePool != other.resourcePool ) {
resourcePool.dispose();
resourcePool = other.resourcePool;
resourcePool.setCatalog(this);
}

resourceLoader = other.resourceLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
*/
package org.geoserver.catalog.impl;

import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogInfo;
import org.geoserver.ows.util.OwsUtils;
import org.geotools.util.logging.Logging;
import org.opengis.feature.type.Name;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -13,9 +20,8 @@
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import java.util.function.Predicate;

import org.geoserver.catalog.CatalogInfo;
import org.opengis.feature.type.Name;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A support index for {@link DefaultCatalogFacade}, can perform fast lookups of {@link CatalogInfo} objects
Expand All @@ -27,6 +33,8 @@
* @param <T>
*/
class CatalogInfoLookup<T extends CatalogInfo> {
static final Logger LOGGER = Logging.getLogger(CatalogInfoLookup.class);

ConcurrentHashMap<Class<T>, Map<String, T>> idMultiMap = new ConcurrentHashMap<>();
ConcurrentHashMap<Class<T>, Map<Name, T>> nameMultiMap = new ConcurrentHashMap<>();
Function<T, Name> nameMapper;
Expand Down Expand Up @@ -210,4 +218,33 @@ <U extends CatalogInfo> U findFirst(Class<U> clazz, Predicate<U> predicate) {

return null;
}

/**
* Sets the specified catalog into all CatalogInfo objects contained in this lookup
*
* @param catalog
*/
public CatalogInfoLookup setCatalog(Catalog catalog) {
for (Map<Name, T> valueMap : nameMultiMap.values()) {
if (valueMap != null) {
for (T v : valueMap.values()) {
if (v instanceof CatalogInfo) {
Method setter = OwsUtils.setter(v.getClass(), "catalog", Catalog.class);
if (setter != null) {
try {
setter.invoke(v, catalog);
} catch (Exception e) {
LOGGER.log(
Level.FINE,
"Failed to switch CatalogInfo to new catalog impl",
e);
}
}
}
}
}
}

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public void update(ResourceInfo proxiedValue) {
}
}
}


@Override
public LayerInfoLookup setCatalog(Catalog catalog) {
super.setCatalog(catalog);
return this;
}
}

/**
Expand Down Expand Up @@ -937,17 +942,17 @@ public void syncTo(CatalogFacade dao) {
//do an optimized sync
DefaultCatalogFacade other = (DefaultCatalogFacade) dao;

other.stores = stores;
other.stores = stores.setCatalog(catalog);
other.defaultStores = defaultStores;
other.resources = resources;
other.resources = resources.setCatalog(catalog);
other.defaultNamespace = defaultNamespace;
other.namespaces = namespaces;
other.namespaces = namespaces.setCatalog(catalog);
other.defaultWorkspace = defaultWorkspace;
other.workspaces = workspaces;
other.layers = layers;
other.workspaces = workspaces.setCatalog(catalog);
other.layers = layers.setCatalog(catalog);
other.maps = maps;
other.layerGroups = layerGroups;
other.styles = styles;
other.layerGroups = layerGroups.setCatalog(catalog);
other.styles = styles.setCatalog(catalog);
} else {
//do a manual import
for (WorkspaceInfo ws : workspaces.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.geoserver.data.test.MockData;
import org.geoserver.test.GeoServerSystemTestSupport;
import org.geotools.data.DataStore;
import org.geotools.feature.NameImpl;
import org.geotools.util.URLs;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

Expand All @@ -41,9 +45,26 @@ public void testLookupNotQualified() throws IOException {
}

@Test
public void testLookupNotExisting() throws IOException {
public void testLookupNotExisting() {
CatalogRepository repository = getCatalog().getResourcePool().getRepository();
DataStore store = repository.dataStore(new NameImpl("foo", "bar"));
assertNull(store);
}

@Test
public void testLookupFreshlyAdded() {
Catalog catalog = getCatalog();
CatalogBuilder cb = new CatalogBuilder(getCatalog());
String nsURI = catalog.getDefaultNamespace().getURI();
URL buildings = MockData.class.getResource("Buildings.properties");
File testData = URLs.urlToFile(buildings).getParentFile();
DataStoreInfo storeInfo = cb.buildDataStore("freshOffTheBoat");
storeInfo.getConnectionParameters().put("directory", testData);
storeInfo.getConnectionParameters().put("namespace", nsURI);
catalog.save(storeInfo);

CatalogRepository repository = getCatalog().getResourcePool().getRepository();
DataStore store = repository.dataStore(new NameImpl("freshOffTheBoat"));
assertNotNull(store);
}
}

0 comments on commit ea6e887

Please sign in to comment.