Revision: 5365
          http://jnode.svn.sourceforge.net/jnode/?rev=5365&view=rev
Author:   fduminy
Date:     2009-04-29 22:16:10 +0000 (Wed, 29 Apr 2009)

Log Message:
-----------
- changed runtime directory layout of the client side of jtestserver, which is 
implemented by the ConfigurationUtils class
- added configuration of java.util.logging

Modified Paths:
--------------
    trunk/core/src/test/org/jtestserver/client/Config.java
    trunk/core/src/test/org/jtestserver/client/ConfigReader.java
    trunk/core/src/test/org/jtestserver/client/TestDriver.java
    trunk/core/src/test/org/jtestserver/server/Config.java
    trunk/core/src/test/org/jtestserver/tests/AllTests.java

Added Paths:
-----------
    trunk/core/src/test/org/jtestserver/client/utils/ConfigurationUtils.java
    trunk/core/src/test/org/jtestserver/client/utils/LoggingFormatter.java
    trunk/core/src/test/org/jtestserver/home/
    trunk/core/src/test/org/jtestserver/home/config/
    trunk/core/src/test/org/jtestserver/home/config/config.properties
    trunk/core/src/test/org/jtestserver/home/config/jnode.properties
    trunk/core/src/test/org/jtestserver/home/config/logging.config.properties

Removed Paths:
-------------
    trunk/core/src/test/org/jtestserver/tests/config.properties
    trunk/core/src/test/org/jtestserver/tests/jnode.properties

Modified: trunk/core/src/test/org/jtestserver/client/Config.java
===================================================================
--- trunk/core/src/test/org/jtestserver/client/Config.java      2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/client/Config.java      2009-04-29 
22:16:10 UTC (rev 5365)
@@ -23,6 +23,7 @@
 import java.util.Properties;
 
 import org.jtestserver.client.process.VMConfig;
+import org.jtestserver.client.utils.ConfigurationUtils;
 import org.jtestserver.common.ConfigUtils;
 
 /**
@@ -83,7 +84,7 @@
         clientTimeout = ConfigUtils.getInt(properties, "client.timeout", 
30000);
         serverName = properties.getProperty("server.name", "localhost");
         serverPort = ConfigUtils.getInt(properties, "server.port", 10000);
-        workDir = ConfigUtils.getDirectory(properties, "work.dir", new 
File("."));
+        workDir = new File(ConfigurationUtils.getHomeDirectory(), "workdir");
         excludingFilters = ConfigUtils.getStringArray(properties, 
"excluding.filters");
         forceUseMauveList = ConfigUtils.getBoolean(properties, 
"force.use.mauve.list", false);
         watchDogPollInterval = ConfigUtils.getInt(properties, 
"watchdog.poll.interval", 10000);

Modified: trunk/core/src/test/org/jtestserver/client/ConfigReader.java
===================================================================
--- trunk/core/src/test/org/jtestserver/client/ConfigReader.java        
2009-04-29 22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/client/ConfigReader.java        
2009-04-29 22:16:10 UTC (rev 5365)
@@ -63,16 +63,16 @@
     
     /**
      * Read the JTestServer configuration and the VM configuration.
-     * @param configDir Directory where configuration is stored.
+     * @param configFile File where configuration is stored.
      * @return the configuration.
      * @throws IOException
      */
