configuratorfactory.java

来自「JGRoups源码」· Java 代码 · 共 549 行 · 第 1/2 页

JAVA
549
字号
            }        }        // try a regular file        if(input == null && properties instanceof File) {            try {                input=new FileInputStream((File)properties);            }            catch(Throwable t) {            }        }        if(input != null) {            return XmlConfigurator.getInstance(input);        }        if(properties instanceof Element) {            return XmlConfigurator.getInstance((Element)properties);        }        return new PlainConfigurator((String)properties);    }    public static InputStream getConfigStream(File file) throws Exception {        if(propertiesOverride != null)            return getConfigStream(propertiesOverride);        checkForNullConfiguration(file);        try {            return new FileInputStream(file);        }        catch(IOException ioe) {            throw createChannelConfigurationException(ioe);        }    }    public static InputStream getConfigStream(URL url) throws Exception {        if (propertiesOverride != null)            return getConfigStream(propertiesOverride);        try {            checkJAXPAvailability();            return url.openStream();        }        catch(Exception ex) {            throw createChannelConfigurationException(ex);        }    }    /**     * Returns a JGroups XML configuration InputStream based on the provided     * properties string.     *     * @param properties a string representing a system resource containing a     *                   JGroups XML configuration, a string representing a URL     *                   pointing to a JGroups ML configuration, or a string     *                   representing a file name that contains a JGroups XML     *                   configuration.     *     * @throws IOException  if the provided properties string appears to be a     *                      valid URL but is unreachable.     */    public static InputStream getConfigStream(String properties) throws IOException {        InputStream configStream = null;        if (propertiesOverride != null)            return getConfigStream(propertiesOverride);        // Check to see if the properties string is the name of a file.        try {            configStream=new FileInputStream(properties);        }        catch(FileNotFoundException fnfe) {            // the properties string is likely not a file        }        catch(AccessControlException access_ex) {            // fixes http://jira.jboss.com/jira/browse/JGRP-94        }        // Check to see if the properties string is a URL.        if(configStream == null) {            try {                configStream=new URL(properties).openStream();            }            catch (MalformedURLException mre) {                // the properties string is not a URL            }        }        // Commented so the caller is notified of this condition, but left in        // the code for documentation purposes.        //        // catch (IOException ioe) {            // the specified URL string was not reachable        // }        // Check to see if the properties string is the name of a resource,        // e.g. udp.xml.        if(configStream == null && properties.endsWith("xml")) {            configStream=Util.getResourceAsStream(properties, ConfiguratorFactory.class);        }        return configStream;    }    public static InputStream getConfigStream(Object properties) throws IOException {        InputStream input=null;        if (propertiesOverride != null)            return getConfigStream(propertiesOverride);        // added by bela: for null String props we use the default properties        if(properties == null)            properties=JChannel.DEFAULT_PROTOCOL_STACK;        if(properties instanceof URL) {            try {                input=((URL)properties).openStream();            }            catch(Throwable t) {            }        }        // if it is a string, then it could be a plain string or a url        if(input == null && properties instanceof String) {            input=getConfigStream((String)properties);        }        // try a regular file        if(input == null && properties instanceof File) {            try {                input=new FileInputStream((File)properties);            }            catch(Throwable t) {            }        }        if(input != null)            return input;        if(properties instanceof Element) {            return getConfigStream((Element)properties);        }        return new ByteArrayInputStream(((String)properties).getBytes());    }    /**     * Returns an XmlConfigurator based on the provided properties string (if     * possible).     *     * @param properties a string representing a system resource containing a     *                   JGroups XML configuration, a string representing a URL     *                   pointing to a JGroups ML configuration, or a string     *                   representing a file name that contains a JGroups XML     *                   configuration.     *     * @return an XmlConfigurator instance based on the provided properties     *         string; <code>null</code> if the provided properties string does     *         not point to an XML configuration.     *     * @throws IOException  if the provided properties string appears to be a     *                      valid URL but is unreachable, or if the JGroups XML     *                      configuration pointed to by the URL can not be     *                      parsed.     */    static XmlConfigurator getXmlConfigurator(String properties) throws IOException {        XmlConfigurator returnValue=null;        InputStream configStream=getConfigStream(properties);        if (configStream != null) {            checkJAXPAvailability();            returnValue=XmlConfigurator.getInstance(configStream);        }        return returnValue;    }    /**     * Creates a <code>ChannelException</code> instance based upon a     * configuration problem.     *     * @param cause the exceptional configuration condition to be used as the     *              created <code>ChannelException</code>'s cause.     */    static ChannelException createChannelConfigurationException(Throwable cause) {        return new ChannelException("unable to load the protocol stack", cause);    }    /**     * Check to see if the specified configuration properties are     * <code>null</null> which is not allowed.     *     * @param properties the specified protocol stack configuration.     *     * @throws NullPointerException if the specified configuration properties     *                              are <code>null</code>.     */    static void checkForNullConfiguration(Object properties) {        if(properties == null)            throw new NullPointerException("the specifed protocol stack configuration was null");    }    /**     * Checks the availability of the JAXP classes on the classpath.     *     * @throws NoClassDefFoundError if the required JAXP classes are not     *                              availabile on the classpath.     */    static void checkJAXPAvailability() {        try {            // TODO: Do some real class checking here instead of forcing the            //       load of a JGroups class that happens (by default) to do it            //       for us.            XmlConfigurator.class.getName();        }        catch (NoClassDefFoundError error) {            Error tmp=new NoClassDefFoundError(JAXP_MISSING_ERROR_MSG);            tmp.initCause(error);            throw tmp;        }    }    /** Replace variables of the form ${var:default} with the getProperty(var, default)     * @param configurator     */    public static void substituteVariables(ProtocolStackConfigurator configurator) {        ProtocolData[] protocols;        try {            protocols=configurator.getProtocolStack();        }        catch(Exception e) {            protocols=null;        }        if(protocols == null)            return;        for(int i=0; i < protocols.length; i++) {            ProtocolData protocol=protocols[i];            if(protocol != null) {                HashMap parms=protocol.getParameters();                Map.Entry entry;                ProtocolParameter parm;                for(Iterator it=parms.entrySet().iterator(); it.hasNext();) {                    entry=(Map.Entry)it.next();                    parm=(ProtocolParameter)entry.getValue();                    String val=parm.getValue();                    String replacement=Util.substituteVariable(val);                    if(!replacement.equals(val)) {                        parm.setValue(replacement);                    }                }            }        }    }}

⌨️ 快捷键说明

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