import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.spi.*;
public class shell
{
public static final String helpStr = "JNDI Shell (c) 1997-1999 " +
"Novell, Inc.";
public static final String jndiInitialContextKey =
"java.naming.factory.initial";
public static void main (String[] argv)
{
System.out.println (shell.helpStr);
boolean continueWStdIn = false;
boolean verbose = false;
boolean startDS = false;
int startArgs = 0;
for (int a = 0; a < argv.length; a++)
{
if (!argv[a].startsWith ("-"))
{
startArgs = a;
break;
}
else
{
String option = argv[a].substring (1);
startArgs = a + 1;
if (option.equals ("verbose"))
verbose = true;
else if (option.equals ("ds"))
startDS = true;
else if (option.toUpperCase ().equals ("H") ||
option.equals ("?"))
{
String[] helpStrs =
{
"usage: java shell [-verbose] [-ds] [<input> [output]]",
"",
" -verbose stack trace printed on all exceptions",
" -ds start with InitialDSContext",
" input input file from which to read commands",
" output output file to which to write results",
"",
"If an input file is specified, but no output file,",
"then control is returned to the terminal after",
"executing all input commands.",
"",
"'#' and ';' are comment characters."
};
for (int x = 0; x < helpStrs.length; x++)
System.out.println (helpStrs[x]);
return;
}
else
{
System.out.println ("error: invalid option: " + option);
return;
}
}
}
Properties props = new Properties ();
File file = new File ("shell.ini");
if (file.exists ())
{
try
{
props.load (new FileInputStream (file));
}
catch (IOException e)
{
System.out.println ("error: reading shell startup properties");
}
}
if (!props.containsKey (Context.INITIAL_CONTEXT_FACTORY))
{
System.out.println ("warning: no '" + jndiInitialContextKey +
"' property, using NetWareInitialContextFactory");
}
InputStream in = System.in;
OutputStream out = System.out;
try
{
if (argv.length - startArgs > 0)
{
in = new FileInputStream (new File (argv[startArgs]));
continueWStdIn = true;
}
if (argv.length - startArgs > 1)
{
out = new FileOutputStream (new File (argv[startArgs + 1]));
continueWStdIn = false;
}
runShell (in, out, props, continueWStdIn, verbose, startDS);
}
catch (Throwable t)
{
if (verbose)
{
PrintWriter err = new PrintWriter (new OutputStreamWriter (
System.out), true);
JNDIShell.printException ("shell", err, t, verbose);
}
else
System.out.println ("shell: " + t.toString ());
}
System.gc ();
System.exit (0);
}
public static void runShell (InputStream in, OutputStream out,
Properties props, boolean continueWStdIn, boolean verbose,
boolean startDS)
throws NamingException
{
if (!props.containsKey (jndiInitialContextKey))
{
props.put (jndiInitialContextKey,
"com.novell.service.nw.NetWareInitialContextFactory");
}
JNDIShell shell;
Context ctx;
if (startDS)
ctx = new InitialDirContext (props);
else
ctx = new InitialContext (props);
shell = new JNDIShell (ctx, in, out, continueWStdIn, verbose);
shell.addCommand ("load", new LoadCommand ());
shell.addCommand ("unload", new UnloadCommand ());
shell.addCommand ("dir", new DirCommand ());
shell.addCommand ("cd", new CdCommand ());
shell.addCommand ("cda", new CdAtomicCommand ());
shell.addCommand ("cds", new CdSchemaRootCommand ());
shell.addCommand ("cdcdef", new CdSchemaClassDefCommand ());
shell.addCommand ("cdadef", new CdSchemaAttributeDefCommand ());
shell.addCommand ("cdsdef", new CdSchemaSyntaxDefCommand ());
shell.addCommand ("attr", new AttrCommand ());
shell.addCommand ("show", new ShowCommand ());
shell.addCommand ("ren", new RenameCommand ());
shell.addCommand ("bind", new BindCommand ());
shell.addCommand ("rebind", new RebindCommand ());
shell.addCommand ("unbind", new UnbindCommand ());
shell.addCommand ("create", new CreateCommand ());
shell.addCommand ("del", new DestroyCommand ());
shell.addCommand ("env", new EnvCommand ());
shell.addCommand ("shell", new ShellCommand ());
shell.addCommand ("exec", new ExecCommand ());
shell.addCommand ("v", new VerboseToggleCommand ());
shell.addCommand ("exit", new ExitCommand ());
shell.addCommand ("quit", new ExitCommand ());
shell.addCommand ("q", new ExitCommand ());
shell.addCommand ("help", new HelpCommand ());
shell.addCommand ("h", new HelpCommand ());
shell.addCommand ("?", new HelpCommand ());
shell.run ();
}
}
class LoadCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length < 2)
throw new ShellException ("error: command name not specified");
if (argv.length < 1)
throw new ShellException ("error: command class not specified");
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
Object obj = null;
try
{
obj = Class.forName (argv[0]).newInstance ();
}
catch (ClassNotFoundException e)
{
throw new ShellException ("error: class not found: " + argv[0]);
}
catch (Exception e)
{
throw new ShellException ("error: could not instantiate class: " +
argv[0]);
}
if (!(obj instanceof Command))
{
throw new ShellException ("error: class is not a Command: " +
argv[0]);
}
shell.addCommand (argv[1], (Command) obj);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("Command sucessfully loaded and bound to '" +
argv[1] + "'");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmdclass> <cmdname>",
" cmdname: overwrites any command already bound to shell",
"Notes:",
"- Adds a command to the shell under the specified command name.",
"- The command class must implement the 'Command' interface.",
};
return (helpStrs);
}
}
class UnloadCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length < 1)
throw new ShellException ("error: command name not specified");
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Command oldCommand = shell.removeCommand (argv[0]);
if (oldCommand == null)
throw new ShellException ("error: no command bound to: " + argv[0]);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("Command sucessfully unbound");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmdname>",
"Notes:",
"- Removes a command from the shell."
};
return (helpStrs);
}
}
class DirCommand implements CommandWithHelp
{
PrintWriter out = null;
private int listContext (Context ctx, String path, int level,
boolean recurse, boolean nns, boolean objects)
throws NamingException
{
Enumeration enum;
int count = 0;
if (path == null)
path = "";
if (objects)
enum = ctx.listBindings (path);
else
enum = ctx.list (path);
while (enum.hasMoreElements ())
{
Object element = null;
boolean elementExists = true;
try
{
element = enum.nextElement ();
}
catch (NoSuchElementException e)
{
elementExists = false;
}
for (int i = 0; i < level; i++)
out.print (" ");
if (elementExists)
out.print (element);
else
out.print ("*** No such element ***");
count++;
if (recurse)
{
Object obj = null;
boolean resolvable = true;
try
{
if (objects)
obj = ((Binding) element).getObject ();
else
obj = ctx.lookup (((NameClassPair) element).getName ());
if (obj == null)
resolvable = false;
}
catch (NamingException e)
{
resolvable = false;
}
if (!resolvable)
{
out.println (" (not resolvable)");
continue;
}
out.println ();
if (obj instanceof Context)
{
count += listContext ((Context) obj, null, level + 1, recurse,
nns, objects);
}
}
else
out.println ();
}
if (nns && count == 0)
{
Object obj = null;
try
{
obj = ctx.lookup ("/");
}
catch (NamingException e)
{
}
if (obj instanceof Context)
{
for (int i = 0; i < level; i++)
out.print (" ");
out.println ("--> implicit next naming system (NNS)");
count += listContext ((Context) obj, null, level + 1, recurse,
nns, objects);
}
}
return (count);
}
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
Hashtable args = new Hashtable ();
String path = "";
for (int i = 0; i < argv.length; i++)
{
if (argv[i].charAt (0) == '-')
args.put (argv[i].substring (1), argv[i].substring(1));
else if (!path.equals (""))
throw new ShellException ("error: too many arguments");
else
path = argv[i];
}
Object obj;
boolean isLiteral = (args.get("literal") != null);
if (!isLiteral)
{
if (path.equals (""))
obj = shell.getCurrCtx ().getContext ();
else
{
obj = shell.resolveRelative (shell.getCurrCtx (), path).
getObject();
}
path = null;
}
else
obj = shell.getCurrCtx ().getContext ();
if (!(obj instanceof Context))
throw new ShellException ("error: path specified is not a Context");
else
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
boolean recurse = (args.get ("s") != null);
boolean nns = (args.get ("nns") != null);
boolean objects = (args.get ("b") != null);
int count;
this.out = out;
count = listContext ((Context) obj, path, 0, recurse, nns, objects);
if (count == 0)
out.println ("No elements found to list!");
else
out.println (count + " element(s) listed.");
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [-s] [-nns] [-b] [-literal] [context]",
" s: lists recursively any binding that is a Context",
" nns: looks for NNS on contexts that have no bindings",
" b: calls listBindings() to obtain list",
" literal: passes context directly to list(),listBindings()",
" context: '..' directives valid unless -literal is used",
"Notes:",
"- Lists bindings of the current or a relative context."
};
return (helpStrs);
}
}
class CdCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length == 0)
{
Context initCtx = shell.getCurrCtx ().getInitialContext ();
shell.setCurrCtx (new NamedContext (new CompositeName (),
initCtx, initCtx));
shell.setPrompt ("");
}
else
{
NamedObject obj;
obj = shell.resolveRelative (shell.getCurrCtx (), argv[0]);
if (!(obj.getObject () instanceof Context))
{
throw new ShellException ("error: path specified is not " +
"a Context");
}
shell.setCurrCtx (new NamedContext (obj.getName (),
(Context) obj.getObject (), shell.getCurrCtx ().
getInitialContext ()));
shell.setPrompt (obj.getName ().toString ());
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [context]",
" context: '..' directives are allowed unless -literal used",
"Notes:",
"- Changes current context to a relative context, tracking the",
" name using the CompositeName naming convention.",
"- If no '..' directives are used, the name is passed directly to",
" the lookup() method of the current context.",
"- If no relative context is specified, then the current context",
" is reset to the initial context.",
};
return (helpStrs);
}
}
class CdAtomicCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length == 0)
throw new ShellException ("error: atomic name must be specified");
NamedObject obj;
obj = shell.resolveRelativeAtomic (shell.getCurrCtx (), argv[0]);
if (!(obj.getObject () instanceof Context))
{
throw new ShellException ("error: path specified is not " +
"a Context");
}
shell.setCurrCtx (new NamedContext (obj.getName (),
(Context) obj.getObject (), shell.getCurrCtx ().
getInitialContext ()));
shell.setPrompt (obj.getName ().toString ());
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [context]",
" context: NO '..' directives allowed",
"Notes:",
"- Changes current context to a literal relative context, tracking",
" the name using the current context's name parser.",
"- The target context must support the getNameParser() method."
};
return (helpStrs);
}
}
class CdSchemaRootCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Object objForSchema = shell.getCurrCtx ().getContext ();
if (!(objForSchema instanceof DirContext))
throw new ShellException ("error: current context is not a " +
"DirContext");
String relName = argv.length > 0 ? argv[0] : "";
DirContext schemaCtx = ((DirContext) objForSchema).getSchema (relName);
JNDIShell child = new JNDIShell (shell, schemaCtx, null, null);
child.run ();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [dircontext]",
" dircontext: no '..' directives allowed",
"Notes:",
"- Starts a child shell with the initial context at the context",
" returned by calling getSchema() with the specified name."
};
return (helpStrs);
}
}
class CdSchemaClassDefCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Object objForSchema = shell.getCurrCtx ().getContext ();
if (!(objForSchema instanceof DirContext))
throw new ShellException ("error: current context is not a " +
"DirContext");
String relName = argv.length > 0 ? argv[0] : "";
DirContext schemaCtx = ((DirContext) objForSchema).
getSchemaClassDefinition (relName);
JNDIShell child = new JNDIShell (shell, schemaCtx, null, null);
child.run ();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [dircontext]",
" dircontext: no '..' directives allowed",
"Notes:",
"- Starts a child shell with the initial context at the context",
" returned by calling getSchemaClassDefinition() with the",
" specified name."
};
return (helpStrs);
}
}
class CdSchemaAttributeDefCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length == 0)
throw new ShellException ("error: attribute id must be specified");
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
Object obj = shell.getCurrCtx ().getContext ();
if (!(obj instanceof DirContext))
throw new ShellException ("error: path specified is not a " +
"DirContext");
String relName;
String attrID;
if (argv.length == 1)
{
relName = "";
attrID = argv[0];
}
else
{
relName = argv[0];
attrID = argv[1];
}
String[] arr = { attrID };
Attributes attrs = ((DirContext) obj).getAttributes (relName, arr);
Attribute attr = attrs.get (attrID);
if (attr == null)
throw new ShellException ("error: " + attrID +
": undefined attribute");
DirContext schemaCtx = attr.getAttributeDefinition ();
if (schemaCtx == null)
throw new ShellException ("error: null attribute definition");
JNDIShell child = new JNDIShell (shell, schemaCtx, null, null);
child.run ();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [dircontext] <attrid>",
" attrid: attribute ID of attribute definition to return",
" dircontext: no '..' directives allowed",
"Notes:",
"- Starts a child shell with the initial context at the context",
" returned by calling getAttributeDefinition() on the Attribute",
" returned by getAttributes() called with the specified name."
};
return (helpStrs);
}
}
class CdSchemaSyntaxDefCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length == 0)
throw new ShellException ("error: attribute id must be specified");
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
Object obj = shell.getCurrCtx ().getContext ();
if (!(obj instanceof DirContext))
throw new ShellException ("error: path specified is not a " +
"DirContext");
String relName;
String attrID;
if (argv.length == 1)
{
relName = "";
attrID = argv[0];
}
else
{
relName = argv[0];
attrID = argv[1];
}
String[] arr = { attrID };
Attributes attrs = ((DirContext) obj).getAttributes (relName, arr);
Attribute attr = attrs.get (attrID);
if (attr == null)
throw new ShellException ("error: " + attrID +
": undefined attribute");
DirContext schemaCtx = attr.getAttributeSyntaxDefinition ();
if (schemaCtx == null)
throw new ShellException ("error: null attribute syntax " +
"definition");
JNDIShell child = new JNDIShell (shell, schemaCtx, null, null);
child.run ();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [dircontext] <attrid>",
" attrid: attribute ID of syntax definition to return",
" dircontext: no '..' directives allowed",
"Notes:",
"- Starts a child shell with the initial context at the context",
" returned by calling getAttributeSyntaxDefinition() on the",
" Attribute returned by getAttributes() called with the",
" specified name."
};
return (helpStrs);
}
}
class AttrCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
String name = null;
Vector attrIDVector = new Vector ();
boolean ids = false;
boolean verbose = false;
for (int i = 0; i < argv.length; i++)
{
String arg = argv[i];
if (arg.startsWith ("["))
{
arg = arg.trim ();
if (!arg.endsWith ("]"))
{
throw new ShellException ("error: attr. ID list missing " +
"ending bracket");
}
arg = arg.substring (1, arg.length () - 1);
int commaPos = 0;
while (commaPos >= 0)
{
int savePos = commaPos;
commaPos = arg.indexOf (',', commaPos);
if (commaPos == -1)
attrIDVector.addElement (arg.substring (savePos));
else
{
attrIDVector.addElement (arg.substring (savePos,commaPos));
commaPos++;
}
}
}
else if (arg.equals ("-ids"))
ids = true;
else if (arg.equals ("-verbose"))
verbose = true;
else if (name == null)
name = arg;
else
throw new ShellException ("error: too many arguments");
}
String[] attrIDs;
if (attrIDVector.size () == 0)
attrIDs = null;
else
{
attrIDs = new String[attrIDVector.size ()];
for (int i = 0; i < attrIDs.length; i++)
attrIDs[i] = (String) attrIDVector.elementAt (i);
}
if (name == null)
name = "";
if (!(shell.getCurrCtx ().getContext () instanceof DirContext))
{
throw new ShellException ("error: current context is not " +
"a DirContext");
}
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
DirContext ctx = (DirContext) shell.getCurrCtx ().getContext ();
Attributes attrs;
NamingEnumeration attrEnum;
if (attrIDs == null)
attrs = ctx.getAttributes (name);
else
attrs = ctx.getAttributes (name, attrIDs);
if (attrs == null)
throw new ShellException ("error: attribute set null for this " +
"DirContext");
if (attrs.size () == 0)
throw new ShellException ("attribute set empty for this " +
"DirContext");
if (ids)
{
attrEnum = attrs.getIDs ();
if (attrEnum == null)
throw new ShellException ("error: attribute set returned null " +
"attribute ID enumeration");
while (attrEnum.hasMoreElements ())
out.println (attrEnum.next () + ":");
}
else
{
attrEnum = attrs.getAll ();
if (attrEnum == null)
throw new ShellException ("error: attribute set returned null " +
"attribute enumeration");
while (attrEnum.hasMoreElements ())
out.println (attrEnum.next ());
}
if (verbose)
{
out.println ("verbose: size: " + attrs.size ());
out.println ("verbose: case ingnored: " + attrs.isCaseIgnored ());
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [-ids] [-verbose] [[attrid,...]] [dircontext]",
" ids: print just the IDs returned by Attribute.getIDs()",
" verbose: print info on Attributes instance returned",
" [attrid,...]: specify limited set of string attr. IDs",
" dircontext: no '..' directives allowed",
"Notes:",
"- Starts a child shell with the initial context at the context",
" returned by calling getAttributeSyntaxDefinition() on the",
" Attribute returned by getAttributes() called with the",
" specified name."
};
return (helpStrs);
}
}
class ShowCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Object obj;
String path;
if (argv.length == 0)
{
obj = shell.getCurrCtx ().getContext ();
path = ".";
}
else
{
obj = shell.resolveRelative (shell.getCurrCtx (), argv[0]).
getObject ();
path = argv[0];
}
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println (path + ": " + obj);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [name]",
" name: name of any bound object, '..' directives allowed",
"Notes:",
"- The object returned by calling lookup() is printed.",
"- lookup() is not called if no name is specified."
};
return (helpStrs);
}
}
class RenameCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
if (argv.length < 2)
throw new ShellException ("error: not enough arguments");
Context ctx = shell.getCurrCtx ().getContext ();
ctx.rename (argv[0], argv[1]);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <oldname> <newname>",
" oldname: name of object; passed directly to rename()",
" newname: new name of object"
};
return (helpStrs);
}
}
class BindCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
if (argv.length < 2)
throw new ShellException ("error: object and binding name" +
"must be specified");
Object obj;
if (argv[0].startsWith ("~"))
{
obj = Class.forName (argv[0].substring (1)).newInstance ();
}
else
{
obj = shell.resolveRelative (shell.getCurrCtx (), argv[0]).
getObject ();
}
Context ctx = shell.getCurrCtx ().getContext ();
ctx.bind (argv[1], obj);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <[relname]|[~class]> <name>",
" relname: name of a bound object, '..' directives allowed",
" class: name of a loadable class with null constructor",
" name: atomic name to which to bind the specified object",
"Notes:",
"- Most providers will bind objects that implement Referenceable."
};
return (helpStrs);
}
}
class RebindCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
if (argv.length < 2)
throw new ShellException ("error: object and binding name" +
"must be specified");
Object obj;
if (argv[0].startsWith ("~"))
{
obj = Class.forName (argv[0].substring (1)).newInstance ();
}
else
{
obj = shell.resolveRelative (shell.getCurrCtx (), argv[0]).
getObject ();
}
Context ctx = shell.getCurrCtx ().getContext ();
ctx.rebind (argv[1], obj);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <[relname]|[~class]> <name>",
" relname: name of a bound object, '..' directives allowed",
" class: name of a loadable class with null constructor",
" name: atomic name to which to bind the specified object",
"Notes:",
"- Most providers will bind objects that implement Referenceable."
};
return (helpStrs);
}
}
class UnbindCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length == 0)
throw new ShellException ("error: binding name must be specified");
Context ctx = shell.getCurrCtx ().getContext ();
ctx.unbind (argv[0]);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <name>",
" name: passed directly to unbind()"
};
return (helpStrs);
}
}
class CreateCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length == 0)
throw new ShellException ("error: context name must be specified");
Context ctx = shell.getCurrCtx ().getContext ();
ctx = ctx.createSubcontext (argv[0]);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("new context: " + ctx);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <name>",
" name: passed directly to Context.createSubcontext()",
"Notes:",
"- Some providers do not support a simple createSubcontext() call;",
" it is necessary to call DirContext.createSubcontext(Attributes)."
};
return (helpStrs);
}
}
class DestroyCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length == 0)
throw new ShellException ("error: context name must be specified");
Context ctx = shell.getCurrCtx ().getContext ();
ctx.destroySubcontext (argv[0]);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <name>",
" name: passed directly to destroySubcontext()"
};
return (helpStrs);
}
}
class EnvCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 2 || argv.length > 1 && !argv[0].equals ("-d"))
throw new ShellException ("error: too many arguments");
Context ctx = shell.getCurrCtx ().getContext ();
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length == 0)
{
Hashtable env = ctx.getEnvironment ();
Enumeration enum = env.keys ();
while (enum.hasMoreElements ())
{
String name = (String) enum.nextElement ();
Object value = env.get (name);
out.println (name + ": " + value);
}
}
else
{
if (argv.length == 1)
{
String arg = argv[0];
int equalPos = arg.indexOf ('=');
if (equalPos == -1)
out.println (arg + ": " + ctx.getEnvironment ().get (arg));
else
{
String argName = arg.substring (0, equalPos);
String argValue = arg.substring (equalPos + 1, arg.length());
ctx.addToEnvironment (argName, argValue);
out.println ("'" + argName + "' set to '" + argValue + "' " +
"in environment");
}
}
else if (argv.length == 2)
{
ctx.removeFromEnvironment (argv[1]);
out.println ("'" + argv[1] + "' removed from environment");
}
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <[prop=[value]]|[-d prop]>",
" prop: name of an environment property",
"Notes:",
"- Although the Context interface allows setting of Object",
" properties, this shell command only allows string settings.",
"- [prop=value] can have no spaces; quote when necessary.",
"- [-d prop] removes a property from the environment."
};
return (helpStrs);
}
}
class ShellCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length < 1)
throw new ShellException ("error: cmd. script must be specified");
InputStream in = null;
try
{
in = new FileInputStream (argv[0]);
}
catch (FileNotFoundException e)
{
throw new ShellException ("error: could not open file: " + argv[0]);
}
JNDIShell child = new JNDIShell (shell, null, in, null);
child.run ();
shell.clearCommands ();
String[] cmdNames = child.getCommandNames ();
for (int i = 0; i < cmdNames.length; i++)
shell.addCommand (cmdNames[i], child.getCommand (cmdNames[i]));
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <script>",
"Notes:",
"- Reads shell commands from the specified script file.",
"- The currently executing shell's environment is duplicated and",
" passed to a child shell.",
"- Output of child shell goes to output of current shell.",
"- Parent shell's command table set to child's at exit."
};
return (helpStrs);
}
}
class ExecCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
Process p = Runtime.getRuntime ().exec (argv);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
int exitCode = p.waitFor ();
out.println ("External process exited: " + exitCode);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmd>",
"Notes:",
"- All parameters passed to this command are passed to the local",
" OS as a command line parameter."
};
return (helpStrs);
}
}
class VerboseToggleCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
if (argv.length > 0)
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("error: too many arguments");
}
shell.setVerbose (!shell.getVerbose ());
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Toggles the verbose flag in the current shell."
};
return (helpStrs);
}
}
class ExitCommand implements CommandWithHelp
{
public void execute (JNDIShell shell, String[] argv)
throws Exception
{
throw new ShellException (true);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Exits the current shell; any parent shell resumes control."
};
return (helpStrs);
}
}
class HelpCommand implements CommandWithHelp
{
String substituteCommandName (String cmdName, String syntax)
{
int cmdPos;
if ((cmdPos = syntax.indexOf ("%c")) != -1)
{
return (syntax.substring (0, cmdPos) + cmdName +
syntax.substring (cmdPos + 2));
}
else
return (syntax);
}
void sortStringArr (String[] arr)
{
for (int i = arr.length; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (arr[i-1].compareTo (arr[j]) < 0)
{
String temp = arr[j];
arr[j] = arr[i-1];
arr[i-1] = temp;
}
}
}
}
public void execute (JNDIShell s, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
PrintWriter out = new PrintWriter (s.getOutputStream (), true);
if (argv.length == 0)
{
out.println (shell.helpStr);
String[] cmdNames = s.getCommandNames ();
String[] helpStrs;
Command cmd;
sortStringArr (cmdNames);
for (int i = 0; i < cmdNames.length; i++)
{
out.print (cmdNames[i] + ": ");
cmd = s.getCommand (cmdNames[i]);
if (cmd instanceof CommandWithHelp)
{
CommandWithHelp hcmd = (CommandWithHelp) cmd;
helpStrs = hcmd.getHelpStrs ();
if (helpStrs.length > 0)
{
out.print (substituteCommandName (cmdNames[i],
helpStrs[0]));
}
}
else
{
out.print ("no help available (" +
cmd.getClass ().getName () + ")");
}
out.println ();
}
}
else if (argv.length == 1)
{
Command cmd = s.getCommand (argv[0]);
if (cmd == null)
throw new ShellException ("error: command undefined: "+ argv[0]);
if (!(cmd instanceof CommandWithHelp))
throw new ShellException ("error: command does not support " +
"help: " + argv[0]);
CommandWithHelp hcmd = (CommandWithHelp) cmd;
String[] helpStrs = hcmd.getHelpStrs();
for (int i = 0; i < helpStrs.length; i++)
out.println (substituteCommandName (argv[0], helpStrs[i]));
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [cmdname]",
" cmdname: name of command for which to display help",
"Notes:",
"- If executed with no parameters, displays syntax of all commands."
};
return (helpStrs);
}
}