All Downloads are FREE. Search and download functionalities are using the official Maven repository.

twitter4j.conf.PropertyConfiguration Maven / Gradle / Ivy

The newest version!
/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j.conf;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Set;

/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 */
class PropertyConfiguration extends ConfigurationBase implements java.io.Serializable {
    public static final String DEBUG = "debug";
    public static final String SOURCE = "source";
    public static final String HTTP_USER_AGENT = "http.userAgent";
    public static final String USER = "user";
    public static final String PASSWORD = "password";

    public static final String HTTP_USE_SSL = "http.useSSL";
    public static final String HTTP_PROXY_HOST = "http.proxyHost";
    public static final String HTTP_PROXY_HOST_FALLBACK = "http.proxyHost";
    public static final String HTTP_PROXY_USER = "http.proxyUser";
    public static final String HTTP_PROXY_PASSWORD = "http.proxyPassword";
    public static final String HTTP_PROXY_PORT = "http.proxyPort";
    public static final String HTTP_PROXY_PORT_FALLBACK = "http.proxyPort";
    public static final String HTTP_CONNECTION_TIMEOUT = "http.connectionTimeout";
    public static final String HTTP_READ_TIMEOUT = "http.readTimeout";

    public static final String HTTP_STREAMING_READ_TIMEOUT = "http.streamingReadTimeout";

    public static final String HTTP_RETRY_COUNT = "http.retryCount";
    public static final String HTTP_RETRY_INTERVAL_SECS = "http.retryIntervalSecs";

    public static final String OAUTH_CONSUMER_KEY = "oauth.consumerKey";
    public static final String OAUTH_CONSUMER_SECRET = "oauth.consumerSecret";
    public static final String OAUTH_ACCESS_TOKEN = "oauth.accessToken";
    public static final String OAUTH_ACCESS_TOKEN_SECRET = "oauth.accessTokenSecret";


    public static final String OAUTH_REQUEST_TOKEN_URL = "oauth.requestTokenURL";
    public static final String OAUTH_AUTHORIZATION_URL = "oauth.authorizationURL";
    public static final String OAUTH_ACCESS_TOKEN_URL = "oauth.accessTokenURL";
    public static final String OAUTH_AUTHENTICATION_URL = "oauth.authenticationURL";

    public static final String REST_BASE_URL = "restBaseURL";
    public static final String SEARCH_BASE_URL = "searchBaseURL";
    public static final String STREAM_BASE_URL = "streamBaseURL";


    public static final String ASYNC_NUM_THREADS = "async.numThreads";

    // hidden portion
    public static final String CLIENT_VERSION = "clientVersion";
    public static final String CLIENT_URL = "clientURL";
    private static final long serialVersionUID = 6458764415636588373L;



    PropertyConfiguration(String treePath) {
        super();
        Properties props = new Properties();
        // load from system properties
        try {
            props = (Properties)System.getProperties().clone();
            normalize(props);
        } catch (AccessControlException ace) {
            // Unsigned applets are not allowed to access System properties
            props = new Properties();
        }
        final String TWITTER4J_PROPERTIES = "twitter4j.properties";
        // override System properties with ./twiter4j.properties in the classpath
        loadProperties(props, "." + File.separatorChar + TWITTER4J_PROPERTIES);
        // then, override with /twiter4j.properties in the classpath
        loadProperties(props, Configuration.class.getResourceAsStream("/" + TWITTER4J_PROPERTIES));
        // then, override with /WEB/INF/twiter4j.properties in the classpath
        loadProperties(props, Configuration.class.getResourceAsStream("/WEB-INF/" + TWITTER4J_PROPERTIES));

        setFieldsWithTreePath(props, treePath);
    }

    /**
     * Creates a root PropertyConfiguration. This constructor is equivalent to new PropertyConfiguration("/").
     */
    PropertyConfiguration() {
        this("/");
    }

