message.java

来自「JXTA&#8482 is a set of open, generalize」· Java 代码 · 共 1,445 行 · 第 1/4 页

JAVA
1,445
字号
    /**     * {@inheritDoc}     * <p/>     * This implementation is intended to assist debugging. You should not     * depend upon the format of the result.     */    @Override    public String toString() {        StringBuilder toString = new StringBuilder(128);        toString.append(getClass().getName());        toString.append('@');        toString.append(super.hashCode());        toString.append('(');        toString.append(modCount);        toString.append("){");        Iterator allLineage = getMessageLineage();        while (allLineage.hasNext()) {            toString.append(allLineage.next().toString());            if (allLineage.hasNext()) {                toString.append(',');            }        }        toString.append('}');        if (GLOBAL_TRACKING_ELEMENT) {            toString.append("[");            Iterator eachUUID = getMessageElements("jxta", "Tracking UUID");            while (eachUUID.hasNext()) {                toString.append("[");                toString.append(eachUUID.next().toString());                toString.append("]");                if (eachUUID.hasNext()) {                    toString.append(',');                }            }            toString.append("]");        }        return toString.toString();    }    /**     * Read this Object in for Java Serialization     *     * @param s The stream from which the Object will be read.     * @throws IOException            for errors reading from the input stream.     * @throws ClassNotFoundException if the serialized representation contains     *                                references to classes which cannot be found.     */    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {        // reads defaultNamespace, modifiable flag        s.defaultReadObject();        MimeMediaType readType = new MimeMediaType(s.readUTF());        // XXX bondolo 20040307 Should do something with encoding here.        Message readMessage = WireFormatMessageFactory.fromWire(s, readType, null);        namespaces = readMessage.namespaces;        elements = readMessage.elements;        if (!namespaces.containsKey(defaultNamespace)) {            throw new IOException("Corrupted Object--does not contain required namespace.");        }        properties = new HashMap<Object, Object>();        lineage = new ArrayList<Integer>();        lineage.add(messagenumber.getAndIncrement());        if (LOG_MODIFICATIONS) {            modHistory = new ArrayList<Throwable>();            incMessageModCount();        }    }    /**     * Write this Object out for Java Serialization     *     * @param s The stream to which the Object will be written.     * @throws IOException for errors writing to the output stream.     */    private void writeObject(ObjectOutputStream s) throws IOException {        s.defaultWriteObject();        MimeMediaType writeType = WireFormatMessageFactory.DEFAULT_WIRE_MIME;        s.writeUTF(writeType.toString());        // XXX bondolo 20040307 Should do something with encoding here.        WireFormatMessage serialed = WireFormatMessageFactory.toWire(this, writeType, null);        serialed.sendToStream(s);    }    /**     * Return the default Namespace of this message.     *     * @return The default namespace for this message.     */    protected String getDefaultNamespace() {        return defaultNamespace;    }    /**     * Add a MessageElement into the message. The MessageElement is stored in     * the default namespace.     *     * @param add the Element to add to the message.     */    public void addMessageElement(MessageElement add) {        addMessageElement(null, add);    }    /**     * Add a MessageElement into the message using the specified namespace.     *     * @param namespace contains the namespace of the element to add. You can     *                  specify null as a shorthand for the default namespace.     * @param add       the MessageElement to add to the message.     */    public void addMessageElement(String namespace, MessageElement add) {        addMessageElement(namespace, add, null);    }    /**     * Add a MessageElement into the Message using the specified namespace.     *     * @param namespace contains the namespace of the element to add. You can     *                  specify null as a shorthand for the default namespace.     * @param add       the MessageElement to add to the message.     * @param signature The signature MessageElement associated with the     *                  MessageElement. This allows for an alternate signature element to the     *                  signature element associated with the message element.     */    public void addMessageElement(String namespace, MessageElement add, MessageElement signature) {        if (null == namespace) {            namespace = getDefaultNamespace();        }        if (null == add) {            throw new IllegalArgumentException("Message Element must be non-null");        }        elements.add(new element(namespace, add, signature));        List<MessageElement> namespaceElements = namespaces.get(namespace);        if (null == namespaceElements) {            namespaceElements = new ArrayList<MessageElement>();            namespaces.put(namespace, namespaceElements);        }        namespaceElements.add(add);        incMessageModCount();        if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) {            LOG.finer( "Added " + namespace + "::" + add.getElementName() + "/" +                     add.getClass().getName() + "@" + add.hashCode() + " to " + this);        }    }    /**     * Replace a {@link net.jxta.endpoint.MessageElement} in the message. This     * method will remove all MessageElement instances in the default namespace     * which match the specified name (if any) and then insert the replacement     * element. The existing version of the element is returned, if more than     * one matching element was removed, a random matching element is returned.     * <p/>     * For greatest control over element replacement, use the     * {@link java.util.ListIterator#set(java.lang.Object)} method as returned     * by {@link #getMessageElements()},     * {@link #getMessageElements(java.lang.String)} or     * {@link #getMessageElementsOfNamespace(java.lang.String)}     *     * @param replacement the Element to be inserted into to the message.     * @return One of the elements which was replaced or null if no existing     *         matching item was located.     */    public MessageElement replaceMessageElement(MessageElement replacement) {        return replaceMessageElement(null, replacement);    }    /**     * Replace a {@link net.jxta.endpoint.MessageElement} in the message using the specified     * namespace. This method will remove all MessageElement instances which     * match the specified name (if any) and then insert the replacement     * element. The existing version of the element is returned, if more than     * one matching element was removed, a random matching element is returned.     * <p/>     * For greatest control over element replacement, use the     * {@link java.util.ListIterator#set(java.lang.Object)} method as returned     * by {@link #getMessageElements()},     * {@link #getMessageElements(java.lang.String)} or     * {@link #getMessageElementsOfNamespace(java.lang.String)}     *     * @param namespace   contains the namespace of the element to be replaced.     *                    You can specify null as a shorthand for the default namespace.     * @param replacement the Element to be inserted into to the message.     * @return One of the elements which was replaced or null if no existing     *         matching item was located.     */    public MessageElement replaceMessageElement(String namespace, MessageElement replacement) {        if (null == namespace) {            namespace = getDefaultNamespace();        }        if (null == replacement) {            throw new IllegalArgumentException("Message Element must be non-null");        }        MessageElement removed = null;        Iterator<MessageElement> allMatching = getMessageElements(namespace, replacement.getElementName());        while (allMatching.hasNext()) {            MessageElement anElement = allMatching.next();            allMatching.remove();            removed = anElement;        }        addMessageElement(namespace, replacement); // updates mod count        return removed;    }    /**     * Returns an iterator of the namespaces present in this message.     *     * @return iterator of strings of the namespaces of this message.     */    public Iterator<String> getMessageNamespaces() {        return Collections.unmodifiableMap(namespaces).keySet().iterator();    }    /**     * Retrieve a message element by name from the message without regard to     * namespace. If there is more than one message element with this name, the     * first message element will be returned.     *     * @param name The name of the message element to attempt to retrieve.     * @return Element the element or null if no matching element could be     *         found.     */    public MessageElement getMessageElement(String name) {        Iterator<element> eachElement = elements.listIterator();        while (eachElement.hasNext()) {            element anElement = eachElement.next();            if (name.equals(anElement.element.getElementName())) {                return anElement.element;            }        }        return null;    }    /**     * Retrieve a message element by name in the specified namespace from the     * message. If there is more than one message element matching this name,     * the first message element will be returned.     *     * @param namespace contains the namespace of the element to get. You can     *                  specify {@code null} as a shorthand for the default namespace.     * @param name      The name of the message element to retrieve.     * @return The Message Element or {@code null} if no matching message     *         element could be found.     */    public MessageElement getMessageElement(String namespace, String name) {        if (null == namespace) {            namespace = getDefaultNamespace();        }        List<MessageElement> namespaceElements = namespaces.get(namespace);        // no namespace means no element.        if (null == namespaceElements) {            return null;        }        Iterator<MessageElement> eachElement = namespaceElements.listIterator();        while (eachElement.hasNext()) {            MessageElement anElement = eachElement.next();            if (name.equals(anElement.getElementName())) {                return anElement;            }        }        return null;    }    /**     * Returns a list iterator of all of the elements contained in this message.     * Elements from all namespaces are returned.     * <p/>     * The iterator returned is not synchronized with the message and will     * throw {@link java.util.ConcurrentModificationException} if the     * message is modified.     *     * @return Enumeration of Elements.     */    public ElementIterator getMessageElements() {        List<element> theMsgElements = new ArrayList<element>(elements);        return new ElementIterator(theMsgElements.listIterator());    }    /**     * Returns a list iterator  of all of the elements contained in this     * message who's name matches the specified name. Elements from all     * namespaces are returned. Message Elements are iterated in the order in     * which they were added to the Message.     * <p/>     * The iterator returned is not synchronized with the message and will     * throw {@link java.util.ConcurrentModificationException} if the     * message is modified.     *     * @param name the name of the elements to match against     * @return iterator of the elements matching the specified name, if any.     */    public ElementIterator getMessageElements(String name) {        List<element> theMsgElements = new ArrayList<element>(elements.size());        for (element anElement : elements) {            if (name.equals(anElement.element.getElementName())) {                theMsgElements.add(anElement);            }        }        return new ElementIterator(theMsgElements.listIterator());    }    /**     * Returns a list iterator of all of the elements contained in this message     * which match the specified namespace. Message Elements are iterated in     * the order in which they were added to the Message.     * <p/>     * The ListIterator returned is not synchronized with the message. If     * you modify the state of the Message, the iterator will throw     * ConcurrentModificationException when {@code next()} or     * {@code previous()} is called.     *     * @param namespace contains the namespace which must be matched in the     *                  elements returned. You can specify {@code null} as a shorthand for the     *                  default namespace.     * @return Iterator of Message Elements matching namespace.     */    public ElementIterator getMessageElementsOfNamespace(String namespace) {        List<element> theMsgElements = new ArrayList<element>(elements.size());        if (null == namespace) {            namespace = getDefaultNamespace();

⌨️ 快捷键说明

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