/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ 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("Setting file not loadable: " + settingsFile, 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); } }