JAL-3933 configure root logger console appender to stderr (2.11.2 version)
[jalview.git] / src / jalview / bin / Cache.java
index ef42589..aa9c030 100755 (executable)
@@ -51,6 +51,12 @@ import org.apache.log4j.ConsoleAppender;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
+import org.apache.log4j.builders.appender.ConsoleAppenderBuilder;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
+import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
+import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
+import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
 
 import jalview.datamodel.PDBEntry;
 import jalview.gui.Preferences;
@@ -182,8 +188,8 @@ import jalview.ws.sifts.SiftsSettings;
  * when shading by annotation</li>
  * <li>ANNOTATIONCOLOUR_MAX (red) Shade used for maximum value of annotation
  * when shading by annotation</li>
- * <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>
+ * <li>www.jalview.org (https://www.jalview.org) a property enabling all HTTP
+ * requests to be redirected to a mirror of https://www.jalview.org</li>
  * <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>
@@ -310,6 +316,9 @@ public class Cache
     }
   };
 
+  /* build Properties (not all saved to .jalview_properties) */
+  public static Properties buildProperties = new Properties();
+
   /** Default file is ~/.jalview_properties */
   static String propertiesFile;
 
@@ -325,13 +334,11 @@ public class Cache
     }
     try
     {
-      // TODO: redirect stdout and stderr here in order to grab the output of
-      // the log
-
-      ConsoleAppender ap = new ConsoleAppender(new SimpleLayout(),
-              "System.err");
-      ap.setName("JalviewLogger");
-      org.apache.log4j.Logger.getRootLogger().addAppender(ap); // catch all for
+      // configure the root logger to stderr
+      ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
+      AppenderComponentBuilder consoleApp = builder.newAppender("stderr", "Console");
+      builder.add(consoleApp);
+      Configurator.initialize(builder.build());
       // log output
       Logger laxis = Logger.getLogger("org.apache.axis");
       Logger lcastor = Logger.getLogger("org.exolab.castor");
@@ -367,16 +374,24 @@ public class Cache
   }
 
   /**
-   * Loads properties from the given properties file. Any existing properties
-   * are first cleared.
+   * Loads properties from the given properties file. Any existing properties are
+   * first cleared.
    */
   public static void loadProperties(String propsFile)
   {
     propertiesFile = propsFile;
+    String releasePropertiesFile = null;
+    boolean defaultProperties = false;
     if (propsFile == null && !propsAreReadOnly)
     {
+      String channelPrefsFilename = ChannelProperties
+              .getProperty("preferences.filename");
+      String releasePrefsFilename = ".jalview_properties";
       propertiesFile = System.getProperty("user.home") + File.separatorChar
-              + ".jalview_properties";
+              + channelPrefsFilename;
+      releasePropertiesFile = System.getProperty("user.home")
+              + File.separatorChar + releasePrefsFilename;
+      defaultProperties = true;
     }
     else
     {
@@ -396,20 +411,29 @@ public class Cache
         InputStream fis;
         try
         {
+          // props file provided as URL
           fis = new URL(propertiesFile).openStream();
           System.out.println(
                   "Loading jalview properties from : " + propertiesFile);
           System.out.println(
                   "Disabling Jalview writing to user's local properties file.");
           propsAreReadOnly = true;
-
         } catch (Exception ex)
         {
           fis = null;
         }
         if (fis == null)
         {
-          fis = new FileInputStream(propertiesFile);
+          String readPropertiesFile = propertiesFile;
+          // if we're using the usual properties file and the channel properties
+          // file doesn't exist, read .jalview_properties
+          // (but we'll still save to the channel properties file).
+          if (defaultProperties && (!new File(propertiesFile).exists())
+                  && (new File(releasePropertiesFile).exists()))
+          {
+            readPropertiesFile = releasePropertiesFile;
+          }
+          fis = new FileInputStream(readPropertiesFile);
         }
         applicationProperties.clear();
         applicationProperties.load(fis);
@@ -503,7 +527,7 @@ public class Cache
                     DEFAULT_CACHE_THRESHOLD_IN_DAYS));
 
     IdOrgSettings.setUrl(getDefault("ID_ORG_HOSTURL",
-            "http://www.jalview.org/services/identifiers"));
+            "https://www.jalview.org/services/identifiers"));
     IdOrgSettings.setDownloadLocation(ID_ORG_FILE);
 
     StructureImportSettings.setDefaultStructureFileFormat(jalview.bin.Cache
@@ -531,7 +555,8 @@ public class Cache
         @Override
         public void run()
         {
-          String buildPropertiesUrl = Cache.getAppbaseBuildProperties();
+          String remoteBuildPropertiesUrl = Cache
+                  .getAppbaseBuildProperties();
 
           String orgtimeout = System
                   .getProperty("sun.net.client.defaultConnectTimeout");
@@ -546,7 +571,7 @@ public class Cache
           {
             System.setProperty("sun.net.client.defaultConnectTimeout",
                     "5000");
-            java.net.URL url = new java.net.URL(buildPropertiesUrl);
+            java.net.URL url = new java.net.URL(remoteBuildPropertiesUrl);
 
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(url.openStream()));
@@ -558,7 +583,7 @@ public class Cache
           {
             System.out
                     .println("Non-fatal exception when checking version at "
-                            + buildPropertiesUrl + ":");
+                            + remoteBuildPropertiesUrl + ":");
             System.out.println(ex);
             remoteVersion = getProperty("VERSION");
           }
@@ -633,8 +658,23 @@ public class Cache
       String buildDetails = resolveResourceURLFor("/.build_properties");
       URL localJarFileURL = new URL(buildDetails);
       InputStream in = localJarFileURL.openStream();
-      applicationProperties.load(in);
+      buildProperties.load(in);
       in.close();
+      if (buildProperties.getProperty("BUILD_DATE", null) != null)
+      {
+        applicationProperties.put("BUILD_DATE",
+                buildProperties.getProperty("BUILD_DATE"));
+      }
+      if (buildProperties.getProperty("INSTALLATION", null) != null)
+      {
+        applicationProperties.put("INSTALLATION",
+                buildProperties.getProperty("INSTALLATION"));
+      }
+      if (buildProperties.getProperty("VERSION", null) != null)
+      {
+        applicationProperties.put("VERSION",
+                buildProperties.getProperty("VERSION"));
+      }
     } catch (Exception ex)
     {
       System.out.println("Error reading build details: " + ex);
@@ -674,11 +714,10 @@ public class Cache
   }
 
   /**
-   * Gets Jalview application property of given key. Returns null if key not
-   * found
+   * Gets Jalview application property of given key. Returns null if key not found
    * 
    * @param key
-   *          Name of property
+   *              Name of property
    * 
    * @return Property value
    */
