use of org.apache.camel.spi.TypeConverterRegistry in project camel by apache.
the class DozerTypeConverterLoader method addMapping.
/**
* Registers Dozer <code>BeanMappingBuilder</code> in current mapper instance.
* This method should be called instead of direct <code>mapper.addMapping()</code> invocation for Camel
* being able to register given type conversion.
*
* @param beanMappingBuilder api-based mapping builder
*/
public void addMapping(BeanMappingBuilder beanMappingBuilder) {
if (mapper == null) {
log.warn("No mapper instance provided to " + this.getClass().getSimpleName() + ". Mapping has not been registered!");
return;
}
mapper.addMapping(beanMappingBuilder);
MappingFileData mappingFileData = beanMappingBuilder.build();
TypeConverterRegistry registry = camelContext.getTypeConverterRegistry();
List<ClassMap> classMaps = new ArrayList<ClassMap>();
classMaps.addAll(mappingFileData.getClassMaps());
registerClassMaps(registry, null, mapper, classMaps);
}
use of org.apache.camel.spi.TypeConverterRegistry in project camel by apache.
the class TypeConverterRegistryStatisticsEnabledTest method testTypeConverterRegistry.
@Test
public void testTypeConverterRegistry() throws Exception {
getMockEndpoint("mock:a").expectedMessageCount(2);
template.sendBody("direct:start", "3");
template.sendBody("direct:start", "7");
assertMockEndpointsSatisfied();
TypeConverterRegistry reg = context.getTypeConverterRegistry();
assertTrue("Should be enabled", reg.getStatistics().isStatisticsEnabled());
Long failed = reg.getStatistics().getFailedCounter();
assertEquals(0, failed.intValue());
Long miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
try {
template.sendBody("direct:start", "foo");
fail("Should have thrown exception");
} catch (Exception e) {
// expected
}
// should now have a failed
failed = reg.getStatistics().getFailedCounter();
assertEquals(1, failed.intValue());
miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
// reset
reg.getStatistics().reset();
failed = reg.getStatistics().getFailedCounter();
assertEquals(0, failed.intValue());
miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
}
use of org.apache.camel.spi.TypeConverterRegistry in project camel by apache.
the class DozerTypeConverterLoader method init.
/**
* Doses the actual querying and registration of {@link DozerTypeConverter}s
* with the {@link CamelContext}.
*
* @param camelContext the context to register the
* {@link DozerTypeConverter} in
* @param mapper the DozerMapperBean to be wrapped as a type converter.
*/
public void init(CamelContext camelContext, DozerBeanMapper mapper) {
this.camelContext = camelContext;
if (mapper != null) {
this.mapper = mapper;
}
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
ClassLoader appcl = camelContext.getApplicationContextClassLoader();
if (appcl != null) {
Thread.currentThread().setContextClassLoader(appcl);
}
Map<String, DozerBeanMapper> mappers = lookupDozerBeanMappers();
// only add if we do not already have it
if (mapper != null && !mappers.containsValue(mapper)) {
mappers.put("parameter", mapper);
}
// add any dozer bean mapper configurations
Map<String, DozerBeanMapperConfiguration> configurations = lookupDozerBeanMapperConfigurations();
if (configurations != null && configuration != null) {
// filter out existing configuration, as we do not want to use it twice
String key = null;
for (Map.Entry<String, DozerBeanMapperConfiguration> entry : configurations.entrySet()) {
if (entry.getValue() == configuration) {
key = entry.getKey();
break;
}
}
if (key != null) {
configurations.remove(key);
}
}
if (configurations != null) {
if (configurations.size() > 1) {
log.warn("Loaded " + configurations.size() + " Dozer configurations from Camel registry." + " Dozer is most efficient when there is a single mapper instance. Consider amalgamating instances.");
}
for (Map.Entry<String, DozerBeanMapperConfiguration> entry : configurations.entrySet()) {
String id = entry.getKey();
DozerBeanMapper beanMapper = createDozerBeanMapper(entry.getValue());
// only add if we do not already have it
if (!mappers.containsValue(beanMapper)) {
mappers.put(id, beanMapper);
}
}
}
if (mappers.size() > 1) {
log.warn("Loaded " + mappers.size() + " Dozer mappers from Camel registry." + " Dozer is most efficient when there is a single mapper instance. Consider amalgamating instances.");
} else if (mappers.size() == 0) {
log.warn("No Dozer mappers found in Camel registry. You should add Dozer mappers as beans to the registry of the type: " + DozerBeanMapper.class.getName());
}
TypeConverterRegistry registry = camelContext.getTypeConverterRegistry();
for (Map.Entry<String, DozerBeanMapper> entry : mappers.entrySet()) {
String mapperId = entry.getKey();
DozerBeanMapper dozer = entry.getValue();
List<ClassMap> all = loadMappings(camelContext, mapperId, dozer);
registerClassMaps(registry, mapperId, dozer, all);
}
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
}
use of org.apache.camel.spi.TypeConverterRegistry in project camel by apache.
the class DefaultManagementLifecycleStrategy method getManagedObjectForService.
@SuppressWarnings("unchecked")
private Object getManagedObjectForService(CamelContext context, Service service, Route route) {
// skip channel, UoW and dont double wrap instrumentation
if (service instanceof Channel || service instanceof UnitOfWork || service instanceof InstrumentationProcessor) {
return null;
}
// skip non managed services
if (service instanceof NonManagedService) {
return null;
}
Object answer = null;
if (service instanceof ManagementAware) {
return ((ManagementAware<Service>) service).getManagedObject(service);
} else if (service instanceof Tracer) {
// special for tracer
Tracer tracer = (Tracer) service;
ManagedTracer mt = managedTracers.get(tracer);
if (mt == null) {
mt = new ManagedTracer(context, tracer);
mt.init(getManagementStrategy());
managedTracers.put(tracer, mt);
}
return mt;
} else if (service instanceof BacklogTracer) {
// special for backlog tracer
BacklogTracer backlogTracer = (BacklogTracer) service;
ManagedBacklogTracer mt = managedBacklogTracers.get(backlogTracer);
if (mt == null) {
mt = new ManagedBacklogTracer(context, backlogTracer);
mt.init(getManagementStrategy());
managedBacklogTracers.put(backlogTracer, mt);
}
return mt;
} else if (service instanceof BacklogDebugger) {
// special for backlog debugger
BacklogDebugger backlogDebugger = (BacklogDebugger) service;
ManagedBacklogDebugger md = managedBacklogDebuggers.get(backlogDebugger);
if (md == null) {
md = new ManagedBacklogDebugger(context, backlogDebugger);
md.init(getManagementStrategy());
managedBacklogDebuggers.put(backlogDebugger, md);
}
return md;
} else if (service instanceof DataFormat) {
answer = getManagementObjectStrategy().getManagedObjectForDataFormat(context, (DataFormat) service);
} else if (service instanceof Producer) {
answer = getManagementObjectStrategy().getManagedObjectForProducer(context, (Producer) service);
} else if (service instanceof Consumer) {
answer = getManagementObjectStrategy().getManagedObjectForConsumer(context, (Consumer) service);
} else if (service instanceof Processor) {
// special for processors as we need to do some extra work
return getManagedObjectForProcessor(context, (Processor) service, route);
} else if (service instanceof ThrottlingInflightRoutePolicy) {
answer = new ManagedThrottlingInflightRoutePolicy(context, (ThrottlingInflightRoutePolicy) service);
} else if (service instanceof ThrottlingExceptionRoutePolicy) {
answer = new ManagedThrottlingExceptionRoutePolicy(context, (ThrottlingExceptionRoutePolicy) service);
} else if (service instanceof ConsumerCache) {
answer = new ManagedConsumerCache(context, (ConsumerCache) service);
} else if (service instanceof ProducerCache) {
answer = new ManagedProducerCache(context, (ProducerCache) service);
} else if (service instanceof DefaultEndpointRegistry) {
answer = new ManagedEndpointRegistry(context, (DefaultEndpointRegistry) service);
} else if (service instanceof TypeConverterRegistry) {
answer = new ManagedTypeConverterRegistry(context, (TypeConverterRegistry) service);
} else if (service instanceof RestRegistry) {
answer = new ManagedRestRegistry(context, (RestRegistry) service);
} else if (service instanceof InflightRepository) {
answer = new ManagedInflightRepository(context, (InflightRepository) service);
} else if (service instanceof AsyncProcessorAwaitManager) {
answer = new ManagedAsyncProcessorAwaitManager(context, (AsyncProcessorAwaitManager) service);
} else if (service instanceof RuntimeEndpointRegistry) {
answer = new ManagedRuntimeEndpointRegistry(context, (RuntimeEndpointRegistry) service);
} else if (service instanceof StreamCachingStrategy) {
answer = new ManagedStreamCachingStrategy(context, (StreamCachingStrategy) service);
} else if (service instanceof EventNotifier) {
answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, (EventNotifier) service);
} else if (service instanceof TransformerRegistry) {
answer = new ManagedTransformerRegistry(context, (TransformerRegistry) service);
} else if (service instanceof ValidatorRegistry) {
answer = new ManagedValidatorRegistry(context, (ValidatorRegistry) service);
} else if (service instanceof RuntimeCamelCatalog) {
answer = new ManagedRuntimeCamelCatalog(context, (RuntimeCamelCatalog) service);
} else if (service != null) {
// fallback as generic service
answer = getManagementObjectStrategy().getManagedObjectForService(context, service);
}
if (answer != null && answer instanceof ManagedService) {
ManagedService ms = (ManagedService) answer;
ms.setRoute(route);
ms.init(getManagementStrategy());
}
return answer;
}
use of org.apache.camel.spi.TypeConverterRegistry in project camel by apache.
the class TypeConverterRegistryStatisticsEnabledTest method testTypeConverterRegistry.
public void testTypeConverterRegistry() throws Exception {
getMockEndpoint("mock:a").expectedMessageCount(2);
template.sendBody("direct:start", "3");
template.sendBody("direct:start", "7");
assertMockEndpointsSatisfied();
TypeConverterRegistry reg = context.getTypeConverterRegistry();
assertTrue("Should be enabled", reg.getStatistics().isStatisticsEnabled());
Long failed = reg.getStatistics().getFailedCounter();
assertEquals(0, failed.intValue());
Long miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
try {
template.sendBody("direct:start", "foo");
fail("Should have thrown exception");
} catch (Exception e) {
// expected
}
// should now have a failed
failed = reg.getStatistics().getFailedCounter();
assertEquals(1, failed.intValue());
miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
// reset
reg.getStatistics().reset();
failed = reg.getStatistics().getFailedCounter();
assertEquals(0, failed.intValue());
miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
}
Aggregations