-    public Config read(File configDir) throws IOException {
-        Properties properties = readProperties(configDir, "config.properties");
+    public Config read(File configFile) throws IOException {
+        Properties properties = readProperties(configFile);
         
         // read the vm configuration
         String vm = ConfigUtils.getString(properties, "use.vm");
-        Properties vmProperties = readProperties(configDir, vm + 
".properties");
+        Properties vmProperties = readProperties(new 
File(configFile.getParentFile(), vm + ".properties"));
         VMConfig vmConfig = createVMConfig(vmProperties, vm);
             
         return new Config(properties, vmConfig);
@@ -104,14 +104,13 @@
     /**
      * Read a properties file.
      * 
-     * @param configDir Directory where configuration is stored.
-     * @param name of the file to read.
+     * @param configFile File where configuration is stored.
      * @return the properties file.
      * @throws IOException
      */
-    private Properties readProperties(File configDir, String name) throws 
IOException {
+    private Properties readProperties(File configFile) throws IOException {
         Properties properties = new Properties();
-        properties.load(new FileInputStream(new File(configDir, name)));
+        properties.load(new FileInputStream(configFile));
         expandVariables(properties);
         return properties;
     }

Modified: trunk/core/src/test/org/jtestserver/client/TestDriver.java
===================================================================
--- trunk/core/src/test/org/jtestserver/client/TestDriver.java  2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/client/TestDriver.java  2009-04-29 
22:16:10 UTC (rev 5365)
@@ -29,6 +29,7 @@
 import java.util.logging.Logger;
 
 import org.jtestserver.client.process.ServerProcess;
+import org.jtestserver.client.utils.ConfigurationUtils;
 import org.jtestserver.client.utils.TestListRW;
 import org.jtestserver.client.utils.WatchDog;
 import org.jtestserver.common.Status;
@@ -36,17 +37,15 @@
 import org.jtestserver.common.protocol.ProtocolException;
 import org.jtestserver.common.protocol.TimeoutException;
 import org.jtestserver.common.protocol.UDPProtocol;
-import org.jtestserver.tests.AllTests;
 
 public class TestDriver {
     private static final Logger LOGGER = 
Logger.getLogger(TestDriver.class.getName());
     
     public static void main(String[] args) {
-        File configDir = AllTests.CONFIG_DIRECTORY;
-        //File configDir = new File(".");
+        ConfigurationUtils.init();
         
         try {
-            TestDriver testDriver = createUDPTestDriver(configDir);
+            TestDriver testDriver = createUDPTestDriver();
             
             if ((args.length > 0) && "kill".equals(args[0])) {
                 testDriver.killRunningServers();
@@ -60,8 +59,8 @@
         }
     }
     
-    private static TestDriver createUDPTestDriver(File configDir) throws 
ProtocolException, IOException {         
-        Config config = new ConfigReader().read(configDir);
+    private static TestDriver createUDPTestDriver() throws ProtocolException, 
IOException {         
+        Config config = new 
ConfigReader().read(ConfigurationUtils.getConfigurationFile());
         InetAddress serverAddress = 
InetAddress.getByName(config.getServerName());
         int serverPort = config.getServerPort();
         UDPProtocol protocol = UDPProtocol.createClient(serverAddress, 
serverPort);

Added: trunk/core/src/test/org/jtestserver/client/utils/ConfigurationUtils.java
===================================================================
--- trunk/core/src/test/org/jtestserver/client/utils/ConfigurationUtils.java    
                        (rev 0)
+++ trunk/core/src/test/org/jtestserver/client/utils/ConfigurationUtils.java    
2009-04-29 22:16:10 UTC (rev 5365)
@@ -0,0 +1,164 @@
+/**
+ * 
+ */
+package org.jtestserver.client.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.logging.LogManager;
+
+/**
+ * This is a utility class to manage directory hierarchy of JTestServer.
+ * 
+ * @author Fabien DUMINY (fdum...@jnode.org)
+ *
+ */
+public class ConfigurationUtils {
+    private static final String HOME_DIRECTORY_PROPERTY = "jtestserver.home";
+    private static final String DEFAULT_HOME_DIRECTORY_NAME = "home";
+    
+    private static final String CONFIG_DIRECTORY_NAME = "config";
+    private static final String CONFIG_FILE_NAME = "config.properties";
+
+    private static boolean INIT = false;
+    private static File HOME = null;
+    
+    /**
+     * Try to find the home directory defined as a directory that contains a 
sub-directory named config, 
+     * which contains a file named config.properties.<br> 
+     * It's searched in the following order : 
+     * <ul>
+     * <li>Get the system property <code>jtestserver.home</code> and, if it 
gives a valid directory, use it.</li>
+     * <li>If the current directory contains a sub-directory named config, 
+     * which contains a file named config.properties, then use it.</li>
+     * <li>Ultimately, try to get it from the classpath of this class and 
walking up to the 
+     * package <code>org.jtestserver.home</code></li>
+     * </ul>
+     * @return the home directory.
+     * @throws RuntimeException if the home directory can't be found.
+     */
+    public static File getHomeDirectory() {        
+        return HOME;
+    }
+    
+    /**
+     * Get the configuration file.
+     * @return
+     */
+    public static File getConfigurationFile() {
+        return new File(getConfigurationDirectory(), CONFIG_FILE_NAME);
+    }
+
+    /**
+     * Initialize everything (search for home directory, init logging ...).
+     * <b>That method must be called before anything else (especially the 
other 
+     * public methods of that class, but not only that).
+     * @throw RuntimeException if something is wrong (typically the home 
directory can't be found).
+     */
+    public static void init() {
+        if (!INIT) {
+            // search home directory 
+            HOME = searchHomeDirectory();
+            
+            // init logging
+            File loggingConfigFile = new File(getConfigurationDirectory(), 
"logging.config.properties");
+            
+            System.setProperty("java.util.logging.config.file", 
loggingConfigFile.getAbsolutePath());
+            try {
+                LogManager.getLogManager().readConfiguration();
+            } catch (SecurityException e1) {
+                e1.printStackTrace();
+            } catch (IOException e1) {
+                e1.printStackTrace();
+            }
+        }
+    }
+    
+    /**
+     * Indicates if the given file is a valid home directory.
+     * @param f file to test
+     * @return true if the given file is a valid home directory.
+     */
+    private static boolean isValidHomeDirectory(File f) {
+        boolean valid = false;
+        if (isValidDirectory(f)) {
+            File configDir = new File(f, CONFIG_DIRECTORY_NAME);
+            if (isValidDirectory(configDir)) {
+                File configFile = new File(configDir, CONFIG_FILE_NAME);
+                valid = configFile.exists() && configFile.isFile() && 
configFile.canRead();
+            }
+        }
+        
+        return valid;
+    }
+    
+    /**
+     * Indicates if the given file is a valid directory.
+     * @param f file to test
+     * @return true if the given file is a valid directory.
+     */
+    private static boolean isValidDirectory(File f) {
+        return f.exists() && f.isDirectory() && f.canRead();
+    }
+
+    /**
+     * Get the configuration directory.
+     * @return configuration directory.
+     */
+    private static File getConfigurationDirectory() {
+        return new File(getHomeDirectory(), CONFIG_DIRECTORY_NAME);
+    }
+
+    /**
+     * Do the actual search of the home directory, as specified in {...@link 
#getHomeDirectory()}.
+     * @return the home directory.
+     * @throws RuntimeException if the home directory can't be found.
+     */
+    private static File searchHomeDirectory() {
+        File home = null;
+        
+        // try from the system property
+        String value = System.getProperty(HOME_DIRECTORY_PROPERTY);
+        if ((value != null) && !value.trim().isEmpty()) {
+            home = new File(value);
+            if (!isValidHomeDirectory(home)) {
+                home = null;
+            } else {
+                System.out.println("using home directory from system property 
: " + home.getAbsolutePath());
+            }
+        }
+        
+        if (home == null) {
+            home = new File(DEFAULT_HOME_DIRECTORY_NAME); 
+            if (!isValidHomeDirectory(home)) {
+                home = null;
+            } else {
+                System.out.println("using current directory as home : " + 
home.getAbsolutePath());
+            }
+        }
+        
+        // try from the classpath
+        if (home == null) {
+            URL myURL = 
ConfigurationUtils.class.getResource(ConfigurationUtils.class.getSimpleName() + 
".class");
+            
+            File f = new File(myURL.getFile());
+            while (!f.getAbsolutePath().endsWith("/jtestserver") && 
!f.getAbsolutePath().isEmpty()) {
+                f = f.getParentFile();
+            }
+            home = new File(f, "home");
+            
+            if (!isValidHomeDirectory(home)) {
+                home = null;
+            } else {
+                System.out.println("using home directory from classpath : " + 
home.getAbsolutePath());
+            }
+        }
+        
+        if (home == null) {
+            throw new RuntimeException("unable to find a valid home 
directory");
+        }
+            
+        return home;
+    }
+}

Added: trunk/core/src/test/org/jtestserver/client/utils/LoggingFormatter.java
===================================================================
--- trunk/core/src/test/org/jtestserver/client/utils/LoggingFormatter.java      
                        (rev 0)
+++ trunk/core/src/test/org/jtestserver/client/utils/LoggingFormatter.java      
2009-04-29 22:16:10 UTC (rev 5365)
@@ -0,0 +1,42 @@
+/*
+JTestServer is a client/server framework for testing any JVM implementation.
+ 
+Copyright (C) 2009  Fabien DUMINY (fdum...@jnode.org)
+
+JTestServer is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+JTestServer is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+package org.jtestserver.client.utils;
+
+import java.util.logging.Formatter;
+import java.util.logging.LogRecord;
+
+/**
+ * This is the formatter we will use for logging.
+ * 
+ * @author Fabien DUMINY (fdum...@jnode.org)
+ *
+ */
+public class LoggingFormatter extends Formatter {
+    /**
+     * {...@inheritdoc}
+     */
+    @Override
+    public String format(LogRecord record) {
+        StringBuilder sb = new 
StringBuilder(record.getLevel().getName()).append(": ");
+        
sb.append(record.getSourceClassName()).append('#').append(record.getSourceMethodName());
+        sb.append(": ").append(record.getMessage()).append('\n');
+        return sb.toString();
+    }
+}

Copied: trunk/core/src/test/org/jtestserver/home/config/config.properties (from 
rev 5345, trunk/core/src/test/org/jtestserver/tests/config.properties)
===================================================================
--- trunk/core/src/test/org/jtestserver/home/config/config.properties           
                (rev 0)
+++ trunk/core/src/test/org/jtestserver/home/config/config.properties   
2009-04-29 22:16:10 UTC (rev 5365)
@@ -0,0 +1,10 @@
+# This is the config file for the client side of JTestServer
+client.timeout=30000
+server.name=192.168.44.2
+server.port=10000
+excluding.filters=awt,swing,Thread,java.net,Object.oom
+force.use.mauve.list=true
+
+watchDogPollInterval=30000
+
+use.vm=jnode

Copied: trunk/core/src/test/org/jtestserver/home/config/jnode.properties (from 
rev 5345, trunk/core/src/test/org/jtestserver/tests/jnode.properties)
===================================================================
--- trunk/core/src/test/org/jtestserver/home/config/jnode.properties            
                (rev 0)
+++ trunk/core/src/test/org/jtestserver/home/config/jnode.properties    
2009-04-29 22:16:10 UTC (rev 5365)
@@ -0,0 +1,21 @@
+#type=kvm
+#type=vmware
+type=jvm
+
+jnode.root=/home/fabien/data/Projets/JNode/jnode
+
+kvm.memory=2048
+kvm.cdrom=${jnode.root}/all/build/cdroms/jnode-x86-lite.iso
+kvm.options=-no-acpi -std-vga
+kvm.serial=stdio 
+kvm.keyboard=fr
+
+vmware.server.host=localhost
+vmware.server.port=8222
+vmware.server.username=jtestserver
+vmware.server.password=JTestServer#~
+vmware.vmName=[standard] JNode/JNode.vmx
+
+jnode.core=${jnode.root}/core/
+jvm.java.home=/home/fabien/apps/java/
+jvm.classpath=${jnode.core}/build/classes,${jnode.core}/lib/mauve.jar

Added: trunk/core/src/test/org/jtestserver/home/config/logging.config.properties
===================================================================
--- trunk/core/src/test/org/jtestserver/home/config/logging.config.properties   
                        (rev 0)
+++ trunk/core/src/test/org/jtestserver/home/config/logging.config.properties   
2009-04-29 22:16:10 UTC (rev 5365)
@@ -0,0 +1,3 @@
+.level=ALL
+handlers=java.util.logging.ConsoleHandler
+java.util.logging.ConsoleHandler.formatter=org.jtestserver.client.utils.LoggingFormatter
\ No newline at end of file

Modified: trunk/core/src/test/org/jtestserver/server/Config.java
===================================================================
--- trunk/core/src/test/org/jtestserver/server/Config.java      2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/server/Config.java      2009-04-29 
22:16:10 UTC (rev 5365)
@@ -20,14 +20,20 @@
 */
 package org.jtestserver.server;
 
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.Properties;
 
+import org.jtestserver.client.utils.ConfigurationUtils;
+
 public class Config {
 
     public static Config read() throws IOException {
         Properties properties = new Properties();
-        
//properties.load(Config.class.getResourceAsStream("config.properties"));
+        
+        //TODO get server's config
+        //properties.load(new 
FileInputStream(ConfigurationUtils.getConfigurationFile()));
+        
         return new Config(properties);
     }
     

Modified: trunk/core/src/test/org/jtestserver/tests/AllTests.java
===================================================================
--- trunk/core/src/test/org/jtestserver/tests/AllTests.java     2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/tests/AllTests.java     2009-04-29 
22:16:10 UTC (rev 5365)
@@ -21,6 +21,7 @@
 
 import java.io.File;
 
+import org.jtestserver.client.utils.ConfigurationUtils;
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 import org.junit.runners.Suite.SuiteClasses;
@@ -29,6 +30,10 @@
 @SuiteClasses({TestProtocol.class, TestUDPProtocol.class, 
TestInputMessage.class,
     TestOutputMessage.class, TestVMware.class, TestKVM.class, TestJVM.class })
 public class AllTests {
-    public static final File CONFIG_DIRECTORY =
-            new 
File(AllTests.class.getResource("config.properties").getFile()).getParentFile();
+    
+    static {
+        ConfigurationUtils.init();
+    }
+    
+    static final File CONFIG_DIRECTORY = 
ConfigurationUtils.getConfigurationFile();
 }

Deleted: trunk/core/src/test/org/jtestserver/tests/config.properties
===================================================================
--- trunk/core/src/test/org/jtestserver/tests/config.properties 2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/tests/config.properties 2009-04-29 
22:16:10 UTC (rev 5365)
@@ -1,11 +0,0 @@
-# This is the config file for the client side of JTestServer
-client.timeout=30000
-server.name=192.168.44.2
-server.port=10000
-work.dir=/home/fabien/data/Projets/JNode/jnode/core/src/test/org/jtestserver/workdir
-excluding.filters=awt,swing,Thread,java.net,Object.oom
-force.use.mauve.list=true
-
-watchDogPollInterval=30000
-
-use.vm=jnode

Deleted: trunk/core/src/test/org/jtestserver/tests/jnode.properties
===================================================================
--- trunk/core/src/test/org/jtestserver/tests/jnode.properties  2009-04-29 
22:01:19 UTC (rev 5364)
+++ trunk/core/src/test/org/jtestserver/tests/jnode.properties  2009-04-29 
22:16:10 UTC (rev 5365)
@@ -1,21 +0,0 @@
-#type=kvm
-type=vmware
-#type=jvm
-
-jnode.root=/home/fabien/data/Projets/JNode/jnode
-
-kvm.memory=2048
-kvm.cdrom=${jnode.root}/all/build/cdroms/jnode-x86-lite.iso
-kvm.options=-no-acpi -std-vga
-kvm.serial=stdio 
-kvm.keyboard=fr
-
-vmware.server.host=localhost
-vmware.server.port=8222
-vmware.server.username=jtestserver
-vmware.server.password=JTestServer#~
-vmware.vmName=[standard] JNode/JNode.vmx
-
-jnode.core=${jnode.root}/core/
-jvm.java.home=/home/fabien/apps/java/
-jvm.classpath=${jnode.core}/build/classes,${jnode.core}/lib/mauve.jar


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to