Search in sources :

Example 6 with RepositoryException

use of org.phoenicis.repository.RepositoryException in project POL-POM-5 by PlayOnLinux.

the class ClasspathRepository method buildScript.

private ScriptDTO buildScript(String typeId, String categoryId, String applicationId, String typeFileName, String categoryFileName, String applicationFileName, String scriptFileName) throws RepositoryException {
    try {
        final String scriptJsonFile = packagePath + "/" + typeFileName + "/" + categoryFileName + "/" + applicationFileName + "/" + scriptFileName + "/script.json";
        final InputStream scriptJsonInputStream = getClass().getResourceAsStream(scriptJsonFile);
        final InputStream scriptFile = getClass().getResourceAsStream(packagePath + "/" + typeFileName + "/" + categoryFileName + "/" + applicationFileName + "/" + scriptFileName + "/script.js");
        if (scriptJsonInputStream == null) {
            return null;
        }
        URI icon = null;
        final String iconResource = packagePath + "/" + typeFileName + "/" + categoryFileName + "/" + applicationFileName + "/" + scriptFileName + "/icon.png";
        URL iconResourceURL = getClass().getResource(iconResource);
        if (iconResourceURL != null) {
            try {
                icon = iconResourceURL.toURI();
            } catch (URISyntaxException e) {
                LOGGER.debug("Could not get URI of script icon.");
            }
        } else {
            LOGGER.debug("Could not find script icon.");
        }
        ScriptDTO.Builder scriptDTOBuilder = new ScriptDTO.Builder(objectMapper.readValue(scriptJsonInputStream, ScriptDTO.class)).withTypeId(typeId).withCategoryId(categoryId).withApplicationId(applicationId).withScript(new String(IOUtils.toByteArray(scriptFile))).withIcon(icon);
        if (StringUtils.isBlank(scriptDTOBuilder.getId())) {
            if (!StringUtils.isBlank(scriptDTOBuilder.getScriptName())) {
                scriptDTOBuilder.withId(scriptDTOBuilder.getScriptName().replaceAll(INVALID_ID_CHARS_REGEX, ""));
            } else {
                scriptDTOBuilder.withId(scriptFileName.replaceAll(INVALID_ID_CHARS_REGEX, ""));
            }
        }
        return scriptDTOBuilder.build();
    } catch (IOException e) {
        throw new RepositoryException("Could not build script", e);
    }
}
Also used : InputStream(java.io.InputStream) RepositoryException(org.phoenicis.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL)

Example 7 with RepositoryException

use of org.phoenicis.repository.RepositoryException in project POL-POM-5 by PlayOnLinux.

the class ClasspathRepository method buildType.

private TypeDTO buildType(String typeFileName) throws RepositoryException {
    try {
        final String jsonTypePath = packagePath + "/" + typeFileName + "/type.json";
        final URL jsonTypeFile = getClass().getResource(jsonTypePath);
        if (jsonTypeFile != null) {
            final TypeDTO typeDTO = objectMapper.readValue(jsonTypeFile, TypeDTO.class);
            TypeDTO.Builder typeDTOBuilder = new TypeDTO.Builder(typeDTO);
            if (StringUtils.isBlank(typeDTO.getId())) {
                if (!StringUtils.isBlank(typeDTO.getName())) {
                    typeDTOBuilder.withId(typeDTO.getName().replaceAll(INVALID_ID_CHARS_REGEX, ""));
                } else {
                    typeDTOBuilder.withId(typeFileName.replaceAll(INVALID_ID_CHARS_REGEX, ""));
                }
            }
            typeDTOBuilder.withCategories(buildCategories(typeDTOBuilder.getId(), typeFileName)).build();
            try {
                typeDTOBuilder.withIcon(new URI(packagePath + "/" + typeFileName + "/icon.png"));
            } catch (URISyntaxException e) {
                LOGGER.warn("Invalid icon path", e);
            }
            return typeDTOBuilder.build();
        } else {
            LOGGER.debug(String.format("type.json %s for classpath repository does not exist", jsonTypePath));
            return new TypeDTO.Builder().build();
        }
    } catch (IOException e) {
        throw new RepositoryException("Could not build type", e);
    }
}
Also used : ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder) HashCodeBuilder(org.apache.commons.lang.builder.HashCodeBuilder) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) RepositoryException(org.phoenicis.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL)

Example 8 with RepositoryException

use of org.phoenicis.repository.RepositoryException in project POL-POM-5 by PlayOnLinux.

the class GitRepository method cloneOrUpdate.

