Search in sources :

Example 1 with TDefinitions

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

the class WineryRepositoryClient method getAllTypes.

/**
 * Fetches java objects at a given URL
 *
 * @param path      the path to use. E.g., "nodetypes" for node types, ...
 * @param className the class of the expected return type. May be TDefinitions or TEntityType. TDefinitions the mode
 *                  is that the import statement are recursively resolved and added to the returned Defintitions
 *                  elment
 */
// we convert an object to T if it T is definitions
// does not work without compiler error
@SuppressWarnings("unchecked")
private <T extends TExtensibleElements> Collection<T> getAllTypes(String path, Class<T> className) {
    Map<WebResource, List<NamespaceIdOptionalName>> wrToNamespaceAndIdListMapOfAllTypes = this.getWRtoNamespaceAndIdListMapOfAllTypes(path);
    // now we now all QNames. We have to fetch the full content now
    Collection<T> res = new LinkedList<T>();
    for (WebResource wr : wrToNamespaceAndIdListMapOfAllTypes.keySet()) {
        WebResource componentListResource = wr.path(path);
        for (NamespaceIdOptionalName nsAndId : wrToNamespaceAndIdListMapOfAllTypes.get(wr)) {
            TDefinitions definitions = WineryRepositoryClient.getDefinitions(componentListResource, nsAndId.getNamespace(), nsAndId.getId());
            if (definitions == null) {
                // try next one
                continue;
            }
            T result;
            if (TDefinitions.class.equals(className)) {
                // mode: complete definitions
                result = (T) definitions;
            } else {
                // convention: first element in list is the element we look for
                if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
                    result = null;
                    LOGGER.error("Type {}/{} was found, but did not return any data", nsAndId.getNamespace(), nsAndId.getId());
                } else {
                    LOGGER.trace("Probably found valid data for {}/{}", nsAndId.getNamespace(), nsAndId.getId());
                    result = (T) definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().get(0);
                // caching disabled as we also handle TServiceTemplates
                // this.cache((TEntityType) result, new QName(nsAndId.getNamespace(), nsAndId.getId()));
                }
            }
            // than the element to insert.
            if (result != null) {
                res.add(result);
            }
        }
    }
    return res;
}
Also used : NamespaceIdOptionalName(org.eclipse.winery.common.beans.NamespaceIdOptionalName) WebResource(com.sun.jersey.api.client.WebResource) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) LinkedList(java.util.LinkedList)

Example 2 with TDefinitions

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

the class WriterUtils method storeTypes.

public static void storeTypes(Path path, String namespace, String id) {
    LOGGER.debug("Store type: {}", id);
    try {
        MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setNamespace(namespace);
        builder.setLocation(id + ".xsd");
        GenericImportId rid = new XSDImportId(namespace, id, false);
        TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid);
        definitions.getImport().add(builder.build());
        CsarImporter.storeDefinitions(rid, definitions);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
        List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
        for (File file : files) {
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
            RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
            RepositoryFactory.getRepository().putContentToFile(fileRef, stream, mediaType);
        }
    } catch (IllegalArgumentException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) TImport(org.eclipse.winery.model.tosca.TImport) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType) Files(java.nio.file.Files) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 3 with TDefinitions

use of org.eclipse.winery.model.tosca.TDefinitions 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

TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)3 WebResource (com.sun.jersey.api.client.WebResource)2 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1 MediaType (org.apache.tika.mime.MediaType)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 NamespaceIdOptionalName (org.eclipse.winery.common.beans.NamespaceIdOptionalName)1 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)1 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)1 TEntityType (org.eclipse.winery.model.tosca.TEntityType)1 TImport (org.eclipse.winery.model.tosca.TImport)1