let users specify configuration file on the command line
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Jul 2006 23:42:25 +0000 (23:42 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Jul 2006 23:42:25 +0000 (23:42 +0000)
src/de/todesbaum/jsite/main/Configuration.java
src/de/todesbaum/jsite/main/Main.java

index 9b43784..e4e0510 100644 (file)
@@ -53,13 +53,21 @@ public class Configuration {
        private SimpleXML rootNode;
 
        public Configuration() {
-               filename = System.getProperty("user.home") + "/.jSite/config7";
-               lockFilename = System.getProperty("user.home") + "/.jSite/lock7";
+               this(System.getProperty("user.home") + "/.jSite/config7");
+       }
+       
+       public Configuration(String filename) {
+               this(filename, filename + ".lock");
+       }
+       
+       public Configuration(String filename, String lockFilename) {
+               this.filename = filename;
+               this.lockFilename = lockFilename;
                readConfiguration();
        }
        
        private boolean createConfigDirectory() {
-               File configDirectory = new File(System.getProperty("user.home"), ".jSite");
+               File configDirectory = new File(filename).getAbsoluteFile().getParentFile();
                return (configDirectory.exists() && configDirectory.isDirectory()) || configDirectory.mkdirs();
        }
 
@@ -112,7 +120,7 @@ public class Configuration {
        public boolean save() {
                File configurationFile = new File(filename);
                if (!configurationFile.exists()) {
-                       File configurationFilePath = configurationFile.getParentFile();
+                       File configurationFilePath = configurationFile.getAbsoluteFile().getParentFile();
                        if (!configurationFilePath.exists() && !configurationFilePath.mkdirs()) {
                                return false;
                        }
index 76d456d..3a62d9a 100644 (file)
@@ -64,7 +64,7 @@ import de.todesbaum.util.swing.WizardListener;
 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
 
        private static boolean debug = false;
-       private Configuration configuration = new Configuration();
+       private Configuration configuration;
        private Freenet7Interface freenetInterface = new Freenet7Interface();
        protected Icon jSiteIcon;
 
@@ -82,6 +82,15 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
        private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
 
        private Main() {
+               this(null);
+       }
+       
+       private Main(String configFilename) {
+               if (configFilename != null) {
+                       configuration = new Configuration(configFilename);
+               } else {
+                       configuration = new Configuration();
+               }
                Locale.setDefault(configuration.getLocale());
                I18n.setLocale(configuration.getLocale());
                if (!configuration.createLockFile()) {
@@ -405,13 +414,37 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
        //
        // MAIN METHOD
        //
-
        public static void main(String[] args) {
                System.setProperty("swing.plaf.metal.userFont", "Tahoma");
                System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
                System.setProperty("swing.aatext", "true");
-               debug = (args.length > 0) && (args[0].equals("--debug"));
-               new Main();
+               String configFilename = null;
+               boolean nextIsConfigFilename = false;
+               for (String argument: args) {
+                       if (nextIsConfigFilename) {
+                               configFilename = argument;
+                               nextIsConfigFilename = false;
+                       }
+                       if ("--help".equals(argument)) {
+                               printHelp();
+                               return;
+                       } else if ("--debug".equals(argument)) {
+                               debug = true;
+                       } else if ("--config-file".equals(argument)) {
+                               nextIsConfigFilename = true;
+                       }
+               }
+               if (nextIsConfigFilename) {
+                       System.out.println("--config-file needs parameter!");
+                       return;
+               }
+               new Main(configFilename);
        }
 
+       private static void printHelp() {
+               System.out.println("--help\tshows this cruft");
+               System.out.println("--debug\tenables some debug output");
+               System.out.println("--config-file <file>\tuse specified configuration file");
+       }
+       
 }