Notes
Slide Show
Outline
1
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
2
Juno classes
3
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
4
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”)
5
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!
6
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



7
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


8
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());
    • }
9
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)
10
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


11
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
12
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)
13
JFile system uses two trees
14
JFile (easy part)
  • private fields for
    • String name Date createDate
    • User owner 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

15
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


16
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
17
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 );
18
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
19
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)
20
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
21
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


22
cd
  • Syntax
    • cd  # change to home directory
    • cd foo # change to subdirectory foo
    • cd . # stay where you are
    • cd .. # change to parent of current directory
    • cd ..\bar\whatever  # not supported in Juno
23
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 );
24
 
25
"mars:\users\eb>"
  • 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          2  backup\
  • 04/05/2004  08:33 AM         13 Foo.java
  • 04/05/2004  08:32 AM         17 memo.txt
  •                2 File(s)     30 bytes
  •                3 Dir(s)


26
JFile getSuffix
  • A unix/linux tradition                                                    appends a / when listing                                               the name  of a Directory
  • We want JFiles to behave                                              this way, but to use the                                                    windows \ instead
  • Ask a JFile to tell you its suffix                                                  by sending it a getSuffix message
  • getSuffix is abstract in JFile.java
27
“\” 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
28
getPathName (JFile line 77)
  • public String getPathName() {
  •    if (this.isRoot()) {
  •       return separator;
  •    }
  •    if (parent.isRoot()) {
  •       return separator + getName();
  •    }
  •    return parent.getPathName() +
  •           separator + getName();
  • }
29
Formatting
  • Dates
  • Numbers
  • I18N
30
Boxes and arrows
  • coming soon