The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Essential Java Classes
Lesson: Accessing System Resources

Deciding What SecurityManager Methods to Override

You may have to override several of SecurityManager's checkXXX() methods depending on which operations you want your security manager to impose restrictions on.

The first column in the following table are objects on which you can perform various operations. The second column lists the SecurityManager methods that approve the operations on the objects in the first column.

Operations On    Approved By
sockets          checkAccept(String host, int port)
                 checkConnect(String host, int port)
                 checkConnect(String host, int port, Object executionContext)
                 checkListen(int port)

threads          checkAccess(Thread thread)
                 checkAccess(ThreadGroup threadgroup)

class loader     checkCreateClassLoader()

file system      checkDelete(String filename)
                 checkLink(String library)
                 checkRead(FileDescriptor filedescriptor)
                 checkRead(String filename)
                 checkRead(String filename, Object executionContext)
                 checkWrite(FileDescriptor filedescriptor)
                 checkWrite(String filename)

system commands  checkExec(String command)

interpreter      checkExit(int status)

package          checkPackageAccess(String packageName)
                 checkPackageDefinition(String packageName)

properties       checkPropertiesAccess()
                 checkPropertyAccess(String key)
                 checkPropertyAccess(String key, String def)

networking       checkSetFactory()

windows          checkTopLevelWindow(Object window)
Depending on your security policy, you can override some or all of these methods. For example, suppose you are writing a Web browser or applet viewer and want to disallow applets from using sockets altogether. To do this you override the four methods that affect socket access.

Many of the checkXXX() methods are called in multiple situations. For example, the checkAccess(ThreadGroup g) method is invoked when you create a ThreadGroup, set its daemon status, stop it, and so on. When overriding a checkXXX() method make sure that you understand all the situations in which it can be invoked.

The default implementation provided by the SecurityManager class for all checkXXX() methods is:

public void checkXXX(. . .) {
    throw new SecurityException();
}
Most security policies that you want to implement will likely be more selective than disallowing everything! So you may find that you have to override all SecurityManager's checkXXX() methods.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search