Example usage for org.apache.commons.configuration.interpol ConfigurationInterpolator registerLookup

List of usage examples for org.apache.commons.configuration.interpol ConfigurationInterpolator registerLookup

Introduction

In this page you can find the example usage for org.apache.commons.configuration.interpol ConfigurationInterpolator registerLookup.

Prototype

public void registerLookup(String prefix, StrLookup lookup) 

Source Link

Document

Registers the given lookup object for the specified prefix at this instance.

Usage

From source file:org.apache.marmotta.platform.core.services.config.ConfigurationServiceImpl.java

/**
 * Initialise the ConfigurationService. Takes the marmotta.home system property from the servlet
 * init parameters/*from   w w w . j  a  v a  2s . c o m*/
 * (web.xml) as bootstrap home. In case a system-config.properties file is found in this
 * directory, the
 * configuration is then loaded from this file. In case a system-config.properties file does not
 * yet exist,
 * the method loads bootstrap settings from the resource default-config.properties in the
 * classpath (in the
 * standard bundling, the file is in the kiwi-core-XXX.jar archive). After loading the
 * configuration, the
 * service initialises the plugin subsystem and the SOLR home directory by unpacking the default
 * configuration
 * if the SOLR configuration does not yet exist.
 * @param override
 */
@Override
public void initialize(String home, Configuration override) {
    lock.writeLock().lock();
    try {
        initialising = true;

        log.info("Apache Marmotta Configuration Service starting up ...");

        if (isTomcat7()) {
            log.info("Apache Marmotta running on Apache Tomcat 7.x");
        } else if (isTomcat6()) {
            log.info("Apache Marmotta running on Apache Tomcat <= 6.x");
        } else if (isJetty7()) {
            log.info("Apache Marmotta running on Jetty 7.x");
        } else if (isJetty6()) {
            log.info("Apache Marmotta running on Jetty <= 6.x");
        } else {
            log.info("Apache Marmotta running on an unknown servlet container");
        }

        setHome(home);

        if (getHome() != null) {
            File f1 = new File(getHome());
            if (!f1.exists()) {
                f1.mkdirs();
            }
            // ensure directory for user configuration files
            File f2 = new File(getHome() + File.separator + DIR_CONFIG);
            if (!f2.exists()) {
                f2.mkdirs();
            }

            // ensure directory for logging messages
            File f3 = new File(getHome() + File.separator + DIR_LOG);
            if (!f3.exists()) {
                f3.mkdirs();
            }

            // ensure directory for importing data
            File f4 = new File(getHome() + File.separator + DIR_IMPORT);
            if (!f4.exists()) {
                f4.mkdirs();
            }

        }

        // the save configuration will be in the  home directory
        try {
            if (getHome() != null) {
                configFileName = getHome() + File.separator + "system-config.properties";
                metaFileName = getHome() + File.separator + "system-meta.properties";

                File configFile = new File(configFileName);
                if (!configFile.exists()) {
                    log.info("creating system configuration in configuration file {}",
                            configFile.getAbsolutePath());
                } else {
                    log.info("reading system configuration from existing configuration file {}",
                            configFile.getAbsolutePath());
                }
                saveConfiguration = new PropertiesConfiguration(configFile);

                File metaFile = new File(metaFileName);
                if (!metaFile.exists()) {
                    log.info("creating system configuration metadata in configuration file {}",
                            metaFile.getAbsolutePath());
                } else {
                    log.info("reading system configuration metadata from existing configuration file {}",
                            metaFile.getAbsolutePath());
                }
                saveMetadata = new PropertiesConfiguration(metaFile);

            } else {
                log.error(
                        "error while initialising configuration: no marmotta.home property given; creating memory-only configuration");
                saveConfiguration = new MapConfiguration(new HashMap<String, Object>());
                saveMetadata = new MapConfiguration(new HashMap<String, Object>());
            }
        } catch (Exception e) {
            log.error("error while initialising configuration file {}: {}; creating memory-only configuration",
                    configFileName, e.getMessage());
            saveConfiguration = new MapConfiguration(new HashMap<String, Object>());
            saveMetadata = new MapConfiguration(new HashMap<String, Object>());
        }
        config = new FallbackConfiguration();
        final ConfigurationInterpolator _int = config.getInterpolator();
        _int.registerLookup("pattern.quote", new StrLookup() {
            @Override
            public String lookup(String key) {
                return Pattern.quote(_int.getDefaultLookup().lookup(key));
            }
        });
        _int.registerLookup("urlencode", new StrLookup() {
            @Override
            public String lookup(String key) {
                try {
                    return URLEncoder.encode(_int.getDefaultLookup().lookup(key), "utf8");
                } catch (UnsupportedEncodingException e) {
                    return _int.getDefaultLookup().lookup(key);
                }
            }
        });
        config.addConfiguration(saveConfiguration, true);

        // load all default-config.properties

        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("config-defaults.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                config.addConfiguration(new PropertiesConfiguration(url));
            }

        } catch (IOException e) {
            log.error("I/O error while loading default configurations", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading default configurations", e);
        }

        // legacy support (to be removed)
        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("default-config.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                config.addConfiguration(new PropertiesConfiguration(url));
                log.warn(
                        "found legacy configuration file {}; should be replaced with per-module configuration!",
                        url);
            }

        } catch (IOException e) {
            log.error("I/O error while loading default configurations", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading default configurations", e);
        }

        // create the configuration that is responsible for getting metadata about configuration keys in the main
        // configuration; since the keys will be different, we store changes also to the main save configuration
        configDescriptions = new FallbackConfiguration();
        configDescriptions.addConfiguration(saveMetadata, true);
        try {
            Enumeration<URL> configs = this.getClass().getClassLoader()
                    .getResources("config-descriptions.properties");
            while (configs.hasMoreElements()) {
                URL url = configs.nextElement();
                configDescriptions.addConfiguration(new PropertiesConfiguration(url));
            }

        } catch (IOException e) {
            log.error("I/O error while loading configuration descriptions", e);
        } catch (ConfigurationException e) {
            log.error("configuration error while loading configuration descriptions", e);
        }

        // setup home if it is given as system property,
        // the bootstrap configuration is overwritten
        if (getHome() != null) {
            config.setProperty("marmotta.home", getHome());
        }

        // in case override configuration is given, change all settings in the configuration accordingly
        if (override != null) {
            for (Iterator<String> it = override.getKeys(); it.hasNext();) {
                String key = it.next();

                config.setProperty(key, override.getProperty(key));
            }
        }

        save();

        // configuration service is now ready to use
        initialised = true;

        loggingStartEvent.fire(new LoggingStartEvent());

        // this should maybe move to the KiWiPreStartupFilter ...
        initDatabaseConfiguration();

        save();

        log.info("Apache Marmotta Configuration Service: initialisation completed");

        configurationInitEvent.fire(new ConfigurationServiceInitEvent());

    } finally {
        lock.writeLock().unlock();
    }

}