Wednesday, January 4, 2017

How to make use of configuration/properties files in Java Web Start ?


There are different ways to do that, I will be discussing when you have configuration/properties files on server and want to use them in your Java Web Start to access them locally.

 Means, I did not find the way to download properties/configuration files by using Java Web Start.
So to do that we can separately download these files by Java and put that code in JWS application and  store them all in your machine and read them by FileInputStream in your application.

Keep your properties,xml or any config file as zip folder eg config.zip on server location
eg. D:\apache-tomcat-8.0.39\webapps\examples and use copyFile method to copy zip file on your local machine at System.getenv(“APPDATA”) location because it is accessible on any machine.

Put below code at very start of your web  start application so that it make all the files available to load for your application.



/**


 calling below methods from main


*/


public static void main(String arggs[]){

String getenv = System.getenv("APPDATA");
              System.out.println(getenv);

              String createFilesFolderPath = createFilesFolderPath(getenv + "\\ops\\");
              System.out.println(createFilesFolderPath);

              copyFile(createFilesFolderPath + "config.zip", "D:/apache-tomcat-8.0.39/webapps/examples/config.zip");
             
              getZipFiles(createFilesFolderPath + "config.zip", createFilesFolderPath);

}

/**
This method is used to copy zip file to local machine
*/
public static void copyFile(String OUTPUT_FOLDER, String INPUT_ZIP_FILE)
                     throws org.apache.commons.vfs2.FileSystemException {

              FileObject destn = VFS.getManager().resolveFile(OUTPUT_FOLDER);

              FileObject fo = VFS.getManager().resolveFile(INPUT_ZIP_FILE);
              destn.copyFrom(fo, Selectors.SELECT_SELF);
              destn.close();

       }



/**
This method is used to extract that zip file to folder named same as that of zip file which is config in my case
*/

      
public static void getZipFiles(String filePath, String extractedFilesLocation) {

              FileInputStream fis = null;
              ZipInputStream zipIs = null;
              ZipEntry zEntry = null;
              try {
                     fis = new FileInputStream(filePath);
                     zipIs = new ZipInputStream(new BufferedInputStream(fis));
                     while ((zEntry = zipIs.getNextEntry()) != null) {
                           try {
                                  byte[] tmp = new byte[4 * 1024];
                                  FileOutputStream fos = null;
                                  if(zEntry.getName().equals("config/")){
                                         createFilesFolderPath(extractedFilesLocation+zEntry.getName());
                                  }
                                  String opFilePath = extractedFilesLocation + zEntry.getName();
                                  System.out.println("Extracting file to " + opFilePath);
                                  fos = new FileOutputStream(opFilePath);
                                  int size = 0;
                                  while ((size = zipIs.read(tmp)) != -1) {
                                         fos.write(tmp, 0, size);
                                  }
                                  fos.flush();
                                  fos.close();
                           } catch (Exception ex) {

                           }
                     }
                     zipIs.close();
              } catch (FileNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
       }





/**
This method is used to create folder of path where you want to store files
*/

public static String createFilesFolderPath(String zaraOpsLocalPath) {

              File file = new File(zaraOpsLocalPath);
              if (!file.exists()) {
                     if (file.mkdir()) {
                           return zaraOpsLocalPath;

                     } else {
                           System.out.println("Failed to create directory!");
                     }
              }
              return zaraOpsLocalPath;
       }

Now you can load properties from your local

Below cade should be there in your Java Web Start applicationto load properties files.

/**
This code is used to load properties files in your code.
*/

InputStream in = null;
              Properties props = new Properties();
              in = new FileInputStream(System.getenv("APPDATA\\")+propsName);           
              if (in != null)
              {
                     props.load(in);
              }


No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...