简体   繁体   中英

JFileChooser causes AccessControlException when invoked through JS, works when called through applet

I'm working on a helper applet to manage transfers of large files. We currently have an applet that manages the full transaction, from selecting the folder containing the files, transferring them, and reporting on success and failure of the operation, but it's very unsatisfactory in a number of ways. So now we just want very subtle assistance from applets to let the user find a folder on their local machine and manage the transfer. We'd like as much as possible of the feedback and user interaction to use Web-based controls. So as you can imagine this requires fairly extensive use of the bridge between applets and Javascript in the browser.

For the most part this is fairly straightforward, but I've found a weird edge case. I have a method that pops up a JFileChooser dialog and looks at the contents of that folder and reports it back. I have this working when I create a JButton in the start() method and use that to pop up the JFileChooser. But I put all the code for the JFileChooser into a method called showFolderSelect() so that I could also try opening the JFileChooser from Javascript. So this call from within the applet works:

public void actionPerformed(ActionEvent event) {
    if ("browse".equals(event.getActionCommand())) {
        showFolderSelect();
    }
}

But this call from Javascript does not:

function selectFolder() {
    var infoApplet = document.getElementById('infoApplet');
    infoApplet.showFolderSelect();
}

The Javascript call gets me the following message in Opera's console:

[5 October, 2003 6:26:56 AM] JavaScript - http://localhost:8080/applet-webapp/
Event thread: click
Uncaught exception: Error: java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read)
Error thrown at line 20, column 12 in selectFolder() in http://localhost:8080/applet-webapp/:
   infoApplet.showFolderSelect();
called from line 1, column 0 in <anonymous function>(event) in http://localhost:8080/applet-webapp/:
   selectFolder();

Any ideas on how I can get this to work from Javascript? Or am I just going to have to use the JButton created in the start() method of the applet?

Please note that I can't expect users to be able to monkey around with java.policy configurations or anything like this. The only nod to security and access control that we can expect is that the applet(s) is(are) digitally signed.

Thanks for any help or insight on this issue.

I think this illustrates what Java can do and what JavaScript cannot. It looks like JavaScript is not allowed to call methods that allow access to the client system. Maybe JavaScript access to Java code is restricted to what would be allowed by an unsigned applet.

I can't imagine any way of getting around that if this is just a limitation in the Java - JavaScript bridge. And to me it looks like it is.

If you are unhappy with the overall user experience within the applet, consider moving this to JavaFX instead of a pure browser interface with JavaScript.

Check out the Java FX Ensemble gallery for a list of the UI components you can use.

On most modern Web browsers, JS applications may only open File dialogs in response to a user action. Thus, you can create an Open File dialog from an onclick handler (including, apparently, a handler in a Java applet), but not from eg an onload handler.

Thus, you may need to create a button or <a href="#"/> for the user to click on, then call showFolderSelect() while inside that event handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM