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