⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtconfig.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description://  Support for hierarchical runtime properties// ----------------------------------------------------------------------------// Change History://  2006/03/26  Martin D. Flynn//     -Initial release//  2006/04/23  Martin D. Flynn//     -Changed support for default properties//  2006/06/30  Martin D. Flynn//     -Repackaged//  2007/03/30  Martin D. Flynn//     -Added support for immutable System properties//  2007/05/06  Martin D. Flynn//     -Added support for checking invalid command-line args//  2007/06/13  Martin D. Flynn//     -Catch 'SecurityException's when getting System properties.//  2007/08/09  Martin D. Flynn//     -Changed the way this module searches for runtime config files.//  2007/09/16  Martin D. Flynn//     -Added method 'insertKeyValues'//     -Added support for key/value replace in config-file value strings// ----------------------------------------------------------------------------package org.opengts.util;import java.io.*;import java.util.*;import java.net.*;public class RTConfig{    // ------------------------------------------------------------------------    // Cannot initialize here, otherwise we would be unable to override 'configFile'    //static { _startupInit(false); }    // ------------------------------------------------------------------------    private static boolean verbose = false;    // ------------------------------------------------------------------------    private static String localhostName = null;    public static String getHostName()    {        /* host name */        if (RTConfig.localhostName == null) {            try {                String hd = InetAddress.getLocalHost().getHostName();                int p = hd.indexOf(".");                RTConfig.localhostName = (p >= 0)? hd.substring(0,p) : hd;            } catch (UnknownHostException uhe) {                RTConfig.localhostName = "UNKNOWN";            }        }        return RTConfig.localhostName;    }    // ------------------------------------------------------------------------    private static final int    THREAD_LOCAL        = 0;    private static final int    SERVLET_CONFIG      = 1;    private static final int    SERVLET_CONTEXT     = 2;    private static final int    COMMAND_LINE        = 3;    private static final int    CONFIG_FILE         = 4;    private static final int    SYSTEM              = 5;    private static RTProperties CFG_PROPERTIES[] = new RTProperties[] {        null,                                       // ThreadLocal properties        null,                                       // ServletConfig properties        null,                                       // ServletContext properties        null,                                       // CommandLine properties [on-demand init]        null,                                       // ConfigFile properties [lazy init]        null // new RTProperties(System.getProperties()), // System properties    };    public static RTProperties getThreadProperties()    {        if (CFG_PROPERTIES[THREAD_LOCAL] == null) {            synchronized (CFG_PROPERTIES) {                if (CFG_PROPERTIES[THREAD_LOCAL] == null) { // still null?                    CFG_PROPERTIES[THREAD_LOCAL] = new RTProperties(new ThreadLocalMap<Object,Object>());                }            }        }        return CFG_PROPERTIES[THREAD_LOCAL];    }    public static RTProperties getServletContextProperties()    {        return CFG_PROPERTIES[SERVLET_CONTEXT]; // may be null if not initialized    }    public static RTProperties getServletConfigProperties()    {        return CFG_PROPERTIES[SERVLET_CONFIG]; // will be null until this is fully implemented    }    public static RTProperties getCommandLineProperties()    {        return CFG_PROPERTIES[COMMAND_LINE]; // may be null if not initialized    }    public static RTProperties getConfigFileProperties()    {        if (CFG_PROPERTIES[CONFIG_FILE] == null) {            // this should have been initialized before, but force initialization now            if (RTConfig.verbose) { Print.logInfo("Late initialization!!!"); }            _startupInit(false);        }        if (CFG_PROPERTIES[CONFIG_FILE] != null) {            return CFG_PROPERTIES[CONFIG_FILE];        } else {            Print.sysPrintln("'RTConfig.getConfigFileProperties()' returning temporary RTProperties");            return new RTProperties();        }    }    public static RTProperties getSystemProperties()    {        if (CFG_PROPERTIES[SYSTEM] == null) {            // this should have been initialized before, but force initialization now            if (RTConfig.verbose) { Print.logInfo("Late initialization!!!"); }            _startupInit(false);        }        return CFG_PROPERTIES[SYSTEM];    }    public static RTProperties getPropertiesForKey(String key)    {        return RTConfig.getPropertiesForKey(key, true);    }    public static RTProperties getPropertiesForKey(String key, boolean dftOk)    {        if (key != null) {            if (!isInitialized()) {                // 'Print._println...' used here to eliminate possible recursion stack-overflow                //Print._println(null, "ConfigFile not yet loaded");                //Thread.dumpStack();                // continue ...            }            // look for key in our property list stack            for (int i = 0; i < CFG_PROPERTIES.length; i++) {                RTProperties rtProps = CFG_PROPERTIES[i];                if ((rtProps != null) && rtProps.hasProperty(key)) {                     return rtProps;                 }            }            // still not found, try the default properties            if (dftOk) {                RTProperties dftProps = RTKey.getDefaultProperties();                if ((dftProps != null) && dftProps.hasProperty(key)) {                    return dftProps;                }            }        }        return null;    }    // ------------------------------------------------------------------------    public static void setCommandLineArgs(String argv[])    {        if (argv != null) {            RTConfig.setCommandLineArgs(new RTProperties(argv), false);        } else {            RTConfig.setCommandLineArgs(new RTProperties(), false);        }    }    public static void setCommandLineArgs(RTProperties cmdLineProps)    {        if (cmdLineProps != null) {            RTConfig.setCommandLineArgs(cmdLineProps, false);        } else {            RTConfig.setCommandLineArgs(new RTProperties(), false);        }    }    public static void setCommandLineArgs(String argv[], boolean testMode)    {        if (argv != null) {            RTProperties cmdArgs = new RTProperties(argv);            RTConfig.setCommandLineArgs(cmdArgs, testMode);        } else {            RTConfig.setCommandLineArgs(new RTProperties(), testMode);        }    }    public static void setCommandLineArgs(RTProperties cmdLineProps, boolean testMode)    {        if (cmdLineProps != null) {            cmdLineProps.setIgnoreKeyCase(true);            cmdLineProps.setProperty(RTKey.MAIN_CLASS, getMainClass());            if (CFG_PROPERTIES[COMMAND_LINE] == null) {                // first initialization                CFG_PROPERTIES[COMMAND_LINE] = cmdLineProps;                     _startupInit(true); // initialize now to allow for overriding 'configFile'            } else {                // subsequent re-initialization                CFG_PROPERTIES[COMMAND_LINE].setProperties(cmdLineProps);            }        } else {            _startupInit(true);        }    }    // ------------------------------------------------------------------------    private static Set<String> _parseArgs(Object argv, Set<String> set)    {        if ((argv != null) && (set != null)) {            if (argv instanceof Object[]) {                Object a[] = (Object[])argv;                for (int i = 0; i < a.length; i++) {                    RTConfig._parseArgs(a[i], set);                }            } else {                set.add(argv.toString());            }        }        return set;    }    public static String[] validateCommandLineArgs(Object argv)    {        RTProperties cmdLineProps = RTConfig.getCommandLineProperties();        if (cmdLineProps != null) {            java.util.List<String> badArgs = new Vector<String>();            Set<String> argSet = new HashSet<String>();            argSet.add(RTKey.MAIN_CLASS);            RTConfig._parseArgs(argv, argSet);            for (Iterator keys = cmdLineProps.keyIterator(); keys.hasNext();) {                String k = (String)keys.next();                if (RTKey.hasDefault(k) || RTKey.COMMAND_LINE_CONF.equals(k)) {                    // "-conf", "-configFile", "-configFileDir" are ok                } else                if (!argSet.contains(k)) {                    badArgs.add(k);                }            }            return badArgs.isEmpty()? null : (String[])badArgs.toArray(new String[badArgs.size()]);        } else {            return null;        }    }        // ------------------------------------------------------------------------    public static void setServletContextProperties(Map<Object,Object> props)    {        // Don't do anything right now except initialize RTConfig        // --------------------------------------------------------------        // Set via servlet v2.3 ServletContextListener        // Reference:        //  http://livedocs.macromedia.com/jrun/4/Programmers_Guide/servletlifecycleevents3.htm        RTConfig.verbose = true; // default to verbose        CFG_PROPERTIES[SERVLET_CONTEXT] = new RTProperties(props);        _startupInit(false);        setWebApp(true); // force isWebapp=true        //Print.logInfo("DebugMode == " + RTConfig.isDebugMode());    }    public static void clearServletContextProperties(Object servlet)    {        CFG_PROPERTIES[SERVLET_CONTEXT] = null;    }    // ------------------------------------------------------------------------    //public static void setServletConfigProperties(Object servlet, Map props)    //{    //    // Don't do anything right not except initialize RTConfig    //    // --------------------------------------------------------------    //    // Ideally, this will use some kind of map indexed by the servlet    //    // class to store the servlet specific properties.  The stack    //    // will then be examined to find the appropriate Class type and    //    // then the corresponding properties will be used.    //    RTConfig._startupInit(false);    //}    //public static void clearServletConfigProperties(Object servlet)    //{    //    CFG_PROPERTIES[SERVLET_CONFIG] = null;    //}    // ------------------------------------------------------------------------    private static int      _didStartupInit  = 0;  // 0=not initialized, 1=initializing, 2=initialized    private static URL      _foundConfigURL  = null;    private static File     _foundConfigFile = null;    public static boolean isInitializing()    {        return (_didStartupInit == 1);    }    public static boolean isInitialized()    {        return (_didStartupInit == 2);    }    public static URL getLoadedConfigURL()    {        return _foundConfigURL;    }    public static File getLoadedConfigFile()    {        if (_foundConfigFile == null) {            if ((_foundConfigURL != null) && (_foundConfigURL.getProtocol().equals("file"))) {                String path = _foundConfigURL.getPath();                _foundConfigFile = new File(path);            }        }

⌨️ 快捷键说明

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