43619f8dc4a3d7970085805864e30dba04563ad6
[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   private static boolean plugin=false;
204   public static void setPlugin(boolean plugin)
205   {
206     Cache.plugin=plugin;
207   }
208   public static boolean isPlugin()
209   {
210     return plugin;
211   }
212   public static void initLogger()
213   {
214     if (log != null)
215     {
216       return;
217     }
218     try
219     {
220       // TODO: redirect stdout and stderr here in order to grab the output of
221       // the log
222
223       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
224               "System.err");
225       ap.setName("JalviewLogger");
226       org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for
227       // log output
228       Logger laxis = Logger.getLogger("org.apache.axis");
229       Logger lcastor = Logger.getLogger("org.exolab.castor");
230       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
231
232       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
233               Level.INFO.toString())));
234       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
235               Level.INFO.toString())));
236       lcastor = Logger.getLogger("org.exolab.castor.xml");
237       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
238               Level.INFO.toString())));
239       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
240       // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
241       // Level.INFO.toString())));
242       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
243               "logs.Jalview.level", Level.INFO.toString())));
244       // laxis.addAppender(ap);
245       // lcastor.addAppender(ap);
246       // jalview.bin.Cache.log.addAppender(ap);
247       // Tell the user that debug is enabled
248       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
249     } catch (Exception ex)
250     {
251       System.err.println("Problems initializing the log4j system\n");
252       ex.printStackTrace(System.err);
253     }
254   }
255
256   /** Called when Jalview is started */
257   public static void loadProperties(String propsFile)
258   {
259     propertiesFile = propsFile;
260     if (propsFile == null)
261     {
262       propertiesFile = System.getProperty("user.home") + File.separatorChar
263               + ".jalview_properties";
264     }
265     else
266     {
267       // don't corrupt the file we've been given.
268       propsAreReadOnly = true;
269     }
270
271     try
272     {
273       InputStream fis;
274       try
275       {
276         fis = new java.net.URL(propertiesFile).openStream();
277         System.out.println("Loading jalview properties from : "
278                 + propertiesFile);
279         System.out
280                 .println("Disabling Jalview writing to user's local properties file.");
281         propsAreReadOnly = true;
282
283       } catch (Exception ex)
284       {
285         fis = null;
286       }
287       if (fis == null)
288       {
289         fis = new FileInputStream(propertiesFile);
290       }
291       applicationProperties.load(fis);
292       applicationProperties.remove("LATEST_VERSION");
293       applicationProperties.remove("VERSION");
294       fis.close();
295     } catch (Exception ex)
296     {
297       System.out.println("Error reading properties file: " + ex);
298     }
299
300     if (getDefault("USE_PROXY", false))
301     {
302       String proxyServer = getDefault("PROXY_SERVER", ""), proxyPort = getDefault(
303               "PROXY_PORT", "8080");
304
305       System.out.println("Using proxyServer: " + proxyServer
306               + " proxyPort: " + proxyPort);
307
308       System.setProperty("http.proxyHost", proxyServer);
309       System.setProperty("http.proxyPort", proxyPort);
310     }
311
312     // LOAD THE AUTHORS FROM THE authors.props file
313     try
314     {
315       String authorDetails = "jar:".concat(Cache.class
316               .getProtectionDomain().getCodeSource().getLocation()
317               .toString().concat("!/authors.props"));
318
319       java.net.URL localJarFileURL = new java.net.URL(authorDetails);
320
321       InputStream in = localJarFileURL.openStream();
322       applicationProperties.load(in);
323       in.close();
324     } catch (Exception ex)
325     {
326       System.out.println("Error reading author details: " + ex);
327       applicationProperties.remove("AUTHORS");
328       applicationProperties.remove("AUTHORFNAMES");
329       applicationProperties.remove("YEAR");
330     }
331
332     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
333     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
334     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
335     try
336     {
337       String buildDetails = "jar:".concat(Cache.class.getProtectionDomain()
338               .getCodeSource().getLocation().toString()
339               .concat("!/.build_properties"));
340
341       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
342
343       InputStream in = localJarFileURL.openStream();
344       applicationProperties.load(in);
345       in.close();
346     } catch (Exception ex)
347     {
348       System.out.println("Error reading build details: " + ex);
349       applicationProperties.remove("VERSION");
350     }
351
352     String jnlpVersion = System.getProperty("jalview.version");
353     String codeVersion = getProperty("VERSION");
354
355     if (codeVersion == null)
356     {
357       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
358       codeVersion = "Test";
359       jnlpVersion = "Test";
360     }
361
362     System.out.println("Jalview Version: " + codeVersion);
363
364     // jnlpVersion will be null if we're using InstallAnywhere
365     // Dont do this check if running in headless mode
366     if (jnlpVersion == null
367             && getDefault("VERSION_CHECK", true)
368             && (System.getProperty("java.awt.headless") == null || System
369                     .getProperty("java.awt.headless").equals("false")))
370     {
371
372       class VersionChecker extends Thread
373       {
374         public void run()
375         {
376           String orgtimeout = System
377                   .getProperty("sun.net.client.defaultConnectTimeout");
378           if (orgtimeout == null)
379           {
380             orgtimeout = "30";
381             System.out.println("# INFO: Setting default net timeout to "
382                     + orgtimeout + " seconds.");
383           }
384           String jnlpVersion = null;
385           try
386           {
387             System.setProperty("sun.net.client.defaultConnectTimeout",
388                     "5000");
389             java.net.URL url = new java.net.URL(Cache.getDefault(
390                     "www.jalview.org", "http://www.jalview.org")
391                     + "/webstart/jalview.jnlp");
392             BufferedReader in = new BufferedReader(new InputStreamReader(
393                     url.openStream()));
394             String line = null;
395             while ((line = in.readLine()) != null)
396             {
397               if (line.indexOf("jalview.version") == -1)
398               {
399                 continue;
400               }
401
402               line = line.substring(line.indexOf("value=") + 7);
403               line = line.substring(0, line.lastIndexOf("\""));
404               jnlpVersion = line;
405               break;
406             }
407           } catch (Exception ex)
408           {
409             System.out
410                     .println("Non-fatal exceptions when checking version at www.jalview.org :");
411             System.out.println(ex);
412             jnlpVersion = getProperty("VERSION");
413           }
414           System.setProperty("sun.net.client.defaultConnectTimeout",
415                   orgtimeout);
416
417           setProperty("LATEST_VERSION", jnlpVersion);
418         }
419       }
420
421       VersionChecker vc = new VersionChecker();
422       vc.start();
423     }
424     else
425     {
426       if (jnlpVersion != null)
427       {
428         setProperty("LATEST_VERSION", jnlpVersion);
429       }
430       else
431       {
432         applicationProperties.remove("LATEST_VERSION");
433       }
434     }
435
436     setProperty("VERSION", codeVersion);
437
438     // LOAD USERDEFINED COLOURS
439     jalview.gui.UserDefinedColours
440             .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
441     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER",
442             false);
443   }
444
445   /**
446    * Gets Jalview application property of given key. Returns null if key not
447    * found
448    * 
449    * @param key
450    *          Name of property
451    * 
452    * @return Property value
453    */
454   public static String getProperty(String key)
455   {
456     return applicationProperties.getProperty(key);
457   }
458
459   /**
460    * These methods are used when checking if the saved preference is different
461    * to the default setting
462    */
463
464   public static boolean getDefault(String property, boolean def)
465   {
466     String string = getProperty(property);
467     if (string != null)
468     {
469       def = Boolean.valueOf(string).booleanValue();
470     }
471
472     return def;
473   }
474
475   /**
476    * These methods are used when checking if the saved preference is different
477    * to the default setting
478    */
479   public static String getDefault(String property, String def)
480   {
481     String string = getProperty(property);
482     if (string != null)
483     {
484       return string;
485     }
486
487     return def;
488   }
489
490   /**
491    * Stores property in the file "HOME_DIR/.jalview_properties"
492    * 
493    * @param key
494    *          Name of object
495    * @param obj
496    *          String value of property
497    * 
498    * @return String value of property
499    */
500   public static String setProperty(String key, String obj)
501   {
502
503     try
504     {
505       applicationProperties.setProperty(key, obj);
506       if (!propsAreReadOnly)
507       {
508         FileOutputStream out = new FileOutputStream(propertiesFile);
509         applicationProperties.store(out, "---JalviewX Properties File---");
510         out.close();
511       }
512     } catch (Exception ex)
513     {
514       System.out.println("Error setting property: " + key + " " + obj
515               + "\n" + ex);
516     }
517     return obj;
518   }
519
520   /**
521    * remove the specified property from the jalview properties file
522    * 
523    * @param string
524    */
525   public static void removeProperty(String string)
526   {
527     applicationProperties.remove(string);
528     saveProperties();
529   }
530
531   /**
532    * save the properties to the jalview properties path
533    */
534   public static void saveProperties()
535   {
536     if (!propsAreReadOnly)
537     {
538       try
539       {
540         FileOutputStream out = new FileOutputStream(propertiesFile);
541         applicationProperties.store(out, "---JalviewX Properties File---");
542         out.close();
543       } catch (Exception ex)
544       {
545         System.out.println("Error saving properties: " + ex);
546       }
547     }
548   }
549
550   /**
551    * internal vamsas class discovery state
552    */
553   private static int vamsasJarsArePresent = -1;
554
555   /**
556    * Searches for vamsas client classes on class path.
557    * 
558    * @return true if vamsas client is present on classpath
559    */
560   public static boolean vamsasJarsPresent()
561   {
562     if (vamsasJarsArePresent == -1)
563     {
564       try
565       {
566         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
567                 "uk.ac.vamsas.client.VorbaId") != null)
568         {
569           jalview.bin.Cache.log
570                   .debug("Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
571           vamsasJarsArePresent = 1;
572           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
573           lvclient.setLevel(Level.toLevel(Cache.getDefault(
574                   "logs.Vamsas.Level", Level.INFO.toString())));
575
576           lvclient.addAppender(log.getAppender("JalviewLogger"));
577           // Tell the user that debug is enabled
578           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
579         }
580       } catch (Exception e)
581       {
582         vamsasJarsArePresent = 0;
583         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
584       }
585     }
586     return (vamsasJarsArePresent > 0);
587   }
588
589   /**
590    * internal vamsas class discovery state
591    */
592   private static int groovyJarsArePresent = -1;
593
594   /**
595    * Searches for vamsas client classes on class path.
596    * 
597    * @return true if vamsas client is present on classpath
598    */
599   public static boolean groovyJarsPresent()
600   {
601     if (groovyJarsArePresent == -1)
602     {
603       try
604       {
605         if (Cache.class.getClassLoader().loadClass(
606                 "groovy.lang.GroovyObject") != null)
607         {
608           jalview.bin.Cache.log
609                   .debug("Found Groovy (groovy.lang.GroovyObject can be loaded)");
610           groovyJarsArePresent = 1;
611           Logger lgclient = Logger.getLogger("groovy");
612           lgclient.setLevel(Level.toLevel(Cache.getDefault(
613                   "logs.Groovy.Level", Level.INFO.toString())));
614
615           lgclient.addAppender(log.getAppender("JalviewLogger"));
616           // Tell the user that debug is enabled
617           lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
618         }
619       } catch (Error e)
620       {
621         groovyJarsArePresent = 0;
622         jalview.bin.Cache.log.debug("Groovy Classes are not present", e);
623       } catch (Exception e)
624       {
625         groovyJarsArePresent = 0;
626         jalview.bin.Cache.log.debug("Groovy Classes are not present");
627       }
628     }
629     return (groovyJarsArePresent > 0);
630   }
631
632   /**
633    * GA tracker object - actually JGoogleAnalyticsTracker null if tracking not
634    * enabled.
635    */
636   protected static Object tracker = null;
637
638   protected static Class trackerfocus = null;
639
640   protected static Class jgoogleanalyticstracker = null;
641
642   /**
643    * Initialise the google tracker if it is not done already.
644    */
645   public static void initGoogleTracker()
646   {
647     if (tracker == null)
648     {
649       if (jgoogleanalyticstracker == null)
650       {
651         // try to get the tracker class
652         try
653         {
654           jgoogleanalyticstracker = Cache.class
655                   .getClassLoader()
656                   .loadClass(
657                           "com.boxysystems.jgoogleanalytics.JGoogleAnalyticsTracker");
658           trackerfocus = Cache.class.getClassLoader().loadClass(
659                   "com.boxysystems.jgoogleanalytics.FocusPoint");
660         } catch (Exception e)
661         {
662           log.debug("com.boxysystems.jgoogleanalytics package is not present - tracking not enabled.");
663           tracker = null;
664           jgoogleanalyticstracker = null;
665           trackerfocus = null;
666           return;
667         }
668       }
669       // now initialise tracker
670       Exception re = null, ex = null;
671       Error err = null;
672       String vrs = "No Version Accessible";
673       try
674       {
675         // Google analytics tracking code for Library Finder
676         tracker = jgoogleanalyticstracker.getConstructor(new Class[]
677         { String.class, String.class, String.class }).newInstance(
678                 new Object[]
679                 {
680                     "Jalview Desktop",
681                     (vrs = jalview.bin.Cache.getProperty("VERSION")
682                             + "_"
683                             + jalview.bin.Cache.getDefault("BUILD_DATE",
684                                     "unknown")), "UA-9060947-1" });
685         jgoogleanalyticstracker.getMethod("trackAsynchronously",
686                 new Class[]
687                 { trackerfocus }).invoke(tracker, new Object[]
688         { trackerfocus.getConstructor(new Class[]
689         { String.class }).newInstance(new Object[]
690         { "Application Started." }) });
691       } catch (RuntimeException e)
692       {
693         re = e;
694       } catch (Exception e)
695       {
696         ex = e;
697       } catch (Error e)
698       {
699         err = e;
700       }
701       if (re != null || ex != null || err != null)
702       {
703         if (log != null)
704         {
705           if (re != null)
706             log.debug("Caught runtime exception in googletracker init:", re);
707           if (ex != null)
708             log.warn(
709                     "Failed to initialise GoogleTracker for Jalview Desktop with version "
710                             + vrs, ex);
711           if (err != null)
712             log.error(
713                     "Whilst initing GoogleTracker for Jalview Desktop version "
714                             + vrs, err);
715         }
716         else
717         {
718           if (re != null)
719           {
720             System.err
721                     .println("Debug: Caught runtime exception in googletracker init:"
722                             + vrs);
723             re.printStackTrace();
724           }
725           if (ex != null)
726           {
727             System.err
728                     .println("Warning:  Failed to initialise GoogleTracker for Jalview Desktop with version "
729                             + vrs);
730             ex.printStackTrace();
731           }
732
733           if (err != null)
734           {
735             System.err
736                     .println("ERROR: Whilst initing GoogleTracker for Jalview Desktop version "
737                             + vrs);
738             err.printStackTrace();
739           }
740         }
741       }
742       else
743       {
744         log.debug("Successfully initialised tracker.");
745       }
746     }
747   }
748
749   /**
750    * get the user's default colour if available
751    * 
752    * @param property
753    * @param defcolour
754    * @return
755    */
756   public static Color getDefaultColour(String property, Color defcolour)
757   {
758     String colprop = getProperty(property);
759     if (colprop == null)
760     {
761       return defcolour;
762     }
763     Color col = jalview.schemes.ColourSchemeProperty
764             .getAWTColorFromName(colprop);
765     if (col == null)
766     {
767       try
768       {
769         col = new jalview.schemes.UserColourScheme(colprop).findColour('A');
770       } catch (Exception ex)
771       {
772         log.warn("Couldn't parse '" + colprop + "' as a colour for "
773                 + property);
774         col = null;
775       }
776     }
777     return (col == null) ? defcolour : col;
778   }
779
780   /**
781    * store a colour as a Jalview user default property
782    * 
783    * @param property
784    * @param colour
785    */
786   public static void setColourProperty(String property, Color colour)
787   {
788     setProperty(property, jalview.util.Format.getHexString(colour));
789   }
790
791   public static final DateFormat date_format = SimpleDateFormat
792           .getDateTimeInstance();
793
794   /**
795    * store a date in a jalview property
796    * 
797    * @param string
798    * @param time
799    */
800   public static void setDateProperty(String property, Date time)
801   {
802     setProperty(property, date_format.format(time));
803   }
804
805   /**
806    * read a date stored in a jalview property
807    * 
808    * @param property
809    * @return valid date as stored by setDateProperty, or null
810    * 
811    */
812   public static Date getDateProperty(String property)
813   {
814     String val = getProperty(property);
815     if (val != null)
816     {
817       try
818       {
819         return date_format.parse(val);
820       } catch (Exception ex)
821       {
822         System.err.println("Invalid or corrupt date in property '"
823                 + property + "' : value was '" + val + "'");
824       }
825     }
826     return null;
827   }
828
829   /**
830    * get and parse a property as an integer. send any parsing problems to
831    * System.err
832    * 
833    * @param property
834    * @return null or Integer
835    */
836   public static Integer getIntegerProperty(String property)
837   {
838     String val = getProperty(property);
839     if (val != null && (val = val.trim()).length() > 0)
840     {
841       try
842       {
843         return Integer.valueOf(val);
844       } catch (NumberFormatException x)
845       {
846         System.err.println("Invalid integer in property '" + property
847                 + "' (value was '" + val + "')");
848       }
849     }
850     return null;
851   }
852
853   private static DasSourceRegistryI sourceRegistry = null;
854
855   /**
856    * initialise and ..
857    * 
858    * @return instance of the das source registry
859    */
860   public static DasSourceRegistryI getDasSourceRegistry()
861   {
862     if (sourceRegistry == null)
863     {
864       sourceRegistry = new DasSourceRegistry();
865     }
866     return sourceRegistry;
867   }
868 }