package com.sun.slp;
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.reflect.*;
import java.awt.*;
public class slpd extends SLPConfig {
private static final String SERVER_BUNDLE_NAME = "com/sun/slp/Server";
static class ServerBundle extends ResourceBundle {
private ResourceBundle bundle = null;
static ResourceBundle
getBundle(ResourceBundle parent, Locale locale)
throws MissingResourceException {
return new ServerBundle(parent, locale);
}
private ServerBundle(ResourceBundle parent, Locale locale)
throws MissingResourceException {
if (parent != null) {
this.parent = parent;
}
try {
URL[] urls = null;
urls = new URL[] {new URL("file:/usr/share/lib/locale/")};
URLClassLoader ld = new URLClassLoader(urls);
bundle =
ResourceBundle.getBundle(SERVER_BUNDLE_NAME, locale, ld);
} catch (MalformedURLException e) {
}
bundle = bundle != null ?
bundle :
ResourceBundle.getBundle(SERVER_BUNDLE_NAME, locale);
}
protected Object handleGetObject(String key)
throws MissingResourceException {
Object ret = null;
try {
ret = bundle.getObject(key);
} catch (MissingResourceException ex) {
ret = parent.getObject(key);
}
return ret;
}
public Enumeration getKeys() {
return bundle.getKeys();
}
}
static class SLPLog extends Writer {
private TextArea taLog = null;
private StringBuffer buf;
SLPLog(TextArea nta) {
taLog = nta;
buf = new StringBuffer();
}
public void write(char[] cbuf, int off, int len)
throws IOException {
buf.append(cbuf, off, len);
}
public void flush() throws IOException {
String date = SLPConfig.getDateString();
taLog.append(
"********" +
date + "\n" +
buf.toString() + "\n" +
"********\n");
buf = new StringBuffer();
}
public void close() throws IOException {}
}
private static String configFile;
private static SLPDgui slpdgui;
private static SLPConfig config;
private static ServerDATable daTable;
protected slpd() {
super();
}
private static void usage() {
ResourceBundle bundle =
getMessageBundleInternal(Locale.getDefault(), null);
System.err.println(formatMessageInternal("slpd_usage",
new Object[0],
bundle));
System.exit(1);
}
public static void main(String args[]) {
boolean bMon = false;
boolean bStop = false;
configFile = null;
Thread.currentThread().setName("slpd");
if (args.length > 3) {
usage();
}
int i, n = args.length;
for (i = 0; i < n; i++) {
if (args[i].equals("-f")) {
if (configFile != null) {
usage();
}
try {
File f = new File(args[++i]);
configFile = args[i];
} catch (Exception ex) {
usage();
}
} else if (args[i].equals("monitor")) {
bMon = true;
} else if (args[i].equals("stop")) {
bStop = true;
} else {
usage();
}
}
ResourceBundle bundle =
getMessageBundleInternal(Locale.getDefault(), null);
try {
if (configFile != null) {
Properties props = System.getProperties();
props.setProperty("sun.net.slp.configURL",
"file:" + configFile);
}
config = initializeSLPConfig();
if (bMon) {
try {
slpdgui = new SLPDgui(configFile);
SLPLog log = new SLPLog(slpdgui.getTALog());
synchronized (config) {
config.log = log;
}
slpdgui.setVisible(true);
} catch (Exception ex) {
System.err.println(formatMessageInternal("slpd_no_gui",
new Object[0],
bundle));
}
}
if (!bStop) {
start();
} else {
stop();
}
} catch (ServiceLocationException ex) {
errorExit(bundle, ex);
}
}
static void start() throws ServiceLocationException {
ServiceTable table = ServiceTable.getServiceTable();
Properties props = System.getProperties();
props.put(DATable.DA_TABLE_CLASS_PROP, "com.sun.slp.SunServerDATable");
try {
if (System.in.available() > 0) {
RequestHandler rh =
new RequestHandler(System.in, System.out, config);
rh.start();
}
} catch (IOException e) {}
StreamListener.initializeStreamListenerOnInterface(
config.getLoopback());
daTable = ServerDATable.getServerDATable();
table.deserializeTable();
Vector interfaces = config.getInterfaces();
int i, n = interfaces.size();
for (i = 0; i < n; i++) {
InetAddress interfac = (InetAddress)interfaces.elementAt(i);
Listener.initializeInterfaceManagers(interfac);
}
if (config.isDA()) {
DAAdvertiser.initializeDAAdvertiserOnInterface(
config.getLocalHost());
}
Vector discoveredScopes = daTable.findScopes();
Vector serverScopes = config.getSAConfiguredScopes();
Vector daAttributes = config.getDAAttributes();
Vector saAttributes = config.getSAAttributes();
if (config.regTest() ||
config.traceMsg() ||
config.traceDrop() ||
config.traceDATraffic()) {
config.writeLog((config.isDA() ? "hello_da":"hello"),
new Object[] {interfaces,
serverScopes,
discoveredScopes,
(config.isDA() ?
daAttributes:saAttributes)});
}
if (config.isV1Supported()) {
SLPV1Manager.start(config, daTable, table);
}
}
static void stop() throws ServiceLocationException {
if (daemonIsDA()) {
stopDA();
} else {
stopSA();
}
}
static boolean daemonIsDA() throws ServiceLocationException {
DATable table =
DATable.getDATable();
Hashtable das =
table.findDAScopes(config.getSAConfiguredScopes());
Vector daRecs = (Vector)das.get(DATable.UNICAST_KEY);
Vector interfaces = config.getInterfaces();
if (daRecs == null) {
return false;
}
int i, n = daRecs.size();
for (i = 0; i < n; i++) {
DATable.DARecord rec =
(DATable.DARecord)daRecs.elementAt(i);
Vector daAddresses = rec.daAddresses;
int j, m = interfaces.size();
for (j = 0; j < m; j++) {
if (daAddresses.contains(interfaces.elementAt(i))) {
return true;
}
}
}
return false;
}
private static void stopDA() throws ServiceLocationException {
ServiceURL url =
new ServiceURL(Defaults.DA_SERVICE_TYPE +
"://" +
config.getLocalHost().getHostAddress(),
ServiceURL.LIFETIME_DEFAULT);
SDAAdvert advert =
new SDAAdvert(new SLPServerHeaderV2(),
(short)0x0,
0L,
url,
config.getSAConfiguredScopes(),
new Vector());
DAAdvertiser daadv = new DAAdvertiser(config.getLocalHost(),
advert.getHeader());
daadv.sendAdvert();
System.exit(0);
}
private static void stopSA() throws ServiceLocationException {
ServiceURL url =
new ServiceURL(Defaults.SA_SERVICE_TYPE + "://" +
config.getLocalHost().getHostAddress(),
ServiceURL.LIFETIME_DEFAULT);
SSAAdvert advert = new SSAAdvert(Defaults.version,
(short)0x0,
config.getLocale(),
url,
config.getSAConfiguredScopes(),
new Vector());
try {
SrvLocMsg msg =
Transact.transactTCPMsg(config.getLoopback(), advert, false);
if (msg.getErrorCode() != ServiceLocationException.OK) {
config.writeLog("slpd_sa_stop_failure",
new Object[] {
Integer.valueOf(msg.getErrorCode())});
}
} catch (ServiceLocationException ex) {
if (ex.getErrorCode() != ServiceLocationException.NETWORK_ERROR) {
config.writeLog("slpd_sa_stop_failure",
new Object[] {Integer.valueOf(ex.getErrorCode())});
}
}
System.exit(0);
}
static void errorExit(ResourceBundle bundle, ServiceLocationException ex) {
switch (ex.getErrorCode()) {
case ServiceLocationException.INTERNAL_SYSTEM_ERROR:
System.err.println(formatMessageInternal("slpd_int_err",
new Object[] {
ex.getMessage()},
bundle));
break;
case ServiceLocationException.NETWORK_INIT_FAILED:
System.err.println(formatMessageInternal("slpd_intnet_err",
new Object[] {
ex.getMessage()},
bundle));
break;
case ServiceLocationException.NETWORK_ERROR:
System.err.println(formatMessageInternal("slpd_net_err",
new Object[] {
ex.getMessage()},
bundle));
break;
default:
System.err.println(formatMessageInternal("slpd_err",
new Object[] {
Integer.valueOf(ex.getErrorCode()),
ex.getMessage()},
bundle));
}
ex.printStackTrace();
System.err.println(formatMessageInternal("exiting_msg",
new Object[0],
bundle));
System.exit(1);
}
private static SLPConfig initializeSLPConfig() {
config.isSA = true;
if (System.getProperty("sun.net.slp.loggerClass") == null) {
Properties props = System.getProperties();
props.setProperty("sun.net.slp.loggerClass", "com.sun.slp.Syslog");
System.setProperties(props);
}
theSLPConfig = new slpd();
return theSLPConfig;
}
boolean isDA() {
return Boolean.getBoolean("net.slp.isDA");
}
boolean isV1Supported() {
if (!isDA() || super.getSLPv1NotSupported()) {
return false;
}
boolean v1Supported = false;
try {
Class.forName("com.sun.slp.SLPV1Manager");
v1Supported = true;
} catch (ClassNotFoundException ex) {
}
return v1Supported;
}
private static final String serverMsgBundle = "Server";
ResourceBundle getMessageBundle(Locale locale) {
ResourceBundle parentBundle = super.getMessageBundle(locale);
return getMessageBundleInternal(locale, parentBundle);
}
static private ResourceBundle getMessageBundleInternal(
Locale locale,
ResourceBundle parentBundle) {
ResourceBundle msgBundle = null;
try {
msgBundle = ServerBundle.getBundle(parentBundle, locale);
} catch (MissingResourceException ex) {
System.out.println("Missing resource bundle ``"+
SERVER_BUNDLE_NAME+
"'' for locale ``"+
locale+
"''");
if (locale.equals(Defaults.locale)) {
System.out.println("Exiting...");
System.exit(1);
}
System.out.println("Using SLP default locale ``" +
Defaults.locale+"''");
msgBundle =
getMessageBundleInternal(Defaults.locale, parentBundle);
}
return msgBundle;
}
}