Search in sources :

Example 51 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class AbstractFileBasedRepository method getContainedFiles.

@Override
public SortedSet<RepositoryFileReference> getContainedFiles(GenericId id) {
    Path dir = this.id2AbsolutePath(id);
    SortedSet<RepositoryFileReference> res = new TreeSet<>();
    if (!Files.exists(dir)) {
        return res;
    }
    assert (Files.isDirectory(dir));
    final OnlyNonHiddenFiles onlyNonHiddenFiles = new OnlyNonHiddenFiles();
    try {
        Files.walk(dir).filter(f -> {
            try {
                return onlyNonHiddenFiles.accept(f);
            } catch (IOException e) {
                LOGGER.debug("Error during crawling", e);
                return false;
            }
        }).map(f -> {
            final Path relativePath = dir.relativize(f.getParent());
            if (relativePath.toString().isEmpty()) {
                return new RepositoryFileReference(id, f.getFileName().toString());
            } else {
                return new RepositoryFileReference(id, relativePath, f.getFileName().toString());
            }
        }).forEach(res::add);
    } catch (IOException e1) {
        LOGGER.debug("Error during crawling", e1);
    }
    return res;
}
Also used : Path(java.nio.file.Path) ConfigurationEvent(org.apache.commons.configuration2.event.ConfigurationEvent) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) NoSuchFileException(java.nio.file.NoSuchFileException) SortedSet(java.util.SortedSet) IdUtil(org.eclipse.winery.model.ids.IdUtil) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) FileTime(java.nio.file.attribute.FileTime) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException) Namespace(org.eclipse.winery.model.ids.Namespace) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) DirectoryStream(java.nio.file.DirectoryStream) GenericId(org.eclipse.winery.model.ids.GenericId) Util(org.eclipse.winery.repository.common.Util) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Collection(java.util.Collection) Constants(org.eclipse.winery.common.Constants) Reader(java.io.Reader) FileSystem(java.nio.file.FileSystem) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) IOUtils(org.apache.commons.io.IOUtils) IRepository(org.eclipse.winery.repository.backend.IRepository) Optional(java.util.Optional) ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) FileSystemProvider(java.nio.file.spi.FileSystemProvider) MediaType(org.apache.tika.mime.MediaType) Constructor(java.lang.reflect.Constructor) TreeSet(java.util.TreeSet) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) XmlId(org.eclipse.winery.model.ids.XmlId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) Files(java.nio.file.Files) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) Configuration(org.apache.commons.configuration2.Configuration) ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) NamespacesId(org.eclipse.winery.model.ids.admin.NamespacesId) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) InputStream(java.io.InputStream) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TreeSet(java.util.TreeSet) IOException(java.io.IOException)

Example 52 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class ImportUtils method getTheImport.

/**
 * SIDE EFFECT: persists the import when something is changed
 */
public static Optional<TImport> getTheImport(IRepository repository, GenericImportId id) {
    Objects.requireNonNull(id);
    TImport theImport;
    boolean needsPersistence = false;
    final TDefinitions definitions = repository.getDefinitions(id);
    if (!repository.exists(id)) {
        return Optional.empty();
    }
    if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
        // definitions exist
        // we have to manually assign our import right
        theImport = definitions.getImport().get(0);
    } else {
        // someone created a new import
        // store it locally
        theImport = (TImport) definitions.getElement();
        // undo the side effect of adding it at the wrong place at TDefinitions
        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
        // add import at the right place
        definitions.getImport().add(theImport);
        // Super class has persisted the definitions
        // We have to persist the new variant
        needsPersistence = true;
    }
    if (theImport.getLocation() == null) {
        // invalid import -- try to synchronize with storage
        SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(id);
        // we are only interested in the non-.definitions
        for (RepositoryFileReference ref : containedFiles) {
            if (!ref.getFileName().endsWith(".definitions")) {
                // associated file found
                // set the filename of the import to the found xsd
                // TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
                theImport.setLocation(ref.getFileName());
                needsPersistence = true;
                break;
            }
        }
    }
    if (needsPersistence) {
        try {
            BackendUtils.persist(repository, id, definitions);
        } catch (IOException e) {
            LOGGER.error("Could not persist changes", e);
        }
    }
    return Optional.of(theImport);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 53 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class Utils method compressTarBallAndAddToArtifact.

public static void compressTarBallAndAddToArtifact(Path tempDirectory, IRepository repository, ArtifactTemplateId generatedArtifactTemplateId, String tarball) throws IOException {
    Path compressedFile = Paths.get(Utils.compressTarFile(tempDirectory.resolve(tarball).toFile()));
    ArtifactTemplateFilesDirectoryId filesDirectoryId = new ArtifactTemplateFilesDirectoryId(generatedArtifactTemplateId);
    try (FileInputStream inputStream = new FileInputStream(compressedFile.toFile())) {
        repository.putContentToFile(new RepositoryFileReference(filesDirectoryId, compressedFile.getFileName().toString()), inputStream, MediaType.parse("application/x-gzip"));
    }
    BackendUtils.synchronizeReferences(repository, generatedArtifactTemplateId);
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) FileInputStream(java.io.FileInputStream)

