Java Web Start Random Access File Demo -------------------------------------- Brief Description : This sample demonstrates features of the JNLPRandomAccessFile API. The file which user picks is opened as a random file for both Writing and Reading. The application first writes some hard coded strings to the file selected by the user. It then reads whatever was written to the file and displays it on to the screen using awt Containter and the TextArea components. The following JNLP API is used by the Application : FileOpenService : openFileDialog will allow the user to choose a single file. Contents of the selected file is returned as a FileContents object. FileContents : This object encapsulates the name and contents of a file. getLength(), getMaxLength() and setMaxLength are used. If the the maximum file size defined by the client is not more than 1024 bytes of the current length, then it will be adjusted accordingly. getName() returns the name of the file selected. getRandomAccessFile() method is used for creating JNLPRandomAccessFile with read/write permissions. JNLPRandomAccessFile : Methods provided by this interface are used for reading and writing to random file. seek() sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. writeUTF() and writeBytes() are demonstrated for writing to a file. readLine() is used for reading the next line from the file. Java Web Start is launched automatically by the browser when the user clicks on a link which is associated with JNLP file. It then downloads and caches the RAFSample application on the local client machine where browser is running. This cached application, when next invoked, is already downloaded and runs as fast as a traditionally installed application. By default applications are run in a protective environment (sandbox) with restricted access to local disk and network resources. Java Web Start only transfers JAR files from the Web server to the location determined by it on the client machine. To provide a simple and convenient packaging format for applications, Java Web Start includes a servlet as a sample in the SDK. The name of the servlet is JnlpDownloadServlet and is packaged into the jnlp-servlet.jar file (from the JNLPDownloadServlet sample under sample/jnlp/servlet directory in the SDK). This servlet eases deployment of application on any J2EE-complaint application server by packing a JNLP files and its associated resources in a Web Archive (.war) file. For more details on Java Web Start product and JNLP API, Please check out online docs at: http://java.sun.com/products/javawebstart/developers.html Files : GNUmakefile - This is the makefile to build the raf sample. src - This directory contains the required source file: randomFile.java lib - This directory contains the jar files generated after the build process. class - This directory containing .class files is generated by the build process. war - This directory contains all the files required for deploying a web based application. Application related .jnlp files and .jar files are inside the app directory. Building & Deployment of Application : 1) Build .class and .jar files by running gnumake in the directory where GNUmakefile file resides. The following environment variable must be set: # environment variable CLASS_PATH should contain path to javaws.jar (under # jre/lib directory) # environment variable SDK_HOME should point to SDK directory If you are running on windows platform, it is assumed you have MKS Toolkit installed and it is in your path environment variable. (Such commands as cp, echo, mkdir and rm are required) The generated classes will go to the classes directory, and the resulting jar files will be in the lib directory by default. You can adjust the output directory to anything you want by changing the GNUmakefile. 2) To ease the deployment of application to on any J2EE-complaint server, all the resources required, including .jnlp and jar files, are packed into a Web Archive (.war) file. Please put the following files into the war directory: war/WEB-INF/lib/jnlp-servlet.jar (from the JNLPDownloadServlet sample under sample/jnlp/servlet directory) war/WEB-INF/lib/ (not needed if servlet container is running J2SE 1.4+; or if the servlet container already comes with a XML parser in it) war/app/randomFile.jar (generated in the lib directory) To build .war file, run below command inside the war directory jar -cvf ../RAFSample.war . 3) Copy the generated RAFSample.war file to your web server directory. For Tomcat server, copy RAFSample.war file to $TOMCAT_HOME/webapps directory and restart the web server.