Search in sources :

Example 1 with TArtifactReference

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

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

the class CsarExporter method addWorkingTreeToArchive.

/**
 * Adds a working tree to an archive
 *
 * @param file        The current directory to add
 * @param zos         Output stream of the archive
 * @param template    Template of the artifact
 * @param rootDir     The root of the working tree
 * @param archivePath The path inside the archive to the working tree
 */
private void addWorkingTreeToArchive(File file, ArchiveOutputStream zos, TArtifactTemplate template, Path rootDir, String archivePath) {
    if (file.isDirectory()) {
        if (file.getName().equals(".git")) {
            return;
        }
        File[] files = file.listFiles();
        if (files != null) {
            for (File f : files) {
                addWorkingTreeToArchive(f, zos, template, rootDir, archivePath);
            }
        }
    } else {
        boolean foundInclude = false;
        boolean included = false;
        boolean excluded = false;
        for (TArtifactReference artifactReference : template.getArtifactReferences().getArtifactReference()) {
            for (Object includeOrExclude : artifactReference.getIncludeOrExclude()) {
                if (includeOrExclude instanceof TArtifactReference.Include) {
                    foundInclude = true;
                    TArtifactReference.Include include = (TArtifactReference.Include) includeOrExclude;
                    String reference = artifactReference.getReference();
                    if (reference.endsWith("/")) {
                        reference += include.getPattern();
                    } else {
                        reference += "/" + include.getPattern();
                    }
                    reference = reference.substring(1);
                    included |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
                } else if (includeOrExclude instanceof TArtifactReference.Exclude) {
                    TArtifactReference.Exclude exclude = (TArtifactReference.Exclude) includeOrExclude;
                    String reference = artifactReference.getReference();
                    if (reference.endsWith("/")) {
                        reference += exclude.getPattern();
                    } else {
                        reference += "/" + exclude.getPattern();
                    }
                    reference = reference.substring(1);
                    excluded |= BackendUtils.isGlobMatch(reference, rootDir.relativize(file.toPath()));
                }
            }
        }
        if ((!foundInclude || included) && !excluded) {
            try (InputStream is = new FileInputStream(file)) {
                ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath + rootDir.relativize(Paths.get(file.getAbsolutePath())));
                zos.putArchiveEntry(archiveEntry);
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
            } catch (Exception e) {
                CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
            }
        }
    }
}
Also used : TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) 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) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Aggregations

TArtifactReference (org.eclipse.winery.model.tosca.TArtifactReference)2 JAXBException (javax.xml.bind.JAXBException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)1 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)1 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)1 RepositoryCorruptException (org.eclipse.winery.repository.exceptions.RepositoryCorruptException)1