jTracer  1.03
Stack trace visualization tool
Registry.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer.installer;
8 
9 import java.util.Vector;
10 import java.util.Hashtable;
11 
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.PrintStream;
15 import java.io.IOException;
16 
17 import java.net.URL;
18 
19 import java.awt.Font;
20 import java.awt.Color;
21 import java.awt.Dimension;
22 import java.awt.Image;
23 
24 import javax.swing.ImageIcon;
25 import javax.swing.filechooser.FileSystemView;
26 
37 public class Registry extends Hashtable<String, Hashtable<String, Object>>
38 {
40  private static final long serialVersionUID = 0x00;
41 
43  private static Registry current = null;
44 
45 
49  Registry()
50  {
51  current = this;
52 
53  Hashtable<String, Object> section = new Hashtable<String, Object>();
54  section.put("font", new Font("Dialog", Font.PLAIN, 12));
55  section.put("fgcolor", Color.decode("0x646464"));
56  section.put("click-delay", new Integer(100));
57  put("component", section);
58 
59  section = new Hashtable<String, Object>();
60  section.put("font", new Font("Dialog", Font.PLAIN, 12));
61  section.put("fgcolor", Color.decode("0x646464"));
62  put("message", section);
63 
64  section = new Hashtable<String, Object>();
65  section.put("font", new Font("Dialog", Font.BOLD, 12));
66  section.put("fgcolor", Color.decode("0x646464"));
67  put("caption", section);
68 
69  section = new Hashtable<String, Object>();
70  section.put("font", new Font("Dialog", Font.BOLD, 10));
71  section.put("fgcolor", Color.decode("0x76a6c7"));
72  put("progressbar", section);
73 
74  section = new Hashtable<String, Object>();
75  section.put("name", "jTracer");
76  section.put("version", "1.03");
77  section.put("license", "GNU General Public License v3.0");
78  section.put("package", "jTracer-1.03");
79  section.put("jar", "jTracer-1.03.jar");
80  put("installed", section);
81 
82  section = new Hashtable<String, Object>();
83  section.put("name", "jTracer.installer");
84  section.put("version", "1.0");
85  section.put("debug", "on");
86  section.put("console", "on");
87  section.put("size", new Dimension(520, 360));
88  section.put("lnf", "javax.swing.plaf.metal.MetalLookAndFeel");
89  section.put("browser", "firefox");
90  section.put("mailer", "thunderbird");
91  put("generic", section);
92  }
93 
94 
106  public Object get(String section, String key)
107  {
108  return get(section).get(key);
109  }
110 
111 
121  public boolean test(String section, String key)
122  {
123  String entry = (String) get(section, key);
124  if (entry == null)
125  return false;
126 
127  return entry.toLowerCase().equals("on");
128  }
129 
130 
140  public File getResource(String path) throws IOException
141  {
142  if (File.separator.equals("/"))
143  return new File(getPrefix(), "res/" + path);
144 
145  path = "res/" + path;
146  path.replace("/", File.separator);
147  return new File(getPrefix(), path);
148  }
149 
150 
158  public ImageIcon loadIcon(String nm)
159  {
160  try {
161  File f = getResource("theme/default/icons/" + nm);
162  return new ImageIcon(f.getCanonicalPath());
163  }
164 
165  catch (Throwable t) {
166  debug(t);
167  }
168 
169  return null;
170  }
171 
172 
178  public void browse(URL url)
179  {
180  try {
181  String browser = (String) get("generic", "browser");
182  ProcessBuilder proc = new ProcessBuilder(browser, url.toString());
183  proc.start();
184  }
185 
186  catch (Throwable t) {
187  debug(t);
188  }
189  }
190 
191 
197  public void mail(URL url)
198  {
199  try {
200  String mailer = (String) get("generic", "mailer");
201  ProcessBuilder proc = new ProcessBuilder(mailer, url.toString());
202  proc.start();
203  }
204 
205  catch (Throwable t) {
206  debug(t);
207  }
208  }
209 
210 
216  public void setup() throws IOException
217  {
218  if (test("generic", "console"))
219  return;
220 
221  long id = System.currentTimeMillis();
222  File log = getResource("var/dbg-" + id + ".log");
223  log.createNewFile();
224 
225  System.out.println("\r\nOutput log: " + log.getCanonicalPath());
226  FileOutputStream fs = new FileOutputStream(log, true);
227  PrintStream ps = new PrintStream(fs, true);
228  System.setOut(ps);
229  System.setErr(ps);
230  }
231 
232 
238  public Vector<Image> getProjectIcons()
239  {
240  Vector<Image> retval = new Vector<Image>();
241  for (int i = 16; i <= 128; i *= 2) {
242  ImageIcon icon = loadIcon("icon" + i + ".png");
243  if (icon != null)
244  retval.add(icon.getImage());
245  }
246 
247  return retval;
248  }
249 
250 
256  public static Registry getCurrent()
257  {
258  return current;
259  }
260 
261 
267  public static void setCurrent(Registry cur)
268  {
269  current = cur;
270  }
271 
272 
278  public static File getHomeDirectory() throws IOException
279  {
280  File retval = FileSystemView.getFileSystemView().getHomeDirectory();
281  String path = retval.getCanonicalPath();
282 
283  String msg = null;
284  if (!retval.isDirectory())
285  msg = "The user home directory '" + path + "' doesn't exist";
286 
287  else if (!retval.canRead())
288  msg = "Can't access the user home directory '" + path + "'";
289 
290  if (msg != null)
291  throw new IOException(msg);
292 
293  return retval;
294  }
295 
296 
302  public static File getPrefix() throws IOException
303  {
304  String tmpdir = System.getProperty("java.io.tmpdir");
305  File retval = new File(tmpdir, ".jTracer.installer");
306  String path = retval.getCanonicalPath();
307 
308  String msg = null;
309  if (!retval.isDirectory())
310  msg = "The installation directory '" + path + "' doesn't exist";
311 
312  else if (!retval.canRead())
313  msg = "Can't access the installation directory '" + path + "'";
314 
315  if (msg != null)
316  throw new IOException(msg);
317 
318  return retval;
319  }
320 
321 
327  public synchronized static void debug(Throwable err)
328  {
329  Registry conf = Registry.getCurrent();
330  if (conf != null && !conf.test("generic", "debug"))
331  return;
332 
333  Thread thr = Thread.currentThread();
334  System.err.println("at thread " + thr.getName() + ":" + thr.getId());
335  err.printStackTrace();
336  }
337 
338 
344  public synchronized static void debug(String msg)
345  {
346  Registry conf = Registry.getCurrent();
347  if (conf != null && !conf.test("generic", "debug"))
348  return;
349 
350  Thread thr = Thread.currentThread();
351  System.err.println("at thread " + thr.getName() + ":" + thr.getId());
352  System.err.println(msg);
353  }
354 
355 
356  public static String getOsName()
357  {
358  return System.getProperty("os.name");
359  }
360 
361 
362  public static boolean isLinux()
363  {
364  String os = getOsName();
365  if (os == null)
366  return false;
367 
368  return os.matches("Linux");
369  }
370 }
371 
void setup()
Setup any process specific resources before the main application frame is launched.
Definition: Registry.java:216
static synchronized void debug(Throwable err)
Output debug text for an exception.
Definition: Registry.java:327
static Registry current
Current registry.
Definition: Registry.java:43
File getResource(String path)
Get a resource file.
Definition: Registry.java:140
void browse(URL url)
Open a URL on the registered web browser.
Definition: Registry.java:178
ImageIcon loadIcon(String nm)
Load an icon from the current theme.
Definition: Registry.java:158
Vector< Image > getProjectIcons()
Get a cross-platform set of project icons.
Definition: Registry.java:238
void mail(URL url)
Open a URL on the registered mail composer.
Definition: Registry.java:197
static File getHomeDirectory()
Get the user home directory.
Definition: Registry.java:278
boolean test(String section, String key)
Get a boolean entry.
Definition: Registry.java:121
static File getPrefix()
Get the installation prefix.
Definition: Registry.java:302
static final long serialVersionUID
Class version.
Definition: Registry.java:40
static synchronized void debug(String msg)
Output a debug message.
Definition: Registry.java:344
static Registry getCurrent()
Get the current registry.
Definition: Registry.java:256
static void setCurrent(Registry cur)
Set the current registry.
Definition: Registry.java:267