loglevel property constants and castor validation logger config
[jalview.git] / src / jalview / bin / Cache.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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
28  * <br><br>Current properties include:
29  * <br>logs.Axis.Level - one of the stringified Levels for log4j controlling the logging level for axis (used for web services)
30  * <br>logs.Castor.Level - one of the stringified Levels for log4j controlling the logging level for castor (used for serialization)
31  * <br>logs.Jalview.Level - Cache.log stringified level.
32  * <br>DISCOVERY_START - Boolean - controls if discovery services are queried on startup
33  * <br>DISCOVERY_URLS - comma separated list of Discovery Service endpoints.
34  * <br>SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_Y=285,SCREEN_X=371,SHOW_FULLSCREEN
35  * FONT_NAME,FONT_SIZE,FONT_STYLE,GAP_SYMBOL,LAST_DIRECTORY,USER_DEFINED_COLOUR
36  * SHOW_FULL_ID,SHOW_IDENTITY,SHOW_QUALITY,SHOW_ANNOTATIONS,SHOW_CONSERVATION,
37  * DEFAULT_COLOUR,DEFAULT_FILE_FORMAT,STARTUP_FILE,SHOW_STARTUP_FILE
38
39  * @author $author$
40  * @version $Revision$
41  */
42 public class Cache
43 {
44   /**
45    * property giving log4j level for CASTOR loggers
46    */
47   public static final String CASTORLOGLEVEL = "logs.Castor.level";
48   /**
49    * property giving log4j level for AXIS loggers
50    */
51   public static final String AXISLOGLEVEL = "logs.Castor.level";
52   /**
53    * property giving log4j level for Jalview Log
54    */
55   public static final String JALVIEWLOGLEVEL = "logs.Jalview.level";
56
57   /**
58    * Initialises the Jalview Application Log
59    */
60   public static Logger log;
61
62   /** Jalview Properties */
63   public static Properties applicationProperties = new Properties();
64
65   /** Default file is  ~/.jalview_properties */
66   static String propertiesFile;
67
68   public static void initLogger()
69   {
70     try
71     {
72       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
73       "System.err");
74       ap.setName("JalviewLogger");
75       org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for log output
76       Logger laxis = Logger.getLogger("org.apache.axis");
77       Logger lcastor = Logger.getLogger("org.exolab.castor");
78       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
79
80       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
81           Level.INFO.toString())));
82       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
83           Level.INFO.toString())));
84       lcastor = Logger.getLogger("org.exolab.castor.xml");
85       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
86               Level.INFO.toString())));
87       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
88           "logs.Jalview.level",
89           Level.INFO.toString())));
90       //laxis.addAppender(ap);
91       //lcastor.addAppender(ap);
92       //jalview.bin.Cache.log.addAppender(ap);
93       // Tell the user that debug is enabled
94       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
95     }
96     catch (Exception ex)
97     {
98       System.err.println("Problems initializing the log4j system\n");
99       ex.printStackTrace(System.err);
100     }
101   }
102
103   /** Called when Jalview is started */
104   public static void loadProperties(String propsFile)
105   {
106     propertiesFile = propsFile;
107     if (propsFile == null)
108     {
109       propertiesFile = System.getProperty("user.home") + File.separatorChar +
110           ".jalview_properties";
111     }
112
113     try
114     {
115       FileInputStream fis = new FileInputStream(propertiesFile);
116       applicationProperties.load(fis);
117       applicationProperties.remove("LATEST_VERSION");
118       applicationProperties.remove("VERSION");
119       fis.close();
120     }
121     catch (Exception ex)
122     {
123       System.out.println("Error reading properties file: " + ex);
124     }
125
126     if (getDefault("USE_PROXY", false))
127     {
128       System.out.println("Using proxyServer: " + getDefault("PROXY_SERVER", null) +
129                          " proxyPort: " + getDefault("PROXY_PORT", null));
130       System.setProperty("http.proxyHost", getDefault("PROXY_SERVER", null));
131       System.setProperty("http.proxyPort", getDefault("PROXY_PORT", null));
132     }
133
134     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
135     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
136     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
137     try
138     {
139       String buildDetails = "jar:"
140           .concat(
141               Cache.class.getProtectionDomain().getCodeSource().getLocation().
142               toString()
143               .concat("!/.build_properties")
144           );
145
146       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
147
148       InputStream in = localJarFileURL.openStream();
149       applicationProperties.load(in);
150       in.close();
151     }
152     catch (Exception ex)
153     {
154       System.out.println("Error reading build details: " + ex);
155       applicationProperties.remove("VERSION");
156     }
157
158     String jnlpVersion = System.getProperty("jalview.version");
159     String codeVersion = getProperty("VERSION");
160
161     if (codeVersion == null)
162     {
163       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
164       codeVersion = "Test";
165       jnlpVersion = "Test";
166     }
167
168     System.out.println("Jalview Version: " + codeVersion);
169
170     // jnlpVersion will be null if we're using InstallAnywhere
171     // Dont do this check if running in headless mode
172     if (jnlpVersion == null && (
173         System.getProperty("java.awt.headless") == null
174         || System.getProperty("java.awt.headless").equals("false")))
175     {
176
177       class VersionChecker
178           extends Thread
179       {
180         public void run()
181         {
182           String jnlpVersion = null;
183           try
184           {
185             java.net.URL url = new java.net.URL(
186                 "http://www.jalview.org/webstart/jalview.jnlp");
187             BufferedReader in = new BufferedReader(new InputStreamReader(url.
188                 openStream()));
189             String line = null;
190             while ( (line = in.readLine()) != null)
191             {
192               if (line.indexOf("jalview.version") == -1)
193               {
194                 continue;
195               }
196
197               line = line.substring(line.indexOf("value=") + 7);
198               line = line.substring(0, line.lastIndexOf("\""));
199               jnlpVersion = line;
200               break;
201             }
202           }
203           catch (Exception ex)
204           {
205             System.out.println(ex);
206             jnlpVersion = getProperty("VERSION");
207           }
208
209           setProperty("LATEST_VERSION", jnlpVersion);
210         }
211       }
212
213       VersionChecker vc = new VersionChecker();
214       vc.start();
215     }
216     else
217     {
218       if (jnlpVersion != null)
219       {
220         setProperty("LATEST_VERSION", jnlpVersion);
221       }
222       else
223       {
224         applicationProperties.remove("LATEST_VERSION");
225       }
226     }
227
228     setProperty("VERSION", codeVersion);
229
230     //LOAD USERDEFINED COLOURS
231     jalview.gui.UserDefinedColours.initUserColourSchemes(getProperty(
232         "USER_DEFINED_COLOURS"));
233     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER", false);
234   }
235
236   /**
237    * Gets Jalview application property of given key. Returns null
238    * if key not found
239    *
240    * @param key Name of property
241    *
242    * @return Property value
243    */
244   public static String getProperty(String key)
245   {
246     return applicationProperties.getProperty(key);
247   }
248
249   /** These methods are used when checking if the saved preference
250    * is different to the default setting
251    */
252
253   public static boolean getDefault(String property, boolean def)
254   {
255     String string = getProperty(property);
256     if (string != null)
257     {
258       def = Boolean.valueOf(string).booleanValue();
259     }
260
261     return def;
262   }
263
264   /** These methods are used when checking if the saved preference
265    * is different to the default setting
266    */
267   public static String getDefault(String property, String def)
268   {
269     String string = getProperty(property);
270     if (string != null)
271     {
272       return string;
273     }
274
275     return def;
276   }
277
278   /**
279    * Stores property in the file "HOME_DIR/.jalview_properties"
280    *
281    * @param key Name of object
282    * @param obj String value of property
283    *
284    * @return String value of property
285    */
286   public static String setProperty(String key, String obj)
287   {
288     try
289     {
290       FileOutputStream out = new FileOutputStream(propertiesFile);
291       applicationProperties.setProperty(key, obj);
292       applicationProperties.store(out, "---JalviewX Properties File---");
293       out.close();
294     }
295     catch (Exception ex)
296     {
297       System.out.println("Error setting property: " + key + " " + obj + "\n" +
298                          ex);
299     }
300     return obj;
301   }
302
303   public static void saveProperties()
304   {
305     try
306     {
307       FileOutputStream out = new FileOutputStream(propertiesFile);
308       applicationProperties.store(out, "---JalviewX Properties File---");
309       out.close();
310     }
311     catch (Exception ex)
312     {
313       System.out.println("Error saving properties: " + ex);
314     }
315   }
316
317   /**
318    * internal vamsas class discovery state
319    */
320   private static int vamsasJarsArePresent = -1;
321   /**
322    * Searches for vamsas client classes on class path.
323    * @return true if vamsas client is present on classpath
324    */
325   public static boolean vamsasJarsPresent()
326   {
327     if (vamsasJarsArePresent == -1)
328     {
329       try
330       {
331         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
332             "uk.ac.vamsas.client.VorbaId") != null)
333         {
334           jalview.bin.Cache.log.debug(
335               "Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
336           vamsasJarsArePresent = 1;
337           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
338           lvclient.setLevel(Level.toLevel(Cache.getDefault("logs.Vamsas.Level",
339               Level.INFO.toString())));
340
341           lvclient.addAppender(log.getAppender("JalviewLogger"));
342           // Tell the user that debug is enabled
343           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
344         }
345       }
346       catch (Exception e)
347       {
348         vamsasJarsArePresent = 0;
349         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
350       }
351     }
352     return (vamsasJarsArePresent > 0);
353   }
354   /**
355    * internal vamsas class discovery state
356    */
357   private static int groovyJarsArePresent = -1;
358   /**
359    * Searches for vamsas client classes on class path.
360    * @return true if vamsas client is present on classpath
361    */
362   public static boolean groovyJarsPresent()
363   {
364     if (groovyJarsArePresent == -1)
365     {
366       try
367       {
368         if (Cache.class.getClassLoader().loadClass(
369             "groovy.lang.GroovyObject") != null)
370         {
371           jalview.bin.Cache.log.debug(
372               "Found Groovy (groovy.lang.GroovyObject can be loaded)");
373           groovyJarsArePresent = 1;
374           Logger lgclient = Logger.getLogger("groovy");
375           lgclient.setLevel(Level.toLevel(Cache.getDefault("logs.Groovy.Level",
376               Level.INFO.toString())));
377
378           lgclient.addAppender(log.getAppender("JalviewLogger"));
379           // Tell the user that debug is enabled
380           lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
381         }
382       }
383       catch (Exception e)
384       {
385         groovyJarsArePresent = 0;
386         jalview.bin.Cache.log.debug("Groovy Classes are not present");
387       }
388     }
389     return (groovyJarsArePresent > 0);
390   }
391
392 }