HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java & Security"
Vol. 5, Issue 10, p. 52

	


Listing 1

//CodeMySecurityManager.java
public class MySecurityManager extends SecurityManager
{
    private boolean flag;
    public MySecurityManager(boolean flag)
    {
        this.flag = flag;
    }

    // Override checkRead function
    public void checkRead (String s)
    {
        if(!flag)
        {
            throw new SecurityException("checkRead");
        }
    }

    // Override checkWrite function
    public void checkWrite (String s)
    {
        if(!flag)
        {
            throw new SecurityException("checkWrite");
        }
    }
}

Listing 2

// Code MyTest.java
import java.io.*;

public class MyTest
{

  public static void main (String[] args)
  {
    try
    {
      MySecurityManager secMgr = new MySecurityManager(true);
        // Set the security manager
      System.setSecurityManager(secMgr);
    }
    catch (SecurityException excp)
    {
      System.out.println("Can't Change the SecurityManager!");
    }
    // Construct an Object
    MyTest test = new MyTest();
    test.testFunc();

  }


  public void testFunc()
  {
    try
    {

      BufferedReader is = new BufferedReader(
                              new FileReader("input.txt"));
      DataOutputStream os = new DataOutputStream(
                                new FileOutputStream("out-      
                                               put.txt"));

      System.out.println("Successfully opened the files for     
                        read/write ");

      String readString;

      while ((readString = is.readLine()) != null)
      {
          os.writeBytes(readString);
          os.writeByte('\n');
      }
      System.out.println("Successfully performed the      
      read/write operation");
      is.close();
      os.close();
    }
    catch (IOException excp)
    {
      System.err.println("IOException.");
    }
  }

}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.