package com.sun.slp;
import java.util.*;
class SLPTemplateRegistry extends TemplateRegistry {
static final String SERVICE_ATTR_ID = "template-type";
static final String DESCRIPTION_ATTR_ID = "template-description";
static final String VERSION_ATTR_ID = "template-version";
static final String SERVICE_URL_ATTR_ID = "template-url-syntax";
static final String TEMPLATE_SERVICE_TYPE = "service:slp-template";
private static TemplateRegistry registry = null;
SLPTemplateRegistry() throws ServiceLocationException {
}
public void registerServiceTemplate(ServiceType serviceType,
String documentURL,
Locale languageLocale,
String version)
throws ServiceLocationException {
Assert.nonNullParameter(serviceType, "serviceType");
Assert.nonNullParameter(documentURL, "documentURL");
Assert.nonNullParameter(languageLocale, "language");
Assert.nonNullParameter(version, "version");
String language = languageLocale.getLanguage();
if (language == null || language.length() <= 0) {
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("template_lang_null",
new Object[] {
documentURL}));
}
String turl = null;
try {
turl = findTemplateURL(serviceType,
languageLocale,
version);
} catch (ServiceLocationException ex) {
if (ex.getErrorCode() !=
ServiceLocationException.LANGUAGE_NOT_SUPPORTED) {
throw ex;
}
}
if (turl != null) {
throw
new ServiceLocationException(
ServiceLocationException.INVALID_REGISTRATION,
"template_already_registered",
new Object[] {
documentURL,
version,
languageLocale});
}
Vector attributes = new Vector();
Vector values = new Vector();
values.addElement(serviceType.toString());
ServiceLocationAttribute attr =
new ServiceLocationAttribute(SERVICE_ATTR_ID, values);
attributes.addElement(attr);
values = new Vector();
values.addElement(version);
attr =
new ServiceLocationAttribute(VERSION_ATTR_ID, values);
attributes.addElement(attr);
ServiceURL surl =
new ServiceURL(TEMPLATE_SERVICE_TYPE +
":"+
documentURL+
";"+
SERVICE_ATTR_ID+
"="+
serviceType+
";"+
VERSION_ATTR_ID+
"="+
version,
ServiceURL.LIFETIME_MAXIMUM);
Advertiser serviceAgent =
ServiceLocationManager.getAdvertiser(languageLocale);
if (serviceAgent == null) {
throw
new ServiceLocationException(
ServiceLocationException.NOT_IMPLEMENTED,
"no_advertiser",
new Object[0]);
}
serviceAgent.register(surl, attributes);
}
public void deregisterServiceTemplate(ServiceType serviceType,
Locale languageLocale,
String version)
throws ServiceLocationException {
Assert.nonNullParameter(serviceType, "serviceType");
Assert.nonNullParameter(languageLocale, "languageLocale");
ServiceURL turl = findVersionedURL(serviceType,
languageLocale,
version);
if (turl == null) {
throw
new ServiceLocationException(
ServiceLocationException.INVALID_REGISTRATION,
"template_not_registered",
new Object[] {
serviceType,
version,
languageLocale});
}
Advertiser serviceAgent =
ServiceLocationManager.getAdvertiser(languageLocale);
if (serviceAgent == null) {
throw
new ServiceLocationException(
ServiceLocationException.NOT_IMPLEMENTED,
"no_advertiser",
new Object[0]);
}
serviceAgent.deregister(turl);
}
public String findTemplateURL(ServiceType serviceType,
Locale languageLocale,
String version)
throws ServiceLocationException {
Assert.nonNullParameter(serviceType, "serviceType");
Assert.nonNullParameter(languageLocale, "languageLocale");
ServiceURL turl = findVersionedURL(serviceType,
languageLocale,
version);
if (turl == null) {
return null;
}
ServiceType type = turl.getServiceType();
String url = turl.toString();
String abstractType = type.getAbstractTypeName();
if (!abstractType.equals(TEMPLATE_SERVICE_TYPE)) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"template_url_malformed",
new Object[] {turl});
}
int idx = url.indexOf(";"+SERVICE_ATTR_ID+"=");
if (idx == -1) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"template_url_malformed",
new Object[] {turl});
}
int jdx = TEMPLATE_SERVICE_TYPE.length() + 1;
return url.substring(jdx, idx);
}
private ServiceURL findVersionedURL(ServiceType serviceType,
Locale languageLocale,
String version)
throws ServiceLocationException {
Vector scopes = ServiceLocationManager.findScopes();
ServiceLocationEnumeration results = null;
String query = "(" + SERVICE_ATTR_ID + "=" + serviceType + ")";
if (version != null) {
query = query + "(" + VERSION_ATTR_ID + "=" + version + ")";
}
query = "(&" + query + ")";
Locator userAgent =
ServiceLocationManager.getLocator(languageLocale);
if (userAgent == null) {
throw
new ServiceLocationException(
ServiceLocationException.NOT_IMPLEMENTED,
"no_locator",
new Object[0]);
}
try {
ServiceType type = new ServiceType(TEMPLATE_SERVICE_TYPE);
results =
userAgent.findServices(type,
scopes,
query);
} catch (ServiceLocationException ex) {
if (ex.getErrorCode() !=
ServiceLocationException.LANGUAGE_NOT_SUPPORTED) {
throw ex;
}
}
if (!results.hasMoreElements()) {
return null;
}
ServiceURL turl = null;
float highest = (float)-1.0;
while (results.hasMoreElements()) {
ServiceURL surl = (ServiceURL)results.nextElement();
String urlPath = surl.getURLPath();
if (version == null) {
String token = ";"+VERSION_ATTR_ID+"=";
int idx = urlPath.indexOf(token);
if (idx == -1) {
continue;
}
urlPath =
urlPath.substring(idx+token.length(), urlPath.length());
idx = urlPath.indexOf(";");
if (idx == -1) {
continue;
}
String temversion = urlPath.substring(0, idx);
float current = (float)0.0;
try {
current = Float.valueOf(temversion).floatValue();
} catch (NumberFormatException ex) {
continue;
}
if (current > highest) {
turl = surl;
}
} else {
if (turl != null) {
throw
new ServiceLocationException(
ServiceLocationException.INTERNAL_SYSTEM_ERROR,
"template_multiple",
new Object[] {
serviceType,
version,
languageLocale});
}
turl = surl;
}
}
return turl;
}
public ServiceLocationAttributeVerifier attributeVerifier(
String documentURL)
throws ServiceLocationException {
Assert.nonNullParameter(documentURL, "documentURL");
return new URLAttributeVerifier(documentURL);
}
}