Search in sources :

Example 6 with TEntityType

use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.

the class BackendUtils method initializeProperties.

/**
 * Properties need to be initialized in the case of K/V Properties
 *
 * @param repository     The repository to work on
 * @param entityTemplate the entity template to update
 */
public static void initializeProperties(IRepository repository, TEntityTemplate entityTemplate) {
    Objects.requireNonNull(repository);
    Objects.requireNonNull(entityTemplate);
    Objects.requireNonNull(entityTemplate.getType());
    final TEntityType entityType = repository.getTypeForTemplate(entityTemplate);
    final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
    if (winerysPropertiesDefinition == null) {
        return;
    }
    Document document;
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        LOGGER.error("Could not create document", e);
        return;
    }
    final String namespace = winerysPropertiesDefinition.getNamespace();
    final Element wrapperElement = document.createElementNS(namespace, winerysPropertiesDefinition.getElementName());
    document.appendChild(wrapperElement);
    // we produce the serialization in the same order the XSD would be generated (because of the usage of xsd:sequence)
    for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitionKVList()) {
        // we always write the element tag as the XSD forces that
        final Element valueElement = document.createElementNS(namespace, propertyDefinitionKV.getKey());
        wrapperElement.appendChild(valueElement);
    }
    TEntityTemplate.Properties properties = new TEntityTemplate.Properties();
    properties.setAny(document.getDocumentElement());
    entityTemplate.setProperties(properties);
}
Also used : PropertyDefinitionKV(org.eclipse.winery.model.tosca.kvproperties.PropertyDefinitionKV) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document)

Example 7 with TEntityType

use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.

the class ToscaExportUtil method writeDefinitionsElement.

/**
 * Writes the Definitions belonging to the given definitgion children to the output stream
 *
 * @return a collection of DefinitionsChildIds referenced by the given component
 * @throws RepositoryCorruptException if tcId does not exist
 */
private Collection<DefinitionsChildId> writeDefinitionsElement(IRepository repository, DefinitionsChildId tcId, OutputStream out) throws JAXBException, RepositoryCorruptException, IOException {
    if (!repository.exists(tcId)) {
        String error = "Component instance " + tcId.toReadableString() + " does not exist.";
        ToscaExportUtil.LOGGER.error(error);
        throw new RepositoryCorruptException(error);
    }
    this.getPrepareForExport(repository, tcId);
    Definitions entryDefinitions = repository.getDefinitions(tcId);
    // BEGIN: Definitions modification
    // the "imports" collection contains the imports of Definitions, not of other definitions
    // the other definitions are stored in entryDefinitions.getImport()
    // we modify the internal definitions object directly. It is not written back to the storage. Therefore, we do not need to clone it
    // the imports (pointing to not-definitions (xsd, wsdl, ...)) already have a correct relative URL. (quick hack)
    URI uri = (URI) this.exportConfiguration.get(ToscaExportUtil.ExportProperties.REPOSITORY_URI.toString());
    if (uri != null) {
        // we are in the plain-XML mode, the URLs of the imports have to be adjusted
        for (TImport i : entryDefinitions.getImport()) {
            String loc = i.getLocation();
            if (!loc.startsWith("../")) {
                LOGGER.warn("Location is not relative for id " + tcId.toReadableString());
            }
            ;
            loc = loc.substring(3);
            loc = uri + loc;
            // now the location is an absolute URL
            i.setLocation(loc);
        }
    }
    // files of imports have to be added to the CSAR, too
    for (TImport i : entryDefinitions.getImport()) {
        String loc = i.getLocation();
        if (Util.isRelativeURI(loc)) {
            // locally stored, add to CSAR
            GenericImportId iid = new GenericImportId(i);
            String fileName = Util.getLastURIPart(loc);
            fileName = Util.URLdecode(fileName);
            RepositoryFileReference ref = new RepositoryFileReference(iid, fileName);
            this.putRefAsReferencedItemInCsar(ref);
        }
    }
    Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
    // adjust imports: add imports of definitions to it
    Collection<TImport> imports = new ArrayList<>();
    for (DefinitionsChildId id : referencedDefinitionsChildIds) {
        this.addToImports(repository, id, imports);
    }
    entryDefinitions.getImport().addAll(imports);
    if (entryDefinitions.getElement() instanceof TEntityType) {
        TEntityType entityType = (TEntityType) entryDefinitions.getElement();
        // we have an entity type with a possible properties definition
        WinerysPropertiesDefinition wpd = entityType.getWinerysPropertiesDefinition();
        if (wpd != null) {
            if (wpd.getIsDerivedFromXSD() == null) {
                // Write WPD only to file if it exists and is NOT derived from an XSD (which may happen during import)
                String wrapperElementNamespace = wpd.getNamespace();
                String wrapperElementLocalName = wpd.getElementName();
                // BEGIN: add import and put into CSAR
                TImport imp = new TImport();
                entryDefinitions.getImport().add(imp);
                // fill known import values
                imp.setImportType(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                imp.setNamespace(wrapperElementNamespace);
                // add "winerysPropertiesDefinition" flag to import tag to support
                Map<QName, String> otherAttributes = imp.getOtherAttributes();
                otherAttributes.put(QNames.QNAME_WINERYS_PROPERTIES_DEFINITION_ATTRIBUTE, "true");
                // Determine location
                String loc = BackendUtils.getImportLocationForWinerysPropertiesDefinitionXSD((EntityTypeId) tcId, uri, wrapperElementLocalName);
                if (uri == null) {
                    ToscaExportUtil.LOGGER.trace("CSAR Export mode. Putting XSD into CSAR");
                    // CSAR Export mode
                    // XSD has to be put into the CSAR
                    Document document = ModelUtilities.getWinerysPropertiesDefinitionXsdAsDocument(wpd);
                    // loc in import is URLencoded, loc on filesystem isn't
                    String locInCSAR = Util.URLdecode(loc);
                    // furthermore, the path has to start from the root of the CSAR; currently, it starts from Definitions/
                    locInCSAR = locInCSAR.substring(3);
                    ToscaExportUtil.LOGGER.trace("Location in CSAR: {}", locInCSAR);
                    this.referencesToPathInCSARMap.put(new DummyRepositoryFileReferenceForGeneratedXSD(document), locInCSAR);
                }
                imp.setLocation(loc);
                // END: add import and put into CSAR
                // BEGIN: generate TOSCA conforming PropertiesDefinition
                PropertiesDefinition propertiesDefinition = new PropertiesDefinition();
                propertiesDefinition.setType(new QName(wrapperElementNamespace, wrapperElementLocalName));
                entityType.setPropertiesDefinition(propertiesDefinition);
            // END: generate TOSCA conforming PropertiesDefinition
            } else {
            // noinspection StatementWithEmptyBody
            // otherwise WPD exists, but is derived from XSD
            // we DO NOT have to remove the winery properties definition from the output to allow "debugging" of the CSAR
            }
        }
    }
    // END: Definitions modification
    this.writeDefinitionsElement(entryDefinitions, out);
    return referencedDefinitionsChildIds;
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) Definitions(org.eclipse.winery.model.tosca.Definitions) TImport(org.eclipse.winery.model.tosca.TImport) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) Document(org.w3c.dom.Document) URI(java.net.URI) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) PropertiesDefinition(org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException)