@@ -694,8 +733,8 @@ public class Cache
   }
 
   /**
-   * These methods are used when checking if the saved preference is different
-   * to the default setting
+   * These methods are used when checking if the saved preference is different to
+   * the default setting
    */
 
   public static boolean getDefault(String property, boolean def)
@@ -728,8 +767,8 @@ public class Cache
   }
 
   /**
-   * Answers the value of the given property, or the supplied default value if
-   * the property is not set
+   * Answers the value of the given property, or the supplied default value if the
+   * property is not set
    */
   public static String getDefault(String property, String def)
   {
@@ -741,9 +780,9 @@ public class Cache
    * Stores property in the file "HOME_DIR/.jalview_properties"
    * 
    * @param key
-   *          Name of object
+   *              Name of object
    * @param obj
-   *          String value of property
+   *              String value of property
    * 
    * @return previous value of property (or null)
    */
@@ -1131,7 +1170,7 @@ public class Cache
    * Loads in user colour schemes from files.
    * 
    * @param files
-   *          a '|'-delimited list of file paths
+   *                a '|'-delimited list of file paths
    */
   public static void initUserColourSchemes(String files)
   {
@@ -1440,8 +1479,8 @@ public class Cache
                   // open Preferences -> Connections
                   String message = MessageManager
                           .getString("label.proxy_password_required");
-                  Preferences.openPreferences(Preferences.CONNECTIONS_TAB,
-                          message);
+                  Preferences.openPreferences(
+                          Preferences.TabRef.CONNECTIONS_TAB, message);
                   Preferences.getInstance()
                           .proxyAuthPasswordCheckHighlight(true, true);
                 }
@@ -1633,11 +1672,22 @@ public class Cache
 
   static
   {
-    Float specversion = Float
-            .parseFloat(System.getProperty("java.specification.version"));
-    releaseAppbase = (specversion < 9)
-            ? "https://www.jalview.org/getdown/release/1.8"
-            : "https://www.jalview.org/getdown/release/11";
+    if (!Platform.isJS())
+    {
+      Float specversion = Float
+              .parseFloat(System.getProperty("java.specification.version"));
+      releaseAppbase = (specversion < 9)
+              ? "https://www.jalview.org/getdown/release/1.8"
+              : "https://www.jalview.org/getdown/release/11";
+    }
+    else
+    {
+      // this value currenly made up, can be changed to URL that will be
+      // "https://www.jalview.org/jalview-js/swingjs/j2s/build_properties"
+      releaseAppbase = "https://www.jalview.org/jalview-js";
+      getdownAppbase = releaseAppbase;
+      getdownDistDir = "/swingjs/j2s";
+    }
   }
 
   // look for properties (passed in by getdown) otherwise default to release
@@ -1651,6 +1701,11 @@ public class Cache
     String distDir = System.getProperty("getdowndistdir");
     if (appbase == null)
     {
+      appbase = buildProperties.getProperty("GETDOWNAPPBASE");
+      distDir = buildProperties.getProperty("GETDOWNAPPDISTDIR");
+    }
+    if (appbase == null)
+    {
       appbase = releaseAppbase;
       distDir = "release";
     }
@@ -1666,13 +1721,13 @@ public class Cache
     getdownDistDir = distDir;
   }
 
-  private static String getGetdownAppbase()
+  public static String getGetdownAppbase()
   {
     setGetdownAppbase();
     return getdownAppbase;
   }
 
-  private static String getAppbaseBuildProperties()
+  public static String getAppbaseBuildProperties()
   {
     String appbase = getGetdownAppbase();
     return appbase + "/" + getdownDistDir + "/build_properties";