jiveglobals.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 487 行 · 第 1/2 页

JAVA
487 行
字号
     * adding a &lt;character-encoding&gt; element to the web.xml/resin.conf     * file. Any Servlet 2.3 compliant application servers also supports the     * method HttpServletRequest.setCharacterEncoding(String). A Servlet 2.3     * Filter called SetCharacterEncodingFilter is installed in the default     * Jive Forums web.xml file, which  will set the incoming character encoding     * to the one reported by this method.     *     * @param characterEncoding the global Jive character encoding.     */    public static void setCharacterEncoding(String characterEncoding) {        JiveGlobals.characterEncoding = characterEncoding;        setJiveProperty("locale.characterEncoding", characterEncoding);    }    /**     * Returns the global TimeZone used by Jive. The default is the VM's     * default time zone.     *     * @return the global time zone used by Jive.     */    public static TimeZone getTimeZone() {        if (locale == null) {            loadProperties();        }        return timeZone;    }    /**     * Sets the global time zone used by Jive. The default time zone is the VM's     * time zone.     */    public static void setTimeZone(TimeZone newTimeZone) {        timeZone = newTimeZone;        dateFormat.setTimeZone(timeZone);        dateTimeFormat.setTimeZone(timeZone);        setJiveProperty("locale.timeZone", timeZone.getID());    }    /**     * Formats a Date object to return a date using the global locale.     *     * @param date the Date to format.     * @return a String representing the date.     */    public static String formatDate(Date date) {        if (locale == null) {            loadProperties();        }        return dateFormat.format(date);    }    /**     * Formats a Date object to return a date and time using the global locale.     *     * @param date the Date to format.     * @return a String representing the date and time.     */    public static String formatDateTime(Date date) {        if (locale == null) {            loadProperties();        }        return dateTimeFormat.format(date);    }    /**     * Returns the location of the <code>jiveHome</code> directory.     *     * @return the location of the jiveHome dir.     */    public static String getJiveHome() {        if (jiveHome == null) {            jiveHome = new InitPropLoader().getJiveHome();            // If jiveHome is still null, try loading it as a system property            if (jiveHome == null) {                jiveHome = System.getProperty("jiveHome");            }        }        return jiveHome;    }    /**     * Returns a Jive property. Jive properties are stored in the file     * <tt>jive_config.xml</tt> that exists in the <tt>jiveHome</tt> directory.     * Properties are always specified as "foo.bar.prop", which would map to     * the following entry in the XML file:     * <pre>     * &lt;foo&gt;     *     &lt;bar&gt;     *         &lt;prop&gt;some value&lt;/prop&gt;     *     &lt;/bar&gt;     * &lt;/foo&gt;     * </pre>     *     * @param name the name of the property to return.     * @return the property value specified by name.     */    public static String getJiveProperty(String name) {        if (properties == null) {            loadProperties();        }        return properties.getProperty(name);    }    /**     * Sets a Jive property. If the property doesn't already exists, a new     * one will be created. Jive properties are stored in the file     * <tt>jive_config.xml</tt> that exists in the <tt>jiveHome</tt> directory.     * Properties are always specified as "foo.bar.prop", which would map to     * the following entry in the XML file:     * <pre>     * &lt;foo&gt;     *     &lt;bar&gt;     *         &lt;prop&gt;some value&lt;/prop&gt;     *     &lt;/bar&gt;     * &lt;/foo&gt;     * </pre>     *     * @param name the name of the property being set.     * @param value the value of the property being set.     */    public static void setJiveProperty(String name, String value) {        if (properties == null) {            loadProperties();        }        properties.setProperty(name, value);    }    /**     * Deletes a Jive property. If the property doesn't exist, the method     * does nothing.     *     * @param name the name of the property to delete.     */    public static void deleteJiveProperty(String name) {        if (properties == null) {            loadProperties();        }        properties.deleteProperty(name);    }    /**     * Loads properties if necessary. Property loading must be done lazily so     * that we give outside classes a chance to set <tt>jiveHome</tt>.     */    private synchronized static void loadProperties() {        if (properties == null) {            // First, try to load it jiveHome as a system property.            if (jiveHome == null) {                System.getProperty("jiveHome");            }            // If jiveHome is still null, no outside process has set it and            // we have to attempt to load the value from jive_init.properties,            // which must be in the classpath.            if (jiveHome == null) {                jiveHome = new InitPropLoader().getJiveHome();            }            // Create a manager with the full path to the xml config file.            try {                properties = new XMLProperties(jiveHome + File.separator + JIVE_CONFIG_FILENAME);            }            catch (IOException ioe) {                ioe.printStackTrace();            }        }        if (locale == null) {            String language = getJiveProperty("locale.language");            if (language == null) {                language = "";            }            String country = getJiveProperty("locale.country");            if (country == null) {                country = "";            }            // If no locale info is specified, default to Locale.US            if (language.equals("") && country.equals("")) {                locale = Locale.US;            }            else {                locale = new Locale(language, country);            }            String charEncoding = getJiveProperty("locale.characterEncoding");            if (charEncoding != null) {                characterEncoding = charEncoding;            }            else {                // The default encoding is ISO 8859_1. We use the version of                // the encoding name that seems to be most widely compatible.                characterEncoding = "ISO8859_1";            }            String timeZoneID = getJiveProperty("locale.timeZone");            if (timeZoneID == null) {                timeZone = TimeZone.getDefault();            }            else {                timeZone = TimeZone.getTimeZone(timeZoneID);            }            dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);            dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,                    DateFormat.MEDIUM, locale);            dateFormat.setTimeZone(timeZone);            dateTimeFormat.setTimeZone(timeZone);        }    }}/** * A very small class to load the jive_init.properties file. The class is * needed since loading files from the classpath in a static context often * fails. */class InitPropLoader {    public String getJiveHome() {        String jiveHome = null;        Properties initProps = new Properties();        InputStream in = null;        try {            in = getClass().getResourceAsStream("/jive_init.properties");            initProps.load(in);        }        catch (Exception e) {            System.err.println("Error reading Jive properties "                + "in JiveGlobals");            e.printStackTrace();        }        finally {            try {                if (in != null) { in.close(); }            } catch (Exception e) {}        }        if (initProps != null) {            jiveHome = initProps.getProperty("jiveHome");            if (jiveHome != null) {                jiveHome = jiveHome.trim();                // Remove trailing slashes.                while (jiveHome.endsWith("/") || jiveHome.endsWith("\\")) {                    jiveHome = jiveHome.substring(0, jiveHome.length()-1);                }            }        }        return jiveHome;    }}

⌨️ 快捷键说明

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