public void process(Contribution contribution, IntrospectionContext context) { try { Source source = contribution.getSource(); Composite composite = loaderRegistry.load(source, Composite.class, context); QName name = composite.getName(); Resource resource = new Resource(contribution, source, "application/xml"); QNameSymbol symbol = new QNameSymbol(name); ResourceElement<QNameSymbol, Composite> element = new ResourceElement<>(symbol); element.setValue(composite); resource.addResourceElement(element); resource.setState(ResourceState.PROCESSED); contribution.addResource(resource); ContributionManifest manifest = contribution.getManifest(); Deployable deployable = new Deployable(name); manifest.addDeployable(deployable); QNameExport export = new QNameExport(name.getNamespaceURI()); manifest.addExport(export); } catch (LoaderException e) { throw new Fabric3Exception(e); } }
private void validateUnique( Resource resource, ResourceElement<QNameSymbol, Composite> element, XMLStreamReader reader, IntrospectionContext context) { Contribution contribution = resource.getContribution(); for (Resource entry : contribution.getResources()) { if (resource == entry) { // skip self since the resource is added to the contribution and will be iterated continue; } if (resource.getContentType().equals(entry.getContentType())) { for (ResourceElement<?, ?> elementEntry : entry.getResourceElements()) { if (element.getSymbol().equals(elementEntry.getSymbol())) { QName name = element.getSymbol().getKey(); Location location = reader.getLocation(); Composite composite = element.getValue(); DuplicateComposite error = new DuplicateComposite( "Duplicate composite found with name: " + name, location, composite); context.addError(error); break; } } } } }
public void index(Resource resource, IntrospectionContext context) { XMLStreamReader reader = null; InputStream stream = null; try { Source source = resource.getSource(); stream = source.openStream(); reader = xmlFactory.createXMLStreamReader(stream); reader.nextTag(); Location startLocation = reader.getLocation(); if (!"composite".equals(reader.getName().getLocalPart())) { // not a composite root element return; } String name = reader.getAttributeValue(null, "name"); if (name == null) { context.addError(new MissingAttribute("Composite name not specified", startLocation)); return; } String targetNamespace = reader.getAttributeValue(null, "targetNamespace"); QName compositeName = new QName(targetNamespace, name); QNameSymbol symbol = new QNameSymbol(compositeName); ResourceElement<QNameSymbol, Composite> element = new ResourceElement<>(symbol); resource.addResourceElement(element); validateUnique(resource, element, reader, context); } catch (XMLStreamException e) { throw new Fabric3Exception(e); } finally { try { if (stream != null) { stream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (reader != null) { reader.close(); } } catch (XMLStreamException e) { e.printStackTrace(); } } }
@SuppressWarnings({"unchecked"}) public void process(Resource resource, IntrospectionContext context) { Source source = resource.getSource(); ClassLoader classLoader = context.getClassLoader(); URI contributionUri = context.getContributionUri(); URL location = source.getBaseLocation(); IntrospectionContext childContext = new DefaultIntrospectionContext(contributionUri, classLoader, location); Composite composite; try { // check to see if the resource has already been evaluated composite = loader.load(source, Composite.class, childContext); } catch (LoaderException e) { throw new Fabric3Exception(e); } if (composite == null) { // composite could not be parsed InvalidXmlArtifact error = new InvalidXmlArtifact("Invalid composite: " + location, null); context.addError(error); return; } boolean found = false; for (ResourceElement element : resource.getResourceElements()) { if (element.getSymbol().getKey().equals(composite.getName())) { element.setValue(composite); found = true; break; } } if (!found) { // should not happen String identifier = composite.getName().toString(); throw new AssertionError("Resource element not found: " + identifier); } if (childContext.hasErrors()) { context.addErrors(childContext.getErrors()); } if (childContext.hasWarnings()) { context.addWarnings(childContext.getWarnings()); } resource.setState(ResourceState.PROCESSED); }