Example 8 with TEntityType

use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.

the class WineryRepositoryClient method getDefinitions.

@Override
public Definitions getDefinitions(DefinitionsChildId id) {
    for (WebResource wr : this.repositoryResources) {
        String path = Util.getUrlPath(id);
        Definitions definitions = WineryRepositoryClient.getDefinitions(wr.path(path));
        if (definitions == null) {
            // in case of an error, just try the next one
            continue;
        }
        TExtensibleElements element = definitions.getElement();
        if (element instanceof TEntityType) {
            this.cache((TEntityType) element, id.getQName());
            return definitions;
        }
    }
    return new Definitions();
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) WebResource(com.sun.jersey.api.client.WebResource) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements)

Example 9 with TEntityType

use of org.eclipse.winery.model.tosca.TEntityType in project winery by eclipse.

the class WineryRepositoryClient method getType.

@Override
@SuppressWarnings("unchecked")
public <T extends TEntityType> T getType(QName qname, Class<T> type) {
    T res = null;
    if (this.entityTypeDataCache.containsKey(type)) {
        Map<QName, TEntityType> map = this.entityTypeDataCache.get(type);
        if (map.containsKey(qname)) {
            res = (T) map.get(qname);
        }
    }
    if (res == null) {
        for (WebResource wr : this.repositoryResources) {
            String path = Util.getURLpathFragmentForCollection(type);
            TDefinitions definitions = WineryRepositoryClient.getDefinitions(wr, path, qname.getNamespaceURI(), qname.getLocalPart());
            if (definitions == null) {
                // in case of an error, just try the next one
                continue;
            }
            res = (T) definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().get(0);
            this.cache(res, qname);
            break;
        }
    }
    return res;
}
Also used : QName(javax.xml.namespace.QName) TEntityType(org.eclipse.winery.model.tosca.TEntityType) WebResource(com.sun.jersey.api.client.WebResource) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Aggregations

TEntityType (org.eclipse.winery.model.tosca.TEntityType)9 QName (javax.xml.namespace.QName)4 WinerysPropertiesDefinition (org.eclipse.winery.model.tosca.kvproperties.WinerysPropertiesDefinition)4 Definitions (org.eclipse.winery.model.tosca.Definitions)3 WebResource (com.sun.jersey.api.client.WebResource)2 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)2 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)2 PropertiesDefinition (org.eclipse.winery.model.tosca.TEntityType.PropertiesDefinition)2 Document (org.w3c.dom.Document)2 ADR (io.github.adr.embedded.ADR)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 JAXBElement (javax.xml.bind.JAXBElement)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)1 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)1