/**
  * Creates DirectoryValidator to ensure directories read from user (if any - default otherwise)
  * settings via keys:
  *
  * <ul>
  *   <li>{@link DeploymentConfiguration#KEY_USER_CACHE_DIR}
  *   <li>{@link DeploymentConfiguration#KEY_USER_PERSISTENCE_CACHE_DIR}
  *   <li>{@link DeploymentConfiguration#KEY_SYSTEM_CACHE_DIR}
  *   <li>{@link DeploymentConfiguration#KEY_USER_LOG_DIR}
  *   <li>{@link DeploymentConfiguration#KEY_USER_TMP_DIR}
  *   <li>{@link DeploymentConfiguration#KEY_USER_LOCKS_DIR}
  * </ul>
  */
 public DirectoryValidator() {
   dirsToCheck = new ArrayList<File>(6);
   DeploymentConfiguration dc = JNLPRuntime.getConfiguration();
   String[] keys =
       new String[] {
         DeploymentConfiguration.KEY_USER_CACHE_DIR,
         DeploymentConfiguration.KEY_USER_PERSISTENCE_CACHE_DIR,
         DeploymentConfiguration.KEY_SYSTEM_CACHE_DIR,
         DeploymentConfiguration.KEY_USER_LOG_DIR,
         DeploymentConfiguration.KEY_USER_TMP_DIR,
         DeploymentConfiguration.KEY_USER_LOCKS_DIR
       };
   for (String key : keys) {
     String value = dc.getProperty(key);
     if (value == null) {
       OutputController.getLogger()
           .log(
               OutputController.Level.MESSAGE_DEBUG,
               "WARNING: key " + key + " has no value, setting to default value");
       value = Defaults.getDefaults().get(key).getValue();
     }
     if (value == null) {
       if (JNLPRuntime.isDebug()) {
         OutputController.getLogger()
             .log(
                 OutputController.Level.MESSAGE_DEBUG,
                 "WARNING: key " + key + " has no value, skipping");
       }
       continue;
     }
     File f = new File(value);
     dirsToCheck.add(f);
   }
 }
 /**
  * This method is package private for testing purposes only.
  *
  * <p>This method verify that directory exists, is directory, file and directory can be created,
  * file can be written into, and subdirectory can be written into.
  *
  * <p>Some steps may looks like redundant, but some permission settings really alow to create file
  * but not directory and vice versa. Also some settings can allow to create file or directory
  * which can not be written into. (eg ACL or network disks)
  */
 static DirectoryCheckResult testDir(File f, boolean verbose, boolean testSubdir) {
   DirectoryCheckResult result = new DirectoryCheckResult(f);
   if (!f.exists()) {
     if (verbose) {
       OutputController.getLogger()
           .log(OutputController.Level.ERROR_ALL, DirectoryCheckResult.notExistsMessage(f));
     }
     result.exists = false;
   }
   if (!f.isDirectory()) {
     if (verbose) {
       OutputController.getLogger()
           .log(OutputController.Level.ERROR_ALL, DirectoryCheckResult.notDirMessage(f));
     }
     result.isDir = false;
   }
   File testFile = null;
   boolean correctPermissions = true;
   try {
     testFile = File.createTempFile("maindir", "check", f);
     if (!testFile.exists()) {
       correctPermissions = false;
     }
     try {
       FileUtils.saveFile("ww", testFile);
       String s = FileUtils.loadFileAsString(testFile);
       if (!s.trim().equals("ww")) {
         correctPermissions = false;
       }
     } catch (Exception ex) {
       if (JNLPRuntime.isDebug()) {
         ex.printStackTrace();
       }
       correctPermissions = false;
     }
     File[] canList = f.listFiles();
     if (canList == null || canList.length == 0) {
       correctPermissions = false;
     }
     testFile.delete();
     if (testFile.exists()) {
       correctPermissions = false;
     } else {
       boolean created = testFile.mkdir();
       if (!created) {
         correctPermissions = false;
       }
       if (testFile.exists()) {
         if (testSubdir) {
           DirectoryCheckResult subdirResult = testDir(testFile, verbose, false);
           if (subdirResult.getFailures() != 0) {
             result.subDir = subdirResult;
             correctPermissions = false;
           }
           testFile.delete();
           if (testFile.exists()) {
             correctPermissions = false;
           }
         }
       } else {
         correctPermissions = false;
       }
     }
   } catch (Exception ex) {
     if (JNLPRuntime.isDebug()) {
       ex.printStackTrace();
     }
     correctPermissions = false;
   } finally {
     if (testFile != null && testFile.exists()) {
       testFile.delete();
     }
   }
   if (!correctPermissions) {
     if (verbose) {
       OutputController.getLogger()
           .log(OutputController.Level.ERROR_ALL, DirectoryCheckResult.wrongPermissionsMessage(f));
     }
     result.correctPermissions = false;
   }
   return result;
 }