Search in sources :

Example 6 with RepositoryFileReference

use of org.eclipse.winery.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(GenericImportId id) {
    Objects.requireNonNull(id);
    TImport theImport;
    boolean needsPersistence = false;
    final IRepository repository = RepositoryFactory.getRepository();
    final Definitions 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(id, definitions);
        } catch (IOException e) {
            LOGGER.error("Could not persist changes", e);
        }
    }
    return Optional.of(theImport);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Definitions(org.eclipse.winery.model.tosca.Definitions) TImport(org.eclipse.winery.model.tosca.TImport) IOException(java.io.IOException)

Example 7 with RepositoryFileReference

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

the class SelfServiceMetaDataUtils method ensureDataXmlExists.

public static void ensureDataXmlExists(SelfServiceMetaDataId id) throws IOException {
    RepositoryFileReference data_xml_ref = getDataXmlRef(id);
    if (!RepositoryFactory.getRepository().exists(data_xml_ref)) {
        final Application application = new Application();
        BackendUtils.persist(application, data_xml_ref, MediaTypes.MEDIATYPE_TEXT_XML);
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Application(org.eclipse.winery.model.selfservice.Application)

Example 8 with RepositoryFileReference

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

the class FilebasedRepository 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(ref -> res.add(ref));
    } catch (IOException e1) {
        LOGGER.debug("Error during crawling", e1);
    }
    return res;
}
Also used : Configuration(org.apache.commons.configuration.Configuration) ZipOutputStream(java.util.zip.ZipOutputStream) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) Definitions(org.eclipse.winery.model.tosca.Definitions) java.util(java.util) ZipInputStream(java.util.zip.ZipInputStream) ToscaElementId(org.eclipse.winery.common.ids.elements.ToscaElementId) LoggerFactory(org.slf4j.LoggerFactory) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException) FileTime(java.nio.file.attribute.FileTime) MediaType(org.apache.tika.mime.MediaType) FileSystemProvider(java.nio.file.spi.FileSystemProvider) Constructor(java.lang.reflect.Constructor) XmlId(org.eclipse.winery.common.ids.XmlId) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) java.nio.file(java.nio.file) Constants(org.eclipse.winery.repository.Constants) Charset(java.nio.charset.Charset) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager) NamespacesId(org.eclipse.winery.common.ids.admin.NamespacesId) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ZipEntry(java.util.zip.ZipEntry) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) FileBasedRepositoryConfiguration(org.eclipse.winery.repository.configuration.FileBasedRepositoryConfiguration) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Logger(org.slf4j.Logger) RepositoryBasedXsdImportManager(org.eclipse.winery.repository.backend.xsd.RepositoryBasedXsdImportManager) SystemUtils(org.apache.commons.lang3.SystemUtils) GenericId(org.eclipse.winery.common.ids.GenericId) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) FileSystem(java.nio.file.FileSystem) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Util(org.eclipse.winery.common.Util) org.eclipse.winery.repository.backend(org.eclipse.winery.repository.backend) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOUtils(org.apache.commons.io.IOUtils) java.io(java.io) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) MediaTypes(org.eclipse.winery.repository.backend.constants.MediaTypes) Namespace(org.eclipse.winery.common.ids.Namespace) ConfigurationException(org.apache.commons.configuration.ConfigurationException) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 9 with RepositoryFileReference

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

the class FilebasedRepository method rename.

@Override
public void rename(DefinitionsChildId oldId, DefinitionsChildId newId) throws IOException {
    Objects.requireNonNull(oldId);
    Objects.requireNonNull(newId);
    if (oldId.equals(newId)) {
        // we do not do anything - even not throwing an error
        return;
    }
    Definitions definitions = this.getDefinitions(oldId);
    RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
    RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
    // oldRef points to the definitions file,
    // getParent() returns the directory
    // we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
    File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
    File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
    org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
    // Update definitions and store it
    // This also updates the definitions of componentInstanceResource
    BackendUtils.updateWrapperDefinitions(newId, definitions);
    // This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
    // newId has to be passed, because the id is final at AbstractComponentInstanceResource
    BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
    try {
        BackendUtils.persist(definitions, newRef, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
    } catch (InvalidPathException e) {
        LOGGER.debug("Invalid path during write", e);
    // QUICK FIX
    // Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
    // We just ignore it for now
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Definitions(org.eclipse.winery.model.tosca.Definitions) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException)

Example 10 with RepositoryFileReference

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

the class CsarExporter method addSelfServiceMetaData.

/**
 * Adds all self service meta data to the targetDir
 *
 * @param targetDir the directory in the CSAR where to put the content to
 * @param refMap    is used later to create the CSAR
 */
private void addSelfServiceMetaData(IRepository repository, ServiceTemplateId entryId, String targetDir, Map<RepositoryFileReference, String> refMap) throws IOException {
    final SelfServiceMetaDataId selfServiceMetaDataId = new SelfServiceMetaDataId(entryId);
    // This method is also called if the directory SELFSERVICE-Metadata exists without content and even if the directory does not exist at all,
    // but the ServiceTemplate itself exists.
    // The current assumption is that this is enough for an existence.
    // Thus, we have to take care of the case of an empty directory and add a default data.xml
    SelfServiceMetaDataUtils.ensureDataXmlExists(selfServiceMetaDataId);
    refMap.put(SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), targetDir + "data.xml");
    // The schema says that the images have to exist
    // However, at a quick modeling, there might be no images
    // Therefore, we check for existence
    final RepositoryFileReference iconJpgRef = SelfServiceMetaDataUtils.getIconJpgRef(selfServiceMetaDataId);
    if (repository.exists(iconJpgRef)) {
        refMap.put(iconJpgRef, targetDir + "icon.jpg");
    }
    final RepositoryFileReference imageJpgRef = SelfServiceMetaDataUtils.getImageJpgRef(selfServiceMetaDataId);
    if (repository.exists(imageJpgRef)) {
        refMap.put(imageJpgRef, targetDir + "image.jpg");
    }
    Application application = SelfServiceMetaDataUtils.getApplication(selfServiceMetaDataId);
    // clear CSAR name as this may change.
    application.setCsarName(null);
    // hack for the OpenTOSCA container to display something
    application.setVersion("1.0");
    List<String> authors = application.getAuthors();
    if (authors.isEmpty()) {
        authors.add("Winery");
    }
    // make the patches to data.xml permanent
    try {
        BackendUtils.persist(application, SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), MediaTypes.MEDIATYPE_TEXT_XML);
    } catch (IOException e) {
        LOGGER.error("Could not persist patches to data.xml", e);
    }
    Options options = application.getOptions();
    if (options != null) {
        SelfServiceMetaDataId id = new SelfServiceMetaDataId(entryId);
        for (ApplicationOption option : options.getOption()) {
            String url = option.getIconUrl();
            if (Util.isRelativeURI(url)) {
                putRefIntoRefMap(targetDir, refMap, repository, id, url);
            }
            url = option.getPlanInputMessageUrl();
            if (Util.isRelativeURI(url)) {
                putRefIntoRefMap(targetDir, refMap, repository, id, url);
            }
        }
    }
}
Also used : Options(org.eclipse.winery.model.selfservice.Application.Options) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ApplicationOption(org.eclipse.winery.model.selfservice.ApplicationOption) SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId) Application(org.eclipse.winery.model.selfservice.Application)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4