view settings in view submenu and user preferences to control display of non-position...
[jalview.git] / src / jalview / bin / Cache.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.bin;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.apache.log4j.*;
25
26 /**
27  * Stores and retrieves Jalview Application Properties Lists and fields within
28  * list entries are separated by '|' symbols unless otherwise stated (|) clauses
29  * are alternative values for a tag. <br>
30  * <br>
31  * Current properties include:
32  * <ul>
33  * <br>
34  * logs.Axis.Level - one of the stringified Levels for log4j controlling the
35  * logging level for axis (used for web services) <br>
36  * </li>
37  * <li>logs.Castor.Level - one of the stringified Levels for log4j controlling
38  * the logging level for castor (used for serialization) <br>
39  * </li>
40  * <li>logs.Jalview.Level - Cache.log stringified level. <br>
41  * </li>
42  * <li>DISCOVERY_START - Boolean - controls if discovery services are queried
43  * on startup </li>
44  * <li>DISCOVERY_URLS - comma separated list of Discovery Service endpoints.
45  * </li>
46  * <li>SCREEN_WIDTH </li>
47  * <li>SCREEN_HEIGHT </li>
48  * <li>SCREEN_Y=285 </li>
49  * <li>SCREEN_X=371 </li>
50  * <li>SHOW_FULLSCREEN boolean </li>
51  * <li>FONT_NAME java font name for alignment text display </li>
52  * <li>FONT_SIZE size of displayed alignment text </li>
53  * <li>FONT_STYLE style of font displayed (sequence labels are always italic)
54  * </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>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
91  * </li>
92  * <li>PAD_GAPS boolean </li>
93  * <li>ID_ITALICS boolean </li>
94  * <li>SHOW_JV_SUFFIX </li>
95  * <li>WRAP_ALIGNMENT </li>
96  * <li>EPS_RENDERING (Prompt each time|Lineart|Text) default for EPS rendering
97  * style check </li>
98  * <li>SORT_ALIGNMENT (No sort|Id|Pairwise Identity) </li>
99  * <li>SEQUENCE_LINKS list of name|URL pairs for opening a url with
100  * $SEQUENCE_ID$ </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>SHOW_NPFEATS_TOOLTIP (true) show non-positional features in the Sequence ID tooltip</li>
107  * <li>SHOW_DBREFS_TOOLTIP (true) show Database Cross References in the Sequence ID tooltip</li>
108  * <li> </li>
109  * 
110  * </ul>
111  * 
112  * @author $author$
113  * @version $Revision$
114  */
115 public class Cache
116 {
117   /**
118    * property giving log4j level for CASTOR loggers
119    */
120   public static final String CASTORLOGLEVEL = "logs.Castor.level";
121
122   /**
123    * property giving log4j level for AXIS loggers
124    */
125   public static final String AXISLOGLEVEL = "logs.Axis.level";
126
127   /**
128    * property giving log4j level for Jalview Log
129    */
130   public static final String JALVIEWLOGLEVEL = "logs.Jalview.level";
131
132   public static final String DAS_LOCAL_SOURCE = "DAS_LOCAL_SOURCE";
133
134   /**
135    * Initialises the Jalview Application Log
136    */
137   public static Logger log;
138
139   /** Jalview Properties */
140   public static Properties applicationProperties = new Properties();
141
142   /** Default file is ~/.jalview_properties */
143   static String propertiesFile;
144
145   public static void initLogger()
146   {
147     try
148     {
149       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
150               "System.err");
151       ap.setName("JalviewLogger");
152       org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for
153                                                                 // log output
154       Logger laxis = Logger.getLogger("org.apache.axis");
155       Logger lcastor = Logger.getLogger("org.exolab.castor");
156       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
157
158       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
159               Level.INFO.toString())));
160       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
161               Level.INFO.toString())));
162       lcastor = Logger.getLogger("org.exolab.castor.xml");
163       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
164               Level.INFO.toString())));
165       // lcastor = Logger.getLogger("org.exolab.castor.xml.Marshaller");
166       // lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
167       // Level.INFO.toString())));
168       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
169               "logs.Jalview.level", Level.INFO.toString())));
170       // laxis.addAppender(ap);
171       // lcastor.addAppender(ap);
172       // jalview.bin.Cache.log.addAppender(ap);
173       // Tell the user that debug is enabled
174       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
175     } catch (Exception ex)
176     {
177       System.err.println("Problems initializing the log4j system\n");
178       ex.printStackTrace(System.err);
179     }
180   }
181
182   /** Called when Jalview is started */
183   public static void loadProperties(String propsFile)
184   {
185     propertiesFile = propsFile;
186     if (propsFile == null)
187     {
188       propertiesFile = System.getProperty("user.home") + File.separatorChar
189               + ".jalview_properties";
190     }
191
192     try
193     {
194       FileInputStream fis = new FileInputStream(propertiesFile);
195       applicationProperties.load(fis);
196       applicationProperties.remove("LATEST_VERSION");
197       applicationProperties.remove("VERSION");
198       fis.close();
199     } catch (Exception ex)
200     {
201       System.out.println("Error reading properties file: " + ex);
202     }
203
204     if (getDefault("USE_PROXY", false))
205     {
206       System.out.println("Using proxyServer: "
207               + getDefault("PROXY_SERVER", null) + " proxyPort: "
208               + getDefault("PROXY_PORT", null));
209       System
210               .setProperty("http.proxyHost", getDefault("PROXY_SERVER",
211                       null));
212       System.setProperty("http.proxyPort", getDefault("PROXY_PORT", null));
213     }
214
215     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
216     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
217     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
218     try
219     {
220       String buildDetails = "jar:".concat(Cache.class.getProtectionDomain()
221               .getCodeSource().getLocation().toString().concat(
222                       "!/.build_properties"));
223
224       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
225
226       InputStream in = localJarFileURL.openStream();
227       applicationProperties.load(in);
228       in.close();
229     } catch (Exception ex)
230     {
231       System.out.println("Error reading build details: " + ex);
232       applicationProperties.remove("VERSION");
233     }
234
235     String jnlpVersion = System.getProperty("jalview.version");
236     String codeVersion = getProperty("VERSION");
237
238     if (codeVersion == null)
239     {
240       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
241       codeVersion = "Test";
242       jnlpVersion = "Test";
243     }
244
245     System.out.println("Jalview Version: " + codeVersion);
246
247     // jnlpVersion will be null if we're using InstallAnywhere
248     // Dont do this check if running in headless mode
249     if (jnlpVersion == null
250             && (System.getProperty("java.awt.headless") == null || System
251                     .getProperty("java.awt.headless").equals("false")))
252     {
253
254       class VersionChecker extends Thread
255       {
256         public void run()
257         {
258           String orgtimeout = System
259                   .getProperty("sun.net.client.defaultConnectTimeout");
260           if (orgtimeout == null)
261           {
262             orgtimeout = "30";
263             System.out.println("# INFO: Setting default net timeout to "
264                     + orgtimeout + " seconds.");
265           }
266           String jnlpVersion = null;
267           try
268           {
269             System.setProperty("sun.net.client.defaultConnectTimeout",
270                     "5000");
271             java.net.URL url = new java.net.URL(
272                     "http://www.jalview.org/webstart/jalview.jnlp");
273             BufferedReader in = new BufferedReader(new InputStreamReader(
274                     url.openStream()));
275             String line = null;
276             while ((line = in.readLine()) != null)
277             {
278               if (line.indexOf("jalview.version") == -1)
279               {
280                 continue;
281               }
282
283               line = line.substring(line.indexOf("value=") + 7);
284               line = line.substring(0, line.lastIndexOf("\""));
285               jnlpVersion = line;
286               break;
287             }
288           } catch (Exception ex)
289           {
290             System.out
291                     .println("Non-fatal exceptions when checking version at www.jalview.org :");
292             System.out.println(ex);
293             jnlpVersion = getProperty("VERSION");
294           }
295           System.setProperty("sun.net.client.defaultConnectTimeout",
296                   orgtimeout);
297
298           setProperty("LATEST_VERSION", jnlpVersion);
299         }
300       }
301
302       VersionChecker vc = new VersionChecker();
303       vc.start();
304     }
305     else
306     {
307       if (jnlpVersion != null)
308       {
309         setProperty("LATEST_VERSION", jnlpVersion);
310       }
311       else
312       {
313         applicationProperties.remove("LATEST_VERSION");
314       }
315     }
316
317     setProperty("VERSION", codeVersion);
318
319     // LOAD USERDEFINED COLOURS
320     jalview.gui.UserDefinedColours
321             .initUserColourSchemes(getProperty("USER_DEFINED_COLOURS"));
322     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER",
323             false);
324   }
325
326   /**
327    * Gets Jalview application property of given key. Returns null if key not
328    * found
329    * 
330    * @param key
331    *                Name of property
332    * 
333    * @return Property value
334    */
335   public static String getProperty(String key)
336   {
337     return applicationProperties.getProperty(key);
338   }
339
340   /**
341    * These methods are used when checking if the saved preference is different
342    * to the default setting
343    */
344
345   public static boolean getDefault(String property, boolean def)
346   {
347     String string = getProperty(property);
348     if (string != null)
349     {
350       def = Boolean.valueOf(string).booleanValue();
351     }
352
353     return def;
354   }
355
356   /**
357    * These methods are used when checking if the saved preference is different
358    * to the default setting
359    */
360   public static String getDefault(String property, String def)
361   {
362     String string = getProperty(property);
363     if (string != null)
364     {
365       return string;
366     }
367
368     return def;
369   }
370
371   /**
372    * Stores property in the file "HOME_DIR/.jalview_properties"
373    * 
374    * @param key
375    *                Name of object
376    * @param obj
377    *                String value of property
378    * 
379    * @return String value of property
380    */
381   public static String setProperty(String key, String obj)
382   {
383     try
384     {
385       FileOutputStream out = new FileOutputStream(propertiesFile);
386       applicationProperties.setProperty(key, obj);
387       applicationProperties.store(out, "---JalviewX Properties File---");
388       out.close();
389     } catch (Exception ex)
390     {
391       System.out.println("Error setting property: " + key + " " + obj
392               + "\n" + ex);
393     }
394     return obj;
395   }
396
397   public static void saveProperties()
398   {
399     try
400     {
401       FileOutputStream out = new FileOutputStream(propertiesFile);
402       applicationProperties.store(out, "---JalviewX Properties File---");
403       out.close();
404     } catch (Exception ex)
405     {
406       System.out.println("Error saving properties: " + ex);
407     }
408   }
409
410   /**
411    * internal vamsas class discovery state
412    */
413   private static int vamsasJarsArePresent = -1;
414
415   /**
416    * Searches for vamsas client classes on class path.
417    * 
418    * @return true if vamsas client is present on classpath
419    */
420   public static boolean vamsasJarsPresent()
421   {
422     if (vamsasJarsArePresent == -1)
423     {
424       try
425       {
426         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
427                 "uk.ac.vamsas.client.VorbaId") != null)
428         {
429           jalview.bin.Cache.log
430                   .debug("Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
431           vamsasJarsArePresent = 1;
432           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
433           lvclient.setLevel(Level.toLevel(Cache.getDefault(
434                   "logs.Vamsas.Level", Level.INFO.toString())));
435
436           lvclient.addAppender(log.getAppender("JalviewLogger"));
437           // Tell the user that debug is enabled
438           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
439         }
440       } catch (Exception e)
441       {
442         vamsasJarsArePresent = 0;
443         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
444       }
445     }
446     return (vamsasJarsArePresent > 0);
447   }
448
449   /**
450    * internal vamsas class discovery state
451    */
452   private static int groovyJarsArePresent = -1;
453
454   /**
455    * Searches for vamsas client classes on class path.
456    * 
457    * @return true if vamsas client is present on classpath
458    */
459   public static boolean groovyJarsPresent()
460   {
461     if (groovyJarsArePresent == -1)
462     {
463       try
464       {
465         if (Cache.class.getClassLoader().loadClass(
466                 "groovy.lang.GroovyObject") != null)
467         {
468           jalview.bin.Cache.log
469                   .debug("Found Groovy (groovy.lang.GroovyObject can be loaded)");
470           groovyJarsArePresent = 1;
471           Logger lgclient = Logger.getLogger("groovy");
472           lgclient.setLevel(Level.toLevel(Cache.getDefault(
473                   "logs.Groovy.Level", Level.INFO.toString())));
474
475           lgclient.addAppender(log.getAppender("JalviewLogger"));
476           // Tell the user that debug is enabled
477           lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
478         }
479       } catch (Exception e)
480       {
481         groovyJarsArePresent = 0;
482         jalview.bin.Cache.log.debug("Groovy Classes are not present");
483       }
484     }
485     return (groovyJarsArePresent > 0);
486   }
487
488 }