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

📄 discoveryresponse.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }        }        try {            while (advs.hasMoreElements()) {                Long l = (Long) exps.nextElement();                Object response = advs.nextElement();                if (response instanceof InputStream) {                    e = adv.createElement(responsesTag, streamToString((InputStream) response));                } else {                    e = adv.createElement(responsesTag, response.toString());                }                adv.appendChild(e);                if (adv instanceof Attributable) {                    ((Attributable) e).addAttribute(expirationTag, l.toString());                }            }        } catch (Exception failed) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Got an Exception during doc creation", failed);            }            IllegalStateException failure = new IllegalStateException("Got an Exception during doc creation");            failure.initCause(failed);            throw failure;        }        return adv;    }    /**     *  Description of the Method     */    private void parseAdvertisements() {        List advertisements = new ArrayList();        Enumeration eachResponse = getResponses();        while (eachResponse.hasMoreElements()) {            Object response = eachResponse.nextElement();            if (response instanceof String) {                String str = (String) response;                try {                    Advertisement adv = (Advertisement)                                        AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, new StringReader(str));                    advertisements.add(adv);                } catch (Exception e) {                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("No advertisements in response element", e);                    }                }            } else                if (response instanceof InputStream) {                    InputStream is = (InputStream) response;                    try {                        Advertisement adv = (Advertisement)                                            AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);                        advertisements.add(adv);                    } catch (Exception e) {                        if (LOG.isEnabledFor(Level.WARN)) {                            LOG.warn("Can not parse Response", e);                        }                    }                } else {                    if (LOG.isEnabledFor(Level.WARN)) {                        LOG.warn("Can not parse Response of type " + response.getClass().getName());                    }                }        }        setAdvertisements(advertisements);    }    /**     *  Parses a document into this object     *     *@param  doc  Document     */    private void readIt(TextElement doc) {        Vector res = new Vector();        Vector exps = new Vector();        try {            Enumeration elements = doc.getChildren();            while (elements.hasMoreElements()) {                TextElement elem = (TextElement) elements.nextElement();                if (elem.getName().equals(typeTag)) {                    type = Integer.parseInt(elem.getTextValue());                    continue;                }                if (elem.getName().equals(peerAdvTag)) {                    String peerString = elem.getTextValue();                    if(null == peerString) {                        continue;                    }                    peerString = peerString.trim();                    if(peerString.length() > 0) {                        setPeerAdvertisement((PeerAdvertisement)                                              AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, new StringReader(elem.getTextValue())));                    }                    continue;                }                if (elem.getName().equals(queryAttrTag)) {                    setQueryAttr(elem.getTextValue());                    continue;                }                if (elem.getName().equals(queryValueTag)) {                    setQueryValue(elem.getTextValue());                    continue;                }                if (elem.getName().equals(responsesTag)) {                    // get the response                    String aResponse = elem.getTextValue();                    if (null == aResponse) {                        if (LOG.isEnabledFor(Level.DEBUG)) {                            LOG.debug("Discarding an empty response tag");                        }                        continue;                    }                    res.add(aResponse);                    long exp;                    // get expiration associated with this response                    if(elem instanceof Attributable) {                        Attribute attr = ((Attributable) elem).getAttribute(expirationTag);                        if (null != attr) {                            exp = Long.parseLong(attr.getValue());                        } else {                            // if there are no attribute use DEFAULT_EXPIRATION                            if (LOG.isEnabledFor(Level.DEBUG)) {                                LOG.debug("Received an old-style DiscoveryResponse.\n " +                                          "You received a response from a peer that does \n" +                                          "not support advertisement aging. \n" +                                          "Setting expiration to DiscoveryService.DEFAULT_EXPIRATION ");                            }                            exp = DiscoveryService.DEFAULT_EXPIRATION;                        }                    } else {                        exp = DiscoveryService.DEFAULT_EXPIRATION;                    }                    exps.add(new Long(exp));                }            }        } catch (Exception failed) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Got an Exception during Parse ", failed);            }            IllegalArgumentException failure = new IllegalArgumentException("Got an Exception during parse");            failure.initCause(failed);            throw failure;        }        setResponses(res);        setExpirations(exps);    }    /**     *  Reads in a stream into a string     *     *@param  is  inputstream     *@return     string representation of a stream     */    private String streamToString(InputStream is) {        StringBuffer stw = new StringBuffer();        Reader reader = null;        try {            reader = new InputStreamReader(is, "UTF-8");        } catch (UnsupportedEncodingException uee) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("InputStreamReader creation error", uee);            }        }        char[] buf = new char[512];        try {            do {                int c = reader.read(buf);                if (c == -1) {                    break;                }                stw.append(buf, 0, c);            } while (true);        } catch (IOException ie) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Got an Exception during stream conversion", ie);            }            return null;        } finally {            try {                is.close();            } catch (IOException ignored) {}        }        return stw.toString();    }    /**     * {@inheritDoc}     */    public String toString() {        try {            StructuredTextDocument doc = (StructuredTextDocument) getDocument(MimeMediaType.XMLUTF8);            return doc.toString();        } catch (Throwable e) {            if (e instanceof Error) {                throw (Error) e;            } else                if (e instanceof RuntimeException) {                    throw (RuntimeException) e;                } else {                    throw new UndeclaredThrowableException(e);                }        }    }}

⌨️ 快捷键说明

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