piperesolvermsg.java
来自「jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,」· Java 代码 · 共 335 行
JAVA
335 行
/*
* Copyright (c) 2002 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 SUN MICROSYSTEMS 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: PipeResolverMsg.java,v 1.3 2002/06/18 20:46:00 hamada Exp $
*/
package net.jxta.impl.protocol;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.StringReader;
import java.io.Writer;
import java.net.URL;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Element;
import net.jxta.document.TextElement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.Document;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredDocumentUtils;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.peer.PeerID;
import net.jxta.pipe.PipeID;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.PipeResolverMessage;
/**
* This class implements net.jxta.protocol.PipeResolverMessage.
*
* It implements the message with the following schema.
*
* <pre><tt>
* <xs:complexType name="PipeResolver">
* <!-- MsgType should be an enumeration choice of "Query" and "Answer" -->
* <xs:element name="MsgType" type="xs:string"/>
* <xs:element name="PipeId" type="JXTAID"/>
* <xs:element name="Type" type="xs:string"/>
* <xs:element name="Peer" type="JXTAID" minOccurs="0" maxOccurs="unbounded"/>
*
* <!-- following are used in the query -->
* <xs:element name="Cached" type="xs:boolean" default="true" minOccurs="0"/>
*
* <!-- following are used in the answer -->
* <xs:element name="Found" type="xs:boolean" default="true" minOccurs="0"/>
* <!-- This should refer to a peer adv, but is instead a whole doc -->
* <xs:element name="PeerAdv" type="xs:string" minOccurs="0"/>
* </xs:complexType>
* </tt></pre>
*
*/
public class PipeResolverMsg extends PipeResolverMessage {
/**
* Log4J categorgy
**/
private static final Category LOG = Category.getInstance( PipeResolverMsg.class.getName());
private final static String MsgTypeTag = "MsgType";
private final static String PipeIdTag = "PipeId";
private final static String PipeTypeTag = "Type";
private final static String PeerIdTag = "Peer";
private final static String CachedTag = "Cached";
private final static String PeerAdvTag = "PeerAdv";
private final static String FoundTag = "Found";
private final static String QueryMsgType = "Query";
private final static String AnswerMsgType = "Answer";
public PipeResolverMsg() {
super();
}
public PipeResolverMsg(Element root) {
super();
initialize( root );
}
/**
* Initializes the message from a document.
**/
public void initialize(Element root) {
if( !TextElement.class.isInstance( root ) )
throw new IllegalArgumentException( getClass().getName() + " only supports TextElement" );
TextElement doc = (TextElement) root;
String docName = doc.getName();
if( !docName.equals(getMessageType()) )
throw new IllegalArgumentException( "Could not construct : "
+ getClass().getName()
+ "from doc containing a "
+ docName );
Enumeration enum = doc.getChildren();
while (enum.hasMoreElements()) {
TextElement elem = (TextElement) enum.nextElement();
if (elem.getName().equals(MsgTypeTag)) {
String msgtype = elem.getTextValue();
if( msgtype.equals( QueryMsgType ) )
setMsgType( PipeResolverMessage.MessageType.QUERY );
else if ( msgtype.equals( AnswerMsgType ) )
setMsgType( PipeResolverMessage.MessageType.ANSWER );
else
throw new IllegalArgumentException( "Unexpected Message Type in parsing." );
continue;
}
if (elem.getName().equals(PipeIdTag)) {
try {
URL pipeID = IDFactory.jxtaURL( elem.getTextValue() );
setPipeID( IDFactory.fromURL( pipeID ) );
} catch ( MalformedURLException badID ) {
throw new IllegalArgumentException( "Bad pipe ID in message" );
} catch ( UnknownServiceException badID ) {
throw new IllegalArgumentException( "Unusable pipe ID in message" );
}
continue;
}
if (elem.getName().equals(PipeTypeTag)) {
setPipeType( elem.getTextValue() );
continue;
}
if (elem.getName().equals(PeerIdTag)) {
try {
URL peerID = IDFactory.jxtaURL( elem.getTextValue() );
addPeerID( IDFactory.fromURL( peerID ) );
} catch ( MalformedURLException badID ) {
throw new IllegalArgumentException( "Bad peer ID in message" );
} catch ( UnknownServiceException badID ) {
throw new IllegalArgumentException( "Unusable peerID in message" );
}
continue;
}
if (elem.getName().equals(FoundTag)) {
setFound( Boolean.valueOf(elem.getTextValue()).booleanValue() );
continue;
}
if (elem.getName().equals(CachedTag)) {
setCachedLookup( Boolean.valueOf(elem.getTextValue()).booleanValue() );
continue;
}
// let's check whether the responder sent us a adv
if (elem.getName().equals(PeerAdvTag)) {
String peerAdv = elem.getTextValue();
try {
setInputPeerAdv( (PeerAdvertisement) AdvertisementFactory.newAdvertisement(
new MimeMediaType("text/xml"),
new StringReader(peerAdv)) );
} catch ( IOException caught ) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug( "Malformed peer adv in message", caught );
throw new IllegalArgumentException( "Malformed peer adv in message : " + caught.getMessage() );
}
continue;
}
}
// Begin checking sanity!
PipeResolverMessage.MessageType msgType = getMsgType();
if ( null == msgType )
throw new IllegalArgumentException( "Message type was never set!" );
ID pipeID = getPipeID();
if( (null == pipeID) || ID.nullID.equals(pipeID) || !(pipeID instanceof PipeID) )
throw new IllegalArgumentException( "Input Pipe ID not set or invalid" );
if ( null == getPipeType() )
throw new IllegalArgumentException( "Pipe type was never set!" );
//Query extra checks
//Response extra checks
if ( PipeResolverMessage.MessageType.ANSWER.equals( msgType ) ) {
if( !getPeerIDs().hasMoreElements() ) {
throw new IllegalArgumentException( "An answer without responses is invalid" );
}
}
}
/**
* Creates a document out of the message.
*
* @param encodeAs The document representation format requested.
* @return the message as a document.
**/
public Document getDocument(MimeMediaType encodeAs) {
StructuredTextDocument doc = (StructuredTextDocument)
StructuredDocumentFactory.newStructuredDocument(encodeAs, getMessageType() );
PipeResolverMessage.MessageType msgType = getMsgType();
if ( null == msgType )
throw new IllegalStateException( "Message type was never set!" );
if ( null == getPipeType() )
throw new IllegalStateException( "Pipe type was never set!" );
ID pipeID = getPipeID();
if( (null == pipeID) || ID.nullID.equals(pipeID) || !(pipeID instanceof PipeID) )
throw new IllegalStateException( "Input Pipe ID not set or invalid" );
Element e = null;
if( PipeResolverMessage.MessageType.QUERY.equals( msgType ) ) {
e = doc.createElement(MsgTypeTag, QueryMsgType);
} else if ( PipeResolverMessage.MessageType.ANSWER.equals( msgType ) ) {
e = doc.createElement(MsgTypeTag, AnswerMsgType);
} else {
throw new IllegalStateException( "Unknown message type :" + msgType.toString() );
}
doc.appendChild(e);
e = doc.createElement(PipeIdTag, pipeID.toString() );
doc.appendChild(e);
String pipeType = getPipeType();
if( (null != pipeType) && (0 != pipeType.length()) ) {
e = doc.createElement( PipeTypeTag, pipeType );
doc.appendChild(e);
}
// Write the peer ids.
Enumeration peers = getPeerIDs();
while( peers.hasMoreElements() ) {
ID aPeer = (ID) peers.nextElement();
if( aPeer instanceof PeerID) {
e = doc.createElement(PeerIdTag, aPeer.toString() );
doc.appendChild(e);
} else {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug( "Malformed peer id in list : " + aPeer );
}
}
if( PipeResolverMessage.MessageType.QUERY.equals( msgType ) ) {
boolean cachedLookup = isCachedLookup();
if ( cachedLookup ) {
e = doc.createElement(CachedTag, Boolean.TRUE.toString() );
doc.appendChild(e);
}
} else if ( PipeResolverMessage.MessageType.ANSWER.equals( msgType ) ) {
e = doc.createElement(FoundTag, (isFound() ? Boolean.TRUE : Boolean.FALSE).toString() );
doc.appendChild(e);
PeerAdvertisement peerAdv = getInputPeerAdv();
if( null == peerAdv )
throw new IllegalStateException( "Peer Advertisement must be present if respoinding with found" );
Writer stringWriter = new StringWriter( );
try {
StructuredTextDocument asDoc = (StructuredTextDocument)
peerAdv.getDocument( new MimeMediaType( "text/xml" ) );
asDoc.sendToWriter( stringWriter );
e = doc.createElement( PeerAdvTag, stringWriter.toString() );
doc.appendChild(e);
} catch ( IOException caught ) {
throw new IllegalStateException( "Malformed Peer Advertisement :" + caught.getMessage() );
}
} else {
throw new IllegalStateException( "Unknown message type :" + msgType.toString() );
}
return doc;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?