/** * Add a reference and its associated with an injection site. * * @param reference the reference to add * @param injectionSite the injection site for the reference */ public void add(Reference<ComponentType> reference, InjectionSite injectionSite) { super.add(reference); Injectable injectable = new Injectable(InjectableType.REFERENCE, reference.getName()); addInjectionSite(injectionSite, injectable); injectionSiteMapping.put(reference, injectionSite); }
/** * Adds a reference with the given name and target. * * @param name the reference name * @param target the target */ public T reference(String name, String target) { checkState(); Component<?> definition = getComponent(); Reference<Component> reference = new Reference<>(name, Multiplicity.ONE_ONE); reference.addTarget(new Target(target)); definition.add(reference); return builder(); }
private void generateReferenceInjectionMapping(Reference<ComponentType> definition, WebComponentType type, Map<String, Map<String, InjectionSite>> mappings) { Map<String, InjectionSite> mapping = mappings.get(definition.getName()); if (mapping == null) { mapping = new HashMap<>(); mappings.put(definition.getName(), mapping); } for (Map.Entry<String, Map<InjectionSite, Injectable>> entry : type.getInjectionSites().entrySet()) { for (Map.Entry<InjectionSite, Injectable> siteMap : entry.getValue().entrySet()) { if (siteMap.getValue().getName().equals(definition.getName())) { mapping.put(entry.getKey(), siteMap.getKey()); } } } ServiceContract contract = definition.getServiceContract(); Class<?> interfaceClass = contract.getInterfaceClass(); // inject the reference into the session context WebContextInjectionSite site = new WebContextInjectionSite(interfaceClass, SESSION_CONTEXT); mapping.put(SESSION_CONTEXT_SITE, site); // also inject the reference into the servlet context WebContextInjectionSite servletContextSite = new WebContextInjectionSite(interfaceClass, SERVLET_CONTEXT); mapping.put(SERVLET_CONTEXT_SITE, servletContextSite); }
private void handleReference(Reference<?> reference, InjectionSite site, Class<?> implClass, Map<AccessibleObject, Supplier<Object>> injectors) { if (reference.getBindings().isEmpty()) { return; } Class<?> interfaze = reference.getServiceContract().getInterfaceClass(); Binding binding = reference.getBindings().get(0); AccessibleObject accessibleObject; if (site instanceof FieldInjectionSite) { accessibleObject = ((FieldInjectionSite) site).getField(); } else if (site instanceof MethodInjectionSite) { accessibleObject = ((MethodInjectionSite) site).getMethod(); } else { // ignore return; } // create supplier to resolve the proxy injectors.put(accessibleObject, () -> serviceResolver.resolve(interfaze, binding, implClass)); } }
public void generateWireSource(JavaWireSource source, LogicalReference reference) { URI uri = reference.getUri(); JavaServiceContract serviceContract = (JavaServiceContract) reference.getDefinition().getServiceContract(); source.setUri(uri); source.setInjectable(new Injectable(InjectableType.REFERENCE, uri.getFragment())); source.setInterfaceClass(serviceContract.getInterfaceClass()); // assume for now that any wire from a Java component can be optimized source.setOptimizable(true); if (reference.getDefinition().isKeyed()) { source.setKeyed(true); DataType type = reference.getDefinition().getKeyDataType(); String className = type.getType().getName(); source.setKeyClassName(className); } }
private void createReferences(Component<?> component, LogicalComponent<?> logicalComponent, ComponentType componentType) { for (Reference<ComponentType> reference : componentType.getReferences().values()) { String name = reference.getName(); URI referenceUri = logicalComponent.getUri().resolve('#' + name); LogicalReference logicalReference = new LogicalReference(referenceUri, reference, logicalComponent); Reference<Component> componentReference = component.getReferences().get(name); if (componentReference != null) { // reference is configured in the component definition for (Binding binding : componentReference.getBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addBinding(logicalBinding); } for (Binding binding : componentReference.getCallbackBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addCallbackBinding(logicalBinding); } } else { // check if reference is configured with bindings in the component type for (Binding binding : reference.getBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addBinding(logicalBinding); } for (Binding binding : reference.getCallbackBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addCallbackBinding(logicalBinding); } } logicalComponent.addReference(logicalReference); } }
private LogicalReference createReference(Class<?> interfaze) { LogicalCompositeComponent domainComponent = lcm.getDomainComposite(); int id = idCounter.getAndIncrement(); String name = "Synthetic" + id; URI componentUri = URI.create(domainComponent.getUri().toString() + "/" + name); URI referenceUri = URI.create(componentUri.toString() + "#reference"); QName qName = new QName(HostNamespaces.SYNTHESIZED, "SyntheticComposite" + id); Composite composite = new Composite(qName); Component<NonManagedImplementation> component = new Component<>(name); component.setParent(composite); component.setContributionUri(Names.HOST_CONTRIBUTION); NonManagedImplementation implementation = new NonManagedImplementation(); component.setImplementation(implementation); Reference<ComponentType> reference = new Reference<>("reference", Multiplicity.ONE_ONE); composite.add(reference); JavaServiceContract contract = introspector.introspect(interfaze); LogicalComponent<NonManagedImplementation> logicalComponent = new LogicalComponent<>(componentUri, component, domainComponent); reference.setServiceContract(contract); LogicalReference logicalReference = new LogicalReference(referenceUri, reference, logicalComponent); logicalReference.setServiceContract(contract); logicalComponent.addReference(logicalReference); return logicalReference; }
protected void processBindingAnnotations(AccessibleObject object, Reference reference, Class<?> implClass, IntrospectionContext context) { A annotations[] = object.getAnnotationsByType(annotationType); if (annotations.length == 0) { // check meta-annotations for (Annotation annotation : object.getAnnotations()) { annotations = annotation.annotationType().getAnnotationsByType(annotationType); if (annotations.length > 0) { break; } } if (annotations.length == 0) { return; } } for (A annotation : annotations) { Binding binding = processReference(annotation, reference, implClass, context); if (binding == null) { continue; } reference.addBinding(binding); ServiceContract contract = reference.getServiceContract(); if (contract.getCallbackContract() != null) { Binding callbackBinding = processReferenceCallback(annotationType.cast(annotation), reference, implClass, context); if (callbackBinding != null) { reference.addCallbackBinding(callbackBinding); } } } }
private Reference<ComponentType> createDefinition(String name, boolean required, Type type, Class<?> implClass, InjectingComponentType componentType, IntrospectionContext context) { TypeMapping typeMapping = context.getTypeMapping(implClass); Class<?> baseType = helper.getBaseType(type, typeMapping); ServiceContract contract = contractProcessor.introspect(baseType, implClass, context, componentType); Reference<ComponentType> definition = new Reference<>(name, contract); helper.processMultiplicity(definition, required, type, typeMapping); return definition; }
/** * Constructor. * * @param uri the reference URI * @param definition the reference type definition * @param parent the parent component */ public LogicalReference(URI uri, Reference<ComponentType> definition, LogicalComponent<?> parent) { super(uri, definition != null ? definition.getServiceContract() : null, parent); this.definition = definition; }
private boolean isBoundReinjection(LogicalReference logicalReference) { Multiplicity multiplicity = logicalReference.getDefinition().getMultiplicity(); if (multiplicity == Multiplicity.ZERO_N || multiplicity == Multiplicity.ONE_N) { for (LogicalBinding<?> binding : logicalReference.getBindings()) { if (binding.getState() == LogicalState.NEW || binding.getState() == LogicalState.MARKED) { return true; } } } return false; }
/** * Returns true if the reference is not keyed, true if the reference is keyed and the target specifies a key, false if the reference is keyed and the target * does not specify a key. * * @param logicalReference the logical reference * @param target the target * @return true if the reference is not keyed, true if the reference is keyed and the target specifies a key, false if the reference is keyed and the target * does not specify a key */ private boolean validKey(LogicalReference logicalReference, LogicalComponent<?> target) { return !logicalReference.getDefinition().isKeyed() || target.getDefinition().getKey() != null || target.getDefinition().getComponentType().getKey() != null; } }
private void addTargets(AccessibleObject accessibleObject, Member member, IntrospectionContext context, Reference definition) { Targets targetsAnnotation = accessibleObject.getAnnotation(Targets.class); if (targetsAnnotation != null) { Multiplicity multiplicity = definition.getMultiplicity(); if (multiplicity != Multiplicity.ONE_N && multiplicity != Multiplicity.ZERO_N) { Class<?> clazz = member.getDeclaringClass(); String name = member.getName(); InvalidAnnotation error = new InvalidAnnotation("Reference is not a multiplicity: " + name, accessibleObject, targetsAnnotation, clazz); context.addError(error); return; } for (String value : targetsAnnotation.value()) { org.fabric3.api.model.type.component.Target target = parseTarget(value, targetsAnnotation, accessibleObject, member, context); if (target != null) { definition.addTarget(target); } } } else { Target targetAnnotation = accessibleObject.getAnnotation(Target.class); if (targetAnnotation != null) { org.fabric3.api.model.type.component.Target target = parseTarget(targetAnnotation.value(), targetAnnotation, accessibleObject, member, context); if (target != null) { definition.addTarget(target); } } } }
public PhysicalWireSource generateSource(LogicalReference reference) throws Fabric3Exception { URI uri = reference.getUri(); SystemWireSource definition = new SystemWireSource(); definition.setOptimizable(true); definition.setUri(uri); definition.setInjectable(new Injectable(InjectableType.REFERENCE, uri.getFragment())); JavaServiceContract serviceContract = (JavaServiceContract) reference.getDefinition().getServiceContract(); definition.setInterfaceClass(serviceContract.getInterfaceClass()); if (reference.getDefinition().isKeyed()) { definition.setKeyed(true); String className = reference.getDefinition().getKeyDataType().getType().getName(); definition.setKeyClassName(className); } return definition; }
private void createReferences(Component<?> component, LogicalComponent<?> logicalComponent, ComponentType componentType) { for (Reference<ComponentType> reference : componentType.getReferences().values()) { String name = reference.getName(); URI referenceUri = logicalComponent.getUri().resolve('#' + name); LogicalReference logicalReference = new LogicalReference(referenceUri, reference, logicalComponent); Reference<Component> componentReference = component.getReferences().get(name); if (componentReference != null) { // reference is configured in the component definition for (Binding binding : componentReference.getBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addBinding(logicalBinding); } for (Binding binding : componentReference.getCallbackBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addCallbackBinding(logicalBinding); } } else { // check if reference is configured with bindings in the component type for (Binding binding : reference.getBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addBinding(logicalBinding); } for (Binding binding : reference.getCallbackBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, logicalReference); logicalReference.addCallbackBinding(logicalBinding); } } logicalComponent.addReference(logicalReference); } }
private void handleReference(Reference<?> reference, InjectionSite site, Class<?> implClass, Map<AccessibleObject, Supplier<Object>> injectors) { if (reference.getBindings().isEmpty()) { return; } Class<?> interfaze = reference.getServiceContract().getInterfaceClass(); Binding binding = reference.getBindings().get(0); AccessibleObject accessibleObject; if (site instanceof FieldInjectionSite) { accessibleObject = ((FieldInjectionSite) site).getField(); } else if (site instanceof MethodInjectionSite) { accessibleObject = ((MethodInjectionSite) site).getMethod(); } else { // ignore return; } // create supplier to resolve the proxy injectors.put(accessibleObject, () -> serviceResolver.resolve(interfaze, binding, implClass)); } }
private LogicalReference createReference(Class<?> interfaze) { LogicalCompositeComponent domainComponent = lcm.getDomainComposite(); int id = idCounter.getAndIncrement(); String name = "Synthetic" + id; URI componentUri = URI.create(domainComponent.getUri().toString() + "/" + name); URI referenceUri = URI.create(componentUri.toString() + "#reference"); QName qName = new QName(HostNamespaces.SYNTHESIZED, "SyntheticComposite" + id); Composite composite = new Composite(qName); Component<NonManagedImplementation> component = new Component<>(name); component.setParent(composite); component.setContributionUri(Names.HOST_CONTRIBUTION); NonManagedImplementation implementation = new NonManagedImplementation(); component.setImplementation(implementation); Reference<ComponentType> reference = new Reference<>("reference", Multiplicity.ONE_ONE); composite.add(reference); JavaServiceContract contract = introspector.introspect(interfaze); LogicalComponent<NonManagedImplementation> logicalComponent = new LogicalComponent<>(componentUri, component, domainComponent); reference.setServiceContract(contract); LogicalReference logicalReference = new LogicalReference(referenceUri, reference, logicalComponent); logicalReference.setServiceContract(contract); logicalComponent.addReference(logicalReference); return logicalReference; }
continue; reference.addBinding(binding); ServiceContract contract = reference.getServiceContract(); if (contract.getCallbackContract() != null) { Binding callbackBinding = processReferenceCallback(castAnnotation, reference, implClass, context); if (callbackBinding != null) { reference.addCallbackBinding(callbackBinding);