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

📄 util.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights *  reserved. * *  Redistribution and use in source and binary forms, with or without *  modification, are permitted provided that the following conditions *  are met: * *  1. Redistributions of source code must retain the above copyright *  notice, this list of conditions and the following disclaimer. * *  2. 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. * *  3. The end-user documentation included with the redistribution, *  if any, must include the following acknowledgment: *  "This product includes software developed by the *  Sun Microsystems, Inc. for Project JXTA." *  Alternately, this acknowledgment may appear in the software itself, *  if and wherever such third-party acknowledgments normally appear. * *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" *  must not be used to endorse or promote products derived from this *  software without prior written permission. For written *  permission, please contact Project JXTA at http://www.jxta.org. * *  5. Products derived from this software may not be called "JXTA", *  nor may "JXTA" appear in their name, without prior written *  permission of Sun. * *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE APACHE SOFTWARE FOUNDATION OR *  ITS CONTRIBUTORS 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. *  ==================================================================== * *  This software consists of voluntary contributions made by many *  individuals on behalf of Project JXTA.  For more *  information on Project JXTA, please see *  <http://www.jxta.org/>. * *  This license is based on the BSD license adopted by the Apache Foundation. * *  $Id: Util.java,v 1.25 2006/06/08 04:31:09 gonzo Exp $ */package net.jxta.ext.config;import net.jxta.ext.http.Dispatcher;import net.jxta.ext.http.Message;import net.jxta.impl.endpoint.IPUtils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.StringReader;import java.io.StreamTokenizer;import java.net.BindException;import java.net.InetAddress;import java.net.MalformedURLException;import java.net.ServerSocket;import java.net.UnknownHostException;import java.net.URI;import java.net.URL;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** * Utilities. * * @author    james todd [gonzo at jxta dot org] */public class Util {    private final static String MACRO_PREFIX = "${";    private final static String MACRO_POSTFIX = "}";    private final static String MACRO_ESCAPE = "\\";    private final static String NEW_LINE = "\n";    private final static String EMPTY_STRING = "";    private final static String COLON = ":";    private final static String BRACKET_OPEN = "[";    private final static String BRACKET_CLOSE = "]";    private final static List FILE_PROPERTIES;    private static final String SCHEME_FILE = "file";    private final static String HOST_ANY = "host.any";    private final static String HOST_LOCAL = "host.local";    private final static long MAX_WAIT = 7 * 1000;    private final static Logger LOG = Logger.getLogger(Util.class.getName());    static {        FILE_PROPERTIES = new ArrayList();                FILE_PROPERTIES.add("java.io.tmpdir");        FILE_PROPERTIES.add("java.home");        FILE_PROPERTIES.add("user.dir");        FILE_PROPERTIES.add("user.home");    }    /**     * Expands a {@link net.jxta.ext.config.Resource} macro expression.     */        public static String expand(String s) {        StringBuffer sb = null;        if (s != null) {            sb = new StringBuffer(s);            int i = -1;            int j = -1;            while ((i = sb.indexOf(MACRO_PREFIX)) > -1 &&                   (i - MACRO_ESCAPE.length() < 0 ||                    (i - MACRO_ESCAPE.length() >= 0 &&                     !sb.substring(i - MACRO_ESCAPE.length(), i).equals(MACRO_ESCAPE))) &&                   (j = sb.indexOf(MACRO_POSTFIX, i + MACRO_PREFIX.length())) > -1) {                String p = expand(sb.substring(i + MACRO_PREFIX.length(), j));                String r = System.getProperty(p);		if (r != null) {		    if (FILE_PROPERTIES.contains(p)) {                        r = new File(r).toURI().toString();		    }		} else {                    if (HOST_ANY.equals(p)) {                        r = IPUtils.ANYADDRESS.getHostAddress();                    } else if (HOST_LOCAL.equals(p)) {                        r = IPUtils.LOOPBACK.getHostAddress();                    } else {                        r = "";                    }                }                sb.replace(i, j + MACRO_POSTFIX.length(), r);            }        }        return sb != null ? sb.toString() : null;    }    /**     * Accessor for the local address.     *     * @return    the local address     */        public static String getLocalHost() {        String s = null;        try {            s = InetAddress.getLocalHost().getHostAddress();        } catch (UnknownHostException uhe) {            if (LOG.isEnabledFor(Level.ERROR)) {                LOG.error("can't resolve local host", uhe);            }        }        return s;    }    /**     * {@link java.net.URI} address validator.     *     * @param   address     address     * @return              validated address     */        public static String validateAddress(URI address) {        return validateAddress(address, (boolean)true);    }    /**     * {@link java.net.URI} address minimum port specification validator.     *     * @param   address         address     * @param   minimumPort     minimum port specification     * @return                  validated address     */        public static String validateAddress(URI address, int minimumPort) {        return validateAddress(address, (String)null, minimumPort);    }    /**     * {@link java.net.URI} address non-specified host validator.     *     * @param   address         address     * @param   requireHost     require host check     * @return                  validated address     */        public static String validateAddress(URI address, boolean requireHost) {        return validateAddress(address, (String)null, requireHost);    }    /**     * {@link java.net.URI} address scheme validator.     *     * @param   address         address     * @param   scheme          address scheme check     * @return                  validated address     */        public static String validateAddress(URI address, String scheme) {        return validateAddress(address, scheme, (boolean)false);    }    /**     * {@link java.net.URI} address scheme and host validator.     *     * @param   address         addresss     * @param   scheme          address scheme check     * @param   requireHost     require host check     * @return                  validated address     */        public static String validateAddress(URI address, String scheme, boolean requireHost) {        return validateAddress(address, scheme, requireHost, Default.MINIMUM_DYNAMIC_PORT);    }    /**     * {@link java.net.URI} address scheme and minimum port validator.     *     * @param   address         addresss     * @param   scheme          address scheme check     * @param   minimumPort     minimum port check     * @return                  validated address     */        public static String validateAddress(URI address, String scheme, int minimumPort) {        return validateAddress(address, scheme, (boolean)false, minimumPort);    }    /**     * {@link java.net.URI} address scheme, host and minium port validator.     *     * @param   address         addresss     * @param   scheme          address scheme check     * @param   requireHost     required host check     * @param   minimumPort     minimum port check     * @return                  validated address     */        public static String validateAddress(URI address, String scheme, boolean requireHost,                                         int minimumPort) {        StringBuffer b = new StringBuffer();        if (address != null) {            if (scheme != null &&                !address.getScheme().equalsIgnoreCase(scheme)) {                b.append((b.length() > 0 ? NEW_LINE : EMPTY_STRING) + "invalid scheme: " +                         scheme);            }            if (requireHost) {                String h = address.getHost();                if (h == null ||                    h.trim().length() == 0) {                    b.append((b.length() > 0 ? NEW_LINE : EMPTY_STRING) + "invalid host: " + h);                }                if (address.getPort() < minimumPort ||                    address.getPort() > Default.MAXIMUM_PORT) {                    b.append((b.length() > 0 ? NEW_LINE : EMPTY_STRING) + "invalid port: " + address.getPort());                }            }        } else {            b.append((b.length() > 0 ? NEW_LINE : EMPTY_STRING) + "null address");        }        return b.toString();    }    /**     * {@link net.jxta.ext.config.Address} normalizer.     *     * @param   address     address     * @return              normalized {@link java.net.URI}     */        public static URI normalize(Address address) {        return normalize(address, true);    }    /**     * {@link net.jxta.ext.config.Address} normalizer including port availability.     *     * @param   address     address     * @param   portScan    port availability check     * @return              normalized {@link java.net.URI}     */    public static URI normalize(Address address, boolean portScan) {        URI u = address != null ? address.getAddress() : null;        return normalize(address, portScan,                         model(u != null ? u.getScheme() : null));    }    /**     * {@link net.jxta.ext.config.Address} normalizer including port availability and {@link java.net.URI}     * template.     *     * @param   address     address     * @param   portScan    port availability check     * @param   model       template {@link java.net.URI}     * @return              normalized {@link java.net.URI}     */        public static URI normalize(Address address, boolean portScan, URI model) {        URI u = address != null ? address.getAddress() : null;        if (u != null) {            String s = u.getScheme();            String h = u.getHost();            int p = u.getPort();            if (model != null) {                if (s == null ||                    s.trim().length() == 0) {                    s = model.getScheme();                }                if (h == null ||                    h.trim().length() == 0) {                    h = model.getHost();                }                if (p == Default.INVALID_PORT) {                    p = model.getPort();                }            }            try {                address.setAddress(new URI(u.getScheme(), u.getUserInfo(),                                           h, p, u.getPath(), u.getQuery(),                                           u.getFragment()));                u = new URI(u.getScheme(), u.getUserInfo(), h,                            portScan ? getNextAvailablePort(address, p) : p,                            u.getPath(), u.getQuery(), u.getFragment());            } catch (URISyntaxException use) {                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("invalid transformation", use);                }            }        }        return u;    }    public static URI normalizeURI(URI u) {        return normalizeURI(u != null ? u.toString() : null);    }        public static URI normalizeURI(String s) {        URI u = null;                s = s != null ? s.trim() : null;                if (s != null &&            s.length() > 0) {            try {                u = new URI(s);            } catch (URISyntaxException use) {                use.printStackTrace();                if (LOG.isEnabledFor(Level.ERROR)) {                    LOG.error("invalid uri: " + s);                }            }                        if (u == null ||                u.getScheme() == null ||                (! u.getScheme().equalsIgnoreCase(SCHEME_FILE) &&                    (u.getHost() == null ||                        u.getPort() == -1))) {                String us = u != null ? u.toString() : null;                String protocol = u != null ?                    u.getScheme() + Protocol.URI_DELIMITER : null;                if (protocol == null ||                    protocol.trim().length() == 0) {                    int i = us.indexOf(Protocol.URI_DELIMITER);                                    if (i > -1) {                        protocol = getProtocolURI(us.substring(0, i));                    }                }                                String host = null;                                if (u != null) {

⌨️ 快捷键说明

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