KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > PrefuseConfig


1 package prefuse.util;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.util.logging.FileHandler JavaDoc;
6 import java.util.logging.Handler JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8 import java.util.logging.SimpleFormatter JavaDoc;
9
10 import prefuse.util.io.IOLib;
11
12 /**
13  * <p>Runtime configuration settings for the prefuse framework. Maintains a set
14  * of hardwired configuration settings that can be overrriden by creating a
15  * text proeprties file containing custom values. By default, prefuse will
16  * look for the file "prefuse.conf" in the current working directory for
17  * configuration name/value pairs. The framework can be instructed to look for
18  * a different file by putting the full path to the file into the
19  * "prefuse.config" System property (for example by using a -D flag at the
20  * Java runtime command line).</p>
21  *
22  * <p>
23  * Some of the supported configuration properties include:
24  * <ul>
25  * <li><code>activity.threadPriority</code> - the thread priority of the
26  * ActivityManager thread. The value should be between 1 and 10, with 5 being
27  * the standard Java default. The default prefuse setting is 6.</li>
28  * <li><code>data.io.worker.threadPriority</code> - the thread priority of
29  * asynchronous database worker threads. The default prefuse setting is 5
30  * (same as the Java thread default).</li>
31  * <li><code>data.filter.optimizeThreshold</code> - the minimum number of items
32  * that must be contained in a table for optimized query plans to be
33  * considered. The default value is 300.</li>
34  * <li><code>util.logdir</code> - the directory in which to write prefuse log
35  * files. The default is "null" which defaults logging output to standard
36  * output.</li>
37  * <li><code>util.logfile</code> - the filename pattern to use for naming
38  * prefuse log files. The default is "prefuse_log_%g.txt", where the %g
39  * indicates a unique number for the log file.</li>
40  * </ul>
41  * </p>
42  *
43  * <p>
44  * Application creators are welcome to add their own custom properties
45  * to the configuration files and use the PrefuseConfig instance to
46  * access those properties. This class is a singleton, accessed through
47  * a static accessor method.
48  * </p>
49  *
50  * @author <a HREF="http://jheer.org">jeffrey heer</a>
51  */

52 public class PrefuseConfig extends Properties JavaDoc {
53
54     private static final Logger JavaDoc s_logger
55         = Logger.getLogger(PrefuseConfig.class.getName());
56     
57     private static final PrefuseConfig s_config = new PrefuseConfig();
58     
59     /**
60      * Get the global PrefuseConfig instance.
61      * @return the configuration instance
62      */

63     public static PrefuseConfig getConfig() {
64         return s_config;
65     }
66     
67     private PrefuseConfig() {
68         setDefaults();
69         
70         String JavaDoc configFile;
71         try {
72             configFile = System.getProperty("prefuse.config");
73         } catch ( Exception JavaDoc e ) {
74             // in applet mode, we could run afoul of the security manager
75
configFile = null;
76         }
77         if ( configFile == null )
78             configFile = "prefuse.conf";
79         try {
80             load(IOLib.streamFromString(configFile));
81             s_logger.info("Loaded config file: "+configFile);
82         } catch ( Exception JavaDoc e ) {
83             // do nothing, just go with the defaults
84
}
85         
86         // direct logging file directory, as set by config properties
87
// default of java.util.Logger is to output to standard error
88
String JavaDoc logdir = getProperty("util.logdir");
89         String JavaDoc logfile = getProperty("util.logfile");
90         if ( logdir != null ) {
91             try {
92                 Logger JavaDoc logger = Logger.getLogger("prefuse");
93                 logger.setUseParentHandlers(false);
94                 Handler JavaDoc fileHandler = new FileHandler JavaDoc(logdir+"/"+logfile);
95                 fileHandler.setFormatter(new SimpleFormatter JavaDoc());
96                 logger.addHandler(fileHandler);
97             } catch ( IOException JavaDoc e ) {
98                 e.printStackTrace();
99             }
100         }
101     }
102     
103     /**
104      * Get a prefuse configuration property.
105      * @param key the name of the property to lookup
106      * @return the property value, or null if the key is not found
107      */

108     public static String JavaDoc get(String JavaDoc key) {
109         return s_config.getProperty(key);
110     }
111
112     /**
113      * Get a prefuse configuration property as an integer.
114      * @param key the name of the property to lookup
115      * @return the property value, or the minimum possible
116      * integer value if the key is not found or parsing
117      * of the number fails.
118      */

119     public static int getInt(String JavaDoc key) {
120         String JavaDoc val = s_config.getProperty(key);
121         try {
122             return Integer.parseInt(val);
123         } catch ( NumberFormatException JavaDoc nfe ) {
124             return Integer.MIN_VALUE;
125         }
126     }
127
128     /**
129      * Get a prefuse configuration property as a long.
130      * @param key the name of the property to lookup
131      * @return the property value, or the minimum possible
132      * long value if the key is not found or parsing
133      * of the number fails.
134      */

135     public static long getLong(String JavaDoc key) {
136         String JavaDoc val = s_config.getProperty(key);
137         try {
138             return Long.parseLong(val);
139         } catch ( NumberFormatException JavaDoc nfe ) {
140             return Long.MIN_VALUE;
141         }
142     }
143     
144     /**
145      * Get a prefuse configuration property as a float.
146      * @param key the name of the property to lookup
147      * @return the property value, or a Float.NaN
148      * value if the key is not found or parsing
149      * of the number fails.
150      */

151     public static float getFloat(String JavaDoc key) {
152         String JavaDoc val = s_config.getProperty(key);
153         try {
154             return Float.parseFloat(val);
155         } catch ( NumberFormatException JavaDoc nfe ) {
156             return Float.NaN;
157         }
158     }
159     
160     /**
161      * Get a prefuse configuration property as a double.
162      * @param key the name of the property to lookup
163      * @return the property value, or a Double.NaN
164      * value if the key is not found or parsing
165      * of the number fails.
166      */

167     public static double getDouble(String JavaDoc key) {
168         String JavaDoc val = s_config.getProperty(key);
169         try {
170             return Double.parseDouble(val);
171         } catch ( NumberFormatException JavaDoc nfe ) {
172             return Double.NaN;
173         }
174     }
175     
176     /**
177      * Get a prefuse configuration property as a boolean.
178      * @param key the name of the property to lookup
179      * @return the property value. False is returned
180      * if the key is not found or does not parse to
181      * a true/false value.
182      */

183     public static boolean getBoolean(String JavaDoc key) {
184         String JavaDoc val = s_config.getProperty(key);
185         return "true".equalsIgnoreCase(val);
186     }
187     
188     /**
189      * Sets default values for Prefuse properties
190      */

191     private void setDefaults() {
192         setProperty("size.scale2D", "0.5");
193         setProperty("activity.threadPriority", "6");
194         setProperty("data.delimiter", ".");
195         setProperty("data.graph.nodeGroup", "nodes");
196         setProperty("data.graph.edgeGroup", "edges");
197         setProperty("data.visual.fieldPrefix", "_");
198         setProperty("data.io.worker.threadPriority",
199                 String.valueOf(Thread.NORM_PRIORITY));
200         
201         // prefuse will only attempt to optimize filtering operations
202
// on tables with more rows than this threshold value
203
setProperty("data.filter.optimizeThreshold", "300");
204         
205         // setProperty("data.graph.nodeKey", null); // intentionally null
206
setProperty("data.graph.sourceKey", "source");
207         setProperty("data.graph.targetKey", "target");
208         setProperty("data.tree.sourceKey", "parent");
209         setProperty("data.tree.targetKey", "child");
210         setProperty("visualization.allItems", "_all_");
211         setProperty("visualization.focusItems", "_focus_");
212         setProperty("visualization.selectedItems", "_selected_");
213         setProperty("visualization.searchItems", "_search_");
214         
215         // setProperty("util.logdir", null); // intentionally null
216
setProperty("util.logfile", "prefuse_log_%g.txt");
217     }
218     
219 } // end of class PrefuseConfig
220
Popular Tags