summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow
diff options
context:
space:
mode:
authorTobias Kellner <tobias.kellner@iaik.tugraz.at>2014-11-27 21:00:57 +0100
committerTobias Kellner <tobias.kellner@iaik.tugraz.at>2014-11-27 21:01:08 +0100
commitc5601292f85bc02ca277c04ef6bbae938d07a335 (patch)
treeff101def1a2cf30e73dcf047cdabdd6d3f7b1d36 /pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow
parent9e50f1a103a37d9dcfbeb5ea45bcf6c6c0c0f6dd (diff)
downloadpdf-over-c5601292f85bc02ca277c04ef6bbae938d07a335.tar.gz
pdf-over-c5601292f85bc02ca277c04ef6bbae938d07a335.tar.bz2
pdf-over-c5601292f85bc02ca277c04ef6bbae938d07a335.zip
Backup old PDF-AS configuration, create new
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java338
1 files changed, 220 insertions, 118 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java
index 70895bfb..3e5e7a2c 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareConfigurationState.java
@@ -17,12 +17,17 @@ package at.asit.pdfover.gui.workflow.states;
//Imports
import java.awt.Desktop;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.URI;
import org.apache.commons.httpclient.HttpClient;
@@ -56,14 +61,14 @@ import at.asit.pdfover.gui.controls.Dialog.ICON;
import at.asit.pdfover.gui.controls.ErrorDialog;
import at.asit.pdfover.gui.exceptions.InitializationException;
import at.asit.pdfover.gui.utils.Messages;
-import at.asit.pdfover.gui.utils.Unzipper;
import at.asit.pdfover.gui.utils.VersionComparator;
+import at.asit.pdfover.gui.utils.Zipper;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.signator.Signator;
/**
* Starting state of workflow proccess
- *
+ *
* Reads configuration, command arguments and initializes configured variables
*/
public class PrepareConfigurationState extends State {
@@ -148,128 +153,158 @@ public class PrepareConfigurationState extends State {
}
}
- private void createConfiguration(File configDir) throws InitializationException {
- boolean allOK = false;
-
- log.info("Creating configuration directory"); //$NON-NLS-1$
+ private void copyPdfOverConfig() throws InitializationException {
+ // 1Kb buffer
+ byte[] buffer = new byte[1024];
+ int byteCount = 0;
+ InputStream inputStream = null;
+ FileOutputStream pdfOverConfig = null;
try {
- if (!configDir.exists()) {
- configDir.mkdir();
+ inputStream = this.getClass().getResourceAsStream(
+ Constants.RES_PKG_PATH + Constants.DEFAULT_CONFIG_FILENAME);
+ pdfOverConfig = new FileOutputStream(
+ getStateMachine().getConfigProvider().getConfigurationDirectory() +
+ FILE_SEPARATOR + Constants.DEFAULT_CONFIG_FILENAME);
+
+ while ((byteCount = inputStream.read(buffer)) >= 0) {
+ pdfOverConfig.write(buffer, 0, byteCount);
}
- // Copy PDFOver config to config Dir
-
- // 1Kb buffer
- byte[] buffer = new byte[1024];
- int byteCount = 0;
-
- InputStream inputStream = null;
- FileOutputStream pdfOverConfig = null;
- try {
- inputStream = this.getClass().getResourceAsStream(
- Constants.RES_PKG_PATH + Constants.DEFAULT_CONFIG_FILENAME);
- pdfOverConfig = new FileOutputStream(
- getStateMachine().getConfigProvider().getConfigurationDirectory() +
- FILE_SEPARATOR + Constants.DEFAULT_CONFIG_FILENAME);
-
- while ((byteCount = inputStream.read(buffer)) >= 0) {
- pdfOverConfig.write(buffer, 0, byteCount);
- }
- } catch (Exception e) {
- log.error(
- "Failed to write PDF Over config file to config directory", e); //$NON-NLS-1$
- throw new InitializationException(
- "Failed to write PDF Over config file to config directory", //$NON-NLS-1$
- e);
- } finally {
- if (pdfOverConfig != null) {
- try {
- pdfOverConfig.close();
- } catch (IOException e) {
- log.warn(
- "Failed to close File stream for PDFOver config", e); //$NON-NLS-1$
- }
+ } catch (Exception e) {
+ log.error(
+ "Failed to write PDF Over config file to config directory", e); //$NON-NLS-1$
+ throw new InitializationException(
+ "Failed to write PDF Over config file to config directory", //$NON-NLS-1$
+ e);
+ } finally {
+ if (pdfOverConfig != null) {
+ try {
+ pdfOverConfig.close();
+ } catch (IOException e) {
+ log.warn(
+ "Failed to close File stream for PDFOver config", e); //$NON-NLS-1$
}
+ }
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- log.warn(
- "Failed to close Resource stream for PDFOver config", e); //$NON-NLS-1$
- }
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.warn(
+ "Failed to close Resource stream for PDFOver config", e); //$NON-NLS-1$
}
}
+ }
+ }
- inputStream = null;
- pdfOverConfig = null;
- try {
- inputStream = this.getClass().getResourceAsStream(
- Constants.RES_PKG_PATH + Constants.DEFAULT_LOG4J_FILENAME);
- String filename = getStateMachine().getConfigProvider().getConfigurationDirectory()
- + FILE_SEPARATOR + Constants.DEFAULT_LOG4J_FILENAME;
- pdfOverConfig = new FileOutputStream(filename);
-
- while ((byteCount = inputStream.read(buffer)) >= 0) {
- pdfOverConfig.write(buffer, 0, byteCount);
- }
+ private void copyLog4jConfig() throws InitializationException {
+ // 1Kb buffer
+ byte[] buffer = new byte[1024];
+ int byteCount = 0;
- PropertyConfigurator.configureAndWatch(filename);
- } catch (Exception e) {
- log.error(
- "Failed to write log4j config file to config directory", e); //$NON-NLS-1$
- throw new InitializationException(
- "Failed to write log4j config file to config directory", //$NON-NLS-1$
- e);
- } finally {
- if (pdfOverConfig != null) {
- try {
- pdfOverConfig.close();
- } catch (IOException e) {
- log.warn(
- "Failed to close File stream for log4j config", e); //$NON-NLS-1$
- }
+ InputStream inputStream = null;
+ FileOutputStream pdfOverConfig = null;
+ try {
+ inputStream = this.getClass().getResourceAsStream(
+ Constants.RES_PKG_PATH + Constants.DEFAULT_LOG4J_FILENAME);
+ String filename = getStateMachine().getConfigProvider().getConfigurationDirectory()
+ + FILE_SEPARATOR + Constants.DEFAULT_LOG4J_FILENAME;
+ pdfOverConfig = new FileOutputStream(filename);
+
+ while ((byteCount = inputStream.read(buffer)) >= 0) {
+ pdfOverConfig.write(buffer, 0, byteCount);
+ }
+
+ PropertyConfigurator.configureAndWatch(filename);
+ } catch (Exception e) {
+ log.error(
+ "Failed to write log4j config file to config directory", e); //$NON-NLS-1$
+ throw new InitializationException(
+ "Failed to write log4j config file to config directory", //$NON-NLS-1$
+ e);
+ } finally {
+ if (pdfOverConfig != null) {
+ try {
+ pdfOverConfig.close();
+ } catch (IOException e) {
+ log.warn(
+ "Failed to close File stream for log4j config", e); //$NON-NLS-1$
}
-
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- log.warn(
- "Failed to close Resource stream for log4j config", e); //$NON-NLS-1$
- }
+ }
+
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.warn(
+ "Failed to close Resource stream for log4j config", e); //$NON-NLS-1$
}
}
-
- InputStream is = this.getClass().getResourceAsStream(
- Constants.RES_CFG_ZIP);
+ }
+ }
+
+ private void unzipPdfAsConfig(File configDir) throws InitializationException {
+ InputStream is = this.getClass().getResourceAsStream(Constants.RES_CFG_ZIP);
+
+ try {
+ Zipper.unzip(is, configDir.getAbsolutePath());
+ } catch (IOException e) {
+ log.error(
+ "Failed to create local configuration directory!", e); //$NON-NLS-1$
+ throw new InitializationException(
+ "Failed to create local configuration directory!", //$NON-NLS-1$
+ e);
+ }
+ }
+
+ private static void updateVersionFile(File configDir) throws InitializationException {
+ File versionFile = new File(configDir, Constants.CONFIG_VERSION_FILENAME);
+ try {
+ BufferedWriter versionWriter = new BufferedWriter(new FileWriter(versionFile));
+ String version = Constants.APP_VERSION == null ? "Unknown" : Constants.APP_VERSION; //$NON-NLS-1$
+ versionWriter.write(version);
+ versionWriter.close();
+ } catch (IOException e) {
+ log.error(
+ "Failed to create configuration version file!", e); //$NON-NLS-1$
+ throw new InitializationException(
+ "Failed to create configuration version file!", //$NON-NLS-1$
+ e);
+ }
+ }
+
+ private void initializeConfig() throws InitializationException {
+ this.initializeFromConfigurationFile(getStateMachine()
+ .getConfigProvider().getConfigurationFile());
+
+ getStateMachine().getConfigManipulator().setSignatureNote(Messages.getString("simple_config.Note_Default")); //$NON-NLS-1$
+
+ try {
+ getStateMachine().getConfigManipulator().saveCurrentConfiguration();
+ } catch (IOException e) {
+ log.error(
+ "Failed to set local configuration signature note!", e); //$NON-NLS-1$
+ throw new InitializationException(
+ "Failed to set local configuration signature note!", //$NON-NLS-1$
+ e);
+ }
+ }
+
+ private void createConfiguration(File configDir) throws InitializationException {
+ boolean allOK = false;
+
+ log.info("Creating configuration directory"); //$NON-NLS-1$
+ if (!configDir.exists()) {
+ configDir.mkdir();
+ }
+
+ try {
+ copyPdfOverConfig();
+ copyLog4jConfig();
+ unzipPdfAsConfig(configDir);
+ updateVersionFile(configDir);
+ initializeConfig();
- try {
- Unzipper.unzip(is, configDir.getAbsolutePath());
- } catch (IOException e) {
- log.error(
- "Failed to create local configuration directory!", e); //$NON-NLS-1$
- throw new InitializationException(
- "Failed to create local configuration directory!", //$NON-NLS-1$
- e);
- }
-
- // initialize from config file
- this.initializeFromConfigurationFile(getStateMachine()
- .getConfigProvider().getConfigurationFile());
-
- getStateMachine().getConfigManipulator().setSignatureNote(Messages.getString("simple_config.Note_Default")); //$NON-NLS-1$
-
- try {
- getStateMachine().getConfigManipulator().saveCurrentConfiguration();
- } catch (IOException e) {
- log.error(
- "Failed to set local configuration signature note!", e); //$NON-NLS-1$
- throw new InitializationException(
- "Failed to set local configuration signature note!", //$NON-NLS-1$
- e);
- }
-
allOK = true;
} finally {
if (!allOK) {
@@ -278,21 +313,88 @@ public class PrepareConfigurationState extends State {
}
}
+ /**
+ * @return The first valid (not empty, non comment) line of the version file
+ * or null if version file cannot be read or does not contain
+ * such a line.
+ */
+ private static String getVersion(File versionFile) {
+ if (versionFile.exists() && versionFile.canRead()) {
+ BufferedReader versionReader = null;
+ try {
+ versionReader = new BufferedReader(new FileReader(versionFile));
+ String version;
+ while ((version = versionReader.readLine()) != null) {
+ version = version.trim();
+ if (version.length() > 0 && !version.startsWith("#")) { //$NON-NLS-1$
+ log.trace("configuration version from " + versionFile //$NON-NLS-1$
+ + ": " + version); //$NON-NLS-1$
+ return version;
+ }
+ }
+ } catch (IOException ex) {
+ log.error("failed to read configuration version from " //$NON-NLS-1$
+ + versionFile, ex);
+ } finally {
+ try {
+ versionReader.close();
+ } catch (IOException ex) {
+ // ignore
+ }
+ }
+ }
+ log.debug("unknown configuration version"); //$NON-NLS-1$
+ return null;
+ }
+
+ /**
+ * Backup old configuration, create new
+ * @param configDir
+ * @throws InitializationException
+ */
+ private void backupAndCreatePdfAsConfiguration(File configDir) throws InitializationException {
+ try {
+ File backup = File.createTempFile(Constants.PDF_AS_CONFIG_BACKUP_FILENAME, ".zip"); //$NON-NLS-1$
+ OutputStream os = new FileOutputStream(backup);
+ Zipper.zip(configDir + FILE_SEPARATOR + "cfg", os, true); //$NON-NLS-1$
+ os.close();
+ unzipPdfAsConfig(configDir);
+ File b = new File(configDir, Constants.PDF_AS_CONFIG_BACKUP_FILENAME + ".zip"); //$NON-NLS-1$
+ int i = 1;
+ while (b.exists()) {
+ b = new File(configDir, Constants.PDF_AS_CONFIG_BACKUP_FILENAME + i++ + ".zip"); //$NON-NLS-1$
+ }
+ backup.renameTo(b);
+ updateVersionFile(configDir);
+ } catch (FileNotFoundException e) {
+ log.error("Backup file not found", e); //$NON-NLS-1$
+ throw new InitializationException("Backup file not found", e); //$NON-NLS-1$
+ } catch (IOException e) {
+ log.error("Error creating configuration backup", e); //$NON-NLS-1$
+ throw new InitializationException("Error creating configuration backup", e); //$NON-NLS-1$
+ }
+ }
+
@Override
public void run() {
// Read config file
try {
-
- File configDir = new File(getStateMachine().getConfigProvider().getConfigurationDirectory());
- File configFile = new File(getStateMachine().getConfigProvider().getConfigurationDirectory()
- + FILE_SEPARATOR + Constants.DEFAULT_CONFIG_FILENAME);
+ String cDir = getStateMachine().getConfigProvider().getConfigurationDirectory();
+ File configDir = new File(cDir);
+ File configFile = new File(configDir, Constants.DEFAULT_CONFIG_FILENAME);
if (!configDir.exists() || !configFile.exists()) {
log.debug("Creating configuration file"); //$NON-NLS-1$
createConfiguration(configDir);
} else {
log.debug("Configuration directory exists!"); //$NON-NLS-1$
+ // Check PDF-AS config version
+ File versionFile = new File(configDir, Constants.CONFIG_VERSION_FILENAME);
+ String configVersion = getVersion(versionFile);
+ if (configVersion == null || VersionComparator.before(configVersion, Constants.MIN_PDF_AS_CONFIG_VERSION))
+ backupAndCreatePdfAsConfiguration(configDir);
}
+
// Read cli arguments for config file first
try {
this.initializeFromArguments(getStateMachine().getCmdArgs(),
@@ -385,7 +487,7 @@ public class PrepareConfigurationState extends State {
} catch (InitializationException e) {
log.error("Failed to initialize: ", e); //$NON-NLS-1$
ErrorDialog error = new ErrorDialog(getStateMachine()
- .getGUIProvider().getMainShell(),
+ .getGUIProvider().getMainShell(),
Messages.getString("error.Initialization"), //$NON-NLS-1$
BUTTONS.OK);
// error.setException(e);
@@ -397,7 +499,7 @@ public class PrepareConfigurationState extends State {
/*
* (non-Javadoc)
- *
+ *
* @see at.asit.pdfover.gui.workflow.states.State#cleanUp()
*/
@Override
@@ -407,7 +509,7 @@ public class PrepareConfigurationState extends State {
/*
* (non-Javadoc)
- *
+ *
* @see at.asit.pdfover.gui.workflow.states.State#setMainWindowBehavior()
*/
@Override