import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.io.*;
public class NSIBrowser extends Applet
{
public static final String cfgFileName = "NSIBrowser.ini";
public static final String jndiInitCtxProp = "java.naming.factory.initial";
public static final String jndiHostProp = "java.naming.provider.url";
static String listClassesProp = "nsi.Browser.listClasses";
static String listBindingsProp = "nsi.Browser.listBindings";
static NSIBrowserFrame mainFrame = null;
public static void main
(
String[] args
)
{
NSIBrowser browser = new NSIBrowser ();
browser.init ();
}
public void init ()
{
mainFrame = new NSIBrowserFrame (
"NSI Browser Demo",
null,
null);
mainFrame.setSize (NSIBrowserFrame.width, NSIBrowserFrame.height);
mainFrame.show ();
readConfigFile ();
mainFrame.setProperties (System.getProperties ());
mainFrame.resetInitCtx ();
}
void readConfigFile ()
{
File cfgFile = new File (cfgFileName);
try
{
if (cfgFile.exists ())
{
if (cfgFile.isFile () && cfgFile.canRead ())
{
FileInputStream inFile = new FileInputStream (cfgFile);
Properties newProps = new Properties ();
newProps.load (inFile);
Properties sysProps = System.getProperties ();
boolean propChange = false;
Enumeration nameEnum = newProps.propertyNames ();
while (nameEnum.hasMoreElements ())
{
String propName = (String) nameEnum.nextElement ();
if (false == sysProps.containsKey (propName))
{
propChange = true;
sysProps.put (propName, newProps.getProperty (propName));
}
}
if (propChange)
{
System.setProperties (sysProps);
}
}
}
}
catch (Exception e)
{
System.out.println ("Unexpected configuration file exception : " + e);
e.printStackTrace ();
}
String initPropVal = System.getProperty (jndiInitCtxProp);
if ((null == initPropVal) || (initPropVal.equals ("")))
{
Frame f = new NSIInitialContextFrame (mainFrame);
f.show ();
}
String propVal;
propVal = System.getProperty (listClassesProp);
if ((null != propVal) && (propVal.equalsIgnoreCase ("true")))
mainFrame.doListClasses = true;
propVal = System.getProperty (listBindingsProp);
if ((null != propVal) && (propVal.equalsIgnoreCase ("true")))
mainFrame.doListBindings = true;
}
public static void saveConfigFile ()
{
Properties myProps = new Properties ();
String propVal;
try
{
FileInputStream in = new FileInputStream (
NSIBrowser.cfgFileName);
myProps.load (in);
}
catch (FileNotFoundException e)
{
}
catch (IOException e)
{
System.out.println (
"Unable to read configuration file:" + cfgFileName);
System.out.println ("Unexpected IO exception : " + e);
e.printStackTrace ();
}
if (null != (propVal = System.getProperty (jndiInitCtxProp)))
myProps.put (jndiInitCtxProp, propVal);
if (null != (propVal = System.getProperty ("nsi.domainName")))
myProps.put ("nsi.domainName", propVal);
if (null != (propVal = System.getProperty (jndiHostProp)))
myProps.put ("java.naming.provider.url", propVal);
myProps.put (
listClassesProp,
(mainFrame.doListClasses) ? "true" : "false");
myProps.put (
listBindingsProp,
(mainFrame.doListBindings) ? "true" : "false");
try
{
myProps.save (
new FileOutputStream (cfgFileName),
"NSI Browser System Properties");
}
catch (IOException e)
{
System.out.println (
"Unable to save configuration file :" + cfgFileName);
System.out.println ("Unexpected IO exception : " + e);
e.printStackTrace ();
}
}
}