📄 address.java
字号:
/************************************************************************
*
* $Id: Address.java,v 1.2 2002/03/04 21:42:57 echtcherbina Exp $
*
* 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 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.
*********************************************************************************/
package net.jxta.impl.endpoint;
import net.jxta.impl.endpoint.*;
import net.jxta.endpoint.*;
import java.io.*;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
/**
* The Address class implements the JXTA EndpointService address.
* A JXTA endpoint address is a URI that has the following form:
*
* protocol://network address/service name/optional parameters
*
* For instance, a JXTA Peer on TCP, will have its PipeService Service address
* defines as:
*
* tcp://192.168.1.10/JxtaPipe
*
* A particular pipe on would have the following address:
*
* tcp://192.168.1.10/JxtaPipe/000...128 bit UUID of the pipe
*
*/
public class Address implements EndpointAddress {
private static final Category LOG = Category.getInstance(Address.class.getName());
String protocol;
String protocolAddress;
String service;
String serviceParam;
/**
* Constructor: builds an empty Address.
*/
public Address () {
protocol = null;
protocolAddress = null;
service = null;
serviceParam = null;
}
/**
* Constructor: builds an Address from a string
*/
public Address (String address) {
parse (address);
}
public Address(byte[] bytes)
{
try {
parse(new String(bytes, "UTF8"));
} catch(UnsupportedEncodingException ex) {
throw new RuntimeException(ex.toString());
}
}
/**
* Returns a clone of this object.
*
* @return the cloned object
*
* @since JXTA 1.0
**/
public Object clone( ) {
Address a = new Address();
a.protocol = protocol;
a.protocolAddress = protocolAddress;
a.service = service;
a.serviceParam = serviceParam;
return a;
}
/**
* Compares two Addresses for equality.
*
* @param target the Address to be compared against.
* @return boolean true if Addresses are equal, false otherwise.
*
* @since JXTA 1.0
**/
public boolean equals( Object target ) {
if( this == target )
return true;
if (target instanceof Address ) {
return toString().equals( target.toString() );
}
else
return false;
}
/**
* Return a String that contains the canonical representation of the
* EndpointService Address.
*
* @return a String containing the EndpointAddress
*/
public synchronized String toString() {
// FIXME: by outputing a trailing "/" when there's no service
// we ellicit an exception when paring that string back into
// an endpoint address. The end-result is correct, though, but
// it more expensive to detect the syntax with an exception or
// an add-hoc test, than just obtaining what is expected, that
// is no trailing "/". If the trailing "/" is required, then
// parse should expect it.
if (service == null) {
return protocol + "://" + protocolAddress + "/";
}
if (serviceParam != null) {
return protocol + "://" + protocolAddress + "/" +
service + "/" + serviceParam;
}
return protocol + "://" + protocolAddress + "/" + service;
}
public synchronized byte[] getBytes()
{
return toString().getBytes();
}
/**
* Return a String that contains the name of the protocol
* contained in the EndpointAddress
*
* @return a String containing the protocol name
* @throw IOExcetion if the EndpointAddress is malformed.
*/
public synchronized String getProtocolName() {
return protocol;
}
/**
* Return a String that contains the name of the protocol
* address contained in the EndpointAddress
*
* @return a String containing the protocol address
* @throw IOExcetion if the EndpointAddress is malformed.
*/
public synchronized String getProtocolAddress() {
return protocolAddress;
}
/**
* Return a String that contains the name of the service name
* contained in the EndpointAddress
*
* @return a String containing the service name
*/
public synchronized String getServiceName() {
return service;
}
/**
* Return a String that contains the name of the service
* parameter contained in the EndpointAddress
*
* @return a String containing the protocol name
*/
public synchronized String getServiceParameter() {
return serviceParam;
}
/**
* Set the protocol name.
*
* @param name String containing the name of the protocol
*/
public synchronized void setProtocolName(String name) {
protocol = name;
}
/**
* Set the protocol address.
*
* @param address String containing the peer address.
*/
public synchronized void setProtocolAddress(String address) {
protocolAddress = address;
}
/**
* Set the service name.
*
* @param name String containing the name of the service
*/
public synchronized void setServiceName(String name) {
service = name;
}
/**
* Set the service parameter
*
* @param name String containing the service parameter
*/
public synchronized void setServiceParameter(String param) {
serviceParam = param;
}
private void parse (String addr) {
int index = addr.indexOf ("://");
String tmp = null;
if (index == -1) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Address is not in absolute form" );
throw new IllegalArgumentException( "Address is not in absolute form" );
}
try {
protocol = addr.substring (0, index);
tmp = addr.substring (index + 3);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Protocol address is missing" );
throw new IllegalArgumentException( "Protocol address is missing" );
}
index = tmp.indexOf ("/");
if (index == -1) {
protocolAddress = tmp;
return;
}
try {
protocolAddress = tmp.substring (0, index);
tmp = tmp.substring (index + 1);
} catch (Exception e) {
// The exception should maybe be propagated.
// XXX: to be checked
return;
}
index = tmp.indexOf ("/");
if (index == -1) {
service = tmp;
return;
}
try {
service = tmp.substring (0, index);
tmp = tmp.substring (index + 1);
} catch (Exception e) {
// The exception should maybe be propagated.
// XXX: to be checked
return;
}
serviceParam = tmp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -