Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: Freenet/Params.java


1   package Freenet;
2   
3   import Freenet.support.*;
4   import java.util.*;
5   import java.io.*;
6   
7   /**
8    * Stores Freenet parameters
9    **/
10  public class Params
11  {
12      Hashtable params = new Hashtable();
13      Vector argVector = new Vector();
14      private Properties props = new Properties();
15      private String savedOption = null;
16      private boolean noMoreOptions = false;
17  
18  
19      public static void main(String[] args)
20      {
21    try {
22        Params p = new Params(".freenetrc", args);
23        System.out.println("Params: " + p.params);
24        System.out.println("Args: " + p.argVector);
25    }
26    catch (IOException e) {
27        System.out.println ("Error reading .freenetrc: " + e);
28    }
29      }
30  
31      public Params() { }
32      
33      public Params(String filename) throws FileNotFoundException
34      {
35    this(ArrayFactory.makeArray(filename), null);
36      }
37  
38      public Params(String[] args)
39      {
40    this((String[]) null, args);
41      }
42  
43      public Params(String filename, String[] args) throws FileNotFoundException
44      {
45    this(ArrayFactory.makeArray(filename), args);
46      }
47  
48  
49      /**
50       * Read in Freenet parameters
51       * 
52       * Really, this is kind of a hack and needs proper semantic option
53       * handling to distinguish between those that take values and
54       * those that don't.
55       *
56       **/
57      public Params(String[] files, String[] args)
58      {
59    super();
60  
61    // read params from files
62    if (files != null) {
63        for (int x = 0; x < files.length; x++) {
64      if (files[x] != null) {
65          try {
66        Logger.log("Params.java", "Reading config file " + files[x], Logger.MINOR);
67        readParams(files[x]);
68          }
69          catch (FileNotFoundException e) {
70        Logger.log("Params.java", "Couldn't load config file " + files[x], Logger.MINOR);
71          }
72      }
73        }
74    }
75  
76    // read params from command line (overrides file)
77    if (args != null) {
78        for (int x = 0; x < args.length; x++) {
79      if (args[x] != null) {
80          if (isOption (args[x])) {
81        // process previous option, if any
82        if (savedOption != null) {
83            setParam (savedOption, "");
84        }
85  
86        // save for next iteration
87        if (!args[x].equals ("--")) {
88            savedOption = args[x].substring(1);
89        }
90        else {
91            noMoreOptions = true;
92            savedOption = null;
93        }
94          }
95          else {
96        if (savedOption != null) {
97            // store value for previous option
98            setParam (savedOption, args[x]);
99        }
100       else {
101           // store command argument
102           argVector.addElement (args[x]);
103       }
104       savedOption = null;
105         }
106     }
107       }
108 
109       // process last option, if any
110       if (savedOption != null)
111     setParam (savedOption, "");
112   }
113     }
114 
115   public void readParams(String filename) throws FileNotFoundException
116   {
117     FileInputStream in=new FileInputStream(filename);
118 
119     try
120     {
121       props.load(in);
122     }
123     catch(IOException e)
124     {
125       Logger.log("Params.java","Could not read config file",Logger.ERROR);
126       return;
127     }
128     Enumeration iterator=props.propertyNames();
129     while(iterator.hasMoreElements())
130     {
131       String name=(String)iterator.nextElement();
132       params.put(name.toLowerCase(), props.getProperty(name));
133     }
134   }
135 
136   public void setParam(String name, String value)
137   {
138       Logger.log("Params.java", "set \""+name+"\" to \""+value+"\"", Logger.MINOR);
139       params.put(name.toLowerCase(), value);
140   }
141 
142   public String getParam(String name, String def)
143   {
144     String temp=getParam(name);
145     if(temp==null) return def;
146     else return temp;
147   }
148 
149   // get number of command arguments
150   public int getNumArgs()
151   {
152     return argVector.size();
153   }
154 
155   // get command argument by position
156   public String getArg(int position)
157   {
158     if (position >= 0 && position < argVector.size())
159   {
160       return (String) argVector.elementAt (position);
161   }
162     else
163   {
164       // no such argument
165       return null;
166   }
167   }
168 
169   public String getParam(String name)
170   {
171     String r = (String)params.get(name.toLowerCase());
172     if (r == null)
173   {
174       Logger.log("Params.java", 
175            "No value specified for parameter '"+name+"'",
176            Logger.DEBUGGING);
177   }
178     return r;
179   }
180 
181   public long getlong(String name, long def)
182   {
183     String temp=getParam(name);
184     if(temp==null) return def;
185     else return getlong(name);
186   }
187 
188   public long getlong(String name)
189   {
190     return (Long.parseLong(getParam(name)));
191   }
192 
193   public int getint(String name, int def)
194   {
195     String temp=getParam(name);
196     if(temp==null) return def;
197     else return getint(name);
198   }
199 
200   public int getint(String name)
201   {
202     return (Integer.parseInt(getParam(name)));
203   }
204 
205   public short getshort(String name, short def)
206   {
207     String temp=getParam(name);
208     if(temp==null) return def;
209     else return getshort(name);
210   }
211 
212   public short getshort(String name)
213   {
214     return (Short.parseShort(getParam(name)));
215   }
216 
217   public void writeParams(String filename)
218   {
219     try
220     {
221     PrintWriter fw=new PrintWriter(new FileWriter(filename));
222 
223     Enumeration iterator=params.keys();
224     while(iterator.hasMoreElements())
225     {
226       String name=(String)iterator.nextElement();
227       fw.println(name.toLowerCase()+"="+params.get(name));
228     }
229     fw.close();
230     }
231     catch(IOException e)
232     {
233       Logger.log("Params.java","Could not write to config file",Logger.ERROR);
234     }
235   }
236 
237     private boolean isOption (String arg) {
238   return (!noMoreOptions && arg.startsWith("-") && !arg.equals("-"));
239     }
240 
241     /**
242      * Hack to make an array on the fly
243      **/
244     static class ArrayFactory {
245   public static String[] makeArray (String filename) {
246       String[] arr = {filename};
247       return arr;
248   }
249     }
250 }
251 
252 
253