jTracer  1.03
Stack trace visualization tool
Registry.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.util.Vector;
10 import java.util.Hashtable;
11 import java.util.Enumeration;
12 
13 import java.io.File;
14 import java.io.FileOutputStream;
15 import java.io.PrintStream;
16 import java.io.IOException;
17 
18 import java.net.URL;
19 import java.net.InetSocketAddress;
20 import java.net.MalformedURLException;
21 
22 import java.awt.Font;
23 import java.awt.Color;
24 import java.awt.Insets;
25 import java.awt.Dimension;
26 import java.awt.Image;
27 
28 import javax.swing.ImageIcon;
29 import javax.swing.filechooser.FileSystemView;
30 
41 public class Registry extends Hashtable<String, Hashtable<String, Object>>
42 {
44  private static final long serialVersionUID = 0x00;
45 
47  private static Registry current = null;
48 
49 
53  Registry() throws MalformedURLException
54  {
55  current = this;
56 
57  Hashtable<String, Object> section = new Hashtable<String, Object>();
58  section.put("font", new Font("Dialog", Font.PLAIN, 12));
59  section.put("fgcolor", Color.decode("0x646464"));
60  section.put("click-delay", 100);
61  put("component", section);
62 
63  section = new Hashtable<String, Object>();
64  section.put("font", new Font("Dialog", Font.PLAIN, 12));
65  section.put("fgcolor", Color.decode("0x646464"));
66  put("message", section);
67 
68  section = new Hashtable<String, Object>();
69  section.put("font", new Font("Dialog", Font.PLAIN, 12));
70  section.put("fgcolor-debug", Color.decode("0x646464"));
71  section.put("fgcolor-status", Color.decode("0x76a6c7"));
72  section.put("fgcolor-data", Color.decode("0x7dc91b"));
73  section.put("fgcolor-alert", Color.decode("0xe94615"));
74  section.put("fgcolor-error", Color.decode("0xe94615"));
75 
76  section.put("font-trace", new Font("Dialog", Font.PLAIN, 12));
77  section.put("fgcolor-delimiter", Color.decode("0x707070"));
78  section.put("fgcolor-plain", Color.decode("0x707070"));
79  section.put("fgcolor-keyword", Color.decode("0xb66127"));
80  section.put("fgcolor-type", Color.decode("0x467697"));
81  section.put("fgcolor-number", Color.decode("0xdfa613"));
82  section.put("fgcolor-file", Color.decode("0x5da90b"));
83  section.put("fgcolor-scope", Color.decode("0xa0a0a0"));
84  section.put("fgcolor-function", Color.decode("0x404040"));
85 
86  section.put("fgcolor-selection", Color.white);
87  section.put("bgcolor-selection", Color.decode("0x76a6c7"));
88  section.put("padding", 4);
89  section.put("line-height", 0.25f);
90  section.put("margin", new Insets(6, 2, 0, 2));
91  put("text", section);
92 
93  section = new Hashtable<String, Object>();
94  section.put("font", new Font("Dialog", Font.BOLD, 10));
95  section.put("fgcolor", Color.white);
96  section.put("fgcolor-error", Color.decode("0x9d1f18"));
97  section.put("bgcolor", Color.decode("0xff8818"));
98  section.put("bgcolor-counter-active", Color.decode("0xff7000"));
99  section.put("bgcolor-uptime-active", Color.decode("0x5da90b"));
100  put("statusbar", section);
101 
102  section = new Hashtable<String, Object>();
103  section.put("all-local", new InetSocketAddress(4242));
104  put("server", section);
105 
106  section = new Hashtable<String, Object>();
107  section.put("size", new Dimension(560, 280));
108  section.put("timeout", 5000);
109  put("session", section);
110 
111  section = new Hashtable<String, Object>();
112  section.put("name", "jTracer");
113  section.put("version", "1.03");
114  section.put("debug", "on");
115  section.put("console", "on");
116  section.put("size", new Dimension(560, 280));
117  section.put("lnf", "javax.swing.plaf.metal.MetalLookAndFeel");
118  section.put("browser", "firefox");
119  section.put("mailer", "thunderbird");
120 
121  section.put(
122  "url-online-documentation",
123  new URL("http://libcsdbg.sourceforge.net/jTracer/")
124  );
125 
126  section.put(
127  "url-bug-tracker",
128  new URL("https://sourceforge.net/p/libcsdbg/tickets/")
129  );
130 
131  section.put(
132  "url-submit-feedback",
133  new URL("https://sourceforge.net/p/libcsdbg/discussion/")
134  );
135 
136  put("generic", section);
137  }
138 
139 
151  public Object get(String section, String key)
152  {
153  return get(section).get(key);
154  }
155 
156 
166  public boolean test(String section, String key)
167  {
168  String entry = (String) get(section, key);
169  if (entry == null)
170  return false;
171 
172  return entry.toLowerCase().equals("on");
173  }
174 
175 
183  public Enumeration getSectionKeys(String nm)
184  {
185  Hashtable<String, Object> sect = get(nm);
186  if (sect != null)
187  return sect.keys();
188 
189  return null;
190  }
191 
192 
202  public File getResource(String path) throws IOException
203  {
204  if (File.separator.equals("/"))
205  return new File(getPrefix(), "res/" + path);
206 
207  path = "res/" + path;
208  path.replace("/", File.separator);
209  return new File(getPrefix(), path);
210  }
211 
212 
220  public ImageIcon loadIcon(String nm)
221  {
222  try {
223  File f = null;
224  if (isLinux())
225  f = getResource("theme/current/icons/" + nm);
226  else
227  f = getResource("theme/default/icons/" + nm);
228 
229  return new ImageIcon(f.getCanonicalPath());
230  }
231 
232  catch (Throwable t) {
233  debug(t);
234  }
235 
236  return null;
237  }
238 
239 
245  public void browse(URL url)
246  {
247  try {
248  String browser = (String) get("generic", "browser");
249  ProcessBuilder proc = new ProcessBuilder(browser, url.toString());
250  proc.start();
251  }
252 
253  catch (Throwable t) {
254  debug(t);
255  }
256  }
257 
258 
264  public void mail(URL url)
265  {
266  try {
267  String mailer = (String) get("generic", "mailer");
268  ProcessBuilder proc = new ProcessBuilder(mailer, url.toString());
269  proc.start();
270  }
271 
272  catch (Throwable t) {
273  debug(t);
274  }
275  }
276 
277 
283  public void setup() throws IOException
284  {
285  if (test("generic", "console"))
286  return;
287 
288  long id = System.currentTimeMillis();
289  File log = getResource("log/dbg-" + id + ".log");
290  log.createNewFile();
291 
292  System.out.println("\r\nOutput log: " + log.getCanonicalPath());
293  FileOutputStream fs = new FileOutputStream(log, true);
294  PrintStream ps = new PrintStream(fs, true);
295  System.setOut(ps);
296  System.setErr(ps);
297  }
298 
299 
305  public Vector<Image> getProjectIcons()
306  {
307  Vector<Image> retval = new Vector<Image>();
308  for (int i = 16; i <= 128; i *= 2) {
309  ImageIcon icon = loadIcon("icon" + i + ".png");
310  if (icon != null)
311  retval.add(icon.getImage());
312  }
313 
314  return retval;
315  }
316 
317 
323  public static Registry getCurrent()
324  {
325  return current;
326  }
327 
328 
334  public static void setCurrent(Registry cur)
335  {
336  current = cur;
337  }
338 
339 
345  public static File getHomeDirectory() throws IOException
346  {
347  File retval = FileSystemView.getFileSystemView().getHomeDirectory();
348  String path = retval.getCanonicalPath();
349 
350  String msg = null;
351  if (!retval.isDirectory())
352  msg = "The user home directory '" + path + "' doesn't exist";
353 
354  else if (!retval.canRead())
355  msg = "Can't access the user home directory '" + path + "'";
356 
357  if (msg != null)
358  throw new IOException(msg);
359 
360  return retval;
361  }
362 
363 
369  public static File getPrefix() throws IOException
370  {
371  File retval = null;
372  if (isLinux())
373  retval = new File(getHomeDirectory(), ".jTracer");
374  else
375  retval = new File("C:\\Program Files\\jTracer");
376 
377  String path = retval.getCanonicalPath();
378 
379  String msg = null;
380  if (!retval.isDirectory())
381  msg = "The installation directory '" + path + "' doesn't exist";
382 
383  else if (!retval.canRead())
384  msg = "Can't access the installation directory '" + path + "'";
385 
386  if (msg != null)
387  throw new IOException(msg);
388 
389  return retval;
390  }
391 
392 
398  public synchronized static void debug(Throwable err)
399  {
400  Registry conf = Registry.getCurrent();
401  if (conf != null && !conf.test("generic", "debug"))
402  return;
403 
404  Thread thr = Thread.currentThread();
405  System.err.println("at thread " + thr.getName() + ":" + thr.getId());
406  err.printStackTrace();
407  }
408 
409 
415  public synchronized static void debug(String msg)
416  {
417  Registry conf = Registry.getCurrent();
418  if (conf != null && !conf.test("generic", "debug"))
419  return;
420 
421  Thread thr = Thread.currentThread();
422  System.err.println("at thread " + thr.getName() + ":" + thr.getId());
423  System.err.println(msg);
424  }
425 
426 
427  public static String getOsName()
428  {
429  return System.getProperty("os.name");
430  }
431 
432 
433  public static boolean isLinux()
434  {
435  String os = getOsName();
436  if (os == null)
437  return false;
438 
439  return os.matches("Linux");
440  }
441 }
442 
static void setCurrent(Registry cur)
Set the current registry.
Definition: Registry.java:334
void setup()
Setup any process specific resources before the main application frame is launched.
Definition: Registry.java:283
Configuration registry.
Definition: Registry.java:41
ImageIcon loadIcon(String nm)
Load an icon from the current theme.
Definition: Registry.java:220
Vector< Image > getProjectIcons()
Get a cross-platform set of project icons.
Definition: Registry.java:305
void mail(URL url)
Open a URL on the registered mail composer.
Definition: Registry.java:264
static final long serialVersionUID
Class version.
Definition: Registry.java:44
static Registry current
Current registry.
Definition: Registry.java:47
boolean test(String section, String key)
Get a boolean entry.
Definition: Registry.java:166
static synchronized void debug(Throwable err)
Output debug text for an exception.
Definition: Registry.java:398
void browse(URL url)
Open a URL on the registered web browser.
Definition: Registry.java:245
static boolean isLinux()
Definition: Registry.java:433
static Registry getCurrent()
Get the current registry.
Definition: Registry.java:323
Enumeration getSectionKeys(String nm)
Get all the keys of a section.
Definition: Registry.java:183
File getResource(String path)
Get a resource file.
Definition: Registry.java:202
static File getPrefix()
Get the installation prefix.
Definition: Registry.java:369
static File getHomeDirectory()
Get the user home directory.
Definition: Registry.java:345
static synchronized void debug(String msg)
Output a debug message.
Definition: Registry.java:415