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