/** * Synthesizes a composite from a collection of composites using inclusion. A counter is maintained for each include name and used to generate include * names in situations where they may clash, e.g. when two composites from different contributions with the same name are deployed together. * * @param composites the composites to synthesize * @return the synthesized composite */ private Composite synthesizeComposite(List<Composite> composites) { Composite synthesized = new Composite(SYNTHETIC_COMPOSITE); Map<QName, AtomicInteger> counters = new HashMap<>(); for (Composite composite : composites) { Include include = new Include(); QName name = composite.getName(); include.setName(name); include.setIncluded(composite); if (synthesized.getIncludes().containsKey(name)) { AtomicInteger counter = counters.get(name); if (counter == null) { counter = new AtomicInteger(); counters.put(name, counter); } include.setName(new QName(name.getNamespaceURI(), name.getLocalPart() + counter.incrementAndGet())); } synthesized.add(include); } return synthesized; }
/** * Adds an included composite. * * @param include the composite to include */ public void add(Include include) { include.setParent(this); includes.put(include.getName(), include); componentsView.putAll(include.getIncluded().getComponents()); wiresView.addAll(include.getIncluded().getWires()); channelsView.putAll(include.getIncluded().getChannels()); resourcesView.addAll(include.getIncluded().getResources()); }
private Composite createComposite(WebImplementation implementation, Contribution contribution) { URI contributionUri = contribution.getUri(); String localPart = createLocalPart(contributionUri); QName compositeName = new QName(HostNamespaces.SYNTHESIZED, localPart); Composite composite = new Composite(compositeName); composite.setContributionUri(contributionUri); Component<WebImplementation> component = new Component<>(localPart, implementation); component.setContributionUri(contributionUri); composite.add(component); return composite; }
/** * Creates a wrapper composite used to deploy the component to the domain. Also registers the wrapper with the Host contribution. * * @param name the composite name * @return the wrapping composite */ private Composite createWrapperComposite(String name) { QName compositeName = new QName(HostNamespaces.SYNTHESIZED, name); Composite wrapper = new Composite(compositeName); URI uri = ContributionResolver.getContribution(); wrapper.setContributionUri(uri); addCompositeToContribution(wrapper); return wrapper; }
/** * Adds a component definition to the composite. * * @param definition the component definition * @return the builder */ public CompositeBuilder component(Component<?> definition) { checkState(); composite.add(definition); return this; }
/** * Adds the contribution URI to the system composite and its children. * * @param contributionUri the contribution URI * @param composite the composite */ private static void addContributionUri(URI contributionUri, Composite composite) { composite.setContributionUri(contributionUri); for (Component<?> definition : composite.getComponents().values()) { if (definition.getComponentType() instanceof Composite) { Composite componentType = Composite.class.cast(definition.getComponentType()); addContributionUri(contributionUri, componentType); } else { definition.setContributionUri(contributionUri); } } }
private void setContributionUris(Composite composite) { URI uri = ContributionResolver.getContribution(); composite.setContributionUri(uri); for (ComponentDefinition<? extends Implementation<?>> definition : composite.getComponents().values()) { definition.setContributionUri(uri); if (definition.getComponentType() instanceof Composite) { setContributionUris((Composite) definition.getComponentType()); } } for (ChannelDefinition definition : composite.getChannels().values()) { definition.setContributionUri(uri); } }
private Resource createResource(Contribution contribution, Composite composite) { QNameSymbol symbol = new QNameSymbol(composite.getName()); ResourceElement<QNameSymbol, Composite> element = new ResourceElement<>(symbol, composite); Resource resource = new Resource(contribution, null, Constants.COMPOSITE_CONTENT_TYPE); resource.addResourceElement(element); resource.setState(ResourceState.PROCESSED); return resource; }
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; }
/** * Includes the composite in the current composite. * * @param included the name of the composite to include * @return the builder */ public CompositeBuilder include(Composite included) { checkState(); Include include = new Include(); include.setIncluded(included); include.setName(included.getName()); composite.add(include); return this; }
public void instantiateChannels(Composite composite, LogicalCompositeComponent parent, InstantiationContext context) { for (Channel definition : composite.getChannels().values()) { URI uri = URI.create(parent.getUri() + "/" + definition.getName()); if (parent.getChannel(uri) != null) { DuplicateChannel error = new DuplicateChannel(uri, parent); context.addError(error); continue; } LogicalChannel channel = new LogicalChannel(uri, definition, parent); for (Binding binding : definition.getBindings()) { LogicalBinding<Binding> logicalBinding = new LogicalBinding<>(binding, channel); channel.addBinding(logicalBinding); } channel.setDeployable(composite.getName()); parent.addChannel(channel); } }
for (Component<? extends Implementation<?>> definition : composite.getComponents().values()) { processor.introspect((InjectingComponentType) definition.getComponentType(), context); QName name = composite.getName(); List<ValidationFailure> errors = context.getErrors(); List<ValidationFailure> warnings = context.getWarnings();
protected CompositeBuilder(QName name) { composite = new Composite(name); } }
@SuppressWarnings({"unchecked"}) private void instantiateChildComponents(LogicalCompositeComponent component, Composite composite, InstantiationContext context) { // create the child components List<LogicalComponent<?>> children = new ArrayList<>(); for (Component<? extends Implementation<?>> child : composite.getComponents().values()) { LogicalComponent<?> childComponent = atomicInstantiator.instantiate(child, component, context); component.addComponent(childComponent); children.add(childComponent); } // resolve the reference wires after the children have been instantiated and added to the parent, otherwise targets will not resolve for (LogicalComponent<?> child : children) { wireInstantiator.instantiateReferenceWires(child, context); } }
/** * Instantiates components in included composites. * * @param composite the root composite to instantiate * @param newComponents the collection to hold instantiated components * @param synthetic true if the root composite is synthetic * @param domain the domain * @param context the instantiation context */ private void instantiateIncludes(Composite composite, List<LogicalComponent<?>> newComponents, boolean synthetic, LogicalCompositeComponent domain, InstantiationContext context) { // instantiate the included components for (Include include : composite.getIncludes().values()) { for (Component<? extends Implementation<?>> definition : include.getIncluded().getComponents().values()) { LogicalComponent<?> logicalComponent = instantiate(definition, domain, context); newComponents.add(logicalComponent); // add to the domain since includes starting from a deployable composite are "collapsed" to the domain level domain.addComponent(logicalComponent); } } }
private void includeResources(Composite composite, LogicalCompositeComponent domain, boolean synthetic) { if (synthetic) { for (Include include : composite.getIncludes().values()) { Composite included = include.getIncluded(); for (Resource definition : included.getResources()) { LogicalResource<?> resource = new LogicalResource<>(definition, domain); domain.addResource(resource); } } } else { for (Resource definition : composite.getResources()) { LogicalResource<?> resource = new LogicalResource<>(definition, domain); domain.addResource(resource); } } }
Collection<Component<? extends Implementation<?>>> definitions = composite.getDeclaredComponents().values(); List<LogicalComponent<?>> newComponents = new ArrayList<>(definitions.size()); for (Component<? extends Implementation<?>> definition : definitions) { for (Include include : composite.getIncludes().values()) {
@SuppressWarnings({"unchecked"}) private void instantiateResources(LogicalCompositeComponent component, Composite composite) { for (Resource definition : composite.getResources()) { LogicalResource<?> resource = new LogicalResource(definition, component); component.addResource(resource); } }
public void instantiateCompositeWires(Composite composite, LogicalCompositeComponent parent, InstantiationContext context) { // instantiate wires held directly in the composite and in included composites for (Wire definition : composite.getWires()) { // resolve the source reference Target referenceTarget = definition.getReferenceTarget(); LogicalReference reference = resolveReference(referenceTarget, parent, context); if (reference == null) { // error resolving, continue processing other targets so all errors are collated continue; } // resolve the target service Target serviceTarget = definition.getServiceTarget(); LogicalService service = resolveService(reference, serviceTarget, parent, context); if (service == null) { // error resolving, continue processing other targets so all errors are collated continue; } // create the wire URI contributionUri = parent.getDefinition().getContributionUri(); LogicalWire wire = new LogicalWire(parent, reference, service, contributionUri); String referenceBindingName = referenceTarget.getBinding(); String serviceBindingName = serviceTarget.getBinding(); resolveBindings(reference, referenceBindingName, service, wire, serviceBindingName, context); parent.addWire(reference, wire); } }
public synchronized void undeploy(Composite composite) throws Fabric3Exception { LogicalCompositeComponent domain = logicalComponentManager.getDomainComposite(); URI contributionUri = composite.getContributionUri(); collector.markForCollection(contributionUri, domain); Deployment deployment = generator.generate(domain); collector.collect(domain); deployer.deploy(deployment); Contribution contribution = metadataStore.find(contributionUri); contribution.undeploy(); }