ref #8189 moving folder funstins from CdmUtils to ConfigFileUtil
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / config / AbstractWebApplicationConfigurer.java
index 89bedcf43c586e64597bbfb6f00931c749ad6bce..c19e4b38a54c0f917c57e2ff376b56c3c67d914f 100644 (file)
-// $Id$\r
 /**\r
  * Copyright (C) 2009 EDIT\r
- * European Distributed Institute of Taxonomy \r
+ * European Distributed Institute of Taxonomy\r
  * http://www.e-taxonomy.eu\r
- * \r
+ *\r
  * The contents of this file are subject to the Mozilla Public License Version 1.1\r
  * See LICENSE.TXT at the top of this package for the full license terms.\r
  */\r
 package eu.etaxonomy.cdm.remote.config;\r
 \r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Properties;\r
+\r
 import org.apache.log4j.Logger;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.context.ApplicationContext;\r
+import org.springframework.core.env.ConfigurableEnvironment;\r
 import org.springframework.web.context.WebApplicationContext;\r
 \r
+import eu.etaxonomy.cdm.common.ConfigFileUtil;\r
+\r
 /**\r
  * @author a.kohlbecker\r
- * @date 20.07.2010\r
+ * @since 20.07.2010\r
  *\r
  */\r
 public abstract class AbstractWebApplicationConfigurer {\r
 \r
-       public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);\r
-       \r
-       WebApplicationContext webApplicationContext;\r
-\r
-       \r
-       @Autowired\r
-       public void setApplicationContext(ApplicationContext applicationContext){\r
-\r
-               if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {\r
-                       this.webApplicationContext = (WebApplicationContext)applicationContext;\r
-               } else {\r
-                       logger.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");\r
-               }\r
-       }\r
-\r
-       protected String findProperty(String property, boolean required) {\r
-               // 1. look for the property in the ServletContext\r
-               Object obj = webApplicationContext.getServletContext().getAttribute(property);\r
-               String value = (String)obj;\r
-               // 2. look for the property in environment variables of the OS\r
-               if(value == null){\r
-                       value = System.getProperty(property);\r
-               }\r
-               if(value == null && required){\r
-                       logger.error("property {" + property + "} not found.");\r
-                       logger.error("--> This property can be set in two ways:");\r
-                       logger.error("-->               1. as attribute to the ServletContext");\r
-                       logger.error("-->               2. as system property e.g. -D" + property);\r
-                       logger.error("Stopping application ...");\r
-                       System.exit(-1);\r
-               }\r
-               return value;\r
-       }\r
-       \r
-   \r
-\r
-}
\ No newline at end of file
+    public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);\r
+\r
+    @Autowired\r
+    protected ConfigurableEnvironment env;\r
+\r
+    private static final String CDMLIB_REMOTE_PROPERTIES = "cdmlib-remote.properties";\r
+\r
+    /**\r
+     * see also <code>eu.etaxonomy.cdm.server.instance.SharedAttributes</code>\r
+     */\r
+    private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages";\r
+\r
+\r
+    protected WebApplicationContext webApplicationContext;\r
+\r
+    static Properties userDefinedProperties = null;\r
+    static {\r
+        if(userDefinedProperties == null) {\r
+            userDefinedProperties = new Properties();\r
+            try {\r
+                InputStream in = new FileInputStream(\r
+                            ConfigFileUtil.perUserCdmFolder()\r
+                            + java.io.File.separator\r
+                            + CDMLIB_REMOTE_PROPERTIES\r
+                    );\r
+                if (in != null) {\r
+                    userDefinedProperties.load(in);\r
+                }\r
+            } catch (IOException e) {\r
+                logger.debug("No per user " + CDMLIB_REMOTE_PROPERTIES + " found.");\r
+            }\r
+        }\r
+    }\r
+\r
+    @Autowired\r
+    public void setApplicationContext(ApplicationContext applicationContext){\r
+\r
+        if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {\r
+            this.webApplicationContext = (WebApplicationContext)applicationContext;\r
+        } else {\r
+            throw new RuntimeException("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Find a property primarily in the ServletContext and secondarily\r
+     * in the environment variables of the OS. So a property can be set\r
+     * by three means:\r
+     * <ol>\r
+     * <li>As attribute to the ServletContext (the cdm-server makes use of this method)</li>\r
+     * <li>as system property e.g. by setting the jvm commandline option like for example\r
+     * <code>-Dcdm.rootpathprefix=my/cdm/remote-instance<code></li>\r
+     * <li>In a per user Java properties file {@code ~/.cdmLibrary/cdmlib-remote.properties}</li>\r
+     * </ol>\r
+     *\r
+     * @param property usually a string constant defined in a subclass of\r
+     *                 <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>\r
+     * @param required\r
+     * @return\r
+     */\r
+    protected String findProperty(String property, boolean required) {\r
+        String value = null;\r
+        if(required){\r
+            try {\r
+                value = env.getRequiredProperty(property);\r
+            } catch (IllegalStateException e) {\r
+                logger.error("property {" + property + "} not found.");\r
+                logger.error("--> This property can be set in three optional ways:");\r
+                logger.error("-->              1. as attribute to the ServletContext");\r
+                logger.error("-->              2. as system property e.g. -D" + property);\r
+                // logger.error("-->           3. in ~/.cdmLibrary/cdmlib-remote.properties");\r
+                logger.error("Stopping application ...");\r
+                System.exit(-1);\r
+            }\r
+        } else {\r
+            value = env.getProperty(property);\r
+        }\r
+        return value;\r
+    }\r
+\r
+    protected void addErrorMessageToServletContextAttributes(String errorMessage) {\r
+        Object o = webApplicationContext.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES);\r
+        List<String> messages;\r
+        if(o != null  && o instanceof List<?>){\r
+            messages = (List<String>) o;\r
+        } else {\r
+            messages = new ArrayList<String>();\r
+        }\r
+        messages.add(errorMessage);\r
+        webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);\r
+    }\r
+\r
+\r
+\r
+}