private void cloneOrUpdate() throws RepositoryException {
    final boolean folderExists = this.localFolder.exists();
    // check that the repository folder exists
    if (!folderExists) {
        LOGGER.info("Creating local folder '{}' for git-repository '{}'", this.localFolder.getAbsolutePath(), this.repositoryUri);
        if (!this.localFolder.mkdirs()) {
            final String message = String.format("Couldn't create local folder '%s' for git-repository '%s'", this.localFolder.getAbsolutePath(), this.repositoryUri);
            throw new RepositoryException(message);
        }
    }
    if (!folderExists) {
        LOGGER.info("Cloning git-repository '{}' to '{}'", this.repositoryUri, this.localFolder.getAbsolutePath());
        try (final Git gitRepository = Git.cloneRepository().setURI(this.repositoryUri.toString()).setDirectory(this.localFolder).setBranch(this.branch).call()) {
            LOGGER.info("Finished cloning git-repository '{}' to '{}'", this.repositoryUri, this.localFolder);
        } catch (GitAPIException e) {
            final String message = String.format("Folder '%s' is no git-repository", this.localFolder.getAbsolutePath());
            throw new RepositoryException(message, e);
        }
    } else {
        LOGGER.info("Opening git-repository at '{}'", this.localFolder.getAbsolutePath());
        // TODO: it might make sense to ensure that our local checkout is not empty / a valid git repository
        try (final Git gitRepository = Git.open(this.localFolder)) {
            LOGGER.info("Pulling new commits to '{}'", this.localFolder.getAbsolutePath());
            gitRepository.pull().call();
            LOGGER.info("Finished pulling new commits to '{}'", this.localFolder.getAbsolutePath());
        } catch (IOException | GitAPIException e) {
            LOGGER.warn("Could not update {0}. Local checkout will be used.", e);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) RepositoryException(org.phoenicis.repository.RepositoryException) IOException(java.io.IOException)

Example 9 with RepositoryException

use of org.phoenicis.repository.RepositoryException in project POL-POM-5 by PlayOnLinux.

the class GitRepository method fetchInstallableApplications.

@Override
public RepositoryDTO fetchInstallableApplications() {
    try {
        cloneOrUpdateWithLock();
        final RepositoryDTO result = this.localRepositoryFactory.createInstance(this.localFolder, this.repositoryUri).fetchInstallableApplications();
        return result;
    } catch (RepositoryException e) {
        final String message = String.format("Could not fetch installable applications for git-repository %s", this.toString());
        throw new RepositoryException(message, e);
    }
}
Also used : RepositoryException(org.phoenicis.repository.RepositoryException) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO)

Example 10 with RepositoryException

use of org.phoenicis.repository.RepositoryException in project POL-POM-5 by PhoenicisOrg.

the class ClasspathRepository method buildApplications.

private List<ApplicationDTO> buildApplications(String typeId, String categoryId, String typeFileName, String categoryFileName) throws RepositoryException {
    try {
        final String categoryScanClassPath = packagePath + "/" + typeFileName + "/" + categoryFileName;
        Resource[] resources = resourceResolver.getResources(categoryScanClassPath + "/*");
        final List<ApplicationDTO> applicationDTOS = new ArrayList<>();
        for (Resource resource : resources) {
            final String fileName = resource.getFilename();
            if (!"icon.png".equals(fileName) && !"category.json".equals(fileName)) {
                final ApplicationDTO application = buildApplication(typeId, categoryId, typeFileName, categoryFileName, fileName);
                if (!application.getScripts().isEmpty()) {
                    applicationDTOS.add(application);
                }
            }
        }
        applicationDTOS.sort(Comparator.comparing(ApplicationDTO::getName));
        return applicationDTOS;
    } catch (IOException e) {
        throw new RepositoryException("Could not build applications", e);
    }
}
Also used : Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) RepositoryException(org.phoenicis.repository.RepositoryException) IOException(java.io.IOException)

Aggregations

RepositoryException (org.phoenicis.repository.RepositoryException)34 IOException (java.io.IOException)32 URI (java.net.URI)12 URISyntaxException (java.net.URISyntaxException)12 URL (java.net.URL)12 ArrayList (java.util.ArrayList)12 Resource (org.springframework.core.io.Resource)12 EqualsBuilder (org.apache.commons.lang.builder.EqualsBuilder)11 HashCodeBuilder (org.apache.commons.lang.builder.HashCodeBuilder)11 ToStringBuilder (org.apache.commons.lang.builder.ToStringBuilder)11 File (java.io.File)9 InputStream (java.io.InputStream)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FileInputStream (java.io.FileInputStream)3 Arrays (java.util.Arrays)3 Comparator (java.util.Comparator)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 IOUtils (org.apache.commons.compress.utils.IOUtils)3 StringUtils (org.apache.commons.lang.StringUtils)3