开发者

Poll applet download progress in jnlp with applet descriptor and in javascript

开发者 https://www.devze.com 2023-02-25 18:34 出处:网络
I have a WebStart applet that I deploy using the deployJava script. The jnlp file looks like this: <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>

I have a WebStart applet that I deploy using the deployJava script. The jnlp file looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jnlp codebase="" href="launch.jnlp" spec="1.0+">
    <information>
        <title>someApp</title>
        <vendor>someCompany</vendor>
        <homepage href="http://www.someCompanysWebsite.net"/>
        <description>someDescription</description>
        <description kind="short">someShortDescription</description>
    <offline-allowed/>
    </information>
<update check="background"/>
<security>
<all-permissions/>
</security>
    <resources>
<j2se version="1.5+"/>
<jar href="mainJar.jar" main="true"/>


<jar href="lib1.jar"/>
<jar href="lib2.jar"/>
<jar href="lib3.jar"/>
<jar href="platform.jar"/>
<jar href="plugin.jar"/>
<jar href="slf4j-api-1.6.1.jar"/>
<jar href="slf4j-simple-1.6.1.jar"/>
</resources>
    <applet-desc height="1" main-class="com.companyName.applet.appletClass" name="appletName" width="1">

    </applet-desc>
</jnlp>

As you can see, my applet is 1x1 in size, so I don't want to display anything with it. It just serves to expose some java methods to javascript. So, if I make a javascript call, for example

//javascript code
var applet = document.getElementById('appletName');

I get a valid handle to the applet and I am able to call its methods in javascript like this:

 //javascript code
    applet.doStuff();

Of course, every applet has the isActive() method defined, so in my javascript code I can check like this if the applet is fully loaded:

    //javascript code
    function isFullyLoaded()
    {
      if ( !applet.isActive() ){
         setTimeout('isFullyLoaded()',1000);
      }
      else 
      {
         //do nice stuff requiring the applet is loaded
      }
开发者_开发百科    }

However, as the loading of the applet might take a while, I would like to have a progress indication of its loading. A quick look at Sun (or Oracle, if you are so inclined) shows this: http://download.oracle.com/javase/tutorial/deployment/applet/customProgressIndicatorForApplet.html

However, this implies that I want to show a loading bar or anything when the applet loads. I dislike applet appearance and that is why I just want to poll applet loading with javascript, like that on some interval:

//javascript code
alert(applet.getLoadingProgress());

What should I do and should I change jnlp declaration or anything like that. Any pointers or comments would be appreciated. Note that I don't want a custom progress bar, but just a structure so that I can poll progress with javascript during loading.

Cheers.

Edit: @amol I will bring to your attention that JNLP deployed applets and application, during their loading, call a generic DownloadServiceListener class's method progress(java.net.URL url, java.lang.String version, long readSoFar, long total, int overallPercent) whenever one of the jars required is downloaded. Knowing the total numbers of jars and the number of jars yet to be downloaded will serve as a perfect progress indicatior. I want also to bring to your attention that if you debug the JNLP deployment, you will see these methods being called and logging as network level events. So, my question is how to get a hang of these methods using javascript so i can have some estimate about progress as I don't want the user to look at java console to estimate progress. I don't want to also show an indeterminate progress loader, as it is a breeze to implement that, but no practical use whatsoever.


I am not sure how to poll as such, but the approach I have used in the past to achieve what you are describing is as follows -

  • When you load the applet (In my case I usually insert relevant html in the DOM on click of a button or something), display your javascript/html specific loading indicator.
  • When applet loads, it calls back a javascript method. JS now knows that the applet is fully loaded, takes down the loading indicator and does any further JS->applet communication, etc.

(Display a determinate progress bar is not possible using this approach. But a a generic loading indicator that masks over the entire html body or an indeterminate progress bar is very much possible and breeze to implement)


In the documentation from the Oracle site which you posted (http://download.oracle.com/javase/tutorial/deployment/applet/customProgressIndicatorForApplet.html), the applet calls the function below to indicate its progress, which then updates the Applet UI and the progress bar accordingly.

public void progress(URL url, String version, long readSoFar,
                 long total, int overallPercent) {        
  // check progress of download and update display
  updateProgressUI(overallPercent);

}

I believe, you can simply, replace the call to updateProgressUI(overallPercent), with some call to a Javascript function which updates a progress indicator in the web page.

Test this and let us know if it is possible.


I dislike applet appearance ..

Change it using the 'image' attribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消