package com.sun.slp;
import java.util.*;
import java.io.*;
class ServiceStoreFactory extends Object {
private static final String DEFAULT_SERVICE_STORE =
"com.sun.slp.ServiceStoreInMemory";
private static final String SERVICE_STORE_PROPERTY =
"sun.net.slp.serviceStoreClass";
final private static char COMMENT_CHAR1 = '#';
final private static char COMMENT_CHAR2 = ';';
final private static String URL_LIST_SEP = ", ";
final private static String SCOPES_ATTR_ID = "scopes";
static ServiceStore createServiceStore()
throws ServiceLocationException {
return createServiceStoreFromProperty(SERVICE_STORE_PROPERTY);
}
private static ServiceStore
createServiceStoreFromProperty(String property)
throws ServiceLocationException {
Properties props = System.getProperties();
String storeClassName =
props.getProperty(property,
DEFAULT_SERVICE_STORE);
Class storeClass = null;
try {
storeClass = Class.forName(storeClassName);
} catch (ClassNotFoundException ex) {
throw
new ServiceLocationException(
ServiceLocationException.INTERNAL_SYSTEM_ERROR,
"ssf_no_class",
new Object[] {storeClassName});
}
ServiceStore store = null;
try {
store = (ServiceStore)storeClass.newInstance();
} catch (InstantiationException ex) {
throw
new ServiceLocationException(
ServiceLocationException.INTERNAL_SYSTEM_ERROR,
"ssf_inst_ex",
new Object[] {
storeClassName,
ex.getMessage()});
} catch (IllegalAccessException ex) {
throw
new ServiceLocationException(
ServiceLocationException.INTERNAL_SYSTEM_ERROR,
"ssf_ill_ex",
new Object[] {
storeClassName,
ex.getMessage()});
} catch (ClassCastException ex) {
throw
new ServiceLocationException(
ServiceLocationException.INTERNAL_SYSTEM_ERROR,
"ssf_class_cast",
new Object[] {storeClassName});
}
return store;
}
static ServiceStore deserializeServiceStore(BufferedReader is)
throws ServiceLocationException {
ServiceStore ss = new ServiceStoreInMemory();
try {
deserialize(is, ss);
} catch (IOException ex) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"ssf_io_deser",
new Object[] {ex.getMessage()});
}
return ss;
}
private static void deserialize(BufferedReader in, ServiceStore store)
throws IOException, ServiceLocationException {
SLPConfig conf = SLPConfig.getSLPConfig();
int linecount = 0;
int scopeLinenum = 0;
while (in.ready()) {
linecount++;
String line = in.readLine().trim();
if (line.length() <= 0) {
continue;
}
char cc = line.charAt(0);
if (cc == COMMENT_CHAR1 ||
cc == COMMENT_CHAR2) {
continue;
}
StringTokenizer tk = new StringTokenizer(line, URL_LIST_SEP);
String surl = null;
String slang = null;
String slifetime = null;
String sType = null;
if (tk.hasMoreTokens()) {
surl = tk.nextToken().trim();
if (tk.hasMoreTokens()) {
slang = tk.nextToken().trim();
if (tk.hasMoreTokens()) {
slifetime = tk.nextToken().trim();
if (tk.hasMoreTokens()) {
sType = tk.nextToken().trim();
if (tk.hasMoreTokens()) {
slang = null;
}
}
}
}
}
if (surl == null || slifetime == null || slang == null) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"ssf_not_valid_url",
new Object[] {line});
}
Locale locale = SLPConfig.langTagToLocale(slang);
ServiceURL url = null;
try {
int lifetime = Integer.parseInt(slifetime);
if (lifetime == ServiceURL.LIFETIME_MAXIMUM) {
lifetime = ServiceURL.LIFETIME_PERMANENT;
}
url = new ServiceURL(surl, lifetime);
if (sType != null) {
ServiceType utype = url.getServiceType();
if (utype.isServiceURL()) {
conf.writeLog("ssf_set_servc_err",
new Object[] {
surl,
utype});
} else {
ServiceType t = new ServiceType(sType);
if (!t.isServiceURL() &&
!t.equals(url.getServiceType())) {
url.setServiceType(t);
}
}
}
} catch (NumberFormatException ex) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"ssf_not_valid_lifetime",
new Object[] {
slifetime, Integer.valueOf(linecount)});
} catch (IllegalArgumentException ex) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"ssf_syntax_err",
new Object[] {
ex.getMessage(), Integer.valueOf(linecount)});
}
Vector attrs = new Vector();
Hashtable ht = new Hashtable();
ServiceLocationAttribute scopeAttr = null;
boolean firstLine = true;
try {
while (in.ready()) {
linecount++;
line = in.readLine();
if (line.length() <= 0) {
break;
}
if (line.indexOf("=") != -1) {
line = "(" + line + ")";
}
ServiceLocationAttribute attr =
new ServiceLocationAttribute(line, false);
if (firstLine) {
firstLine = false;
if (attr.getId().equalsIgnoreCase(SCOPES_ATTR_ID)) {
scopeAttr = attr;
continue;
}
}
ServiceLocationAttribute.mergeDuplicateAttributes(attr,
ht,
attrs,
false);
}
} catch (ServiceLocationException e) {
e.makeAddendum(" (line " + linecount + ")");
throw e;
}
Vector scopes = null;
if (scopeAttr == null) {
scopes = conf.getSAConfiguredScopes();
} else {
scopes = (Vector)scopeAttr.getValues();
try {
SLPHeaderV2.unescapeScopeStrings(scopes);
DATable.validateScopes(scopes, locale);
} catch (ServiceLocationException e) {
e.makeAddendum(" (line " + scopeLinenum + ")");
throw e;
}
}
store.register(url, attrs, scopes, locale, null, null);
CSrvReg creg = new CSrvReg(true, locale, url, scopes,
attrs, null, null);
ServerDATable daTable = ServerDATable.getServerDATable();
daTable.forwardSAMessage(creg, conf.getLoopback());
}
}
static void serialize(BufferedWriter out, ServiceStore store)
throws IOException, ServiceLocationException {
Enumeration recs = store.getServiceRecordsByScope(null);
while (recs.hasMoreElements()) {
ServiceStore.ServiceRecord rec =
(ServiceStore.ServiceRecord)recs.nextElement();
ServiceURL url = rec.getServiceURL();
String surl = url.toString();
Vector attrs = (Vector)rec.getAttrList().clone();
Locale locale = rec.getLocale();
Vector scopes = rec.getScopes();
StringBuffer line = new StringBuffer();
line.append(surl);
line.append(", ");
line.append(SLPConfig.localeToLangTag(locale));
line.append(", ");
line.append(Integer.toString(url.getLifetime()));
if (!surl.startsWith(Defaults.SERVICE_PREFIX)) {
ServiceType type = url.getServiceType();
line.append(", ");
line.append(type.toString());
}
out.write(line.toString(), 0, line.length());
out.newLine();
line.setLength(0);
if (scopes.size() > 1 &&
!Defaults.DEFAULT_SCOPE.equals((String)scopes.elementAt(0))) {
attrs.insertElementAt(
new ServiceLocationAttribute(SCOPES_ATTR_ID,
scopes),
0);
}
int i, n = attrs.size();
for (i = 0; i < n; i++) {
ServiceLocationAttribute attr =
(ServiceLocationAttribute)attrs.elementAt(i);
Vector vals = attr.getValues();
line.append(
ServiceLocationAttribute.escapeAttributeString(attr.getId(),
false));
if (vals != null) {
line.append("=");
int j, m = vals.size();
for (j = 0; j < m; j++) {
Object v = vals.elementAt(j);
if (j > 0) {
line.append(", ");
}
line.append(ServiceLocationAttribute.escapeValue(v));
}
}
out.write(line.toString(), 0, line.length());
out.newLine();
line.setLength(0);
}
out.newLine();
}
out.flush();
}
}