Search in sources :

Example 41 with CobiGenRuntimeException

use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.

the class ExternalProcess method downloadExecutable.

/**
 * Downloads the external server on the specified folder (which will normally be .cobigen folder)
 *
 * @param filePath path where the external server should be downloaded to
 * @param fileName name of the external server
 * @return path of the external server
 * @throws IOException {@link IOException} occurred while downloading the file
 */
private String downloadExecutable(String filePath, String fileName) throws IOException {
    String tarFileName = "";
    File exeFile = new File(filePath);
    String parentDirectory = exeFile.getParent();
    URL url = new URL(this.serverDownloadUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.connect();
    // Downloading tar file
    File tarFile;
    Path tarPath;
    LOG.info("Downloading server from {} to {}", this.serverDownloadUrl, filePath);
    try (InputStream inputStream = conn.getInputStream()) {
        tarFileName = conn.getURL().getFile().substring(conn.getURL().getFile().lastIndexOf("/") + 1);
        tarFile = new File(parentDirectory + File.separator + tarFileName);
        tarPath = tarFile.toPath();
        if (!tarFile.exists()) {
            Files.copy(inputStream, tarPath, StandardCopyOption.REPLACE_EXISTING);
        }
    }
    conn.disconnect();
    // Do we have write access?
    if (Files.isWritable(Paths.get(parentDirectory))) {
        LOG.info("Extracting server to users folder...");
        try (FileInputStream in = new FileInputStream(tarPath.toString());
            InputStream is = new GZIPInputStream(in);
            TarArchiveInputStream tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is)) {
            TarArchiveEntry entry;
            while ((entry = tarInputStream.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                if (entry.getName().contains(this.serverVersion)) {
                    // We don't want the directories (src/main/server.exe), we just want to create the
                    // file (server.exe)
                    File targetFile = new File(parentDirectory, fileName);
                    try (FileOutputStream fos = new FileOutputStream(targetFile)) {
                        IOUtils.copy(tarInputStream, fos);
                        fos.flush();
                        // We need to wait until it has finished writing the file
                        fos.getFD().sync();
                        break;
                    }
                }
            }
        } catch (ArchiveException e) {
            throw new CobiGenRuntimeException("Error while extracting the external server.", e);
        }
    } else {
        // We are not able to extract the server
        Files.deleteIfExists(tarPath);
        throw new CobiGenRuntimeException("Enable to extract the external server package. Possibly a corrupt download. Please try again");
    }
    // Remove tar file
    Files.deleteIfExists(tarPath);
    return filePath;
}
Also used : Path(java.nio.file.Path) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) HttpURLConnection(java.net.HttpURLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 42 with CobiGenRuntimeException

use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.

the class ConfigurationFinder method readConfigrationFile.

/**
 * This is a helper method to read a given cobigen configuration file
 *
 * @param cobigenConfigFile cobigen configuration file
 * @return Properties containing configuration
 */
private static Properties readConfigrationFile(Path cobigenConfigFile) {
    Properties props = new Properties();
    try {
        String configFileContents = Files.readAllLines(cobigenConfigFile, Charset.forName("UTF-8")).stream().collect(Collectors.joining("\n"));
        configFileContents = configFileContents.replace("\\", "\\\\");
        try (StringReader strReader = new StringReader(configFileContents)) {
            props.load(strReader);
        }
    } catch (IOException e) {
        throw new CobiGenRuntimeException("An error occured while reading the config file " + cobigenConfigFile, e);
    }
    return props;
}
Also used : CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) StringReader(java.io.StringReader) IOException(java.io.IOException) Properties(java.util.Properties)

Example 43 with CobiGenRuntimeException

use of com.devonfw.cobigen.api.exception.CobiGenRuntimeException in project cobigen by devonfw.

the class ExtractTemplatesUtil method extractTemplates.

/**
 * Extracts templates project to the given path
 *
 * @param extractTo Path to extract the templates into
 * @param forceOverride force to overwrite the contents of the target folder
 * @throws DirectoryNotEmptyException if the given directory is not empty. Can be used to ask for overwriting
 */
public static void extractTemplates(Path extractTo, boolean forceOverride) throws DirectoryNotEmptyException {
    // find templates will also download jars if needed as a side effect and will return the path to the
    // files.
    URI findTemplatesLocation = ConfigurationFinder.findTemplatesLocation();
    if (Files.isDirectory(Paths.get(findTemplatesLocation))) {
        LOG.info("Templates already found at {}. You can edit them in place to adapt your generation results.", extractTo);
        return;
    }
    Objects.requireNonNull(extractTo, "Target path cannot be null");
    if (!Files.isDirectory(extractTo)) {
        try {
            Files.createDirectories(extractTo);
        } catch (IOException e) {
            throw new CobiGenRuntimeException("Unable to create directory " + extractTo);
        }
    }
    try {
        if (!isEmpty(extractTo) && !forceOverride) {
            throw new DirectoryNotEmptyException(extractTo.toString());
        }
        LOG.info("CobiGen is attempting to download the latest CobiGen_Templates.jar and will extract it to cobigen home directory {}. please wait...", ConfigurationConstants.DEFAULT_HOME);
        File templatesDirectory = extractTo.toFile();
        processJar(templatesDirectory.toPath());
        LOG.info("Successfully downloaded and extracted templates to @ {}", templatesDirectory.toPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES));
    } catch (DirectoryNotEmptyException e) {
        throw e;
    } catch (IOException e) {
        throw new CobiGenRuntimeException("Not able to extract templates to " + extractTo, e);
    }
}
Also used : CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File)

Aggregations

CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)43 IOException (java.io.IOException)22 Path (java.nio.file.Path)15 File (java.io.File)8 LoggerFactory (org.slf4j.LoggerFactory)5 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)4 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)4 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)4 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)4 InputStream (java.io.InputStream)4 URL (java.net.URL)4 URLClassLoader (java.net.URLClassLoader)4 CobiGen (com.devonfw.cobigen.api.CobiGen)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)3 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)3 BackupFailedException (com.devonfw.cobigen.impl.exceptions.BackupFailedException)3 Paths (java.nio.file.Paths)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Logger (org.slf4j.Logger)3