home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / networking / security / example / PasswordSecurityManager.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.1 KB  |  65 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18.  
  19. class PasswordSecurityManager extends SecurityManager {
  20.  
  21.     private String password;
  22.  
  23.     PasswordSecurityManager(String password) {
  24.         super();
  25.         this.password = password;
  26.     }
  27.  
  28.     private boolean accessOK() {
  29.         int c;
  30.         DataInputStream dis = new DataInputStream(System.in);
  31.         String response;
  32.  
  33.         System.out.println("What's the secret password?");
  34.         try {
  35.             response = dis.readLine();
  36.             if (response.equals(password))
  37.                 return true;
  38.             else
  39.                 return false;
  40.         } catch (IOException e) {
  41.             return false;
  42.         }
  43.     }
  44.     public void checkRead(FileDescriptor filedescriptor) {
  45.         if (!accessOK())
  46.             throw new SecurityException("Not a Chance!");
  47.     }
  48.     public void checkRead(String filename) {
  49.         if (!accessOK())
  50.             throw new SecurityException("No Way!");
  51.     }
  52.     public void checkRead(String filename, Object executionContext) {
  53.         if (!accessOK())
  54.             throw new SecurityException("Forget It!");
  55.     }
  56.     public void checkWrite(FileDescriptor filedescriptor) {
  57.         if (!accessOK())
  58.             throw new SecurityException("Not!");
  59.     }
  60.     public void checkWrite(String filename) {
  61.         if (!accessOK())
  62.             throw new SecurityException("Not Even!");
  63.     }
  64. }
  65.