From 29ad090c29567ff1a4d3a2ec9b8ad0b5d80ee24d Mon Sep 17 00:00:00 2001 From: pdanner Date: Mon, 6 Dec 2010 16:34:52 +0000 Subject: git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@671 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../egiz/pdfas/web/helper/WebSettingsReader.java | 210 +++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/WebSettingsReader.java (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/WebSettingsReader.java') diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/WebSettingsReader.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/WebSettingsReader.java new file mode 100644 index 0000000..c7f6b7e --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/helper/WebSettingsReader.java @@ -0,0 +1,210 @@ +package at.gv.egiz.pdfas.web.helper; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +import org.apache.commons.lang.text.StrSubstitutor; +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; +import at.gv.egiz.pdfas.utils.ConfigUtils; +import at.knowcenter.wag.egov.egiz.exceptions.SettingsException; + +/** + * + * @author exthex + * + */ +public class WebSettingsReader { + + /** + * The configuration key that replaces a dynamically generated retrieve signature data url. + */ + private final static String RETRIEVE_SIGNATURE_DATA_URL_OVERRIDE_KEY = "retrieve_signature_data_url_override"; + + private final static String SIGNING_TIME_TOLERANCE_KEY = "signing_time_tolerance"; + + private static final Log logger_ = LogFactory.getLog(WebSettingsReader.class); + + public static String RESOURCES_PATH = null; + + private static String TMP_PATH = null; + + private static String CONFIG_PATH; + + private static String CFG = "cfg"; + + private String WEB_CONFIG_FILE_DEFAULT_NAME = "pdf-as-web.properties"; + + public static synchronized void initialize(String work_dir) { + + String tmpDir = null; + + String defaultConfigDeployedTo = null; + // resolve work directory + // configuration explicitely given ? + if (work_dir == null) { + + // configuration via system property ? + logger_.debug("No configuration directory given. Looking for system property \"" + + Constants.CONFIG_DIR_SYSTEM_PROPERTY + "\"."); + work_dir = System.getProperty(Constants.CONFIG_DIR_SYSTEM_PROPERTY); + if (work_dir == null) { + + // configuration via user's home directory ? + logger_ + .debug("System property not set. Trying to locate configuration within the user's home directory."); + String userHome = System.getProperty("user.home"); + if (userHome == null || userHome.length() == 0) { + throw new RuntimeException("Unable to resolve user's home directory."); + } + work_dir = ConfigUtils.assertFileSeparator(userHome) + Constants.USERHOME_CONFIG_FOLDER; + try { + defaultConfigDeployedTo = ConfigUtils.deployDefaultConfiguration(work_dir, false); + } catch (ConfigUtilsException e) { + throw new RuntimeException(e); + } + if (defaultConfigDeployedTo != null) { + logger_.info("** Default configuration successfully deployed to \"" + + defaultConfigDeployedTo + "\" **"); + } else { + logger_ + .debug("Default configuration has NOT been deployed. Maybe the configuration already exists."); + } + } else { + logger_.debug("Configuration set by system property."); + tmpDir = work_dir; + } + } else { + logger_.debug("Configuration path explicitely set."); + } + File configdirFile = new File(StrSubstitutor.replaceSystemProperties(work_dir)); + try { + work_dir = ConfigUtils.assertFileSeparator(configdirFile.getCanonicalPath()); + } catch (IOException e) { + work_dir = ConfigUtils.assertFileSeparator(configdirFile.getPath()); + } + if (!configdirFile.isDirectory()) { + throw new IllegalArgumentException("The config directory \"" + work_dir + + "\" does not exist or is not a directory."); + } + + // resolve temporary dir + if (tmpDir == null) { + logger_ + .debug("Temporary directory not explicitely set. Looking for user's temp directory."); + tmpDir = System.getProperty("java.io.tmpdir"); + if (tmpDir == null) { + logger_ + .debug("Unable to resolve user's temporary directory. Assuming temporary directory located within config dir."); + tmpDir = work_dir; + } + } else { + logger_.debug("Temporary directory explicitely set."); + } + File tmpdirFile = new File(StrSubstitutor.replaceSystemProperties(ConfigUtils + .assertFileSeparator(tmpDir) + Constants.TEMP_DIR_NAME)); + try { + tmpDir = ConfigUtils.assertFileSeparator(tmpdirFile.getCanonicalPath()); + } catch (IOException e) { + tmpDir = ConfigUtils.assertFileSeparator(tmpdirFile.getPath()); + } + + RESOURCES_PATH = work_dir; + TMP_PATH = tmpDir; + File tmpFile = new File(TMP_PATH); + if (!tmpFile.exists()) + tmpFile.mkdirs(); + CONFIG_PATH = RESOURCES_PATH + CFG + System.getProperty("file.separator"); + + if (defaultConfigDeployedTo != null) { + logger_.debug("** Default configuration successfully deployed to \"" + + defaultConfigDeployedTo + "\" **"); + } + logger_.debug("Setting system property \"" + Constants.CONFIG_DIR_SYSTEM_PROPERTY + + "\" to \"" + configdirFile.getPath() + "\"."); + System.setProperty(Constants.CONFIG_DIR_SYSTEM_PROPERTY, configdirFile.getPath()); + } + + private static WebSettingsReader instance; + + /** + * + * @return + * @throws SettingsException + */ + public static synchronized WebSettingsReader getInstance() throws SettingsException { + if (instance == null) + instance = new WebSettingsReader(); + return instance; + } + + /** + * + */ + public static void clearTemporaryDirectory() { + File temp_dir = new File(TMP_PATH); + logger_.debug("Clearing temporary directory: " + temp_dir); + + if (!temp_dir.exists()) + { + return; + } + + File[] files = temp_dir.listFiles(); + for (int i = 0; i < files.length; i++) + { + // added by tknall: do not try to remove svn-metadata + if (files[i].getName().endsWith(".svn")) { + continue; + } + logger_.debug(" Clearing temporary file: " + files[i]); + boolean delete_success = files[i].delete(); + if (!delete_success) + { + logger_.error("Couldn't delete the temporary file: " + files[i]); + } + } + } + + /** + * + * @return + */ + public static File getTemporaryDirectory() { + return new File(TMP_PATH); + } + + private Properties props; + + private WebSettingsReader() throws SettingsException { + String settingsFile = CONFIG_PATH + WEB_CONFIG_FILE_DEFAULT_NAME; + props = new Properties(); + try { + props.load(new FileInputStream(settingsFile)); + }catch(IOException ioe){ + throw new SettingsException("", ioe); + } + } + + /** + * + * @return + */ + public String getSigningTimeTolerance() { + return props.getProperty(SIGNING_TIME_TOLERANCE_KEY); + } + + /** + * + * @return + */ + public String getRetrieveSignatureDataURLOverride() { + return props.getProperty(RETRIEVE_SIGNATURE_DATA_URL_OVERRIDE_KEY); + } + +} -- cgit v1.2.3