Formatting
[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    * Initialises the Apache Axis logger
46    */
47   public static Logger log;
48
49   /** Jalview Properties */
50   public static Properties applicationProperties = new Properties();
51
52   /** Default file is  ~/.jalview_properties */
53   static String propertiesFile;
54
55   public static void initLogger()
56   {
57     try
58     {
59       Logger laxis = Logger.getLogger("org.apache.axis");
60       Logger lcastor = Logger.getLogger("org.exolab.castor");
61       jalview.bin.Cache.log = Logger.getLogger("jalview.bin.Jalview");
62
63       laxis.setLevel(Level.toLevel(Cache.getDefault("logs.Axis.Level",
64           Level.INFO.toString())));
65       lcastor.setLevel(Level.toLevel(Cache.getDefault("logs.Castor.Level",
66           Level.INFO.toString())));
67       jalview.bin.Cache.log.setLevel(Level.toLevel(Cache.getDefault(
68           "logs.Jalview.level",
69           Level.INFO.toString())));
70       ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
71                                                "System.err");
72       ap.setName("JalviewLogger");
73
74       laxis.addAppender(ap);
75       lcastor.addAppender(ap);
76       jalview.bin.Cache.log.addAppender(ap);
77       // Tell the user that debug is enabled
78       jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
79     }
80     catch (Exception ex)
81     {
82       System.err.println("Problems initializing the log4j system\n");
83     }
84   }
85
86   /** Called when Jalview is started */
87   public static void loadProperties(String propsFile)
88   {
89     propertiesFile = propsFile;
90     if (propsFile == null)
91     {
92       propertiesFile = System.getProperty("user.home") + File.separatorChar +
93           ".jalview_properties";
94     }
95
96     try
97     {
98       FileInputStream fis = new FileInputStream(propertiesFile);
99       applicationProperties.load(fis);
100       applicationProperties.remove("LATEST_VERSION");
101       applicationProperties.remove("VERSION");
102       fis.close();
103     }
104     catch (Exception ex)
105     {
106       System.out.println("Error reading properties file: " + ex);
107     }
108
109     if (getDefault("USE_PROXY", false))
110     {
111       System.out.println("Using proxyServer: " + getDefault("PROXY_SERVER", null) +
112                          " proxyPort: " + getDefault("PROXY_PORT", null));
113       System.setProperty("http.proxyHost", getDefault("PROXY_SERVER", null));
114       System.setProperty("http.proxyPort", getDefault("PROXY_PORT", null));
115     }
116
117     // FIND THE VERSION NUMBER AND BUILD DATE FROM jalview.jar
118     // MUST FOLLOW READING OF LOCAL PROPERTIES FILE AS THE
119     // VERSION MAY HAVE CHANGED SINCE LAST USING JALVIEW
120     try
121     {
122       String buildDetails = "jar:"
123           .concat(
124               Cache.class.getProtectionDomain().getCodeSource().getLocation().
125               toString()
126               .concat("!/.build_properties")
127           );
128
129       java.net.URL localJarFileURL = new java.net.URL(buildDetails);
130
131       InputStream in = localJarFileURL.openStream();
132       applicationProperties.load(in);
133       in.close();
134     }
135     catch (Exception ex)
136     {
137       System.out.println("Error reading build details: " + ex);
138       applicationProperties.remove("VERSION");
139     }
140
141     String jnlpVersion = System.getProperty("jalview.version");
142     String codeVersion = getProperty("VERSION");
143
144     if (codeVersion == null)
145     {
146       // THIS SHOULD ONLY BE THE CASE WHEN TESTING!!
147       codeVersion = "Test";
148       jnlpVersion = "Test";
149     }
150
151     System.out.println("Jalview Version: " + codeVersion);
152
153     // jnlpVersion will be null if we're using InstallAnywhere
154     // Dont do this check if running in headless mode
155     if (jnlpVersion == null && (
156         System.getProperty("java.awt.headless") == null
157         || System.getProperty("java.awt.headless").equals("false")))
158     {
159
160       class VersionChecker
161           extends Thread
162       {
163         public void run()
164         {
165           String jnlpVersion = null;
166           try
167           {
168             java.net.URL url = new java.net.URL(
169                 "http://www.jalview.org/webstart/jalview.jnlp");
170             BufferedReader in = new BufferedReader(new InputStreamReader(url.
171                 openStream()));
172             String line = null;
173             while ( (line = in.readLine()) != null)
174             {
175               if (line.indexOf("jalview.version") == -1)
176               {
177                 continue;
178               }
179
180               line = line.substring(line.indexOf("value=") + 7);
181               line = line.substring(0, line.lastIndexOf("\""));
182               jnlpVersion = line;
183               break;
184             }
185           }
186           catch (Exception ex)
187           {
188             System.out.println(ex);
189             jnlpVersion = getProperty("VERSION");
190           }
191
192           setProperty("LATEST_VERSION", jnlpVersion);
193         }
194       }
195
196       VersionChecker vc = new VersionChecker();
197       vc.start();
198     }
199     else
200     {
201       if (jnlpVersion != null)
202       {
203         setProperty("LATEST_VERSION", jnlpVersion);
204       }
205       else
206       {
207         applicationProperties.remove("LATEST_VERSION");
208       }
209     }
210
211     setProperty("VERSION", codeVersion);
212
213     //LOAD USERDEFINED COLOURS
214     jalview.gui.UserDefinedColours.initUserColourSchemes(getProperty(
215         "USER_DEFINED_COLOURS"));
216     jalview.io.PIRFile.useModellerOutput = Cache.getDefault("PIR_MODELLER", false);
217   }
218
219   /**
220    * Gets Jalview application property of given key. Returns null
221    * if key not found
222    *
223    * @param key Name of property
224    *
225    * @return Property value
226    */
227   public static String getProperty(String key)
228   {
229     return applicationProperties.getProperty(key);
230   }
231
232   /** These methods are used when checking if the saved preference
233    * is different to the default setting
234    */
235
236   public static boolean getDefault(String property, boolean def)
237   {
238     String string = getProperty(property);
239     if (string != null)
240     {
241       def = Boolean.valueOf(string).booleanValue();
242     }
243
244     return def;
245   }
246
247   /** These methods are used when checking if the saved preference
248    * is different to the default setting
249    */
250   public static String getDefault(String property, String def)
251   {
252     String string = getProperty(property);
253     if (string != null)
254     {
255       return string;
256     }
257
258     return def;
259   }
260
261   /**
262    * Stores property in the file "HOME_DIR/.jalview_properties"
263    *
264    * @param key Name of object
265    * @param obj String value of property
266    *
267    * @return String value of property
268    */
269   public static String setProperty(String key, String obj)
270   {
271     try
272     {
273       FileOutputStream out = new FileOutputStream(propertiesFile);
274       applicationProperties.setProperty(key, obj);
275       applicationProperties.store(out, "---JalviewX Properties File---");
276       out.close();
277     }
278     catch (Exception ex)
279     {
280       System.out.println("Error setting property: " + key + " " + obj + "\n" +
281                          ex);
282     }
283     return obj;
284   }
285
286   public static void saveProperties()
287   {
288     try
289     {
290       FileOutputStream out = new FileOutputStream(propertiesFile);
291       applicationProperties.store(out, "---JalviewX Properties File---");
292       out.close();
293     }
294     catch (Exception ex)
295     {
296       System.out.println("Error saving properties: " + ex);
297     }
298   }
299
300   /**
301    * internal vamsas class discovery state
302    */
303   private static int vamsasJarsArePresent = -1;
304   /**
305    * Searches for vamsas client classes on class path.
306    * @return true if vamsas client is present on classpath
307    */
308   public static boolean vamsasJarsPresent()
309   {
310     if (vamsasJarsArePresent == -1)
311     {
312       try
313       {
314         if (jalview.jbgui.GDesktop.class.getClassLoader().loadClass(
315             "uk.ac.vamsas.client.VorbaId") != null)
316         {
317           jalview.bin.Cache.log.debug(
318               "Found Vamsas Classes (uk.ac..vamsas.client.VorbaId can be loaded)");
319           vamsasJarsArePresent = 1;
320           Logger lvclient = Logger.getLogger("uk.ac.vamsas");
321           lvclient.setLevel(Level.toLevel(Cache.getDefault("logs.Vamsas.Level",
322               Level.INFO.toString())));
323
324           lvclient.addAppender(log.getAppender("JalviewLogger"));
325           // Tell the user that debug is enabled
326           lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
327         }
328       }
329       catch (Exception e)
330       {
331         vamsasJarsArePresent = 0;
332         jalview.bin.Cache.log.debug("Vamsas Classes are not present");
333       }
334     }
335     return (vamsasJarsArePresent > 0);
336   }
337
338 }