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