Java Reference
In-Depth Information
in which you specify the details of the policy file. The program makes it fairly
easy to set the codebase information, to choose what permissions you need, to
set the target names and action settings, and to select where to put the pol-
icy file. See the Web Course Chapter 14 for more information about using this
program.
A program may need to check if a particular action is allowed before attempting
it. Perhaps you create a class that will be used with other people's programs who
might be using different security settings. It might waste a lot of execution time
in your code if an action in the final step is not allowed.
So you can check if an action is allowed by first creating an instance
of the particular permission and passing it to checkPermission (java.
security.Permission) in the SecurityManager class. If it throws a
SecurityException ,you will then know the action is not allowed. For exam-
ple, you could check on access to files with the following:
java.security.Permission permit =
new java.io.FilePermission ( " /- " , " read " );
SecurityManager security = System.getSecurityManager ();
if (security! = null) {
try {
security.checkPermission (permit);
}
catch (SecurityException se) {
System.out.println ("File access blocked");
}
}
14.6 A client application
We might want to communicate with our server using a custom client program
rather than just with a browser. With Java we can create a standalone client
application to request and read files from the server just like a web browser but it
could also interact in other ways. For example, maybe the client sends a command
to the server to instruct it to carry out some action on the local platform such
as running a diagnostic program. (See Chapter 23 for a discussion of running
external programs from within a Java program.) As we see in the next chapter, we
can even maintain a connection with the client indefinitely and avoid the stateless
condition of the usual browser-server interaction.
Here we show the run() method in client application ClientApp . This
method sends a request to the server for a file. It connects to the server by the
creation of a Socket instance with the server's address and port number. Using
the input and output streams obtained from the socket, the client sends a request
to the server and then reads the data returned by the server one line at a time. The
client then displays this data in a text area.
Search WWH ::




Custom Search