CS110 Lecture 18 Tuesday, April 6, 2004 • Announcements – hw8 due Thursday, April 8 – pass/fail, withdraw deadline Thursday, April 8 • Agenda – Questions – Juno – JFile system internals Lecture 18 1 Juno classes Lecture 18 2 Shell • “shell” is standard computer science terminology for an operating system’s command line interface • Windows Command Prompt is a shell • xemacs gives you access to the same shell • Juno has a shell, presented to a user after she logs in Lecture 18 3 Shell object • Constructor sets some fields – the Juno system that created this Shell (37) (like issuing Bank in BankAccount) – the User and the console (38, 39) – the current Directory (the User’s home) (40) • Then invokes CLIShell (command line interface) which works just like LoginInterpreter – get an input line from the user (50) – invoke this Shell’s interpret method – done when interpret returns false for moreWork (user has typed “logout”) Lecture 18 4 Shell interpret method (60) • Create a StringTokenizer for the input line, after throwing away Juno comments (# …) • First token is the commandName (66) • If it’s “logout”, then done (return false) • Replace if else if … with dispatch table – (70,71) look up commandObject in command table (commandName String is key) – (76) send commandObject a doIt() message • Polymorphism! Lecture 18 5 Polymorphism poly (many) + morph (shape) • ShellCommandTable.java – maintains a list of (abstract) ShellCommand objects – client retrieves them and sends them messages – without knowing what kinds of JFiles they are! • Client refers to objects of type Parent that are really instances of a Child extending Parent • Powerful design tool - ignorance is bliss Lecture 18 6 abstract class ShellCommand • Documentation managed here – helpString and argstring fields (19, 20) – initialized by protected constructor (31, 32) • doIt() method (54): • abstract public void doIt ( StringTokenizer args, Shell sh ); • doIt is passed the rest of the text on the Juno command line, and the Shell it’s acting for • Each concrete ShellCommand implements its own doIt() - polymorphism Lecture 18 7 Designing a ShellCommand object • MkdirCommand extends ShellCommand (18) • Constructor (24) – super invokes ShellCommand constructor, telling it help string and argument string for mkdir • implement abstract method doIt (37) – next token on line is the name of the Directory to be made – tell Directory constructor the name, owner, parent public void doIt( StringTokenizer args, Shell sh ) { String filename = args.nextToken(); new Directory(filename, sh.getUser(), sh.getDot()); } Lecture 18 8 ShellCommandTable • Juno constructor creates a ShellCommandTable (Juno.java line 52) • ShellCommandTable.java – declare and initialize a TreeMap (line 23) – constructor (line 31) invokes fillTable (line 69) – fillTable creates one of each concrete ShellCommand objects, invokes install (line 61) to put it in the table – client (a Shell) invokes lookup (43), which wraps Map get method (and does the cast) Lecture 18 9 How the dispatch table works (reprise) In CLIShell loop: – get first token on the line: commandName – lookup commandObject with commandName key – send doIt() message • Each particular ShellCommand extends the abstract ShellCommand class, implementing doIt() in its own way • Polymorphism at work Lecture 18 10 How LoginInterpreter interpret works (reprise) • get first token on the line • use if - else if - else if … logic – – – – if “exit” return false! // leave loop in CLIlogin if “register” // create account for new user if “help” // give help else // input is a username Lecture 18 11 Dispatch table vs if-else if-else if • To add new commands just add a table entry • Command semantics separate from syntax • Lots of design overhead, hard to understand • Good for large command sets that will grow (Juno shell commands) • To add new commands must edit the main loop • Command semantics and syntax in same place • Quick and dirty, easy to understand and code • Good for small command sets that stay put (Juno login loop) Lecture 18 12 JFile system uses two trees Java class hierarchy Juno Directory and TextFile hierarchy \ class Object users\ eb\ class JFile backup\ Foo.java class Directory class TextFile memo.txt bill\ Lecture 18 13 JFile (easy part) • private fields for String name User owner Date createDate Date modDate • getters and setters as appropriate • abstract getSize method since each child must provide its own implementation: – number of JFiles in a Directory – number of characters in a TextFile Lecture 18 14 Testing JFile, Directory, TextFile • JFile version 5 (before Juno) has static code for testing JFile and its subclasses • We didn’t study it • Best way to test these classes as a part of Juno is to write Juno commands – type to test newfile (done) – cd and list to test mkdir (done for Juno 7) • Read CdCommand • hw9: improve the ListCommand we will provide Lecture 18 15 JFiles in Juno • Directory constructor signature: (String directoryName, User owner, Directory parent) • User constructor signature (String loginName, Directory home, String realName) • Circular reference problem needs solving Lecture 18 16 Set up Juno file system (Juno.java 56-60) // create root directory slash = new Directory( "", null, null ); // create system administrator (a User) User root = new User ( "root", slash, "Rick Martin" ); // add system administrator to user table users.put( "root", root ); // system administrator owns his home slash.setOwner(root); // create Directory for regular user homes userHomes = new Directory ( "users", root, slash ); Lecture 18 17 Home Directory • Each User has a home directory, in users, created when User is created (LoginInterpreter register method, line 100) • Any user can read or write in any other user’s home directory Lecture 18 18 Managing the JFile tree • A Directory – keeps a TreeMap of JFiles in its jfile field, keyed by name – has methods to add and retrieve JFiles by name – has a method that allows client to loop on contents • A JFile has a parent field (line 37) in which it keeps a reference to the Directory it lives in (like BankAccount – Bank) Lecture 18 19 JFile constructor • JFile.java, line 49 protected: visible to children, not public • lines 51-52 are easy: they initialize fields if (parent != null) (line 53) parent.addJFile( name, this ); • if this JFile has a parent (not top of JFile tree) send message to parent to add this JFile (Directory or TextFile) to its TreeMap, with name as key. (Directory.java line 67) • Careful: parent directory != parent class Lecture 18 20 Current Directory • When a Juno user logs in, the current Directory is her home Directory • Convention (in the world of shells) . signifies the current Directory .. signifies its parent • Juno Shell provides getDot and setDot methods Lecture 18 21 cd • Syntax cd cd cd cd cd # change to home directory foo # change to subdirectory foo . # stay where you are .. # change to parent of current directory ..\bar\whatever # not supported in Juno Lecture 18 22 doIt() in CdCommand class String dirname = ""; Directory d = sh.getUser().getHome(); if ( args.hasMoreTokens() ) { dirname = args.nextToken(); if (dirname.equals("..")) { if (sh.getDot().isRoot()) d = sh.getDot(); // no change else d = sh.getDot().getParent(); } else if (dirname.equals(".")) d = sh.getDot(); // no change else d = (Directory)sh.getDot(). retrieveJFile(dirname)); } sh.setDot( d ); Lecture 18 23 JFile getSuffix \ • A unix/linux tradition users\ appends a / when listing the name of a Directory eb\ • We want JFiles to behave backup\ this way, but to use the Foo.java windows \ instead memo.txt • Ask a JFile to tell you its suffix bill\ by sending it a getSuffix message • getSuffix is abstract in JFile.java Lecture 18 24 “\” vs “/” • Windows uses one, Unix the other • Java knows about both • File.java (in the Java API) declares public static final String separator • JFile.java declares public static String separator = File.separator Lecture 18 25 dir (windows) full path name in shell prompt directories first, including . and .. file, directory and character counts, including . and .. Lecture 18 26 list (Juno) full path name in shell prompt mars:\users\eb> list Directory of \users\eb 04/05/2004 08:34 AM 04/05/2004 08:34 AM 04/05/2004 08:31 AM 04/05/2004 08:33 AM 04/05/2004 08:32 AM 2 File(s) 3 Dir(s) 2 13 17 30 . .. backup\ Foo.java memo.txt bytes file, directory and character counts, including . and .. Lecture 18 27 getPathName (JFile line 77) public String getPathName() { if (this.isRoot()) { return separator; } if (parent.isRoot()) { return separator + getName(); } return parent.getPathName() + separator + getName(); } Lecture 18 28 Boxes and arrows • coming soon Lecture 18 29 Formatting • Dates • Numbers • I18N Lecture 18 30