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