|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheHighScoreclass stores and protects access to the user's high score forTerrysGame(and any other games that call it). For simplicity, this class saves the high score value into a file, called.highscore, in the user's home directory. However, before allowingTerrysGameto retrieve or update the user's high score value, this class checks to make sure that the user has grantedTerrysGamepermission to access the high score in his/her security policy file.
TerrysGame has the HighScorePermissionTo check whether or notTerrysGamehas permission to access the user's high score, theHighScoreclass must:Here is the code:
- Call
System.getSecurityManager()to get the currently installed security manager.
- If the result is not null (i.e., there is a security manager, as opposed to the caller being an application that is unrestricted), then
- Construct a
HighScorePermissionobject, and
- Call the security manager's
checkPermissionmethod, and pass it the newly constructedHighScorePermissionobject.TheSecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new HighScorePermission(gameName)); }checkPermissionmethod essentially asks the security manager ifTerrysGamehas the specifiedHighScorePermission. In other words, it asks the security manager ifTerrysGamehas permission to update the user's high score value for the specified game (TerrysGame). The underlying security framework will consult the user's security policy to see ifTerrysGameindeed has this permission.
Hereis the complete source code for the
HighScoreclass.Note: The
doPrivilegedmethod calls are used to enableHighScoreto temporarily access resources that are available to it but that are not available to the code that calls it (TerrysGame). For example, it is expected that the policy file will grantHighScorepermission to access the.highscorefile in the user's home directory, but it will not grant this permission to games, such asTerrysGame. For more information about the use ofdoPrivilegedmethods, see New Privileged Block APIon the public
java.sun.comweb site.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.