URLPropertiesProvider Class Included in NuoDB

The first is the default URLPropertiesProvider class used by both the NuoDB broker and by the NuoDB REST service. This class is contained in both NUODB_HOME/jar/nuoagent.jar and in NUODB_HOME/jar/nuodb-rest-api.jar. URLPropertiesProvider is a simple class that loads properties from a file.

By default the URLPropertiesProvider in the broker reads properties from NUODB_HOME/etc/default.properties. An alternative properties file may be specified using the JVM propertiesUrl property when starting the roker, as in ‑DpropertiesUrl=path_to_alternative_file.

By default the URLPropertiesProvider in the NuoDB REST service reads properties from the NUODB_HOME/etc/default.properties file. It currently only reads the domainPassword property and if the required domainPassword property does not exist in the nuodb-rest-api.yml file, then the NuoDB REST service will look for domainPassword in the default.properties file.

Following is the source code for URLPropertiesProvider.java:

/* Copyright 2015 NuoDB, Inc. All rights reserved */
 
package com.nuodb.agent.plugin;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import com.nuodb.agent.PropertiesProvider;
import com.nuodb.impl.util.Env;
 
/**
 * A very simple {@code PropertiesProvider} that loads based on a URL. The location of the properties file is specified
 * with the {@code propertiesUrl} property. If no URL is specified then this will try to fall back on the default
 * properties file in {@code etc/default.properties}.
 */
public class URLPropertiesProvider implements PropertiesProvider {
 
    // logger for this component
    private static final Logger logger = Logger.getLogger(URLPropertiesProvider.class.getName());
 
    // the properties loaded (once only) from a file identified by URL
    private final Properties properties = new Properties();
 
    /**
     * Creates an instance of {@code URLPropertiesProvider}. The properties that this instance will provide are loaded
     * from the encoded properties located at the URL identified by the {@code propertiesUrl} system property. If no
     * value is specified for this property then this provider will not provide any property values. This class only
     * loads the properties once.
     *
     * @param systemProperties the {@code Properties} for the local system
     *
     * @throws IllegalArgumentException if the specified URL cannot be used
     */
    public URLPropertiesProvider(Properties systemProperties) {
        String urlPath = systemProperties.getProperty("propertiesUrl", null);
        InputStream input = null;
 
        if (urlPath != null) {
            try {
                URL url = new URL(urlPath);
                input = url.openStream();
            } catch (IOException ioe) {
                logger.log(Level.SEVERE, "failed to resolve url", ioe);
 
                throw new IllegalArgumentException("Invalid URL", ioe);
            }
 
            if (logger.isLoggable(Level.INFO)) {
                logger.info("using properties from: " + urlPath);
            }
        } else {
            File rootDir = Env.getContainingDirectory(Env.class).getParentFile();
 
            if (rootDir != null) {
                File etcDir = new File(rootDir, "etc");
 
                if (etcDir.exists() && etcDir.isDirectory() && etcDir.canRead()) {
                   File defaultProperties = new File(etcDir, "default.properties");
                    if (defaultProperties.exists() && defaultProperties.canRead()) {
                        try {
                            input = new FileInputStream(defaultProperties);
                        } catch (FileNotFoundException fnfe) {
                            throw new IllegalStateException("No default file");
                        }
 
                        if (logger.isLoggable(Level.INFO)) {
                            logger.info("using default properties file: " + defaultProperties.getPath());
                        }
                    }
                }
            }
        }
 
        if (input == null) {
            logger.config("no source of backing properties is available");
 
            return;
        }
 
        try {
            try {
                properties.load(input);
            } finally {
                input.close();
            }
        } catch (IOException ioe) {
            if (logger.isLoggable(Level.SEVERE)) {
                logger.log(Level.SEVERE, "failed to load properties", ioe);
            }
 
            throw new IllegalArgumentException("Failed to load properties", ioe);
        }
    }
 
    /* Implement PropertiesProvider */
    @Override
    public String getProperty(String key) {
        return properties.getProperty(key, null);
    }
 
 
}