Security Manager Example

Previous     Java Source      NullSecurityManager.java      SecurityManager Class    SecurityException Class     All lectures

import java.io.*;
import java.awt.*;
import java.awt.event.*;

class Editor extends Frame implements ActionListener {
   TextField messages = new TextField();
   TextArea text = new TextArea();
   MenuItem load, save, saas, quit;
   String file, savefile;
   
   Editor() {
      super("Editor");
      setLayout(new BorderLayout());
      add ("North", messages);
      messages.setEditable(false);
      add ("Center", text);
      Menu menu = new Menu ("File");
      menu.add (load = new MenuItem("Load ..."));
      menu.add (save = new MenuItem("Save"));
      menu.add (saas = new MenuItem("Save As..."));
      menu.add (quit = new MenuItem("Quit"));
      MenuBar mb = new MenuBar();
      mb.add(menu);
      setMenuBar (mb);
      setSize(600,440);
      setVisible(true);
      load.addActionListener(this);
      save.addActionListener(this);
      saas.addActionListener(this);      
      quit.addActionListener(this);
      messages.setText("Editing Clean Slate");
   }
   
   public void actionPerformed (ActionEvent e) {
      if (e.getSource() == load) loadFile();
      else
      if (e.getSource() == saas) saveFile();
      else
      if (e.getSource() == save) saveIt();
      else
      if (e.getSource() == quit) dispose();
   }
   
   public void loadFile() {
      FileDialog fd;
      try {
         fd = new FileDialog (this, "Load The File", FileDialog.LOAD);
         fd.setVisible(true);
         if ((file = fd.getFile()) == null) return;
         savefile = fd.getFile();
      } catch (Exception e) {
         messages.setText(String.valueOf(e));
         System.out.println("Load Exception (1): "+e.toString());
         savefile = "abcd";
      }

      try {
         FileInputStream fis = new FileInputStream (savefile);
         byte data[] = new byte[fis.available()];
         fis.read(data);
         text.setText(new String(data));
         messages.setText("Editing file "+savefile);
      } catch (Exception e) {
         messages.setText("Editing new file");
         System.out.println("Load Exception (2): "+e.toString());
      }
   }
   
   public void saveFile() {
      FileDialog fd;
      try {
         fd = new FileDialog(this, "Save the File", FileDialog.SAVE);
         fd.setVisible(true);
         if ((file = fd.getFile()) == null) return;
         savefile = fd.getFile();
      } catch (Exception e) {
         messages.setText(String.valueOf(e));
         System.out.println("Save Exception (1): "+e.toString());
         savefile = "abcd";
      }

      try {
         FileOutputStream fos = new FileOutputStream (savefile);
         String out = text.getText();
         byte data[] = new byte[out.length()];
         for (int i=0 ; i < out.length() ; i++)
            data[i] = (byte)out.charAt(i);
         fos.write(data);
         messages.setText("Contents saved to "+savefile);
      } catch (SecurityException ee) {
         messages.setText(String.valueOf(ee));
         System.out.println("Save Exception (2): "+ee.toString());
      } catch (Exception e) {
         messages.setText("Cannot write to file - read only?");
         System.out.println("Save Exception (3): "+e.toString());
      }
   }
   
   public void saveIt() {
      if (savefile == null) {
         messages.setText("No File To SAVE!!!  Use Save As...");
         return;
      }

      try {
         FileOutputStream fos = new FileOutputStream (savefile);
         String out = text.getText();
         byte data[] = new byte[out.length()];
         for (int i=0 ; i < out.length() ; i++)
            data[i] = (byte)out.charAt(i);
         fos.write(data);
         messages.setText("Contents saved to "+savefile);
      } catch (SecurityException se) {
         messages.setText(String.valueOf(se));
         System.out.println("SaveIt Exception (1): "+se.toString());
         return;
      } catch (Exception e) {
         messages.setText("Cannot write to file "+savefile+" - read only?");
         System.out.println("SaveIt Exception (2): "+e.toString());
      }
   }
}

class SmallFrame extends Frame implements ActionListener {
   Button go;
   TextField messages;
   SmallFrame () {
      add ("Center", go = new Button("Activate Security"));
      add ("South", messages = new TextField());
      go.addActionListener(this);
      setSize(400,120);
      setVisible(true);
   }

   public void actionPerformed (ActionEvent e) {
      try {
         if (e.getSource() == go)
            System.setSecurityManager(new MySecurityManager(messages));
      } catch (Exception se) {
         messages.setText(String.valueOf(se));
         System.out.println("Button Exception: "+se.toString());
      }
   }
}

class MySecurityManager extends NullSecurityManager {
   TextField messages;
   
   MySecurityManager (TextField messages) {  this.messages = messages;  }
   
   public void checkRead (String file) {
      messages.setText("CheckRead: "+file+" ");
      System.out.println("-----------");
      System.out.println("CheckRead: "+file+" ");
      System.out.println("-----------");

      InputStream is = null;
      String command = "grep sex "+file;
      try {
         is = Runtime.getRuntime().exec(command).getInputStream(); 
      } catch (IOException e) {}

      String str = null;
      try { 
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         str = br.readLine();
      } catch (IOException e) {}

      if (str == null) {
         messages.setText(file+" OK");
         System.out.println("-----------");
         System.out.println(file+" OK.");
         System.out.println("-----------");
         return;
      } else {
         messages.setText(file+" contains bad word, aborting.");
         System.out.println("-----------");
         System.out.println(file+" contains bad word, aborting.");
         System.out.println("-----------");
         throw new SecurityException("no good");
      }
   }
   
   public void checkWrite (String s) {
      messages.setText("CheckWrite: "+s);
      System.out.println("CheckWrite: "+s);
      throw new SecurityException ("Security violation on "+s);
   }
}

public class SecurityApplet {
   public static void main (String args[]) {
      new SmallFrame();
      new Editor();
   }
}