// Copyright (C) 2011 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Timer;

import org.slf4j.cal10n.LocLogger;

import jsaf.intf.util.IProperty;

/**
 * This class is used to retrieve JOVAL-wide resources, like jOVAL properties and the jOVAL event system timer.
 * It is also used to configure properties that affect the behavior of sessions.
 *
 * @author David A. Solin
 * @version %I% %G%
 */
public class JOVALSystem {
    /**
     * Property indicating the product name.
     */
    public static final String SYSTEM_PROP_PRODUCT = "productName";

    /**
     * Property indicating the product version.
     */
    public static final String SYSTEM_PROP_VERSION = "version";

    /**
     * Property indicating the product build date.
     */
    public static final String SYSTEM_PROP_BUILD_DATE = "build.date";

    private static final String SYSPROPS_RESOURCE = "joval.system.properties";

    private static Timer timer;
    private static Properties sysProps;
    static {
	timer = new Timer("jOVAL system timer", true);
	sysProps = new Properties();
	try {
	    ClassLoader cl = Thread.currentThread().getContextClassLoader();

	    InputStream rsc = cl.getResourceAsStream(SYSPROPS_RESOURCE);
	    if (rsc == null) {
		JOVALMsg.getLogger().warn(JOVALMsg.getMessage(JOVALMsg.ERROR_MISSING_RESOURCE, SYSPROPS_RESOURCE));
	    } else {
		sysProps.load(rsc);
	    }
	} catch (IOException e) {
	    JOVALMsg.getLogger().error(JOVALMsg.getMessage(JOVALMsg.ERROR_EXCEPTION), e);
	}
    }

    /**
     * Retrieve the daemon Timer used for scheduled jOVAL tasks.
     */
    public static Timer getTimer() {
	return timer;
    }

    /**
     * Return a directory suitable for storing transient application data, like state information that may persist
     * between invocations.  This is either a directory called .jOVAL beneath the user's home directory, or on Windows,
     * it will be a directory named jOVAL in the appropriate AppData storage location.
     */
    public static synchronized File getDataDirectory() {
	File dataDir = null;
	if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
	    String s = System.getenv("LOCALAPPDATA");
	    if (s == null) {
		s = System.getenv("APPDATA");
	    }
	    if (s != null) {
		File appDataDir = new File(s);
		dataDir = new File(appDataDir, "jOVAL");
	    }
	}
	if (dataDir == null) {
	    File homeDir = new File(System.getProperty("user.home"));
	    dataDir = new File(homeDir, ".jOVAL");
	}
	if (!dataDir.exists()) {
	    dataDir.mkdirs();
	}
	return dataDir;
    }

    /**
     * Retrieve an OVAL system property.
     *
     * @param key specify one of the SYSTEM_PROP_* keys
     */
    public static String getSystemProperty(String key) {
	return sysProps.getProperty(key);
    }

    public static void setSystemProperty(String key, String value) {
	sysProps.setProperty(key, value);
    }
}

@@ -1038,24 +1038,172 @@
 ersion%22;%0D%0A%0D%0A
+    /**%0D%0A     * Property indicating the default data directory.%0D%0A     */%0D%0A    public static final String SYSTEM_PROP_DATADIR = %22data.directory%22;%0D%0A%0D%0A
     /**%0D%0A   
@@ -2521,93 +2521,340 @@
    *
-/%0D%0A    public static synchronized File getDataDirectory() %7B%0D%0A%09File dataDir = null;%0D%0A%09
+%0D%0A     * This location can also be determined by setting the SYSTEM_PROP_DATADIR jOVAL system property. %0D%0A     */%0D%0A    public static synchronized File getDataDirectory() %7B%0D%0A%09File dataDir = null;%0D%0A%09if (sysProps.containsKey(SYSTEM_PROP_DATADIR)) %7B%0D%0A%09    dataDir = new File(sysProps.getProperty(SYSTEM_PROP_DATADIR));%0D%0A%09%7D else %7B%0D%0A%09    
 if (
@@ -2927,20 +2927,17 @@
 -1) %7B%0D%0A%09
-    
+%09
 String s
@@ -2968,28 +2968,25 @@
 PPDATA%22);%0D%0A%09
-    
+%09
 if (s == nul
@@ -2993,16 +2993,20 @@
 l) %7B%0D%0A%09%09
+    
 s = Syst
@@ -3033,20 +3033,14 @@
 ;%0D%0A%09
-    %7D%0D%0A%09    
+%09%7D%0D%0A%09%09
 if (
@@ -3055,16 +3055,20 @@
 l) %7B%0D%0A%09%09
+    
 File app
@@ -3093,16 +3093,20 @@
 (s);%0D%0A%09%09
+    
 dataDir 
@@ -3135,24 +3135,29 @@
  %22jOVAL%22);%0D%0A
+%09%09%7D%0D%0A
 %09    %7D%0D%0A%09%7D%0D%0A
@@ -3153,20 +3153,20 @@
     %7D%0D%0A%09
-%7D%0D%0A%09
+    
 if (data
@@ -3182,20 +3182,17 @@
 ll) %7B%0D%0A%09
-    
+%09
 File hom
@@ -3243,20 +3243,17 @@
 e%22));%0D%0A%09
-    
+%09
 dataDir 
@@ -3280,24 +3280,32 @@
 %22.jOVAL%22);%0D%0A
+%09    %7D%0D%0A
 %09%7D%0D%0A%09if (!da

Add a system property to govern the default data directory.