Jalview 2.8 Source Header
[jalview.git] / src / jalview / bin / Cache.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.bin;
19
20 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
21 import jalview.ws.dbsources.das.datamodel.DasSourceRegistry;
22
23 import java.awt.Color;
24 import java.io.*;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.*;
28
29 import org.apache.log4j.*;
30
31 /**
32  * Stores and retrieves Jalview Application Properties Lists and fields within
33  * list entries are separated by '|' symbols unless otherwise stated (|) clauses
34  * are alternative values for a tag. <br>
35  * <br>
36  * Current properties include:
37  * <ul>
38  * <br>
39  * logs.Axis.Level - one of the stringified Levels for log4j controlling the
40  * logging level for axis (used for web services) <br>
41  * </li>
42  * <li>logs.Castor.Level - one of the stringified Levels for log4j controlling
43  * the logging level for castor (used for serialization) <br>
44  * </li>
45  * <li>logs.Jalview.Level - Cache.log stringified level. <br>
46  * </li>
47  * <li>SCREEN_WIDTH</li>
48  * <li>SCREEN_HEIGHT</li>
49  * <li>SCREEN_Y=285</li>
50  * <li>SCREEN_X=371</li>
51  * <li>SHOW_FULLSCREEN boolean</li>
52  * <li>FONT_NAME java font name for alignment text display</li>
53  * <li>FONT_SIZE size of displayed alignment text</li>
54  * <li>FONT_STYLE style of font displayed (sequence labels are always italic)</li>
55  * <li>GAP_SYMBOL character to treat as gap symbol (usually -,.,' ')</li>
56  * <li>LAST_DIRECTORY last directory for browsing alignment</li>
57  * <li>USER_DEFINED_COLOURS list of user defined colour scheme files</li>
58  * <li>SHOW_FULL_ID show id with '/start-end' numbers appended</li>
59  * <li>SHOW_IDENTITY show percentage identity annotation</li>
60  * <li>SHOW_QUALITY show alignment quality annotation</li>
61  * <li>SHOW_ANNOTATIONS show alignment annotation rows</li>
62  * <li>SHOW_CONSERVATION show alignment conservation annotation</li>
63  * <li>CENTRE_COLUMN_LABELS centre the labels at each column in a displayed
64  * annotation row</li>
65  * <li>DEFAULT_COLOUR default colour scheme to apply for a new alignment</li>
66  * <li>DEFAULT_FILE_FORMAT file format used to save</li>
67  * <li>STARTUP_FILE file loaded on startup (may be a fully qualified url)</li>
68  * <li>SHOW_STARTUP_FILE flag to control loading of startup file</li>
69  * <li>VERSION the version of the jalview build</li>
70  * <li>BUILD_DATE date of this build</li>
71  * <li>LATEST_VERSION the latest jalview version advertised on the
72  * www.jalview.org</li>
73  * <li>PIR_MODELLER boolean indicating if PIR files are written with MODELLER
74  * descriptions</li>
75  * <li>(FASTA,MSF,PILEUP,CLUSTAL,BLC,PIR,PFAM)_JVSUFFIX boolean for adding jv
76  * suffix to file</li>
77  * <li>RECENT_URL list of recently retrieved URLs</li>
78  * <li>RECENT_FILE list of recently opened files</li>
79  * <li>USE_PROXY flag for whether a http proxy is to be used</li>
80  * <li>PROXY_SERVER the proxy</li>
81  * <li>PROXY_PORT</li>
82  * <li>NOQUESTIONNAIRES true to prevent jalview from checking the questionnaire
83  * service</li>
84  * <li>QUESTIONNAIRE last questionnaire:responder id string from questionnaire
85  * service</li>
86  * <li>USAGESTATS (false - user prompted) Enable google analytics tracker for
87  * collecting usage statistics</li>
88  * <li>DAS_LOCAL_SOURCE list of local das sources</li>
89  * <li>SHOW_OVERVIEW boolean for overview window display</li>
90  * <li>ANTI_ALIAS boolean for smooth fonts</li>
91  * <li>RIGHT_ALIGN_IDS boolean</li>
92  * <li>AUTO_CALC_CONSENSUS boolean for automatic recalculation of consensus</li>
93  * <li>PAD_GAPS boolean</li>
94  * <li>ID_ITALICS boolean</li>
95  * <li>SHOW_JV_SUFFIX</li>
96  * <li>WRAP_ALIGNMENT</li>
97  * <li>EPS_RENDERING (Prompt each time|Lineart|Text) default for EPS rendering
98  * style check</li>
99  * <li>SORT_ALIGNMENT (No sort|Id|Pairwise Identity)</li>
100  * <li>SEQUENCE_LINKS list of name|URL pairs for opening a url with
101  * $SEQUENCE_ID$</li>
102  * <li>GROUP_LINKS list of name|URL[|&lt;separator&gt;] tuples - see
103  * jalview.utils.GroupURLLink for more info</li>
104  * <li>DAS_REGISTRY_URL the registry to query</li>
105  * <li>DEFAULT_BROWSER for unix</li>
106  * <li>DAS_ACTIVE_SOURCE list of active sources</li>
107  * <li>SHOW_MEMUSAGE boolean show memory usage and warning indicator on desktop
108  * (false)</li>
109  * <li>VERSION_CHECK (true) check for the latest release version from
110  * www.jalview.org (or the alias given by the www.jalview.org property)</li>
111  * <li>SHOW_NPFEATS_TOOLTIP (true) show non-positional features in the Sequence
112  * ID tooltip</li>
113  * <li>SHOW_DBREFS_TOOLTIP (true) show Database Cross References in the Sequence
114  * ID tooltip</li>
115  * <li>SHOW_UNCONSERVED (false) only render unconserved residues - conserved
116  * displayed as '.'</li>
117  * <li>SORT_BY_TREE (false) sort the current alignment view according to the
118  * order of a newly displayed tree</li>
119  * <li>DBFETCH_USEPICR (false) use PICR to recover valid DB references from
120  * sequence ID strings before attempting retrieval from any datasource</li>
121  * <li>SHOW_GROUP_CONSENSUS (false) Show consensus annotation for groups in the
122  * alignment.</li>
123  * <li>SHOW_GROUP_CONSERVATION (false) Show conservation annotation for groups
124  * in the alignment.</li>
125  * <li>SHOW_CONSENSUS_HISTOGRAM (false) Show consensus annotation row's
126  * histogram.</li>
127  * <li>SHOW_CONSENSUS_LOGO (false) Show consensus annotation row's sequence
128  * logo.</li>
129  * <li>NORMALISE_CONSENSUS_LOGO (false) Show consensus annotation row's sequence
130  * logo normalised to row height rather than histogram height.</li>
131  * <li>FOLLOW_SELECTIONS (true) Controls whether a new alignment view should
132  * respond to selections made in other alignments containing the same sequences.
133  * </li>
134  * <li>SHOW_WSDISCOVERY_ERRORS (true) Controls if the web service URL discovery
135  * warning dialog box is displayed.</li>
136  * <li>ANNOTATIONCOLOUR_MIN (orange) Shade used for minimum value of annotation
137  * when shading by annotation</li>
138  * <li>ANNOTATIONCOLOUR_MAX (red) Shade used for maximum value of annotation
139  * when shading by annotation</li>
140  * <li>www.jalview.org (http://www.jalview.org) a property enabling all HTTP
141  * requests to be redirected to a mirror of http://www.jalview.org</li>
142  * 
143  * <li>FIGURE_AUTOIDWIDTH (false) Expand the left hand column of an exported
144  * alignment figure to accommodate even the longest sequence ID or annotation
145  * label.</li>
146  * <li>FIGURE_FIXEDIDWIDTH Specifies the width to use for the left-hand column
147  * when exporting an alignment as a figure (setting FIGURE_AUTOIDWIDTH to true
148  * will override this).</li>
149  * <li></li>
150  * 
151  * </ul>
152  * Deprecated settings:
153  * <ul>
154  * *
155  * <li>DISCOVERY_START - Boolean - controls if discovery services are queried on
156  * startup (JWS1 services only)</li>
157  * <li>DISCOVERY_URLS - comma separated list of Discovery Service endpoints.
158  * (JWS1 services only)</li>
159  * <li>SHOW_JWS1_SERVICES (true) enable or disable the original Jalview 2
160  * services in the desktop GUI</li>
161  * <li>ENABLE_RSBS_EDITOR (false for 2.7 release) enable or disable RSBS editing
162  * panel in web service preferences</li>
163  * </ul>
164  * 
165  * @author $author$
166  * @version $Revision$
167  */
168 public class Cache
169 {
170   /**
171    * property giving log4j level for CASTOR loggers
172    */
173   public static final String CASTORLOGLEVEL = "logs.Castor.level";
174
175   /**
176    * property giving log4j level for AXIS loggers
177    */
178   public static final String AXISLOGLEVEL = "logs.Axis.level";
179
180   /**
181    * property giving log4j level for Jalview Log
182    */
183   public static final String JALVIEWLOGLEVEL = "logs.Jalview.level";
184
185   public static final String DAS_LOCAL_SOURCE = "DAS_LOCAL_SOURCE";
186
187   public static final String DAS_REGISTRY_URL = "DAS_REGISTRY_URL";
188
189   public static final String DAS_ACTIVE_SOURCE = "DAS_ACTIVE_SOURCE";
190
191   /**
192    * Initialises the Jalview Application Log
193    */
194   public static Logger log;
195
196   /** Jalview Properties */
197   public static Properties applicationProperties = new Properties();
198
199   /** Default file is ~/.jalview_properties */
200   static String propertiesFile;
201
202   private static boolean propsAreReadOnly = false;
203
204   public static void initLogger()
205   {
206     if (log != null)
207     {
208       return;
209     }
210     try
211     {
212       // TODO: redirect stdout and stderr here in order to grab the output of
213       // the log
214
215       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
216               "System.err");
217       ap.setName("JalviewLogger");
218       org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for
219       // log output
220       Logger laxis = Logger.getLogger("org.apache.axis");
221       Logger lcastor = Logger.getLogger("org.exolab.castor");
222       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
223
224       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
225               Level.INFO.toString())));
226       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
227               Level.INFO.toString())));
228       lcastor = Logger.getLogger("org.exolab.castor.xml");
229       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
230               Level.INFO.toString())));
231       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
232       // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
233       // Level.INFO.toString())));
234       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
235               "logs.Jalview.level", Level.INFO.toString())));
236       // laxis.addAppender(ap);
237       // lcastor.addAppender(ap);
238       // jalview.bin.Cache.log.addAppender(ap);
239       // Tell the user that debug is enabled
240       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
241     } catch (Exception ex)
242     {
243       System.err.println("Problems initializing the log4j system\n");
244       ex.printStackTrace(System.err);
245     }
246   }
247
248   /** Called when Jalview is started */
249   public static void loadProperties(String propsFile)
250   {
251     propertiesFile = propsFile;
252     if (propsFile == null)
253     {
254       propertiesFile = System.getProperty("user.home") + File.separatorChar
255               + ".jalview_properties";
256     }
257     else
258     {
259       // don't corrupt the file we've been given.
260       propsAreReadOnly = true;
261     }
262
263     try
264     {
265       InputStream fis;
266       try
267       {
268         fis = new java.net.URL(propertiesFile).openStream();
269         System.out.println("Loading jalview properties from : "
270                 + propertiesFile);
271         System.out
272                 .println("Disabling Jalview writing to user's local properties file.");
273         propsAreReadOnly = true;
274
275       } catch (Exception ex)
276       {
277         fis = null;
278       }
279       if (fis == null)
280       {
281         fis = new FileInputStream(propertiesFile);
282       }
283       applicationProperties.load(fis);
284       applicationProperties.remove("LATEST_VERSION");
285       applicationProperties.remove("VERSION");
286       fis.close();
287     } catch (Exception ex)
288     {
289       System.out.println("Error reading properties file: " + ex);
290     }
291
292     if (getDefault("USE_PROXY", false))
293     {
294       String proxyServer = getDefault("PROXY_SERVER", ""), proxyPort = getDefault(
295               "PROXY_PORT", "8080");
296
297       System.out.println("Using proxyServer: " + proxyServer
298               + " proxyPort: " + proxyPort);
299
300       System.setProperty("http.proxyHost", proxyServer);
301       System.setProperty("http.proxyPort", proxyPort);
302     }
303
304     // LOAD THE AUTHORS FROM THE authors.props file
305     try
306     {
307       String authorDetails = "jar:".concat(Cache.class
308               .getProtectionDomain().getCodeSource().getLocation()
309               .toString().concat("!/authors.props"));
310
311       java.net.URL localJarFileURL = new java.net.URL(authorDetails);
312
313       InputStream in = localJarFileURL.openStream();
314       applicationProperties.load(in);
315       in.close();
316     } catch (Exception ex)
317     {
318       System.out.println("Error reading author details: " + ex);
319       applicationProperties.remove("AUTHORS");
320       applicationProperties.remove("AUTHORFNAMES");
321       applicationProperties.remove("YEAR");
322     }
323
324     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
325     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
326     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
327     try
328     {
329       String buildDetails = "jar:".concat(Cache.class.getProtectionDomain()
330               .getCodeSource().getLocation().toString()
331               .concat("!/.build_properties"));
332
333       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
334
335       InputStream in = localJarFileURL.openStream();
336       applicationProperties.load(in);
337       in.close();
338     } catch (Exception ex)
339     {
340       System.out.println("Error reading build details: " + ex);
341       applicationProperties.remove("VERSION");
342     }
343
344     String jnlpVersion = System.getProperty("jalview.version");
345     String codeVersion = getProperty("VERSION");
346
347     if (codeVersion == null)
348     {
349       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
350       codeVersion = "Test";
351       jnlpVersion = "Test";
352     }
353
354     System.out.println("Jalview Version: " + codeVersion);
355
356     // jnlpVersion will be null if we're using InstallAnywhere
357     // Dont do this check if running in headless mode
358     if (jnlpVersion == null
359             && getDefault("VERSION_CHECK", true)
360             && (System.getProperty("java.awt.headless") == null || System
361                     .getProperty("java.awt.headless").equals("false")))
362     {
363
364       class VersionChecker extends Thread
365       {
366         public void run()
367         {
368           String orgtimeout = System
369                   .getProperty("sun.net.client.defaultConnectTimeout");
370           if (orgtimeout == null)
371           {
372             orgtimeout = "30";
373             System.out.println("# INFO: Setting default net timeout to "
374                     + orgtimeout + " seconds.");
375           }
376           String jnlpVersion = null;
377           try
378           {
379             System.setProperty("sun.net.client.defaultConnectTimeout",
380                     "5000");
381             java.net.URL url = new java.net.URL(Cache.getDefault(
382                     "www.jalview.org", "http://www.jalview.org")
383                     + "/webstart/jalview.jnlp");
384             BufferedReader in = new BufferedReader(new InputStreamReader(
385                     url.openStream()));
386             String line = null;
387             while ((line = in.readLine()) != null)
388             {
389               if (line.indexOf("jalview.version") == -1)
390               {
391                 continue;
392               }
393
394               line = line.substring(line.indexOf("value=") + 7);
395               line = line.substring(0, line.lastIndexOf("\""));
396               jnlpVersion = line;
397               break;
398             }
399           } catch (Exception ex)
400           {
401             System.out
402                     .println("Non-fatal exceptions when checking version at www.jalview.org :");
403             System.out.println(ex);
404             jnlpVersion = getProperty("VERSION");
405           }
406           System.setProperty("sun.net.client.defaultConnectTimeout",
407                   orgtimeout);
408
409           setProperty("LATEST_VERSION", jnlpVersion);
410         }
411       }
412
413       VersionChecker vc = new VersionChecker();
414       vc.start();
415     }
416     else
417     {
418       if (jnlpVersion != null)
419       {
420         setProperty("LATEST_VERSION", jnlpVersion);
421       }
422       else
423       {
424         applicationProperties.remove("LATEST_VERSION");
425       }
426     }
427
428     setProperty("VERSION", codeVersion);
429
430     // LOAD USERDEFINED COLOURS
431     jalview.gui.UserDefinedColours
432             .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
433     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER",
434             false);
435   }
436
437   /**
438    * Gets Jalview application property of given key. Returns null if key not
439    * found
440    * 
441    * @param key
442    *          Name of property
443    * 
444    * @return Property value
445    */
446   public static String getProperty(String key)
447   {
448     return applicationProperties.getProperty(key);
449   }
450
451   /**
452    * These methods are used when checking if the saved preference is different
453    * to the default setting
454    */
455
456   public static boolean getDefault(String property, boolean def)
457   {
458     String string = getProperty(property);
459     if (string != null)
460     {
461       def = Boolean.valueOf(string).booleanValue();
462     }
463
464     return def;
465   }
466
467   /**
468    * These methods are used when checking if the saved preference is different
469    * to the default setting
470    */
471   public static String getDefault(String property, String def)
472   {
473     String string = getProperty(property);
474     if (string != null)
475     {
476       return string;
477     }
478
479     return def;
480   }
481
482   /**
483    * Stores property in the file "HOME_DIR/.jalview_properties"
484    * 
485    * @param key
486    *          Name of object
487    * @param obj
488    *          String value of property
489    * 
490    * @return String value of property
491    */
492   public static String setProperty(String key, String obj)
493   {
494
495     try
496     {
497       applicationProperties.setProperty(key, obj);
498       if (!propsAreReadOnly)
499       {
500         FileOutputStream out = new FileOutputStream(propertiesFile);
501         applicationProperties.store(out, "---JalviewX Properties File---");
502         out.close();
503       }
504     } catch (Exception ex)
505     {
506       System.out.println("Error setting property: " + key + " " + obj
507               + "\n" + ex);
508     }
509     return obj;
510   }
511
512   /**
513    * remove the specified property from the jalview properties file
514    * 
515    * @param string
516    */
517   public static void removeProperty(String string)
518   {
519     applicationProperties.remove(string);
520     saveProperties();
521   }
522
523   /**
524    * save the properties to the jalview properties path
525    */
526   public static void saveProperties()
527   {
528     if (!propsAreReadOnly)
529     {
530       try
531       {
532         FileOutputStream out = new FileOutputStream(propertiesFile);
533         applicationProperties.store(out, "---JalviewX Properties File---");
534         out.close();
535       } catch (Exception ex)
536       {
537         System.out.println("Error saving properties: " + ex);
538       }
539     }
540   }
541
542   /**
543    * internal vamsas class discovery state
544    */
545   private static int vamsasJarsArePresent = -1;
546
547   /**
548    * Searches for vamsas client classes on class path.
549    * 
550    * @return true if vamsas client is present on classpath
551    */
552   public static boolean vamsasJarsPresent()
553   {
554     if (vamsasJarsArePresent == -1)
555     {
556       try
557       {
558         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
559                 "uk.ac.vamsas.client.VorbaId") != null)
560         {
561           jalview.bin.Cache.log
562                   .debug("Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
563           vamsasJarsArePresent = 1;
564           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
565           lvclient.setLevel(Level.toLevel(Cache.getDefault(
566                   "logs.Vamsas.Level", Level.INFO.toString())));
567
568           lvclient.addAppender(log.getAppender("JalviewLogger"));
569           // Tell the user that debug is enabled
570           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
571         }
572       } catch (Exception e)
573       {
574         vamsasJarsArePresent = 0;
575         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
576       }
577     }
578     return (vamsasJarsArePresent > 0);
579   }
580
581   /**
582    * internal vamsas class discovery state
583    */
584   private static int groovyJarsArePresent = -1;
585
586   /**
587    * Searches for vamsas client classes on class path.
588    * 
589    * @return true if vamsas client is present on classpath
590    */
591   public static boolean groovyJarsPresent()
592   {
593     if (groovyJarsArePresent == -1)
594     {
595       try
596       {
597         if (Cache.class.getClassLoader().loadClass(
598                 "groovy.lang.GroovyObject") != null)
599         {
600           jalview.bin.Cache.log
601                   .debug("Found Groovy (groovy.lang.GroovyObject can be loaded)");
602           groovyJarsArePresent = 1;
603           Logger lgclient = Logger.getLogger("groovy");
604           lgclient.setLevel(Level.toLevel(Cache.getDefault(
605                   "logs.Groovy.Level", Level.INFO.toString())));
606
607           lgclient.addAppender(log.getAppender("JalviewLogger"));
608           // Tell the user that debug is enabled
609           lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
610         }
611       } catch (Error e)
612       {
613         groovyJarsArePresent = 0;
614         jalview.bin.Cache.log.debug("Groovy Classes are not present", e);
615       } catch (Exception e)
616       {
617         groovyJarsArePresent = 0;
618         jalview.bin.Cache.log.debug("Groovy Classes are not present");
619       }
620     }
621     return (groovyJarsArePresent > 0);
622   }
623
624   /**
625    * GA tracker object - actually JGoogleAnalyticsTracker null if tracking not
626    * enabled.
627    */
628   protected static Object tracker = null;
629
630   protected static Class trackerfocus = null;
631
632   protected static Class jgoogleanalyticstracker = null;
633
634   /**
635    * Initialise the google tracker if it is not done already.
636    */
637   public static void initGoogleTracker()
638   {
639     if (tracker == null)
640     {
641       if (jgoogleanalyticstracker == null)
642       {
643         // try to get the tracker class
644         try
645         {
646           jgoogleanalyticstracker = Cache.class
647                   .getClassLoader()
648                   .loadClass(
649                           "com.boxysystems.jgoogleanalytics.JGoogleAnalyticsTracker");
650           trackerfocus = Cache.class.getClassLoader().loadClass(
651                   "com.boxysystems.jgoogleanalytics.FocusPoint");
652         } catch (Exception e)
653         {
654           log.debug("com.boxysystems.jgoogleanalytics package is not present - tracking not enabled.");
655           tracker = null;
656           jgoogleanalyticstracker = null;
657           trackerfocus = null;
658           return;
659         }
660       }
661       // now initialise tracker
662       Exception re = null, ex = null;
663       Error err = null;
664       String vrs = "No Version Accessible";
665       try
666       {
667         // Google analytics tracking code for Library Finder
668         tracker = jgoogleanalyticstracker.getConstructor(new Class[]
669         { String.class, String.class, String.class }).newInstance(
670                 new Object[]
671                 {
672                     "Jalview Desktop",
673                     (vrs = jalview.bin.Cache.getProperty("VERSION")
674                             + "_"
675                             + jalview.bin.Cache.getDefault("BUILD_DATE",
676                                     "unknown")), "UA-9060947-1" });
677         jgoogleanalyticstracker.getMethod("trackAsynchronously",
678                 new Class[]
679                 { trackerfocus }).invoke(tracker, new Object[]
680         { trackerfocus.getConstructor(new Class[]
681         { String.class }).newInstance(new Object[]
682         { "Application Started." }) });
683       } catch (RuntimeException e)
684       {
685         re = e;
686       } catch (Exception e)
687       {
688         ex = e;
689       } catch (Error e)
690       {
691         err = e;
692       }
693       if (re != null || ex != null || err != null)
694       {
695         if (log != null)
696         {
697           if (re != null)
698             log.debug("Caught runtime exception in googletracker init:", re);
699           if (ex != null)
700             log.warn(
701                     "Failed to initialise GoogleTracker for Jalview Desktop with version "
702                             + vrs, ex);
703           if (err != null)
704             log.error(
705                     "Whilst initing GoogleTracker for Jalview Desktop version "
706                             + vrs, err);
707         }
708         else
709         {
710           if (re != null)
711           {
712             System.err
713                     .println("Debug: Caught runtime exception in googletracker init:"
714                             + vrs);
715             re.printStackTrace();
716           }
717           if (ex != null)
718           {
719             System.err
720                     .println("Warning:  Failed to initialise GoogleTracker for Jalview Desktop with version "
721                             + vrs);
722             ex.printStackTrace();
723           }
724
725           if (err != null)
726           {
727             System.err
728                     .println("ERROR: Whilst initing GoogleTracker for Jalview Desktop version "
729                             + vrs);
730             err.printStackTrace();
731           }
732         }
733       }
734       else
735       {
736         log.debug("Successfully initialised tracker.");
737       }
738     }
739   }
740
741   /**
742    * get the user's default colour if available
743    * 
744    * @param property
745    * @param defcolour
746    * @return
747    */
748   public static Color getDefaultColour(String property, Color defcolour)
749   {
750     String colprop = getProperty(property);
751     if (colprop == null)
752     {
753       return defcolour;
754     }
755     Color col = jalview.schemes.ColourSchemeProperty
756             .getAWTColorFromName(colprop);
757     if (col == null)
758     {
759       try
760       {
761         col = new jalview.schemes.UserColourScheme(colprop).findColour('A');
762       } catch (Exception ex)
763       {
764         log.warn("Couldn't parse '" + colprop + "' as a colour for "
765                 + property);
766         col = null;
767       }
768     }
769     return (col == null) ? defcolour : col;
770   }
771
772   /**
773    * store a colour as a Jalview user default property
774    * 
775    * @param property
776    * @param colour
777    */
778   public static void setColourProperty(String property, Color colour)
779   {
780     setProperty(property, jalview.util.Format.getHexString(colour));
781   }
782
783   public static final DateFormat date_format = SimpleDateFormat
784           .getDateTimeInstance();
785
786   /**
787    * store a date in a jalview property
788    * 
789    * @param string
790    * @param time
791    */
792   public static void setDateProperty(String property, Date time)
793   {
794     setProperty(property, date_format.format(time));
795   }
796
797   /**
798    * read a date stored in a jalview property
799    * 
800    * @param property
801    * @return valid date as stored by setDateProperty, or null
802    * 
803    */
804   public static Date getDateProperty(String property)
805   {
806     String val = getProperty(property);
807     if (val != null)
808     {
809       try
810       {
811         return date_format.parse(val);
812       } catch (Exception ex)
813       {
814         System.err.println("Invalid or corrupt date in property '"
815                 + property + "' : value was '" + val + "'");
816       }
817     }
818     return null;
819   }
820
821   /**
822    * get and parse a property as an integer. send any parsing problems to
823    * System.err
824    * 
825    * @param property
826    * @return null or Integer
827    */
828   public static Integer getIntegerProperty(String property)
829   {
830     String val = getProperty(property);
831     if (val != null && (val = val.trim()).length() > 0)
832     {
833       try
834       {
835         return Integer.valueOf(val);
836       } catch (NumberFormatException x)
837       {
838         System.err.println("Invalid integer in property '" + property
839                 + "' (value was '" + val + "')");
840       }
841     }
842     return null;
843   }
844
845   private static DasSourceRegistryI sourceRegistry = null;
846
847   /**
848    * initialise and ..
849    * 
850    * @return instance of the das source registry
851    */
852   public static DasSourceRegistryI getDasSourceRegistry()
853   {
854     if (sourceRegistry == null)
855     {
856       sourceRegistry = new DasSourceRegistry();
857     }
858     return sourceRegistry;
859   }
860 }