Thursday, May 08, 2008

Applets Reloaded - TS-6290

These are the tidied up notes I took from the session at the JavaOne 2008 conference in San Francisco, California. There may well be mistakes and ommissions. I will come back and correct them once the conference has completed. However my first priority is to get the information published before the backlog gets too large and swamps me. All comments welcome. Enjoy!

- Ken Russell & Sven Goethel

- Applets in Java6_u10 is a ground up re-write
- Applets no longer ru the browser process. (in the classic java browser plugin all the applets ran in same VM in the browser process)
- Now there is a thin JVM broker in the browser process and you can launch a new (seperate) VM per applet when required
- Advantages: Improved reliability (no browser crashes), no browser hang when loading, built in JNLP support (specify command line args on per applet instance), improved java/javascripts bridge and improved Vista support (signed applets run in protected mode IE)

Deploying via JNLP
- You can launch applets directly from JNLP files
- These follow the same descriptor as those in the Java Web Start architecture (therefore you can run your app as an applet or JWS application)
- Referencing resources - You can also reference your resources using relative (rather than absolute as with the old plugin) code paths. This allows simple migration of content from server to server and place to place
- There is also a new applet-desc tag. This references the main class, width, height
- There is also an information tag and a resources tag which references the other jar files providing info info like eager loading etc.

Command Line Arguments
- You can now define the CL args used to start the applet's JVM such as heap size, GC type, JRE version, plus properties - e.g. if you want to start an OpenGL applet
- The plugin launches a new JVM machine instance when necessary (separate_jvm tag)
- Demo: Quake 2 now runs in an applet - this shows the isolation and seperate JVM features
- The end user can specify CLIs too. There is a secure and a non-secure set.
- You must sign your jar and jnlp descriptor to get the capability to get an unrestricted JVM for your app

JRE Version Selection
- This allows you to run a particular applet in a particular version of the JRE
- This is useful for enterprises who want to QA applets against a particular JRE version
- It uses 'plus' semantics so you can have a JRE or later, or a specific family (1.4, 1.5 etc.)
- Auto download of requried but not installed JREs is supported by JNLP - you can also reference an installer for JWS
- Versions?: As early as 1.4.2 is supported on windows. >= 1.5 is supported on X11(Linux). Nothing for Apple.

Performance and Compatability Tips
- Tip: Avoiding relaunches - The first JVM used to parse the JNLP file might not satisfy the CLI args. If not download a new one
- Tip: Faster startup - use the update tag in the JNLP file to drastically reduce 2nd time startup (it downloads the new version and caches it in the background) The current run will use the version of the applet already in the cache but next time the newer version will be loaded. Downloads are done in the background
- Tip: Maintaining backward compatability - You can author content which runs on earlier versions of the plugin but which takes advantage of the new features. Simply provide a fully-formed applet tag as well as the jnlp_href parameter. Earlier versions will ignore the jnlp_href parameter. The new plugin will ignore the archive and code parameters
- Tip: Using JNLP extensions. It is now trivial to use extensions (e.g. OpenGL) which are available to the desktop. Use OpenGL via JOGL, OpenAL via JOAL, plus JavaFX player components such as webkit, video, and scene graph. Simply refer to the extension JNLP in the resources section of your JNLP file

- You can now replace the standard Java logo with an animated GIF. This is centred in the middle of the applet. There is also better control over foreground and background colours
- Can still do better. Phase the app as a tiny app first, then download the rest behind it (using the JNLP download services.) You can use this to draw a progress indicator

Java/Javascript Integration
- The bridge has been re written. It has been made as compatible as possible with previous implicit LiveConnect spec from Mozilla (in association with Mozilla)
- It is essentially as fast in all cases as the previous applet plugin
- You can now call static java methods in your applet code from javascript. e.g. new JFrame and launch a new JFrame window. NOTE: Can't do this in flash
- All this functionality is implemented on all browsers
- It is also multithread safe ("It will not crash the broowser")
- There is in addition a unified security model - this allows you to call from javascript to java. Security wise, this treats the JavaScript like an unsigned applet coming from the origin domain of the web page (which may not be the same as the applet). This is now done on all browsers. This does break signed JavaScript if anyne is using it (but Mozilla thought no-one used it)
- You can pass a JavaScript array into Java and it gets converted automagically.
- Overloaded methods are supported
- There is a new keyword "packages" which gives you access to the java namespace from JavaScript. This means you can execute static methods etc. This means the functions from your applet are extended into the surrounding web page
- Java is Multithreaded but JavaScript is not. How do you reconcile this? There is a per-applet worker thread which handles JavaScript to Java calls. It is distinct from all the other Java threads in your applet (e.g. AWT Dispatch thread etc.) If you call into the applet from JavaScript, this will do the processing

Manipulating the DOM
- Your applet can now manipulate the page on which it lives
- But the W3C DOM access has been sketchy support until now.
- Use org.w3c.dom.Document. Can now get the document from java code. is a nice way to do this in Java code

Access to Web Services
- Flash does not have as rich a security architecture
- Cross-Domain XML standards specify how untrusted code may gain access to arbitrary web services on the internet (c.f.
- Now you can access all the web services which provide services via this spec. e.g. Amazon, Facebook, Flickr, picase, etc.
- BUT Domain restrictions are not supported

Experimental Functionality
- JavaFX applets. You can now drag and drop an applet out of the browser. Even if you shut down the browser it will still run
- Stress: This is OPTIONAL. You must state that you want to switch this feature on
- Consequently the Applet is now a mini app which runs everywhere. Sun have bridged the in- and out-of- browser deployment gap
- You turn this on with the "draggable" parameter. You then simply Alt-left click and drag. (the key binding is customisable). By default the new frame is undecorated and opaque
- A seperate "close" button is created by default. (Again this is customisable). You as a developer provide a seperate method called setAppletCloseListener. If this is implemented, some sort of close button will be provided in your applet. This is called when you shut the applet down
- While the page the applet came from is still visible you can still talk to the page. When the page goes away, the applet is dynamically morphed into a JWS application. NOTE: You can't talk to the page you came from, but you can still load a URL. Therefore services degrade very gracefully (JavaScript calls will error etc.). When your applet is closed, the normal applet shutdown process is executed

- What about older browsers and JREs? - The team have worked closely with Mozilla to massively simplifythings. As a result 90,000 lines of code thrown out from the browser (glue code). Unfortunately they can't back port it to Firefox 2 but Firefox 3 is supported. IE 5 and 6 are also supported.
- Who takes care of the 1.4 and 1.5 experience? - There is a new deployment toolkit which will help with the graceful degradation of functionality
- What about JOGL; do you still have to jump through security hoops like you used to? - With the new seperate JVM launcher (with your params) this is really easy. If the user hasn't seen the certificate JOGL requires to run, they will see a dialog and be asked to trust it
- What about 64 bit on Linux? - Sun are working on a beta program now. The binaries are provided to the windows platform now. The next step is Linux (Expect something beta in the next few months)
- Mac story? - no comments on a Mac port at this time. thankyou
- Can I specify update version of JVM? - yes, in the JNLP file, but there are issues with auto downloading pre-release versions of the JRE. However, if I download the JRE/SDK then the plugin comes down and is on by default
- Is there an email to hit with questions?:
- Java Web Start used to behave v. differently from Applets in many ways. Have you fixed it? - Yes. Multiple signers from multiple files are now possible as long as you split them across multiple JNLP files
- Can I detect that I have dragged an applet off a page and subsequently the page is reloaded so I can make sure only one applet is running?: - Yes. The JNLP will check and if one is already started the start in the page will fail

No comments: