package com.sun.slp;
import java.util.*;
import java.io.*;
import java.net.*;
public class ServiceURL extends Object implements Serializable {
private final static String IPX = "ipx";
private final static String AT = "at";
public static final int NO_PORT = 0;
public static final int LIFETIME_NONE = 0;
public static final int LIFETIME_DEFAULT = 10800;
public static final int LIFETIME_MAXIMUM = 0xFFFF;
public static final int LIFETIME_PERMANENT = -1;
static final int PORT_MAXIMUM = 0xFFFF;
private ServiceType serviceType = null;
private ServiceType originalServiceType = null;
private String transport = "";
private String host = "";
private int port = NO_PORT;
private String URLPath = "";
private int lifetime = LIFETIME_DEFAULT;
private boolean isPermanent = false;
private boolean noDoubleSlash = false;
public ServiceURL(String URL, int iLifetime)
throws IllegalArgumentException {
Assert.nonNullParameter(URL, "URL");
if ((iLifetime > LIFETIME_MAXIMUM) ||
(iLifetime < LIFETIME_PERMANENT)) {
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("lifetime_error",
new Object[0]));
}
checkURLString(URL);
parseURL(URL);
if (iLifetime == LIFETIME_PERMANENT) {
isPermanent = true;
iLifetime = LIFETIME_MAXIMUM;
}
lifetime = iLifetime;
}
public ServiceType getServiceType() {
return serviceType;
}
public void setServiceType(ServiceType type) {
if (!serviceType.isServiceURL()) {
serviceType = type;
}
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getURLPath() {
return URLPath;
}
public int getLifetime() {
return lifetime;
}
public String toString() {
return
originalServiceType.toString() +
":/" + transport + (noDoubleSlash == false ? "/":"") +
host + (port != NO_PORT ? (":" + port) : "") +
URLPath;
}
public int hashCode() {
return
serviceType.hashCode() +
transport.hashCode() +
host.hashCode() +
port +
URLPath.hashCode();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ServiceURL)) {
return false;
}
ServiceURL surl = (ServiceURL)obj;
return
serviceType.equals(surl.serviceType) &&
transport.equals(surl.transport) &&
host.equals(surl.host) &&
(port == surl.port) &&
(noDoubleSlash == surl.noDoubleSlash) &&
URLPath.equals(surl.URLPath);
}
boolean getIsPermanent() {
return isPermanent;
}
private void checkURLString(String s)
throws IllegalArgumentException {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '/' || c == ':' || c == '-' || c == ':' ||
c == '.' || c == '%' || c == '_' || c == '\'' ||
c == '*' || c == '(' || c == ')' || c == '$' ||
c == '!' || c == ',' || c == '+' || c == '\\') {
continue;
}
if (c == ';' || c == '@' || c == '?' || c == '&' || c == '=') {
continue;
}
if (Character.isLetterOrDigit(c)) {
continue;
}
SLPConfig conf = SLPConfig.getSLPConfig();
throw
new IllegalArgumentException(
conf.formatMessage("url_char_error",
new Object[] {
Character.valueOf(c)}));
}
}
private void parseURL(String sURL)
throws IllegalArgumentException {
StringTokenizer st = new StringTokenizer(sURL, "/", true);
try {
do {
String typeName = st.nextToken();
if (typeName.equals("/")) {
break;
}
if (!typeName.endsWith(":")) {
break;
}
serviceType =
new ServiceType(typeName.substring(0,
typeName.length() - 1));
originalServiceType = serviceType;
String slash1 = st.nextToken();
if (!slash1.equals("/")) {
break;
}
String slash2 = st.nextToken();
String sAddr = "";
if (!slash2.equals("/")) {
if (!serviceType.isServiceURL()) {
sAddr = slash2;
noDoubleSlash = true;
} else {
if (!slash2.equalsIgnoreCase(IPX) &&
!slash2.equalsIgnoreCase(AT)) {
if (serviceType.isAbstractType()) {
sAddr = slash2;
noDoubleSlash = true;
} else {
break;
}
} else {
transport = slash2.toLowerCase();
if (!st.nextToken().equals("/")) {
break;
}
sAddr = st.nextToken();
}
}
} else {
sAddr = st.nextToken();
}
if (sAddr.equals("/")) {
URLPath = "/" + st.nextToken("");
return;
}
host = sAddr;
if (transport.equals("")) {
StringTokenizer tk = new StringTokenizer(host, ":");
host = tk.nextToken();
if (tk.hasMoreTokens()) {
String p = tk.nextToken();
if (tk.hasMoreTokens()) {
break;
}
try {
port = Integer.parseInt(p);
} catch (NumberFormatException ex) {
break;
}
if (port <= 0 || port > PORT_MAXIMUM) {
break;
}
}
}
if (st.hasMoreTokens() == false) {
return;
}
String sSep = st.nextToken();
if (!sSep.equals("/")) {
break;
}
URLPath = sSep;
if (st.hasMoreTokens()) {
URLPath += st.nextToken("");
}
URLPath = URLPath.trim();
return;
} while (false);
} catch (NoSuchElementException ex) {
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("url_syntax_error",
new Object[] {sURL}));
}
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("url_syntax_error",
new Object[] {sURL}));
}
}