propertyhandler.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,648 行 · 第 1/5 页

JAVA
1,648
字号
                        + propsFileName);            }        } else {            if (showDebugMessages) {                Debug.output("Loading properties from file " + propsFileName                        + " from package of class " + getClass());            }        }        if (propsIn != null) {            foundProperties = PropUtils.loadProperties(tmpProperties, propsIn);            init(tmpProperties, "resources");            tmpProperties.clear();        }        if (!foundProperties && (Environment.isApplet() || showDebugMessages)) {            Debug.output("PropertyHandler: Unable to locate as resource: "                    + propsFileName);        }        // Seems like we can kick out here in event of Applet...        if (Environment.isApplet()) {            Environment.init(getProperties());            return;        }        Properties systemProperties;        try {            systemProperties = System.getProperties();        } catch (java.security.AccessControlException ace) {            systemProperties = new Properties();        }        String configDirProperty = propertyPrefix                + PropertyHandler.configDirProperty;        String openmapConfigDirectory = systemProperties.getProperty(configDirProperty);        if (openmapConfigDirectory == null) {            Vector cps = Environment.getClasspathDirs();            String defaultExtraDir = "share";            for (int searchCount = 0; searchCount < cps.size(); searchCount++) {                File shareDir = new File((String) cps.elementAt(searchCount), defaultExtraDir);                if (shareDir.exists()) {                    // Debug.output("Found share directory: " +                    // shareDir.getPath());                    openmapConfigDirectory = shareDir.getPath();                    break;                    // } else {                    // Debug.output("No share directory in: " +                    // shareDir.getPath());                }            }        }        Environment.addPathToClasspaths(openmapConfigDirectory);        // in OpenMap config directory        if (showDebugMessages) {            Debug.output("PropertyHandler: Looking for "                    + propsFileName                    + " in configuration directory: "                    + (openmapConfigDirectory == null ? "not set"                            : openmapConfigDirectory));        }        // We want foundProperties to reflect if properties have ever        // been found.        foundProperties |= PropUtils.loadProperties(tmpProperties,                openmapConfigDirectory,                propsFileName);        // Include properties from config file properties.        includeProperties = getIncludeProperties(tmpProperties.getProperty(propertyPrefix                + includeProperty),                tmpProperties);        merge(includeProperties,                "include file properties",                openmapConfigDirectory);        // OK, now merge the config file properties into the main        // properties        merge(tmpProperties, propsFileName, openmapConfigDirectory);        // Clear out the tmp        tmpProperties.clear();        // Let system properties take precidence over resource and        // config dir properties.        merge(systemProperties, "system properties", "system");        // in user's home directory, most precedence.        String userHomeDirectory = systemProperties.getProperty("user.home");        if (showDebugMessages) {            Debug.output("PropertyHandler: Looking for " + propsFileName                    + " in user's home directory: " + userHomeDirectory);        }        // We want foundProperties to reflect if properties have ever        // been found.        foundProperties |= PropUtils.loadProperties(tmpProperties,                userHomeDirectory,                propsFileName);        if (showDebugMessages) {            Debug.output("***** Done with property search ****");        }        if (!foundProperties && !Environment.isApplet()) {            PropUtils.copyProperties(PropUtils.promptUserForProperties(),                    properties);        }        // Before we the user properties into the overall properties,        // need to check for the include properties URLs, and load        // those first.        includeProperties = getIncludeProperties(tmpProperties.getProperty(propertyPrefix                + includeProperty),                tmpProperties);        merge(includeProperties, "include file properties", userHomeDirectory);        // Now, load the user home preferences last, since they take        // the highest precedence.        merge(tmpProperties, propsFileName, userHomeDirectory);        // Well, they used to take the highest precedence. Now, we        // look for a localized property file, and write those        // properties on top.        localizedProperties = getLocalizedProperties(tmpProperties.getProperty(propertyPrefix                + localizedProperty),                userHomeDirectory);        merge(localizedProperties, "localized properties", null);        Environment.init(getProperties());    }    /**     * Load the localized properties that will take precidence over all other     * properties. If the localizedPropertyFile is null, a localized version of     * the openmap.properties file will be searched for in the classpath and in     * the user home directory (if that isn't null as well).     */    protected Properties getLocalizedProperties(String localizedPropertyFile,                                                String userHomeDirectory) {        Properties props = null;        if (localizedPropertyFile == null) {            java.util.Locale loc = java.util.Locale.getDefault();            localizedPropertyFile = "openmap_" + loc.toString() + ".properties";        }        boolean tryHomeDirectory = false;        if (DEBUG) {            Debug.output("PropertyHandler: Looking for localized file: "                    + localizedPropertyFile);        }        try {            URL propsURL = PropUtils.getResourceOrFileOrURL(localizedPropertyFile);            if (propsURL == null) {                tryHomeDirectory = true;            } else {                if (DEBUG) {                    Debug.output("Found localized properties in classpath");                }                props = fetchProperties(propsURL);            }        } catch (MalformedURLException murle) {            Debug.error("PropertyHandler can't find localized property file: "                    + localizedPropertyFile);            tryHomeDirectory = true;        }        if (tryHomeDirectory) {            props = new Properties();            if (!PropUtils.loadProperties(props,                    userHomeDirectory,                    localizedPropertyFile)) {                props = null;            } else {                if (DEBUG) {                    Debug.output("Found localized properties in home directory");                }            }        }        if (props == null) {            props = new Properties();        }        return props;    }    /**     * Initialize internal properties from Properties object. Appends all the     * properties it finds, overwriting the ones with the same key. Called by     * the two constructors where a Properties object is passed in, or when a     * URL for a Properties file is provided. This is not called by the     * consstructor that has to go looking for the properties to use.     *      * @param props the properties to merge into the properties held by the     *        PropertyHandler.     * @param howString a string describing where the properties come from. Just     *        used for debugging purposes, so passing in a null value is no big     *        deal.     * @return the properties contained in this PropertyHandler.     */    protected void init(Properties props, String howString) {        // Include properties noted in resources properties.        Properties includeProperties = getIncludeProperties(props.getProperty(getPropertyPrefix()                + includeProperty),                props);        merge(includeProperties, "include file properties", howString);        if (!Environment.isApplet()) {            Properties systemProperties = System.getProperties();            merge(systemProperties, props);        }        merge(props, "loaded", howString);        if (DEBUG) {            Debug.output("PropertyHandler: loaded properties");        }    }    /**     * Take a marker name list (space separated names), and open the properties     * files listed in the propertu with keys of marker.URL.     *      * @param markerList space separated marker names in a single string that     *        needs to be parsed.     * @param props the properties that the markerList comes from, in order to     *        get the marker.URL properties.     * @return an allocated Properties object containing all the properties from     *         the inlude files. If no include files are listed, the Properties     *         object is empty, not null.     */    protected Properties getIncludeProperties(String markerList,                                              Properties props) {        Properties newProps = new Properties();        Properties tmpProps = new Properties();        Vector includes = PropUtils.parseSpacedMarkers(markerList);        int size = includes.size();        if (size > 0) {            if (Debug.debugging("propertiesdetail")) {                Debug.output("PropertyHandler: handling include files: "                        + includes);            }            for (int i = 0; i < size; i++) {                String includeName = (String) includes.elementAt(i);                String includeProperty = includeName + ".URL";                String include = props.getProperty(includeProperty);                if (include == null) {                    Debug.error("PropertyHandler.getIncludeProperties(): Failed to locate include file \""                            + includeName                            + "\" with URL \""                            + includeProperty                            + "\"\n  Skipping include file \"" + include + "\"");                    continue;                }                try {                    tmpProps.clear();                    // Open URL to read in properties                    URL tmpInclude = PropUtils.getResourceOrFileOrURL(null,                            include);                    if (tmpInclude == null) {                        continue;                    }                    InputStream is = tmpInclude.openStream();                    tmpProps.load(is);                    if (DEBUG) {                        Debug.output("PropertyHandler.getIncludeProperties(): located include properties file URL: "                                + include);                    }                    // Include properties noted in resources                    // properties - a little recursive action,                    // here.                    Properties includeProperties = getIncludeProperties(tmpProps.getProperty(includeProperty),                            tmpProps);                    merge(includeProperties,                            newProps,                            "include file properties",                            "within " + include);                    merge(tmpProps,                            newProps,                            "include file properties",                            include);                } catch (MalformedURLException e) {                    Debug.error("PropertyHandler: malformed URL for include file: |"                            + include + "| for " + includeName);                } catch (IOException ioe) {                    Debug.error("PropertyHandler: IOException processing "                            + include + "| for " + includeName);                }            }        } else {            Debug.message("properties",                    "PropertyHandler.getIncludeProperties(): no include files found.");        }        return newProps;    }    /**     * Take the from properties, copy them into the internal PropertyHandler     * properties.     *      * @param from the source properties.     */    protected void merge(Properties from) {        merge(from, getProperties(), null, null);    }    /**     * Take the from properties, copy them into the to properties.     *      * @param from the source properties.     * @param to the destination properties.     */    protected void merge(Properties from, Properties to) {        merge(from, to, null, null);    }    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?