001/* Copyright (C) 2014 konik.io
002 *
003 * This file is part of the Konik library.
004 *
005 * The Konik library is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * The Konik library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>.
017 */
018package io.konik.carriage.pdfbox;
019
020import static java.util.logging.Level.CONFIG;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.Map.Entry;
025import java.util.Properties;
026import java.util.logging.Logger;
027
028/**
029 * The Global Konik Configuration.
030 * 
031 * Try to load Konik Configuration from file
032 * `io.konik.configuration.properties`. System properties provided with the
033 * +-Dio.konik*+ or ```System.setProperties("io.konik*")`` will override the
034 * file content.
035 * 
036 */
037public enum Configuration {
038
039        /** The singleton configuration instance. */
040        INSTANCE;
041
042        private static final Logger LOG = Logger.getLogger(Configuration.class.getName());
043        private final Properties properties;
044
045        Configuration() {
046                properties = new Properties();
047                loadPropertiesFromFile();
048                overwriteWithSystemProperties();
049        }
050
051        void reload() {
052                loadPropertiesFromFile();
053                overwriteWithSystemProperties();
054        }
055
056        private void loadPropertiesFromFile() {
057                String fileName = Configuration.class.getName().toLowerCase();
058                InputStream propertiesStream = this.getClass().getResourceAsStream("/" + fileName + ".properties");
059                if (propertiesStream != null) {
060                        try {
061                                properties.load(propertiesStream);
062                        } catch (IOException e) {
063                                LOG.log(CONFIG, "could not load properties file" + fileName + " from classpath", e);
064                        }
065                }
066        }
067
068        private void overwriteWithSystemProperties() {
069                for (Entry<Object, Object> sysProperty : System.getProperties().entrySet()) {
070                        if (sysProperty.getKey() instanceof String
071                                        && ((String) sysProperty.getKey()).startsWith("io.konik.carriage.pdf")) {
072                                properties.put(sysProperty.getKey(), sysProperty.getValue());
073                        }
074                }
075        }
076
077        /**
078         * Searches for the property with the specified key in this property list.
079         * If the key is not found in this property list, the default property list,
080         * and its defaults, recursively, are then checked. The method returns
081         * <code>null</code> if the property is not found.
082         *
083         * @param key
084         *            the property key.
085         * @return the value in this property list with the specified key value.
086         * @see Configuration#getProperty(String, String)
087         */
088        public String getProperty(String key) {
089                return properties.getProperty(key);
090        }
091
092        /**
093         * Searches for the property with the specified key in this property list.
094         * If the key is not found in this property list, the default property list,
095         * and its defaults, recursively, are then checked. The method returns the
096         * default value argument if the property is not found.
097         *
098         * @param key
099         *            the hashtable key.
100         * @param defaultValue
101         *            a default value.
102         *
103         * @return the value in this property list with the specified key value.
104         * @see Configuration#getProperty(String)
105         */
106        public String getProperty(String key, String defaultValue) {
107                return properties.getProperty(key, defaultValue);
108        }
109
110        @Override
111        public String toString() {
112                return "PDFBox Carriage Configuration dump\n" + properties.toString();
113        }
114}