Site With The Lamp

A LAMP Portal


Linux
Java
PHP


Blog
etc

The totally Rad Software Company - Sponsor

Signed Applets Howto.

New Articles

J2ME Development with the 6600.

Swing slow paint problem.

The case of the missing errors.

In the introduction to tutorial you would not have seen any images getting loaded when you clicked on the applet. If you did congratulations you have detected a major bug in the java plug-in. If you missed the demo link here it is again. You might want to take a look at the source code as well.

If you open the console window, it should display something similar to the following stacktrace:

   java.security.AccessControlException: access denied 
   (java.net.SocketPermission radinks.com resolve)
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
	at java.security.AccessController.checkPermission(AccessController.java:401)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
	at java.lang.SecurityManager.checkConnect(SecurityManager.java:1023)
	at sun.awt.SunToolkit.createImage(SunToolkit.java:519)
	at com.radinks.tests.UnsignedApplet.init(UnsignedApplet.java:32)
	at sun.applet.AppletPanel.run(AppletPanel.java:353)
	at java.lang.Thread.run(Thread.java:534)

Sponsered Content

Rad SFTP - A Pure java secure FTP applet.

Close inspection reveals that the plug-in is pointing the fingure at line 32 of our applet. That's the line which attempts to create an image from a remote URL. This is an example of how the sandbox prevents applets from carrying out certains tasks. The java plug-in does not think our applet should be allowed to open socket connections to remote hosts and throws an exception.

Some clues to the solution can also be found in the stacktrace. It mentions an AccessController class. Reading through the docs reveals that it can be used overcome the Sandbox restrictions. So we will change our code for the init method to make use of the AccessController.


 public void init()
 {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            try {
                /*
                * create the url that references an image stored on a remote server
                */
                URL u = new URL("http://radinks/images/images/drop-logo100.jpg");
                img = getToolkit().createImage(u);

                System.out.println("ok" + img);
            }
            catch (MalformedURLException ex) {
                System.err.println("err: malformed url");
            }
            return null;
        }
    });
 }

Even after you recompile the applet you find that the same exception is still being thown. Now the problem is that your applet is still untrusted. Even though your code is now equiped to break out of the sandbox you still need to package it properly, and digitally sign it. That would enable the applet to ask for and obtain the required permissions. We will look into that in the next step in this guide.



Short cuts
  Part 1   Introduction, AccessController, keytool, jarsigner
  Part 2   The help of a proxy