Example 54 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class GitBasedRepository method hasChangesInFile.

@Override
public boolean hasChangesInFile(DefinitionsChildId id) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
    try (Git git = getGit()) {
        if (!git.status().call().isClean()) {
            List<DiffEntry> diffEntries = git.diff().call();
            List<DiffEntry> entries = diffEntries.stream().filter(item -> item.getNewPath().startsWith(BackendUtils.getPathInsideRepo(ref.getParent()))).collect(Collectors.toList());
            git.getRepository().close();
            return entries.size() > 0;
        }
    } catch (GitAPIException e) {
        LOGGER.trace(e.getMessage(), e);
    } catch (IOException e) {
        LOGGER.error("Error initializing Git.", e);
    }
    return false;
}
Also used : IWrappingRepository(org.eclipse.winery.repository.backend.IWrappingRepository) Status(org.eclipse.jgit.api.Status) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) FileTime(java.nio.file.attribute.FileTime) OTTopologyFragmentRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) OTComplianceRule(org.eclipse.winery.model.tosca.extensions.OTComplianceRule) ResetCommand(org.eclipse.jgit.api.ResetCommand) Map(java.util.Map) GenericId(org.eclipse.winery.model.ids.GenericId) Path(java.nio.file.Path) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TEntityType(org.eclipse.winery.model.tosca.TEntityType) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Constants(org.eclipse.winery.common.Constants) CommitCommand(org.eclipse.jgit.api.CommitCommand) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) NoWorkTreeException(org.eclipse.jgit.errors.NoWorkTreeException) QName(javax.xml.namespace.QName) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) Iterables(com.google.common.collect.Iterables) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TopologyFragmentRefinementModelId(org.eclipse.winery.model.ids.extensions.TopologyFragmentRefinementModelId) MediaType(org.apache.tika.mime.MediaType) GitBasedRepositoryConfiguration(org.eclipse.winery.common.configuration.GitBasedRepositoryConfiguration) ArrayList(java.util.ArrayList) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) TestRefinementModelId(org.eclipse.winery.model.ids.extensions.TestRefinementModelId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) CleanCommand(org.eclipse.jgit.api.CleanCommand) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) PushCommand(org.eclipse.jgit.api.PushCommand) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Configuration(org.apache.commons.configuration2.Configuration) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Repository(org.eclipse.jgit.lib.Repository) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) PullCommand(org.eclipse.jgit.api.PullCommand) SortedSet(java.util.SortedSet) Date(java.util.Date) ComplianceRuleId(org.eclipse.winery.model.ids.extensions.ComplianceRuleId) LoggerFactory(org.slf4j.LoggerFactory) Namespace(org.eclipse.winery.model.ids.Namespace) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) AddCommand(org.eclipse.jgit.api.AddCommand) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) OTTestRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTestRefinementModel) ResetType(org.eclipse.jgit.api.ResetCommand.ResetType) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Collection(java.util.Collection) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Collectors(java.util.stream.Collectors) List(java.util.List) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) GenericImportId(org.eclipse.winery.model.ids.definitions.imports.GenericImportId) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) Ref(org.eclipse.jgit.lib.Ref) Optional(java.util.Optional) ListBranchCommand(org.eclipse.jgit.api.ListBranchCommand) RmCommand(org.eclipse.jgit.api.RmCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) EventBus(com.google.common.eventbus.EventBus) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) Environments(org.eclipse.winery.common.configuration.Environments) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) HasInheritanceId(org.eclipse.winery.model.ids.definitions.HasInheritanceId) RefinementId(org.eclipse.winery.model.ids.extensions.RefinementId) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) InputStream(java.io.InputStream) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) IOException(java.io.IOException) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 55 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getMapFromLocalNameToXSD.

@Override
public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final Namespace namespace, final boolean getTypes) {
    Set<XSDImportId> importsOfNamespace = this.getImportsOfNamespace(namespace);
    Map<String, RepositoryFileReference> result = new HashMap<>();
    for (XSDImportId imp : importsOfNamespace) {
        final List<String> allDefinedLocalNames = this.getAllDefinedLocalNames(namespace, getTypes);
        Optional<RepositoryFileReference> ref = getXsdFileReference(imp);
        if (!ref.isPresent()) {
            LOGGER.error("Ref is not defined");
        } else {
            for (String localName : allDefinedLocalNames) {
                result.put(localName, ref.get());
            }
        }
    }
    return result;
}
Also used : XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) HashMap(java.util.HashMap)

Aggregations

RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)79 IOException (java.io.IOException)33 Path (java.nio.file.Path)24 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)17 QName (javax.xml.namespace.QName)14 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)14 InputStream (java.io.InputStream)13 ArrayList (java.util.ArrayList)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)9 Test (org.junit.jupiter.api.Test)9 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)8 PlanId (org.eclipse.winery.model.ids.elements.PlanId)8 PlansId (org.eclipse.winery.model.ids.elements.PlansId)8 IRepository (org.eclipse.winery.repository.backend.IRepository)8 MediaType (org.apache.tika.mime.MediaType)7 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 BufferedInputStream (java.io.BufferedInputStream)6 HashMap (java.util.HashMap)6 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)6