deployment - How to restart java web start application programatically -


i trying achieve automatic update java web-start applicaiton. logic: fetching jnlp file server , comparing timestamp current one. if there difference download latest file , restart application javaws command. have 2 problems. 1. not able fetch local jnlp file (because location jnlp file different different operating system mentioned here 2. not able find graceful way restart application after killing current running application. appreciate if there other graceful solution available.

my code:

import java.io.bufferedinputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.date;  import javax.swing.joptionpane;  import org.apache.log4j.logger;  import com.ibm.nzna.projects.qit.app.propertysystem;  /**  * qitupdater . java  *   * @info checks updates jws application  *   *       program try use given url connect online check  *       newer version of jnlpfile. program compares last  *       modified dates of local copy , url copy, , notifies  *       user if url copy newer, via dialog box popup. newer  *       version can then, downloaded , installed using jws.  *   * @warnings if server containing qit changes directory or location  *           program error out every time since url link  *           hard-coded.  *   * @author ashish tyagi  * @version 4/22/2013  *   */ public abstract class qitupdater { //  constant hold location of property file , server location      public final static string jnlpfile = propertysystem.getstring(propertysystem.property_jnlp_file);     public final static string filedirectoryurl = propertysystem.getstring(propertysystem.property_jnlp_server);     static logger logger = logger.getlogger(qitupdater.class.getname());       // private variables     private static httpurlconnection huc;     private static file oldfile = null;     private static long localdate = 0;     private static long remotedate = 0;      public static void checkupdates(boolean displayerrors,boolean displayisuptodate,boolean autorunner){         logger.info("value of jnlp file: "+filedirectoryurl+"/"+jnlpfile);         if(jnlpfile !=null && filedirectoryurl != null)             checkforupdates(displayerrors, displayisuptodate, autorunner);     }      /**      * starts new task check qit updates.      *       * if there no connection or program date, method      * returns normally. otherwise, exit old program , execute      * newer qit program.      *       * @param displayerrors      *            show error messages in popups, if occur.      *            recommended false on boostrapping, 'offline user'      *            not annoyed.      *       * @param displayisuptodate      *            show popup letting user know if current      *            version of qit date, if have date      *            version. otherwise, ignore popup.      *       * @param autorunnewer      *            automatically execute newer jnlp file, if found      *            or downloaded. recommended false if method      *            being called within user action, button      *            press. if set false, dialog ask user      *            restart qit in order finish updating program, if      *            update found.      */     public static void checkforupdates(boolean displayerrors,             boolean displayisuptodate, boolean autorunnewer) {         // try find named file         // in current local directory         oldfile = new file(jnlpfile);          if (oldfile.exists()) // find jnlp file         {             // grab local last modified date compare             localdate = oldfile.lastmodified();         }          // try access base url         int code = 404;         try {             url u = new url(filedirectoryurl);             huc = (httpurlconnection) u.openconnection();             huc.setrequestmethod("get");             huc.connect();             code = huc.getresponsecode();         } catch (malformedurlexception e) {             if (displayerrors)                 printerror("error: not check updates.\nmalformedurlexception: "                         + e.getmessage()                         + "\n\nclick ok continue using qit.");             return;         } catch (ioexception e) {             if (displayerrors)                 printerror("error: not check updates.\nioexception: "                         + e.getmessage()                         + "\n\nclick ok continue using qit.");             return;         }          if (code == 200) {             // 200 valid connection             // scan url versions             try {                 stringbuilder sb = new stringbuilder();                 sb.append(filedirectoryurl).append("/").append(jnlpfile);                 url u = new url(sb.tostring());                 huc = (httpurlconnection) u.openconnection();                 huc.setrequestmethod("get");                 huc.connect();                  // grab last modified date                 remotedate = huc.getlastmodified();             } catch (malformedurlexception e) {                 if (displayerrors)                     printerror("error: failed download.\n\n"                             + e.getmessage()                             + "\n\nclick ok continue using qit.");                 return;             } catch (ioexception e) {                 if (displayerrors)                     printerror("error: failed download.\n\n"                             + e.getmessage()                             + "\n\nclick ok continue using qit.");                 return;             }              // compare last modified dates of jnlp files             if (remotedate != localdate) {                 // found newer version of jnlp                  // ask download                 if (0 == printquestion("an updated version of qit available.\n\nlast updated:\n"                         + new date(remotedate).tostring()                         + "\n\ndo want download , install it?")) {                     // download , install                     try {                         downloadurlfile(jnlpfile, filedirectoryurl + "/"                                 + jnlpfile);                          oldfile.setlastmodified(remotedate);                         // set date date on server                          if (autorunnewer) {                             // run jnlp file                             try {                                 runtime.getruntime()                                         .exec("javaws "+jnlpfile);                                  system.exit(0);// quit version of qit                              } catch (ioexception e) {                                 if (displayerrors)                                     printerror("error:\n"                                             + e.getmessage()                                             + "\n\nclick ok continue using qit.");                             }                         } else                             printinfo("in order finish installing the\nupdate, qit needs restarted.");                      } catch (exception e) {                         if (displayerrors)                             printerror("error: failed download " + jnlpfile                                     + ".\n\n" + e.getmessage()                                     + "\n\nclick ok continue using qit.");                     }                  }             } else {                 // modified dates same                  // try launch current jnlp                 if(oldfile.exists())                 {                     // run jnlp file                     try {                         runtime.getruntime()                                 .exec("javaws "+jnlpfile);                          system.exit(0);// quit version of qit                      } catch (ioexception e) {                         if (displayerrors)                             printerror("error:\n"                                     + e.getmessage()                                     + "\n\nclick ok continue using qit.");                     }                 }                  // date                 if (displayisuptodate)                     printinfo("qit date.\n\nlast update was:\n"                             + (new date(localdate).tostring())                             + "\n\nclick ok continue.");             }         }          return;     }      /**      * downloads urlstring filename @ current directory.      *       * @param filename      * @param urlstring      * @throws malformedurlexception      * @throws ioexception      */     public static void downloadurlfile(string filename, string urlstring)             throws malformedurlexception, ioexception {         bufferedinputstream in = null;         fileoutputstream fout = null;          try {             url url = new url(urlstring);             huc = (httpurlconnection) url.openconnection();             in = new bufferedinputstream(url.openstream());             fout = new fileoutputstream(filename);              byte data[] = new byte[1024];             int count;             while ((count = in.read(data, 0, 1024)) != -1) {                 fout.write(data, 0, count);             }         } {             if (in != null)                 in.close();             if (fout != null)                 fout.close();         }     }      /**      * display error      *       * @param e      */     private static void printerror(string e) {         joptionpane.showmessagedialog(null, e, "error",                 joptionpane.error_message);     }      /**      * display information      *       * @param s      */     private static void printinfo(string s) {         joptionpane.showmessagedialog(null, s, "information",                 joptionpane.information_message);     }      /**      * prompt yes/no question      *       * @param string      *            q      * @return int 0 means yes, 1 means no, -1 means clicked "x" close      */     private static int printquestion(string q) {         return joptionpane.showconfirmdialog(null, q, "question",                 joptionpane.yes_no_option);     } } 

i trying achieve automatic update java web-start application

use downloadservice of jnlp api.

downloadservice service allows application control how own resources cached, determine of resources cached, force resources cached, , remove resources cache. jnlp client responsible providing specific implementation of service.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -