/** * A strategy method to allow implementations to perform some custom JBI * based injection of the POJO * * @param bean the bean to be injected */ protected void injectFields(final Object bean, final String beanName) { ReflectionHelper.doWithFields(bean.getClass(), new ReflectionHelper.FieldCallback() { public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { PropertyInject propertyInject = field.getAnnotation(PropertyInject.class); if (propertyInject != null && getPostProcessorHelper().matchContext(propertyInject.context())) { injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), bean, beanName); } BeanInject beanInject = field.getAnnotation(BeanInject.class); if (beanInject != null && getPostProcessorHelper().matchContext(beanInject.context())) { injectFieldBean(field, beanInject.value(), bean, beanName); } EndpointInject endpointInject = field.getAnnotation(EndpointInject.class); if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) { injectField(field, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), bean, beanName); } Produce produce = field.getAnnotation(Produce.class); if (produce != null && getPostProcessorHelper().matchContext(produce.context())) { injectField(field, produce.uri(), produce.ref(), produce.property(), bean, beanName); } } }); }
public synchronized CacheManager getInstance() { if (cacheManager == null) { cacheManager = createCacheManagerInstance(); // always turn off ET phone-home LOG.debug("Turning off EHCache update checker ..."); Configuration config = cacheManager.getConfiguration(); try { // need to set both the system property and bypass the setUpdateCheck method as that can be changed dynamically System.setProperty("net.sf.ehcache.skipUpdateCheck", "true"); ReflectionHelper.setField(config.getClass().getDeclaredField("updateCheck"), config, false); LOG.info("Turned off EHCache update checker. updateCheck={}", config.getUpdateCheck()); } catch (Throwable e) { // ignore LOG.warn("Error turning off EHCache update checker. Beware information sent over the internet!", e); } } return cacheManager; }
public static <T> void configure(CamelContext context, T target, Logger log) { Class clazz = target.getClass(); for (Field field : clazz.getDeclaredFields()) { try { String propertyValue = context.resolvePropertyPlaceholders("{{" + field.getName() + "}}"); if (!propertyValue.isEmpty()) { // Try to convert the value and set the field Object convertedValue = convertValue(propertyValue, field.getGenericType()); ReflectionHelper.setField(field, target, convertedValue); log.debug("Set field " + field.getName() + " with value " + propertyValue); } } catch (Exception e) { log.debug("Field " + field.getName() + " skipped", e); } } }
protected static void populateParameterConfigurationMap( final SortedMap<String, ParameterConfiguration> parameterMap, Class<?> aClass, final String prefix) { ReflectionHelper.doWithFields(aClass, new ReflectionHelper.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { UriParam uriParam = field.getAnnotation(UriParam.class); if (uriParam != null) { String name = uriParam.name(); if (ObjectHelper.isEmpty(name)) { name = field.getName(); } String propertyName = prefix + name; // is the parameter a nested configuration object Class<?> fieldType = field.getType(); UriParams uriParams = fieldType.getAnnotation(UriParams.class); if (uriParams != null) { String nestedPrefix = uriParams.prefix(); if (nestedPrefix == null) { nestedPrefix = ""; } nestedPrefix = (prefix + nestedPrefix).trim(); populateParameterConfigurationMap(parameterMap, fieldType, nestedPrefix); } else { if (parameterMap.containsKey(propertyName)) { LOG.warn("Duplicate property name " + propertyName + " defined on field " + field); } else { parameterMap.put(propertyName, ParameterConfiguration.newInstance(propertyName, field, uriParam)); } } } } }); }
protected void injectMethods(final Object bean, final String beanName) { ReflectionHelper.doWithMethods(bean.getClass(), new ReflectionHelper.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { setterInjection(method, bean, beanName); getPostProcessorHelper().consumerInjection(method, bean, beanName); } }); }
public void injectField(Field field, String endpointUri, String endpointRef, String endpointProperty, Object bean, String beanName) { ReflectionHelper.setField(field, bean, getPostProcessorHelper().getInjectionValue(field.getType(), endpointUri, endpointRef, endpointProperty, field.getName(), bean, beanName)); }
public void injectFieldBean(Field field, String name, Object bean, String beanName) { ReflectionHelper.setField(field, bean, getPostProcessorHelper().getInjectionBeanValue(field.getType(), name)); }
public void injectFieldProperty(Field field, String propertyName, String propertyDefaultValue, Object bean, String beanName) { ReflectionHelper.setField(field, bean, getPostProcessorHelper().getInjectionPropertyValue(field.getType(), propertyName, propertyDefaultValue, field.getName(), bean, beanName)); }
@Test public void testCheckExpiredDataFromCache() throws Exception { context.addRoutes(new RouteBuilder() { public void configure() { onException(CacheException.class). handled(true). to("log:LOGGER"). to("mock:CacheProducerTest.cacheException"); from("direct:a"). setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)). setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")). setBody(constant("Test body")). to("cache://TestCache1?timeToLiveSeconds=1"); from("direct:b"). setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_URL_CHECK)). setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")). to("cache://TestCache1"). choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()). to("mock:CacheProducerTest.result").end(); } }); // Put an element to the cache resultEndpoint.expectedMessageCount(1); cacheExceptionEndpoint.expectedMessageCount(0); context.start(); log.debug("------------Beginning CacheProducer Check An Element Does Not Exist After Expiry Test---------------"); sendOriginalFile(); // simulate the cache element to appear as "expired" (without having to wait for it to happen) // however as there's no public API to do so we're forced to make use of the reflection API here Cache testCache = cache.getCacheManagerFactory().getInstance().getCache("TestCache1"); Element element = testCache.getQuiet("Ralph_Waldo_Emerson"); ReflectionHelper.setField(Element.class.getDeclaredField("creationTime"), element, System.currentTimeMillis() - 10000); testCache.putQuiet(element); // Check that the element is not found when expired template.sendBody("direct:b", "dummy"); resultEndpoint.assertIsSatisfied(); cacheExceptionEndpoint.assertIsSatisfied(); }
private void injectField(Field field, String uri, String ref, String property, String context, Object bean, String beanName) { ReflectionHelper.setField(field, bean, getPostProcessorHelper(context).getInjectionValue(field.getType(), uri, ref, property, field.getName(), bean, beanName)); }
private void injectFieldProperty(Field field, String property, String defaultValue, String context, Object bean, String beanName) { ReflectionHelper.setField(field, bean, getPostProcessorHelper(context).getInjectionPropertyValue(field.getType(), property, defaultValue, field.getName(), bean, beanName)); }