Search in sources :

Example 1 with TArtifactTemplate

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

the class BackendUtils method getTArtifactTemplate.

/**
 * @param directoryId DirectoryID of the TArtifactTemplate that should be returned.
 * @return The TArtifactTemplate corresponding to the directoryId.
 */
public static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        Definitions defs = ((Definitions) u.unmarshal(is));
        for (TExtensibleElements elem : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
            if (elem instanceof TArtifactTemplate) {
                return (TArtifactTemplate) elem;
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    } catch (JAXBException e) {
        LOGGER.error("Error in XML in " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with TArtifactTemplate

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

the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.

public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
    final Definitions definitions = createWrapperDefinitions(id);
    HasIdInIdOrNameField element;
    if (id instanceof RelationshipTypeImplementationId) {
        element = new TRelationshipTypeImplementation();
    } else if (id instanceof NodeTypeImplementationId) {
        element = new TNodeTypeImplementation();
    } else if (id instanceof RequirementTypeId) {
        element = new TRequirementType();
    } else if (id instanceof NodeTypeId) {
        element = new TNodeType();
    } else if (id instanceof RelationshipTypeId) {
        element = new TRelationshipType();
    } else if (id instanceof CapabilityTypeId) {
        element = new TCapabilityType();
    } else if (id instanceof ArtifactTypeId) {
        element = new TArtifactType();
    } else if (id instanceof PolicyTypeId) {
        element = new TPolicyType();
    } else if (id instanceof PolicyTemplateId) {
        element = new TPolicyTemplate();
    } else if (id instanceof ServiceTemplateId) {
        element = new TServiceTemplate();
    } else if (id instanceof ArtifactTemplateId) {
        element = new TArtifactTemplate();
    } else if (id instanceof XSDImportId) {
        // TImport has no id; thus directly generating it without setting an id
        TImport tImport = new TImport();
        definitions.setElement(tImport);
        return definitions;
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
    copyIdToFields(element, id);
    definitions.setElement((TExtensibleElements) element);
    return definitions;
}
Also used : ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TImport(org.eclipse.winery.model.tosca.TImport) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) RelationshipTypeImplementationId(org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PolicyTypeId(org.eclipse.winery.common.ids.definitions.PolicyTypeId) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId)

Example 3 with TArtifactTemplate

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

the class BackendUtils method synchronizeReferences.

public static void synchronizeReferences(ArtifactTemplateId id) throws IOException {
    TArtifactTemplate template = RepositoryFactory.getRepository().getElement(id);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = RepositoryFactory.getRepository().getContainedFiles(fileDir);
    if (files.isEmpty()) {
        // clear artifact references
        template.setArtifactReferences(null);
    } else {
        TArtifactTemplate.ArtifactReferences artifactReferences = new TArtifactTemplate.ArtifactReferences();
        template.setArtifactReferences(artifactReferences);
        List<TArtifactReference> artRefList = artifactReferences.getArtifactReference();
        for (RepositoryFileReference ref : files) {
            // determine path
            // path relative from the root of the CSAR is ok (COS01, line 2663)
            // double encoded - see ADR-0003
            String path = Util.getUrlPath(ref);
            // put path into data structure
            // we do not use Include/Exclude as we directly reference a concrete file
            TArtifactReference artRef = new TArtifactReference();
            artRef.setReference(path);
            artRefList.add(artRef);
        }
    }
    BackendUtils.persist(id, template);
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference)

Example 4 with TArtifactTemplate

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

the class CsarExporter method addArtifactTemplateToZipFile.

/**
 * Special handling for artifact template directories source and files
 *
 * @param zos         Output stream for the archive that should contain the file
 * @param ref         Reference to the file that should be added to the archive
 * @param archivePath Path to the file inside the archive
 * @throws IOException thrown when the temporary directory can not be created
 */
private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref, String archivePath) throws IOException {
    GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());
    if (gitInfo == null) {
        try (InputStream is = repository.newInputStream(ref)) {
            if (is != null) {
                ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
                zos.putArchiveEntry(archiveEntry);
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
            }
        } catch (Exception e) {
            CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
        }
        return;
    }
    // TODO: This is not quite correct. The files should reside checked out at "source/"
    Path tempDir = Files.createTempDirectory(WINERY_TEMP_DIR_PREFIX);
    try {
        Git git = Git.cloneRepository().setURI(gitInfo.URL).setDirectory(tempDir.toFile()).call();
        git.checkout().setName(gitInfo.BRANCH).call();
        String path = "artifacttemplates/" + Util.URLencode(((ArtifactTemplateId) ref.getParent().getParent()).getQName().getNamespaceURI()) + "/" + ((ArtifactTemplateId) ref.getParent().getParent()).getQName().getLocalPart() + "/files/";
        TArtifactTemplate template = BackendUtils.getTArtifactTemplate((DirectoryId) ref.getParent());
        addWorkingTreeToArchive(zos, template, tempDir, path);
    } catch (GitAPIException e) {
        CsarExporter.LOGGER.error(String.format("Error while cloning repo: %s / %s", gitInfo.URL, gitInfo.BRANCH), e);
    } finally {
        deleteDirectory(tempDir);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) GitInfo(org.eclipse.winery.repository.GitInfo) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JAXBException(javax.xml.bind.JAXBException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)

Example 5 with TArtifactTemplate

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

the class FilebasedRepositoryTest method getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate.

@Test
public void getTypeForTemplateReturnsCorrectTypeForMyTinyTestArtifactTemplate() throws Exception {
    this.setRevisionTo("1374c8c13ec64899360511dbe0414223b88d3b01");
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://opentosca.org/artifacttemplates", "MyTinyTest", false);
    final TArtifactTemplate artifactTemplate = this.repository.getElement(artifactTemplateId);
    final TEntityType typeForTemplate = this.repository.getTypeForTemplate(artifactTemplate);
    Assert.assertEquals(new QName("http://winery.opentosca.org/test/artifacttypes", "MiniArtifactType"), new QName(typeForTemplate.getTargetNamespace(), typeForTemplate.getName()));
}
Also used : TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TEntityType(org.eclipse.winery.model.tosca.TEntityType) QName(javax.xml.namespace.QName) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) Test(org.junit.Test)

Aggregations

TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 JAXBException (javax.xml.bind.JAXBException)3 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)3 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)3 Definitions (org.eclipse.winery.model.tosca.Definitions)3 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 TransformerException (javax.xml.transform.TransformerException)2 ArtifactTypeId (org.eclipse.winery.common.ids.definitions.ArtifactTypeId)2 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1