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;
}
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;
}
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);
}
}
Aggregations