package com.sun.slp;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
abstract public class ServiceLocationManager extends Object {
protected static DATable dat = null;
protected static SLPConfig config = null;
protected static Hashtable locators = new Hashtable();
protected static Hashtable advertisers = new Hashtable();
protected static Class locatorClass = null;
protected static Class advertiserClass = null;
public static Locator getLocator(Locale locale)
throws ServiceLocationException
{
if (locale == null) {
locale = config.getLocale();
}
String lang = locale.getLanguage();
Locator locator = (Locator)locators.get(lang);
if (locator == null) {
if (locatorClass == null) {
String className =
System.getProperty("sun.net.slp.LocatorImpl");
if (className == null) {
className = "com.sun.slp.UARequester";
}
locatorClass = getClass(className);
}
locator = (Locator)getInstance(locatorClass, locale);
if (locator != null) {
locators.put(lang, locator);
}
}
return locator;
}
public static Advertiser getAdvertiser(Locale locale)
throws ServiceLocationException {
if (locale == null) {
locale = config.getLocale();
}
String lang = locale.getLanguage();
Advertiser advertiser = (Advertiser)advertisers.get(lang);
if (advertiser == null) {
if (advertiserClass == null) {
String className =
System.getProperty("sun.net.slp.AdvertiserImpl");
if (className == null) {
className = "com.sun.slp.SARequester";
}
advertiserClass = getClass(className);
}
advertiser = (Advertiser)getInstance(advertiserClass, locale);
if (advertiser != null) {
advertisers.put(lang, advertiser);
}
}
return advertiser;
}
public static synchronized Vector findScopes()
throws ServiceLocationException {
Vector accessableScopes = null;
accessableScopes = config.getConfiguredScopes();
if (accessableScopes.size() <= 0) {
accessableScopes = dat.findScopes();
if (accessableScopes.size() <= 0) {
accessableScopes = performSADiscovery();
if (accessableScopes.size() <= 0) {
accessableScopes.addElement(Defaults.DEFAULT_SCOPE);
}
}
}
return accessableScopes;
}
public static int getRefreshInterval() throws ServiceLocationException {
Vector tags = new Vector();
tags.addElement(Defaults.MIN_REFRESH_INTERVAL_ATTR_ID);
Vector saOnlyScopes = config.getSAOnlyScopes();
CAttrMsg msg = new CAttrMsg(Defaults.locale,
Defaults.SUN_DA_SERVICE_TYPE,
saOnlyScopes,
tags);
CAttrMsg rply =
(CAttrMsg)Transact.transactTCPMsg(config.getLoopback(), msg, true);
if (rply == null ||
rply.getErrorCode() != ServiceLocationException.OK) {
short errCode =
(rply == null ?
ServiceLocationException.INTERNAL_SYSTEM_ERROR :
rply.getErrorCode());
throw
new ServiceLocationException(errCode,
"loopback_error",
new Object[] {
Short.valueOf(errCode)});
}
int ri = 0;
Vector attrs = rply.attrList;
ServiceLocationAttribute attr =
(attrs.size() > 0 ?
(ServiceLocationAttribute)attrs.elementAt(0):
null);
Vector values = (attr != null ? attr.getValues():new Vector());
int i, n = values.size();
for (i = 0; i < n; i++) {
Integer mri = (Integer)values.elementAt(i);
int mriv = mri.intValue();
if (mriv > ri) {
ri = mriv;
}
}
return ri;
}
private static Class getClass(String name) {
Class ret = null;
try {
ret = Class.forName(name);
} catch (ClassNotFoundException ex) {
}
return ret;
}
private static Object getInstance(Class cobj, Locale locale) {
Object ret = null;
if (cobj != null) {
try {
Class[] paramClasses = {locale.getClass()};
Constructor con = cobj.getDeclaredConstructor(paramClasses);
Object[] params = {locale};
ret = con.newInstance(params);
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (InvocationTargetException ex) {
} catch (NoSuchMethodException ex) {
}
}
return ret;
}
private static Vector performSADiscovery()
throws ServiceLocationException {
Vector hint = config.getTypeHint();
StringBuffer buf = new StringBuffer();
int i, n = hint.size();
for (i = 0; i < n; i++) {
buf.append("(");
buf.append(Defaults.SERVICE_TYPE_ATTR_ID);
buf.append("=");
buf.append(hint.elementAt(i).toString());
}
if (i > 1) {
buf.insert(0, "(|");
buf.append(")");
}
CSrvMsg rqst = new CSrvMsg(config.getLocale(),
Defaults.SA_SERVICE_TYPE,
new Vector(),
buf.toString());
Vector scopes =
Transact.transactActiveAdvertRequest(Defaults.SA_SERVICE_TYPE,
rqst,
null);
return scopes;
}
static {
if (config == null) {
config = SLPConfig.getSLPConfig();
}
if (dat == null) {
dat = DATable.getDATable();
}
}
}