@Uri("") @Produces @Typed(MockEndpoint.class) // Qualifiers are dynamically added in CdiCamelExtension private static MockEndpoint mockEndpointFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { Uri uri = getQualifierByType(ip, Uri.class).get(); try { CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance); return context.getEndpoint(uri.value(), MockEndpoint.class); } catch (Exception cause) { throw new InjectionException("Error injecting mock endpoint annotated with " + uri + " into " + ip, cause); } }
@Produces @Dependent @Property public Boolean produceBooleanProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); if (value != null) { return Boolean.valueOf(value); } final Type type = injectionPoint.getType(); return type.equals(boolean.class) ? Boolean.FALSE : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public Integer produceIntegerProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); if (value != null) { return Integer.valueOf(value); } final Type type = injectionPoint.getType(); return type.equals(int.class) ? Integer.valueOf(0) : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public Long produceLongProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); if (value != null) { return Long.valueOf(value); } final Type type = injectionPoint.getType(); return type.equals(long.class) ? Long.valueOf(0L) : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public Float produceFloatProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); if (value != null) { return Float.valueOf(value); } final Type type = injectionPoint.getType(); return type.equals(float.class) ? Float.valueOf(0f) : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public Double produceDoubleProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); if (value != null) { return Double.valueOf(value); } final Type type = injectionPoint.getType(); return type.equals(double.class) ? Double.valueOf(0d) : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public BigInteger produceBigIntegerProperty(InjectionPoint injectionPoint) { try { final BigDecimal value = produceBigDecimalProperty(injectionPoint); if (value != null) { return value.toBigInteger(); } } catch (Exception e) { throw new InjectionException(e); } return null; }
@Produces @Dependent @Property public Date produceDateProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); final Date date; if (value != null) { final Property annotation = injectionPoint.getAnnotated().getAnnotation(Property.class); final String pattern = annotation.pattern(); DateFormat format = new SimpleDateFormat(pattern.isEmpty() ? "yyyy-MM-dd'T'HH:mm:ss.SSSZ" : pattern); date = format.parse(value); } else { date = null; } return date; } catch (Exception e) { throw new InjectionException(e); } }
@Override public T produce(CreationalContext<T> ctx) { T context = super.produce(ctx); // Register the context in the OSGi registry BundleContext bundle = BundleContextUtils.getBundleContext(getClass()); context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundle)); if (!(context instanceof DefaultCamelContext)) { // Fail fast for the time being to avoid side effects by some methods get declared on the CamelContext interface throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); } DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(OsgiCamelContextHelper.wrapRegistry(context, context.getRegistry(), bundle)); CamelContextNameStrategy strategy = context.getNameStrategy(); OsgiCamelContextHelper.osgiUpdate(adapted, bundle); // FIXME: the above call should not override explicit strategies provided by the end user or should decorate them instead of overriding them completely if (!(strategy instanceof DefaultCamelContextNameStrategy)) { context.setNameStrategy(strategy); } return context; }
private boolean addRouteToContext(Bean<?> routeBean, Bean<?> contextBean, BeanManager manager, AfterDeploymentValidation adv) { try { CamelContext context = getReference(manager, CamelContext.class, contextBean); try { Object route = getReference(manager, Object.class, routeBean); if (route instanceof RoutesBuilder) { context.addRoutes((RoutesBuilder) route); } else if (route instanceof RouteContainer) { context.addRouteDefinitions(((RouteContainer) route).getRoutes()); } else { throw new IllegalArgumentException( "Invalid routes type [" + routeBean.getBeanClass().getName() + "], " + "must be either of type RoutesBuilder or RouteContainer!"); } return true; } catch (Exception cause) { adv.addDeploymentProblem( new InjectionException( "Error adding routes of type [" + routeBean.getBeanClass().getName() + "] " + "to Camel context [" + context.getName() + "]", cause)); } } catch (Exception exception) { adv.addDeploymentProblem(exception); } return false; }
private void setFieldValueOrAddDefinitionError(T instance, Field field, Object value, ValueConverter acceptingConverter) { if (acceptingConverter == null) { pit.addDefinitionError(new InjectionException(String.format(MESSAGE_NO_CONVERTER_FOUND, field.getName(), field.getType(), pit.getAnnotatedType().getJavaClass().getName()))); } else if (value == null) { pit.addDefinitionError(new InjectionException(String.format(MESSAGE_NO_VALUE_FOUND, field.getName(), field.getType(), pit.getAnnotatedType().getJavaClass().getName()))); } else { try { Object convert = acceptingConverter.convert(value); boolean accessible = field.isAccessible(); field.setAccessible(true); field.set(instance, convert); field.setAccessible(accessible); } catch (IllegalAccessException e) { pit.addDefinitionError(e); } } }
@Override public void inject(X instance, CreationalContext<X> ctx) { wrapped.inject(instance, ctx); final Class<? extends Creature> klass = instance.getClass(); for (Field field : klass.getDeclaredFields()) { field.setAccessible(true); final String fieldValueFromXml = xmlBacking.getAttribute(field.getName()); try { if (field.getType().isAssignableFrom(Integer.TYPE)) { field.set(instance, Integer.parseInt(fieldValueFromXml)); } else if (field.getType().isAssignableFrom(String.class)) { field.set(instance, fieldValueFromXml); } else { // TODO: left up for the reader throw new InjectionException("Cannot convert to type " + field.getType()); } } catch (IllegalAccessException e) { throw new InjectionException("Cannot access field " + field); } } }
private Neo4jUplink resolve(String connection) { if (uplinks.isUnsatisfied()) { throw new InjectionException( "no neo4j uplinks found. please provide implementation for connection: " + connection); } else if (uplinks.isAmbiguous()) { if ("".equals(connection) || "default" .equals(connection)) { return uplinks.select(new DefaultLiteral()).get(); } else { for (Neo4jUplink uplink : uplinks) { System.out.println("found: " + uplink.getClass().getName()); } return uplinks.select(new NamedLiteral(connection)).get(); } } else if ("".equals(connection) || "default".equals(connection)) { return uplinks.get(); } else { throw new InjectionException( "cannot find a neo4j-uplink for connection: " + connection); } }
@Produces @Dependent @PropertyResource public Properties produceProperties(InjectionPoint point) { final Annotated annotated = point.getAnnotated(); if (point.getType() != Properties.class) { throw new InjectionException(Properties.class + " can not be injected to type " + point.getType()); } final Class<?> beanType = point.getBean().getBeanClass(); final ClassLoader loader = beanType.getClassLoader(); final PropertyResource annotation = annotated.getAnnotation(PropertyResource.class); final PropertyResourceFormat format = annotation.format(); String locator = annotation.url(); if (locator.isEmpty()) { locator = beanType.getName().replace('.', '/') + ".properties"; } try { return factory.getProperties(loader, locator, format); } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public String produceProperty(InjectionPoint injectionPoint) { try { return getProperty(injectionPoint); } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public BigDecimal produceBigDecimalProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); final BigDecimal number; if (value != null) { final Property annotation = injectionPoint.getAnnotated().getAnnotation(Property.class); final String pattern = annotation.pattern(); if (pattern.isEmpty()) { number = new BigDecimal(value); } else { if (logger.isLoggable(Level.FINER)) { logger.log(Level.FINER, "Parsing number with using pattern [" + pattern + ']'); } DecimalFormat format = new DecimalFormat(pattern); format.setParseBigDecimal(true); number = (BigDecimal) format.parse(value); } } else { number = null; } return number; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public JsonArray produceJsonArrayProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); return value != null ? Json.createReader(new StringReader(value)).readArray() : null; } catch (Exception e) { throw new InjectionException(e); } }
@Produces @Dependent @Property public JsonObject produceJsonObjectProperty(InjectionPoint injectionPoint) { try { final String value = getProperty(injectionPoint); return value != null ? Json.createReader(new StringReader(value)).readObject() : null; } catch (Exception e) { throw new InjectionException(e); } }
@Test(expected = InjectionException.class) public void testProducePropertyStringInvalid() { Property property = this.mockProperty("testProducePropertyStringInvalid", "io/xlate/inject/Invalid.properties", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProducePropertyStringInvalid", -1); bean.produceProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyBooleanInvalid() { Property property = this.mockProperty("testProducePropertyBooleanInvalid", "io/xlate/inject/Invalid.properties", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProducePropertyBooleanInvalid", -1); bean.produceBooleanProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyIntegerInvalid() { Property property = this.mockProperty("testProducePropertyIntegerInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProducePropertyIntegerInvalid", -1); bean.produceIntegerProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyLongInvalid() { Property property = this.mockProperty("testProducePropertyLongInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Long.class, Member.class, "testProducePropertyLongInvalid", -1); bean.produceLongProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyFloatInvalid() { Property property = this.mockProperty("testProducePropertyFloatInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProducePropertyFloatInvalid", -1); bean.produceFloatProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyDoubleInvalid() { Property property = this.mockProperty("testProducePropertyDoubleInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProducePropertyDoubleInvalid", -1); bean.produceDoubleProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyBigDecimalInvalid() { Property property = this.mockProperty("testProducePropertyBigDecimalInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this .mockInjectionPoint(property, Member.class, "testProducePropertyBigDecimalInvalid", -1); bean.produceBigDecimalProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyBigIntegerInvalid() { Property property = this.mockProperty("testProducePropertyBigIntegerInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this .mockInjectionPoint(property, Member.class, "testProducePropertyBigIntegerInvalid", -1); bean.produceBigIntegerProperty(point); }
@Test(expected = InjectionException.class) public void testProduceDatePropertyInvalid() { Property property = this.mockProperty("testProduceDatePropertyInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this.mockInjectionPoint(property, Member.class, "testProduceDatePropertyInvalid", -1); bean.produceDateProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyJsonArrayInvalid() { Property property = this.mockProperty("testProducePropertyJsonArrayInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this .mockInjectionPoint(property, Member.class, "testProducePropertyJsonArrayInvalid", -1); bean.produceJsonArrayProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertyJsonObjectInvalid() { Property property = this.mockProperty("testProducePropertyJsonObjectInvalid", "", PropertyResourceFormat.PROPERTIES, "", Property.DEFAULT_NULL); InjectionPoint point = this .mockInjectionPoint(property, Member.class, "testProducePropertyJsonObjectInvalid", -1); bean.produceJsonObjectProperty(point); }
@Test(expected = InjectionException.class) public void testProducePropertiesInvalidUrl() { PropertyResource annotation = annotation("nowhere://io/xlate/inject/test/does-not-exist!.properties", PropertyResourceFormat.PROPERTIES, false); InjectionPoint point = injectionPoint(annotation, Properties.class, Member.class, "", -1); bean.produceProperties(point); }
@Override public T produce(CreationalContext<T> ctx) { T context = super.produce(ctx); // Do not override the name if it's been already set (in the bean constructor for example) if (context.getNameStrategy() instanceof DefaultCamelContextNameStrategy) { context.setNameStrategy(nameStrategy(annotated)); } // Add bean registry and Camel injector if (context instanceof DefaultCamelContext) { DefaultCamelContext adapted = context.adapt(DefaultCamelContext.class); adapted.setRegistry(new CdiCamelRegistry(manager)); adapted.setInjector(new CdiCamelInjector(context.getInjector(), manager)); } else { // Fail fast for the time being to avoid side effects by the time these two methods get declared on the CamelContext interface throw new InjectionException("Camel CDI requires Camel context [" + context.getName() + "] to be a subtype of DefaultCamelContext"); } // Add event notifier if at least one observer is present Set<Annotation> qualifiers = annotated.getAnnotations().stream() .filter(isAnnotationType(Named.class).negate() .and(q -> manager.isQualifier(q.annotationType()))) .collect(toSet()); qualifiers.add(ANY); if (qualifiers.size() == 1) { qualifiers.add(DEFAULT); } qualifiers.retainAll(extension.getObserverEvents()); if (!qualifiers.isEmpty()) { context.getManagementStrategy().addEventNotifier(new CdiEventNotifier(manager, qualifiers)); } return context; }
private static ProducerTemplate producerTemplateFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension, Uri uri) { try { CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance); ProducerTemplate producerTemplate = context.createProducerTemplate(); Endpoint endpoint = context.getEndpoint(uri.value(), Endpoint.class); producerTemplate.setDefaultEndpoint(endpoint); return producerTemplate; } catch (Exception cause) { throw new InjectionException("Error injecting producer template annotated with " + uri + " into " + ip, cause); } }
private static ProducerTemplate defaultProducerTemplate(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { try { CamelContext context = selectContext(ip, instance, extension); return context.createProducerTemplate(); } catch (Exception cause) { throw new InjectionException("Error injecting producer template into " + ip, cause); } }
@Produces @Typed(MockEndpoint.class) // Qualifiers are dynamically added in CdiCamelExtension private static MockEndpoint mockEndpointFromMember(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { String uri = "mock:" + ip.getMember().getName(); try { return selectContext(ip, instance, extension).getEndpoint(uri, MockEndpoint.class); } catch (Exception cause) { throw new InjectionException("Error injecting mock endpoint into " + ip, cause); } }
@Mock @Produces @Typed(MockEndpoint.class) // Qualifiers are dynamically added in CdiCamelExtension private static MockEndpoint createMockEndpoint(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { Mock mock = getQualifierByType(ip, Mock.class).get(); try { CamelContext context = mock.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(mock.context(), instance); return context.getEndpoint(mock.value(), MockEndpoint.class); } catch (Exception cause) { throw new InjectionException("Error injecting mock endpoint annotated with " + mock + " into " + ip, cause); } }
@Uri("") @Produces // Qualifiers are dynamically added in CdiCamelExtension private static Endpoint endpoint(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension) { Uri uri = getQualifierByType(ip, Uri.class).get(); try { CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance); return context.getEndpoint(uri.value(), Endpoint.class); } catch (Exception cause) { throw new InjectionException("Error injecting endpoint annotated with " + uri + " into " + ip, cause); } }
@Override public void inject(T instance, CreationalContext<T> ctx) { super.inject(instance, ctx); try { // TODO: see how to retrieve the bean name processor.postProcessBeforeInitialization(instance, instance.getClass().getName()); processor.postProcessAfterInitialization(instance, instance.getClass().getName()); } catch (Exception cause) { throw new InjectionException("Camel annotations post processing of [" + delegate + "] failed!", cause); } }
@Override public void destroy(T instance, CreationalContext<T> creationalContext) { try { target.preDestroy(instance); target.dispose(instance); } catch (Exception cause) { throw new InjectionException("Error while destroying " + this, cause); } finally { creationalContext.release(); } }
@Override public void inject(T instance, CreationalContext<T> cc) { super.inject(instance, cc); try { // TODO: see how to retrieve the bean name processor.postProcessBeforeInitialization(instance, instance.getClass().getName()); processor.postProcessAfterInitialization(instance, instance.getClass().getName()); } catch (Exception cause) { throw new InjectionException("Camel annotations post processing of " + delegate + " failed!", cause); } }