aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
new file mode 100644
index 0000000..c98cb59
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
@@ -0,0 +1,176 @@
+package at.gv.egiz.pdfas.utils;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import at.gv.egiz.pdfas.api.commons.Constants;
+import at.gv.egiz.pdfas.api.exceptions.ConfigUtilsException;
+
+/**
+ * @author <a href="mailto:thomas.knall@egiz.gv.at">Thomas Knall</a>
+ */
+public final class ConfigUtils {
+
+ private ConfigUtils() {
+ }
+
+ /**
+ * The log.
+ */
+ private static final Log logger_ = LogFactory.getLog(ConfigUtils.class);
+
+ /**
+ * Deploys the default configuration with apache commons vfs.
+ *
+ * @param destination The destination folder.
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ */
+ /*
+ private static boolean deployWithCommonsVFS(String destination, boolean overwriteExisting) throws ConfigUtilsException {
+ try {
+ FileSystemManager fsManager = VFS.getManager();
+ FileObject defaultConfigurationFile = fsManager.resolveFile("res:DefaultConfiguration");
+ FileObject destinationFile = fsManager.resolveFile(destination);
+
+ if (destinationFile.exists() && !overwriteExisting) {
+ return false;
+ }
+
+ destinationFile.copyFrom(defaultConfigurationFile, new AllFileSelector());
+ return true;
+ } catch (FileSystemException e) {
+ throw new ConfigUtilsException(e);
+ }
+ }
+ */
+
+
+ /**
+ * Deploys the default configuration from an included zip file.
+ *
+ * @param destination The destination folder.
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ */
+ private static boolean deployFromZIP(String destination, boolean overwriteExisting) throws ConfigUtilsException {
+ try {
+ File destinationFolder = new File(destination);
+ if (destinationFolder.exists() && !overwriteExisting) {
+ return false;
+ }
+ InputStream in = ConfigUtils.class.getClassLoader().getResourceAsStream(Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE);
+ if (in == null) {
+ throw new ConfigUtilsException("Unable to find default configuration resource \"" + Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE + "\".");
+ }
+ ZipInputStream zis = new ZipInputStream(in);
+ ZipEntry ze;
+ destinationFolder.mkdirs();
+ logger_.debug("Extracting default configuration to folder \"" + destinationFolder.getCanonicalPath() + "\".");
+ while ((ze = zis.getNextEntry()) != null) {
+ if (ze.isDirectory()) {
+ File newFolder = new File(destinationFolder, ze.getName());
+ logger_.debug("Extracting folder \"" + newFolder.getPath() + "\".");
+ newFolder.mkdirs();
+ } else {
+ File destFile = new File(destinationFolder, ze.getName());
+ logger_.trace("Extracting file \"" + destFile.getName() + "\".");
+ toFile(zis, destFile);
+ }
+ zis.closeEntry();
+ }
+ zis.close();
+ return true;
+ } catch (IOException e) {
+ throw new ConfigUtilsException(e);
+ }
+ }
+
+ /**
+ * Deploys the default configuration to the given destination folder.
+ *
+ * @param destination The destination folder.
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ */
+ public static boolean deployDefaultConfiguration(String destination, boolean overwriteExisting) throws ConfigUtilsException {
+ if (destination == null) {
+ throw new NullPointerException("Destination must not be null.");
+ }
+ if (destination.length() == 0) {
+ throw new IllegalArgumentException("Destination must not be empty.");
+ }
+ return deployFromZIP(destination, overwriteExisting);
+ }
+
+ /**
+ * Deploys the default configuration to the user's home directory to the subdirectory specified by
+ * <code>Constants.Constants.USERHOME_CONFIG_FOLDER</code>.
+ *
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ * @see Constants#USERHOME_CONFIG_FOLDER
+ */
+ public static boolean deployDefaultConfiguration(boolean overwriteExisting) throws ConfigUtilsException {
+ String userHome = System.getProperty("user.home");
+ if (userHome == null || userHome.length() == 0) {
+ return false;
+ }
+ return deployDefaultConfiguration(userHome + File.separator + Constants.USERHOME_CONFIG_FOLDER, overwriteExisting);
+ }
+
+ /**
+ * Deploys the default configuration to the user's home directory to the subdirectory specified by
+ * <code>Constants.Constants.USERHOME_CONFIG_FOLDER</code>.
+ *
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ * @see Constants#USERHOME_CONFIG_FOLDER
+ */
+ public static boolean deployDefaultConfiguration() throws ConfigUtilsException {
+ return deployDefaultConfiguration(false);
+ }
+
+ public static boolean toFile(InputStream inputStream, File file) throws IOException {
+ boolean result = false;
+ BufferedOutputStream bufferedOutputStream = null;
+ try {
+ bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
+ writeInputStreamToOutputStream(inputStream, bufferedOutputStream);
+ } finally {
+ if (bufferedOutputStream != null) {
+ try {
+ bufferedOutputStream.close();
+ result = true;
+ } catch (IOException e) {
+ result = false;
+ }
+ }
+ }
+ return result;
+ }
+
+ public static void writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream) throws IOException {
+ final int bufferSize = 1024;
+ byte[] buffer = new byte[bufferSize];
+ int len = -1;
+ while ((len = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, len);
+ }
+ outputStream.flush();
+ }
+
+}