use of ddf.catalog.source.SourceDescriptor in project ddf by codice.
the class SourceOperations method getFanoutSourceInfo.
/**
* Retrieves the {@link SourceDescriptor} info for all {@link FederatedSource}s in the fanout
* configuration, but the all of the source info, e.g., content types, for all of the available
* {@link FederatedSource}s is packed into one {@link SourceDescriptor} for the
* fanout configuration with the fanout's site name in it. This keeps the individual
* {@link FederatedSource}s' source info hidden from the external client.
*/
private SourceInfoResponse getFanoutSourceInfo(SourceInfoRequest sourceInfoRequest) throws SourceUnavailableException {
SourceInfoResponse response;
SourceDescriptorImpl sourceDescriptor;
try {
// request
if (sourceInfoRequest == null) {
throw new IllegalArgumentException("SourceInfoRequest was null");
}
Set<SourceDescriptor> sourceDescriptors = new LinkedHashSet<>();
Set<String> ids = sourceInfoRequest.getSourceIds();
// specified
if (ids != null) {
Optional<String> notLocal = ids.stream().filter(s -> !s.equals(getId())).findFirst();
if (notLocal.isPresent()) {
SourceUnavailableException sourceUnavailableException = new SourceUnavailableException("Unknown source: " + notLocal.get());
LOGGER.debug("Throwing SourceUnavailableException for unknown source: {}", notLocal.get(), sourceUnavailableException);
throw sourceUnavailableException;
}
}
// Fanout will only add one source descriptor with all the contents
Set<Source> availableSources = frameworkProperties.getFederatedSources().values().stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source != null && source.isAvailable()).collect(Collectors.toSet());
Set<ContentType> contentTypes = availableSources.stream().map(source -> frameworkProperties.getSourcePoller().getCachedSource(source)).filter(source -> source.getContentTypes() != null).map(Source::getContentTypes).flatMap(Collection::stream).collect(Collectors.toSet());
if (catalog != null) {
Source localSource = frameworkProperties.getSourcePoller().getCachedSource(catalog);
if (localSource != null && localSource.isAvailable()) {
availableSources.add(localSource);
contentTypes.addAll(localSource.getContentTypes());
}
}
// only reveal this sourceDescriptor, not the federated sources
sourceDescriptor = new SourceDescriptorImpl(this.getId(), contentTypes);
if (this.getVersion() != null) {
sourceDescriptor.setVersion(this.getVersion());
}
sourceDescriptor.setAvailable(availableSources.size() > 0);
sourceDescriptors.add(sourceDescriptor);
response = new SourceInfoResponseImpl(sourceInfoRequest, null, sourceDescriptors);
} catch (RuntimeException re) {
throw new SourceUnavailableException("Exception during runtime while performing getSourceInfo", re);
}
return response;
}
use of ddf.catalog.source.SourceDescriptor in project ddf by codice.
the class SourceOperations method getFederatedSourceDescriptors.
/**
* Creates a {@link Set} of {@link SourceDescriptor} based on the incoming list of
* {@link Source}.
*
* @param sources {@link Collection} of {@link Source} to obtain descriptor information from
* @return new {@link Set} of {@link SourceDescriptor}
*/
private Set<SourceDescriptor> getFederatedSourceDescriptors(Collection<FederatedSource> sources, boolean addCatalogProviderDescriptor) {
SourceDescriptorImpl sourceDescriptor;
Set<SourceDescriptor> sourceDescriptors = new TreeSet<>(new SourceDescriptorComparator());
if (sources != null) {
for (Source source : sources) {
if (source != null) {
String sourceId = source.getId();
LOGGER.debug("adding sourceId: {}", sourceId);
Source cachedSource = null;
// check the poller for cached information
if (frameworkProperties.getSourcePoller() != null && frameworkProperties.getSourcePoller().getCachedSource(source) != null) {
cachedSource = frameworkProperties.getSourcePoller().getCachedSource(source);
}
sourceDescriptor = new SourceDescriptorImpl(sourceId, source.getContentTypes());
sourceDescriptor.setVersion(source.getVersion());
sourceDescriptor.setAvailable((cachedSource != null) && cachedSource.isAvailable());
sourceDescriptors.add(sourceDescriptor);
}
}
}
if (addCatalogProviderDescriptor) {
addCatalogSourceDescriptor(sourceDescriptors);
}
return sourceDescriptors;
}
use of ddf.catalog.source.SourceDescriptor in project ddf by codice.
the class SourceConfigurationAdminPlugin method getConfigurationData.
/**
* Returns a map of configuration data that should be appended to the configurationDataMap
* parameter. The configurationDataMap that is passed into this function is unmodifiable and is
* passed in to simply expose what information already exists.
*
* @param configurationPid service.pid for the ConfigurationAdmin configuration
* @param configurationDataMap map of what properties have already been added to the configuration in question
* @param bundleContext used to retrieve list of services
* @return Map defining additional properties to add to the configuration
*/
@Override
public Map<String, Object> getConfigurationData(String configurationPid, Map<String, Object> configurationDataMap, BundleContext bundleContext) {
LOGGER.debug("Obtaining configuration data for the following configuration PID: {}", configurationPid);
Map<String, Object> statusMap = new HashMap<String, Object>();
try {
List<ServiceReference<? extends Source>> refs = new ArrayList<ServiceReference<? extends Source>>();
refs.addAll(bundleContext.getServiceReferences(FederatedSource.class, null));
refs.addAll(bundleContext.getServiceReferences(CatalogProvider.class, null));
Set<SourceDescriptor> sources = null;
if (catalogFramework != null) {
sources = catalogFramework.getSourceInfo(new SourceInfoRequestEnterprise(true)).getSourceInfo();
}
boolean foundSources = CollectionUtils.isNotEmpty(sources);
for (ServiceReference<? extends Source> ref : refs) {
Source superService = bundleContext.getService(ref);
if (superService instanceof ConfiguredService) {
ConfiguredService cs = (ConfiguredService) superService;
LOGGER.debug("ConfiguredService configuration PID: {}", cs.getConfigurationPid());
boolean csConfigPidMatchesTargetPid = false;
if (StringUtils.isNotEmpty(cs.getConfigurationPid()) && cs.getConfigurationPid().equals(configurationPid)) {
csConfigPidMatchesTargetPid = true;
}
if (foundSources) {
// class name, then we can match them up this way.
if (csConfigPidMatchesTargetPid || cs.getClass().getCanonicalName().equals(configurationPid)) {
for (SourceDescriptor descriptor : sources) {
if (descriptor.getSourceId().equals(superService.getId())) {
statusMap.put("available", descriptor.isAvailable());
statusMap.put("sourceId", descriptor.getSourceId());
return statusMap;
}
}
}
} else if (csConfigPidMatchesTargetPid) {
// we don't want to call isAvailable because that can
// potentially block execution but if for some reason we
// have no catalog framework, just hit the source
// directly
statusMap.put("available", superService.isAvailable());
return statusMap;
}
}
}
} catch (org.osgi.framework.InvalidSyntaxException ise) {
// this should never happen because the filter is always null
LOGGER.debug("Error reading LDAP service filter", ise);
} catch (SourceUnavailableException sue) {
LOGGER.info("Unable to retrieve sources from Catalog Framework", sue);
}
return statusMap;
}
use of ddf.catalog.source.SourceDescriptor in project ddf by codice.
the class CatalogFrameworkImplTest method testGetFederatedSourcesDuplicates.
@Test
public void testGetFederatedSourcesDuplicates() {
List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true);
// Duplicate Site
FederatedSource siteC2 = new MockSource("C", "Site C2", "v1.0", "DDF", null, true, new Date());
federatedSources.add(siteC2);
// Expected Sites
List<FederatedSource> expectedSources = createDefaultFederatedSourceList(true);
// Mock register the federated sources in the container
// Mock the source poller
SourcePoller mockPoller = mock(SourcePoller.class);
when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setSourcePoller(mockPoller);
Map<String, FederatedSource> sources = new HashMap<>();
for (FederatedSource federatedSource : expectedSources) {
sources.put(federatedSource.getId(), federatedSource);
}
frameworkProperties.setFederatedSources(sources);
CatalogFrameworkImpl framework = createFramework(frameworkProperties);
// Returned Sites
SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
SourceInfoResponse response = null;
try {
response = framework.getSourceInfo(request);
} catch (SourceUnavailableException e) {
LOGGER.debug("SourceUnavilable", e);
fail();
}
Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
// should contain ONLY the original federated sites and the catalog framework's
// site info (even though it has no local catalog provider configured) - hence,
// the "+1"
assertEquals(expectedSources.size(), sourceDescriptors.size());
}
use of ddf.catalog.source.SourceDescriptor in project ddf by codice.
the class CatalogFrameworkImplTest method testGetUnavailableFederatedSources.
@Test
public void testGetUnavailableFederatedSources() {
List<FederatedSource> federatedSources = createDefaultFederatedSourceList(false);
CatalogProvider catalogProvider = mock(CatalogProvider.class);
// Mock register the federated sources in the container
SourcePollerRunner runner = new SourcePollerRunner();
SourcePoller poller = new SourcePoller(runner);
for (FederatedSource source : federatedSources) {
runner.bind(source);
}
runner.bind(catalogProvider);
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setSourcePoller(poller);
Map<String, FederatedSource> sources = new HashMap<>();
for (FederatedSource federatedSource : federatedSources) {
sources.put(federatedSource.getId(), federatedSource);
}
frameworkProperties.setFederatedSources(sources);
frameworkProperties.setCatalogProviders(Collections.singletonList(catalogProvider));
CatalogFrameworkImpl framework = createFramework(frameworkProperties);
SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
SourceInfoResponse response = null;
try {
response = framework.getSourceInfo(request);
} catch (SourceUnavailableException e) {
fail();
}
Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
for (SourceDescriptor descriptor : sourceDescriptors) {
LOGGER.debug("Descriptor id: {}", descriptor.getSourceId());
if (StringUtils.isNotBlank(descriptor.getId())) {
assertFalse(descriptor.isAvailable());
// No contentTypes should be listed if the source is unavailable
assertTrue(descriptor.getContentTypes().isEmpty());
}
}
// The "+1" is to account for the CatalogFramework source descriptor.
// Even if no local catalog provider is configured, the catalog
// framework's
// site info is included in the SourceDescriptos list.
assertEquals(federatedSources.size() + 1, sourceDescriptors.size());
}
Aggregations