    private boolean notNull(Properties props, String prefix, String name) {
        return null != props.getProperty(prefix + name);
    }

    private boolean loadProperties(Properties props, String path) {
        FileInputStream fis = null;
        try {
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                fis = new FileInputStream(file);
                props.load(new FileInputStream(file));
                normalize(props);
                return true;
            }
        } catch (Exception ignore) {
        } finally {
            try {
                if (null != fis) {
                    fis.close();
                }
            } catch (IOException ignore) {

            }
        }
        return false;
    }

    private boolean loadProperties(Properties props, InputStream is) {
        try {
            props.load(is);
            normalize(props);
            return true;
        } catch (Exception ignore) {
        }
        return false;
    }

    private void normalize(Properties props) {
        Set keys = props.keySet();
        ArrayList toBeNormalized = new ArrayList(10);
        for (Object key : keys) {
            String keyStr = (String) key;
            if (-1 != (keyStr.indexOf("twitter4j."))) {
                toBeNormalized.add(keyStr);
            }
        }
        for (String keyStr : toBeNormalized) {
            String property = props.getProperty(keyStr);
            int index = keyStr.indexOf("twitter4j.");
            String newKey = keyStr.substring(0, index) + keyStr.substring(index + 10);
            props.setProperty(newKey, property);
        }
    }

    /**
     * passing "/foo/bar" as treePath will result:
* 1. load [twitter4j.]restBaseURL
* 2. override the value with foo.[twitter4j.]restBaseURL
* 3. override the value with foo.bar.[twitter4j.]restBaseURL
* * @param props properties to be loaded * @param treePath the path */ private void setFieldsWithTreePath(Properties props, String treePath) { setFieldsWithPrefix(props, ""); String[] splitArray = treePath.split("/"); String prefix = null; for (String split : splitArray) { if (!"".equals(split)) { if (null == prefix) { prefix = split + "."; } else { prefix += split + "."; } setFieldsWithPrefix(props, prefix); } } } private void setFieldsWithPrefix(Properties props, String prefix) { if (notNull(props, prefix, DEBUG)) { setDebug(getBoolean(props, prefix, DEBUG)); } if (notNull(props, prefix, SOURCE)) { setSource(getString(props, prefix, SOURCE)); } if (notNull(props, prefix, USER)) { setUser(getString(props, prefix, USER)); } if (notNull(props, prefix, PASSWORD)) { setPassword(getString(props, prefix, PASSWORD)); } if (notNull(props, prefix, HTTP_USE_SSL)) { setUseSSL(getBoolean(props, prefix, HTTP_USE_SSL)); } else if (notNull(props, prefix, USER) && notNull(props, prefix, PASSWORD)) { // use SSL with Basic Auth setUseSSL(true); } if (notNull(props, prefix, HTTP_PROXY_HOST)) { setHttpProxyHost(getString(props, prefix, HTTP_PROXY_HOST)); } else if (notNull(props, prefix, HTTP_PROXY_HOST_FALLBACK)) { setHttpProxyHost(getString(props, prefix, HTTP_PROXY_HOST_FALLBACK)); } if (notNull(props, prefix, HTTP_PROXY_USER)) { setHttpProxyUser(getString(props, prefix, HTTP_PROXY_USER)); } if (notNull(props, prefix, HTTP_PROXY_PASSWORD)) { setHttpProxyPassword(getString(props, prefix, HTTP_PROXY_PASSWORD)); } if (notNull(props, prefix, HTTP_PROXY_PORT)) { setHttpProxyPort(getIntProperty(props, prefix, HTTP_PROXY_PORT)); } else if (notNull(props, prefix, HTTP_PROXY_PORT_FALLBACK)) { setHttpProxyPort(getIntProperty(props, prefix, HTTP_PROXY_PORT_FALLBACK)); } if (notNull(props, prefix, HTTP_CONNECTION_TIMEOUT)) { setHttpConnectionTimeout(getIntProperty(props, prefix, HTTP_CONNECTION_TIMEOUT)); } if (notNull(props, prefix, HTTP_READ_TIMEOUT)) { setHttpReadTimeout(getIntProperty(props, prefix, HTTP_READ_TIMEOUT)); } if (notNull(props, prefix, HTTP_STREAMING_READ_TIMEOUT)) { setHttpStreamingReadTimeout(getIntProperty(props, prefix, HTTP_STREAMING_READ_TIMEOUT)); } if (notNull(props, prefix, HTTP_RETRY_COUNT)) { setHttpRetryCount(getIntProperty(props, prefix, HTTP_RETRY_COUNT)); } if (notNull(props, prefix, HTTP_RETRY_INTERVAL_SECS)) { setHttpRetryIntervalSecs(getIntProperty(props, prefix, HTTP_RETRY_INTERVAL_SECS)); } if (notNull(props, prefix, OAUTH_CONSUMER_KEY)) { setOAuthConsumerKey(getString(props, prefix, OAUTH_CONSUMER_KEY)); } if (notNull(props, prefix, OAUTH_CONSUMER_SECRET)) { setOAuthConsumerSecret(getString(props, prefix, OAUTH_CONSUMER_SECRET)); } if (notNull(props, prefix, OAUTH_ACCESS_TOKEN)) { setOAuthAccessToken(getString(props, prefix, OAUTH_ACCESS_TOKEN)); } if (notNull(props, prefix, OAUTH_ACCESS_TOKEN_SECRET)) { setOAuthAccessTokenSecret(getString(props, prefix, OAUTH_ACCESS_TOKEN_SECRET)); } if (notNull(props, prefix, ASYNC_NUM_THREADS)) { setAsyncNumThreads(getIntProperty(props, prefix, ASYNC_NUM_THREADS)); } if (notNull(props, prefix, CLIENT_VERSION)) { setClientVersion(getString(props, prefix, CLIENT_VERSION)); } if (notNull(props, prefix, CLIENT_URL)) { setClientURL(getString(props, prefix, CLIENT_URL)); } if (notNull(props, prefix, HTTP_USER_AGENT)) { setUserAgent(getString(props, prefix, HTTP_USER_AGENT)); } if (notNull(props, prefix, OAUTH_REQUEST_TOKEN_URL)) { setOAuthRequestTokenURL(getString(props, prefix, OAUTH_REQUEST_TOKEN_URL)); } if (notNull(props, prefix, OAUTH_AUTHORIZATION_URL)) { setOAuthAuthorizationURL(getString(props, prefix, OAUTH_AUTHORIZATION_URL)); } if (notNull(props, prefix, OAUTH_ACCESS_TOKEN_URL)) { setOAuthAccessTokenURL(getString(props, prefix, OAUTH_ACCESS_TOKEN_URL)); } if (notNull(props, prefix, OAUTH_AUTHENTICATION_URL)) { setOAuthAuthenticationURL(getString(props, prefix, OAUTH_AUTHENTICATION_URL)); } if (notNull(props, prefix, REST_BASE_URL)) { setRestBaseURL(getString(props, prefix, REST_BASE_URL)); } if (notNull(props, prefix, SEARCH_BASE_URL)) { setSearchBaseURL(getString(props, prefix, SEARCH_BASE_URL)); } if (notNull(props, prefix, STREAM_BASE_URL)) { setStreamBaseURL(getString(props, prefix, STREAM_BASE_URL)); } } protected boolean getBoolean(Properties props, String prefix, String name) { String value = props.getProperty(prefix + name); return Boolean.valueOf(value); } protected int getIntProperty(Properties props, String prefix, String name) { String value = props.getProperty(prefix + name); try { return Integer.parseInt(value); } catch (NumberFormatException nfe) { return -1; } } protected String getString(Properties props, String prefix, String name) { return props.getProperty(prefix + name); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy