package com.sun.slp;
import java.util.*;
import java.net.*;
import java.io.*;
class SunServerDATable extends ServerDATable {
static final private String TIMESTAMP_ID =
"424242SUN-TABLE-TIMESTAMP424242";
static final private String ADDRESS_ID = "424242SUN-TABLE-ADDRESS424242";
private ServiceTable serviceTable = null;
private Vector saOnlyScopes = null;
SunServerDATable() {
try {
serviceTable = ServiceTable.getServiceTable();
} catch (ServiceLocationException ex) {
}
saOnlyScopes = conf.getSAOnlyScopes();
Assert.slpassert(saOnlyScopes.size() > 0,
"no_sa_scopes",
new Object[0]);
}
public synchronized long
recordNewDA(ServiceURL url,
Vector scopes,
long timestamp,
int version,
Vector attrs,
String spis) {
String addr = url.getHost();
long formerTimestamp = -1L;
Vector v = (Vector)scopes.clone();
ServiceLocationAttribute attr =
new ServiceLocationAttribute(SunDATable.SCOPES_ID, scopes);
attrs.addElement(attr);
Vector vals = new Vector();
vals.addElement(Long.toString(timestamp));
attr =
new ServiceLocationAttribute(SunServerDATable.TIMESTAMP_ID, vals);
attrs.addElement(attr);
vals = new Vector();
vals.addElement(Integer.valueOf(version));
attr = new ServiceLocationAttribute(SunDATable.VERSION_ID, vals);
attrs.addElement(attr);
vals = new Vector();
vals.addElement(url.getHost());
attr = new ServiceLocationAttribute(SunServerDATable.ADDRESS_ID, vals);
attrs.addElement(attr);
ServiceURL adURL = formServiceTableDAURL(url, attrs);
ServiceStore store = serviceTable.store;
try {
Vector tags = new Vector();
tags.addElement(SunServerDATable.TIMESTAMP_ID);
Hashtable attrRec =
store.findAttributes(adURL,
saOnlyScopes,
tags,
Defaults.locale);
Vector formerAttrs =
(Vector)attrRec.get(ServiceStore.FA_ATTRIBUTES);
if (formerAttrs != null && !(formerAttrs.size() <= 0)) {
attr = (ServiceLocationAttribute)formerAttrs.elementAt(0);
vals = attr.getValues();
String stamp = (String)vals.elementAt(0);
try {
formerTimestamp = Long.parseLong(stamp.trim());
} catch (NumberFormatException ex) {
Assert.slpassert(false,
"ssdat_number_format",
new Object[0]);
}
}
store.register(adURL,
attrs,
saOnlyScopes,
Defaults.locale,
null,
null);
LinkedList spiList =
AuthBlock.commaSeparatedListToLinkedList(spis);
InetAddress inetAddr = null;
try {
inetAddr = InetAddress.getByName(addr);
} catch (UnknownHostException e) {}
if (addr != null) {
daSPIsHash.put(inetAddr, spiList);
}
} catch (ServiceLocationException ex) {
conf.writeLog("ssdat_register_error",
new Object[] {
ex.getMessage(),
adURL,
saOnlyScopes});
}
return formerTimestamp;
}
public synchronized boolean removeDA(InetAddress address, Vector scopes) {
String query = "(" + ADDRESS_ID + "=" + address.getHostAddress() + ")";
ServiceStore store = serviceTable.store;
try {
Hashtable das = returnMatchingDAs(query);
Enumeration daURLs = das.keys();
while (daURLs.hasMoreElements()) {
ServiceURL adURL = (ServiceURL)daURLs.nextElement();
store.deregister(adURL, saOnlyScopes, null);
}
} catch (ServiceLocationException ex) {
conf.writeLog("ssdat_deregister_error",
new Object[] {
ex.getMessage(),
address,
saOnlyScopes});
return false;
}
return true;
}
public synchronized Hashtable returnMatchingDAs(String query)
throws ServiceLocationException {
ServiceStore store = ServiceTable.getServiceTable().store;
Vector saOnlyScopes = conf.getSAOnlyScopes();
Hashtable returns =
store.findServices(Defaults.SUN_DA_SERVICE_TYPE.toString(),
saOnlyScopes,
query,
Defaults.locale);
return (Hashtable)returns.get(ServiceStore.FS_SERVICES);
}
public synchronized Hashtable findDAScopes(Vector scopes)
throws ServiceLocationException {
int i, n = scopes.size();
StringBuffer buf = new StringBuffer();
for (i = 0; i < n; i++) {
buf.append("(");
buf.append(SunDATable.SCOPES_ID);
buf.append("=");
buf.append((String)scopes.elementAt(i));
buf.append(")");
}
if (i > 1) {
buf.insert(0, "(|");
buf.append(")");
}
if (i > 0) {
buf.insert(0, "(&");
}
buf.append("(");
buf.append(SunDATable.VERSION_ID);
buf.append("=");
buf.append((Integer.valueOf(Defaults.version)).toString());
buf.append(")");
if (i > 0) {
buf.append(")");
}
ServiceStore store = serviceTable.store;
Hashtable returns =
store.findServices(Defaults.SUN_DA_SERVICE_TYPE.toString(),
saOnlyScopes,
buf.toString(),
Defaults.locale);
Hashtable retRec = (Hashtable)returns.get(ServiceStore.FS_SERVICES);
Enumeration en = retRec.keys();
Vector ret = new Vector();
Vector multiScopes = (Vector)scopes.clone();
Vector attrTags = new Vector();
attrTags.addElement(SunDATable.SCOPES_ID);
while (en.hasMoreElements()) {
ServiceURL url = (ServiceURL)en.nextElement();
Vector urlScopes = (Vector)retRec.get(url);
Hashtable attrRec =
store.findAttributes(url,
urlScopes,
attrTags,
Defaults.locale);
Vector retAttrs = (Vector)attrRec.get(ServiceStore.FA_ATTRIBUTES);
String host = url.getHost();
Vector retScopes = null;
n = retAttrs.size();
for (i = 0; i < n; i++) {
ServiceLocationAttribute attr =
(ServiceLocationAttribute)retAttrs.elementAt(i);
String id = attr.getId();
Vector vals = attr.getValues();
if (id.equals(SunDATable.SCOPES_ID)) {
retScopes = vals;
}
}
DATable.addToEquivClass(host, retScopes, ret);
DATable.filterScopes(multiScopes, retScopes, false);
}
retRec.clear();
if (multiScopes.size() > 0) {
retRec.put(DATable.MULTICAST_KEY, multiScopes);
}
if (ret.size() > 0) {
retRec.put(DATable.UNICAST_KEY, ret);
}
return retRec;
}
private ServiceURL formServiceTableDAURL(ServiceURL url, Vector attrs) {
StringBuffer buf = new StringBuffer();
int i, n = attrs.size();
for (i = 0; i < n; i++) {
ServiceLocationAttribute attr =
(ServiceLocationAttribute)attrs.elementAt(i);
String id = attr.getId();
if (id.equals(SunDATable.SCOPES_ID)) {
String rep = "";
try {
rep = attr.externalize();
} catch (ServiceLocationException ex) {
conf.writeLog("ssdat_inter_attr_err",
new Object[] {attr, ex.getMessage()});
continue;
}
if (buf.length() > 0) {
buf.append(";");
}
buf.append(rep.substring(1, rep.length()-1));
}
}
ServiceURL daURL =
new ServiceURL(Defaults.SUN_DA_SERVICE_TYPE+
"://"+
url.getHost()+
"/"+
buf.toString(),
url.getLifetime());
return daURL;
}
}