use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.
the class CatalogFrameworkImplTest method testMetacardTransformWithTransformException.
@Test(expected = CatalogTransformerException.class)
public void testMetacardTransformWithTransformException() throws Exception {
BundleContext context = mock(BundleContext.class);
MetacardTransformer transformer = mock(MetacardTransformer.class);
ServiceReference reference = mock(ServiceReference.class);
ServiceReference[] serviceReferences = new ServiceReference[] { reference };
when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
when(transformer.transform(isA(Metacard.class), isA(Map.class))).thenThrow(new CatalogTransformerException("Could not transform"));
CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
MetacardImpl newCard = new MetacardImpl();
newCard.setId(null);
framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
}
use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.
the class CatalogFrameworkImplTest method testMetacardTransform.
@Test
public void testMetacardTransform() throws Exception {
BundleContext context = mock(BundleContext.class);
MetacardTransformer transformer = mock(MetacardTransformer.class);
ServiceReference reference = mock(ServiceReference.class);
ServiceReference[] serviceReferences = new ServiceReference[] { reference };
when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
when(transformer.transform(isA(Metacard.class), isA(Map.class))).thenReturn(new BinaryContentImpl(null));
CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
MetacardImpl newCard = new MetacardImpl();
newCard.setId(null);
BinaryContent content = framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
assertNotNull(content);
}
use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.
the class CswTransformProvider method marshal.
/**
* Marshals Metacards to an xml. This method is not typically be called directly, instead it is
* called by another XStream Converter using MarshallingContext.convertAnother();
*
* @param o - metacard to transform.
* @param writer - writes the XML.
* @param context - the marshalling context. Should contain a map entry for {@link
* org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_KEY} {@link
* org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_VALUE} to
* identify which transformer to use. Also contains properties for any arguments to provide
* the transformer.
*/
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
if (o == null) {
return;
}
Metacard metacard = (Metacard) o;
String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
MetacardTransformer transformer;
if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
transformer = metacardTransformerManager.getTransformerByProperty(keyArg, valArg);
} else {
transformer = metacardTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
}
if (transformer == null) {
throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
}
BinaryContent content;
try {
content = transformer.transform(metacard, getArguments(context));
} catch (CatalogTransformerException e) {
throw new ConversionException("Unable to transform Metacard", e);
}
writeXml(content, writer);
}
use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.
the class CswQueryResponseTransformer method multiThreadedMarshal.
/**
* Multi-threaded marshal of metacard assumes that the query size is unbounded to guard against
* resource exhaustion with fixed thread-pool and fixed work-queue. CPU-bound for optimum
* utilization from availableProcessors()+1 thread pool.
*
* @param results - the list of results to marshal
* @param recordSchema - the schema
* @param arguments - additional args
* @return - the marshaled results
* @throws CatalogTransformerException
*/
private String multiThreadedMarshal(List<Result> results, AtomicLong numResults, String recordSchema, final Map<String, Serializable> arguments) throws CatalogTransformerException {
CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor);
final MetacardTransformer transformer = metacardTransformerManager.getTransformerBySchema(recordSchema);
if (transformer == null) {
throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema);
}
Map<Future<BinaryContent>, Result> futures = new HashMap<>(results.size());
for (Result result : results) {
final Metacard mc = result.getMetacard();
// the "current" thread will run submitted task when queueSize exceeded; effectively
// blocking enqueue of more tasks.
futures.put(completionService.submit(() -> {
BinaryContent content = transformer.transform(mc, arguments);
return content;
}), result);
}
InputStream[] contents = new InputStream[results.size()];
while (!futures.isEmpty()) {
try {
Future<BinaryContent> completedFuture = completionService.take();
int index = results.indexOf(futures.get(completedFuture));
try {
contents[index] = completedFuture.get().getInputStream();
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error transforming Metacard", e);
numResults.decrementAndGet();
} catch (InterruptedException e) {
numResults.decrementAndGet();
Thread.currentThread().interrupt();
throw new CatalogTransformerException("Metacard transform interrupted", e);
} finally {
futures.remove(completedFuture);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CatalogTransformerException("Metacard transform interrupted", e);
}
}
CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
for (InputStream is : contents) {
try {
if (is != null) {
IOUtils.copy(is, accum);
}
} catch (IOException e) {
LOGGER.debug("Error copying Metacard Binary content", e);
}
}
return accum.toString();
}
Aggregations