/* * Copyright (c) 1999,2009 Declarative Engineering LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Declarative Engineering LLC * verson 1 which accompanies this distribution, and is available at * http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */ package com.de22.orb; import com.common.comparison.Comparator; import com.common.debug.*; public class Address implements IAddress { public static final int INVALID_PORT = -1; private String name = null; private int port = INVALID_PORT; private String resource = null; /** * Address constructor. */ public Address() { this(null, -1, null); }//Address()// /** * Address constructor. * @param address The address in string form which follows the URL rules. */ public Address(String address) { super(); parseAddress(address); }//Address()// /** * Address constructor. * @param name The address URL or IP address. * @param port The address port number. */ public Address(String name, int port) { this(name, port, null); }//Address()// /** * Address constructor. *

Note: At least one of the parameters should normally be non-null. Some methods will require that multiple parameters be non-null.

* @param name The address URL or IP address. * @param port The address port number. * @param resource The referenced resource at the address. */ public Address(String name, int port, String resource) { super(); this.name = name; this.port = port; this.resource = resource; }//Address()// /* (non-Javadoc) * @see java.lang.Object#clone() */ public Object clone() throws CloneNotSupportedException { return super.clone(); }//clone()// /* (non-Javadoc) * @see java.lang.Object#hashCode() */ public int hashCode() { return port ^ (name != null ? name.hashCode() : 0) ^ (resource != null ? resource.hashCode() : 0); }//hashCode()// /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object object) { return object instanceof Address && ((Address) object).port == port && Comparator.equals(((Address) object).name, name) && Comparator.equals(((Address) object).resource, resource); }//equals()// /** * Gets the ip address or machine name that identifies the first part of the address. * @return The IP or URL name of the computer being addressed. */ public String getName() { return name; }//getName()// /** * Gets the port number that identifies the second part of the address. * @return The addressed computer's port identifying the listener (server socket) being addressed. */ public int getPort() { return port; }//getPort()// /** * Gets the name of the resource that is being referenced at the address. * @return The addressed resource's name. */ public String getResource() { return resource; }//getResource()// /** * Parses the address from a string containing the name and port in the format (or variation of): [protocol]://[name]:[port]/[object]. * @param address The address in string form. */ private void parseAddress(String address) { address = address.trim(); try { int lastIndex = 0; int index = address.indexOf("://", lastIndex); //Try both slash directions.// if(index == -1) { index = address.indexOf(":\\\\", lastIndex); }//if// //Ignore beginning protocol and slashes.// if(index != -1) { //TODO: Capture the protocol.// lastIndex = index + 3; }//if// index = address.indexOf("//", lastIndex); //Try both slash directions.// if(index == -1) { index = address.indexOf("\\\\", lastIndex); }//if// //Ignore beginning slashes.// if(index != -1) { lastIndex = index + 2; }//if// index = address.indexOf(':', lastIndex); //Capture the name and or port.// if((index != -1) && (address.indexOf('/', lastIndex) != lastIndex) && (address.indexOf('\\', lastIndex) != lastIndex)) { int index2 = 0; if(index != lastIndex) { name = address.substring(lastIndex, index); }//if// lastIndex = index + 1; index = address.indexOf('/', lastIndex); index2 = address.indexOf("\\", lastIndex); index = (index2 != -1) && (index2 < index) ? index2 : index; if(index == -1) { port = Integer.parseInt(address.substring(lastIndex)); lastIndex = -1; }//if// else { port = Integer.parseInt(address.substring(lastIndex, index)); lastIndex = index; }//else// }//if// if((lastIndex != -1) && (name == null) && (address.indexOf('/', lastIndex) != lastIndex) && (address.indexOf('\\', lastIndex) != lastIndex)) { int index2 = address.indexOf("\\", lastIndex); index = address.indexOf('/', lastIndex); index = (index2 != -1) && (index2 < index) ? index2 : index; if(index == -1) { String part = address.substring(lastIndex); boolean isNumber = true; for(int ch = 0; isNumber && ch < part.length(); ch++) { isNumber = Character.isDigit(part.charAt(ch)); }//for// if(isNumber) { //This allows input of just a port number.// port = Integer.parseInt(part); }//if// else { name = address.substring(lastIndex); }//else// lastIndex = -1; }//if// else { name = address.substring(lastIndex, index); lastIndex = index + 1; }//else// }//if// //Read the resource name.// if(lastIndex != -1) { if(address.startsWith("\\", lastIndex) || address.startsWith("/", lastIndex)) { lastIndex++; }//if// resource = address.substring(lastIndex); }//if// }//try// catch(Throwable e) { Debug.log(e); Debug.halt(); }//catch// }//parseAddress()// /** * Sets the ip address or machine name that identifies the first part of the address. * @param name The IP or URL name of the computer being addressed. */ public void setName(String name) { this.name = name; }//setName()// /** * Sets the port number that identifies the second part of the address. * @param port The addressed computer's port identifying the listener (server socket) being addressed. */ public void setPort(int port) { this.port = port; }//setPort()// /** * Sets the name of the resource that is being referenced at the address. * @param resource The addressed resource's name. */ public void setResource(String resource) { this.resource = resource; }//setResource()// /** * Gets a string representation of the address. * @return A representation of the address in string form. */ public String toString() { String result = ""; if(name != null) { result += name; if(port != INVALID_PORT) { result += ":" + port; }//if// }//if// else if(port != INVALID_PORT) { result += ":" + port; }//else if// if(resource != null) { result += '/' + resource; }//if// return result; }//toString()// }//Address//