You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by fe...@apache.org on 2011/08/10 16:45:32 UTC

svn commit: r1156200 - in /james/hupa/trunk: server/src/main/java/org/apache/hupa/server/guice/ server/src/main/java/org/apache/hupa/server/utils/ server/src/test/java/org/apache/hupa/server/guice/ src/site/xdoc/

Author: felixk
Date: Wed Aug 10 14:45:31 2011
New Revision: 1156200

URL: http://svn.apache.org/viewvc?rev=1156200&view=rev
Log:
- Add validation for mandatory and optional configuration properties
- Add documentation for this
- Add default values for missing optional configuration properties
- Throw a more meaningful exception
(HUPA-71)

Added:
    james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java   (with props)
Modified:
    james/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/GuiceServerModule.java
    james/hupa/trunk/server/src/test/java/org/apache/hupa/server/guice/ServerModulTest.java
    james/hupa/trunk/src/site/xdoc/configuration.xml

Modified: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/GuiceServerModule.java
URL: http://svn.apache.org/viewvc/james/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/GuiceServerModule.java?rev=1156200&r1=1156199&r2=1156200&view=diff
==============================================================================
--- james/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/GuiceServerModule.java (original)
+++ james/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/GuiceServerModule.java Wed Aug 10 14:45:31 2011
@@ -52,10 +52,13 @@ import org.apache.hupa.server.preference
 import org.apache.hupa.server.servlet.DownloadAttachmentServlet;
 import org.apache.hupa.server.servlet.MessageSourceServlet;
 import org.apache.hupa.server.servlet.UploadAttachmentServlet;
+import org.apache.hupa.server.utils.ConfigurationProperties;
 import org.apache.hupa.shared.data.Settings;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 
 import javax.mail.Session;
@@ -78,6 +81,8 @@ public class GuiceServerModule extends A
 
     private String configDir;
     
+    private boolean demoMode = false;
+    
     public GuiceServerModule(String rootPath) {
         configDir = rootPath + "/" + CONF_DIR;
     }
@@ -149,13 +154,44 @@ public class GuiceServerModule extends A
         }
 
         // Put Hupa in demo mode
-        if (properties == null || DemoModeConstants.DEMO_MODE.equals(properties.get("IMAPServerAddress"))) {
+        demoMode = DemoModeConstants.DEMO_MODE.equals(properties.get("IMAPServerAddress"));
+        if (demoMode) {
             properties = DemoModeConstants.demoProperties;
         }
         
-        return properties;
+        // Validate for mandatory and complete properties with default values
+        return validateProperties(properties);
     }
 
+    protected Properties validateProperties(Properties properties) {
+	List<String> errors = new ArrayList<String>();
+	
+        // Test for mandatory and complete properties with default values when missing
+	for (ConfigurationProperties confProps : ConfigurationProperties.values()) {
+	    if (confProps.isMandatory()) {
+		if (properties.get(confProps.getProperty()) == null) {
+		    errors.add("The mandatory Property '" + confProps.getProperty() + "' is not set.");
+		}
+	    } else {
+		if (properties.get(confProps.getProperty()) == null) {
+		    properties.setProperty(confProps.getProperty(), confProps.getPropValue());
+		}
+	    }
+	}
+
+	// Test for unknown properties set in configuration
+	for (Object key : properties.keySet()) {
+	    if (ConfigurationProperties.lookup((String)key) == null) {
+		errors.add("The Property '" + key + "' has no configuration impacts, it's unknown");
+	    }
+	}
+	if (!demoMode && !errors.isEmpty()) {
+	    throw new IllegalArgumentException(errors.toString());
+	}
+	
+	return properties;
+    }
+    
     protected Properties loadProperties(String name) {
 
         if (name == null)

Added: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java
URL: http://svn.apache.org/viewvc/james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java?rev=1156200&view=auto
==============================================================================
--- james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java (added)
+++ james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java Wed Aug 10 14:45:31 2011
@@ -0,0 +1,100 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.hupa.server.utils;
+
+/**
+ * Enumeration of valid configuration properties
+ */
+public enum ConfigurationProperties {
+    // Mandatory configuration properties
+    IMAP_SERVER_ADDRESS("IMAPServerAddress", true, null),
+    SMTP_SERVER_ADDRESS("SMTPServerAddress", true, null),
+    IMAP_SERVER_PORT("IMAPServerPort", true, null),
+    SMTP_SERVER_PORT("SMTPServerPort", true, null),
+
+    // Optional configuration properties
+    IMAP_CONNECTION_POOL_SIZE("IMAPConnectionPoolSize", false, "4"),
+    IMAP_CONNECTION_POOL_TIMEOUT("IMAPConnectionPoolTimeout", false, "300000"),
+    IMAPS("IMAPS", false, "false"),
+    TRUST_STORE("TrustStore", false, ""),
+    TRUST_STORE_PASSWORD("TrustStorePassword", false, ""),
+    DEFAULT_SENT_FOLDER("DefaultSentFolder", false, "\\Sent"),
+    DEFAULT_TRASH_FOLDER("DefaultTrashFolder", false, "\\Trash"),
+    DEFAULT_DRAFTS_FOLDER("DefaultDraftsFolder", false, "\\Drafts"),
+    DEFAULT_INBOX_FOLDER("DefaultInboxFolder", false, "INBOX"),
+    POST_FETCH_MESSAGE_COUNT("PostFetchMessageCount", false, "0"),
+    SESSION_DEBUG("SessionDebug", false, "false"),
+    SMTP_AUTH("SMTPAuth", false, "true"),
+    SMTPS("SMTPS", false, "false");
+    
+    private String property;
+    private boolean mandatory;
+    private String propValue;
+    
+    private ConfigurationProperties (String property, boolean mandatory, String propValue) {
+	this.property = property;
+	this.mandatory = mandatory;
+	this.propValue = propValue;
+    }
+    
+    /**
+     * Return a ConfigurationProperties enumeration that matches the passed command.
+     * 
+     * @param property
+     *            The property to use for lookup.
+     * @return the ConfigurationProperties enumeration that matches the passed property, or null
+     *         if not found.
+     */
+    public static ConfigurationProperties lookup(String property) {
+        if (property != null) {
+            for (ConfigurationProperties confProp : values())
+                if (confProp.getProperty().equalsIgnoreCase(property))
+                    return confProp;
+        }
+        return null;
+    }
+    
+    /**
+     * Return the name of property.
+     * 
+     * @return the name of property.
+     */
+    public String getProperty() {
+        return this.property;
+    }
+
+    /**
+     * Return if property is mandatory
+     * 
+     * @return true if mandatory else false.
+     */
+    public boolean isMandatory() {
+        return this.mandatory;
+    }
+
+    /**
+     * Return the value of the property.
+     * 
+     * @return the value of the property.
+     */
+    public String getPropValue() {
+        return this.propValue;
+    }
+}
\ No newline at end of file

Propchange: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/ConfigurationProperties.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/hupa/trunk/server/src/test/java/org/apache/hupa/server/guice/ServerModulTest.java
URL: http://svn.apache.org/viewvc/james/hupa/trunk/server/src/test/java/org/apache/hupa/server/guice/ServerModulTest.java?rev=1156200&r1=1156199&r2=1156200&view=diff
==============================================================================
--- james/hupa/trunk/server/src/test/java/org/apache/hupa/server/guice/ServerModulTest.java (original)
+++ james/hupa/trunk/server/src/test/java/org/apache/hupa/server/guice/ServerModulTest.java Wed Aug 10 14:45:31 2011
@@ -66,14 +66,22 @@ public class ServerModulTest {
         File tmp = File.createTempFile("foo", ".properties");
         tmp.deleteOnExit();
 
-        Properties p = module.loadProperties(tmp.toString());
-        Assert.assertNotNull(p);
-        Assert.assertNull(p.get("IMAPServerAddress"));
+        try {
+            module.loadProperties(tmp.toString());
+        } catch (IllegalArgumentException e) {
+            // This must be thrown because of missing mandatory configuration properties
+        } catch (Exception e) {
+            Assert.fail("IllegalArgumentException must be thrown because of missing mandatory configuration properties");
+        }
 
         System.setProperty(GuiceServerModule.SYS_PROP_CONFIG_FILE, tmp.toString());
-        p = module.loadProperties();
-        Assert.assertNotNull(p);
-        Assert.assertNull(p.get("IMAPServerAddress"));
+        try {
+            module.loadProperties();
+        } catch (IllegalArgumentException e) {
+            // This must be thrown because of missing mandatory configuration properties
+        } catch (Exception e) {
+            Assert.fail("IllegalArgumentException must be thrown because of missing mandatory configuration properties");
+        }
         System.clearProperty(GuiceServerModule.SYS_PROP_CONFIG_FILE);
 
     }

Modified: james/hupa/trunk/src/site/xdoc/configuration.xml
URL: http://svn.apache.org/viewvc/james/hupa/trunk/src/site/xdoc/configuration.xml?rev=1156200&r1=1156199&r2=1156200&view=diff
==============================================================================
--- james/hupa/trunk/src/site/xdoc/configuration.xml (original)
+++ james/hupa/trunk/src/site/xdoc/configuration.xml Wed Aug 10 14:45:31 2011
@@ -39,21 +39,25 @@
                 <table>
                     <tr>
                         <th>Property</th>
-                        <th>sample</th>
+                        <th>Mandatory</th>
+                        <th>default</th>
                         <th>Comment</th>
                     </tr>
                     <tr>
                         <td>IMAPServerAddress</td>
-                        <td>imap.gmail.com</td>
+                        <td>yes</td>
+                        <td></td>
                         <td>The IP or domainname of the IMAP server</td>
                     </tr>
                     <tr>
                         <td>IMAPServerPort</td>
-                        <td>993</td>
+                        <td>yes</td>
+                        <td></td>
                         <td>The port of the IMAP server</td>
                     </tr>
                     <tr>
                         <td>IMAPConnectionPoolSize</td>
+                        <td>no</td>
                         <td>4</td>
                         <td>
                             Set the connections amount which will get maximal opened per user for processing requests
@@ -63,72 +67,86 @@
                     </tr>
                     <tr>
                         <td>IMAPConnectionPoolTimeout</td>
+                        <td>no</td>
                         <td>300000</td>
                         <td>Set the timeout for connections in milliseconds</td>
                     </tr>
                     <tr>
                         <td>IMAPS</td>
-                        <td>true</td>
+                        <td>no</td>
+                        <td>false</td>
                         <td>Use SSL/TLS to connect to the IMAP server</td>
                     </tr>
                     <tr>
                         <td>TrustStore</td>
-                        <td>my-truststore</td>
-                        <td>The Truststore when using self-signed certificates on the server</td>
+                        <td>no</td>
+                        <td></td>
+                        <td>The Truststore (including path) when using self-signed certificates on the server</td>
                     </tr>
                     <tr>
                         <td>TrustStorePassword</td>
-                        <td>changeit</td>
+                        <td>no</td>
+                        <td></td>
                         <td>The Truststore's password</td>
                     </tr>
                     <tr>
                         <td>DefaultSentFolder</td>
-                        <td>[Gmail]\Sent</td>
+                        <td>no</td>
+                        <td>\Sent</td>
                         <td></td>
                     </tr>
                     <tr>
                         <td>DefaultTrashFolder</td>
-                        <td>[Gmail]\Trash</td>
+                        <td>no</td>
+                        <td>\Trash</td>
                         <td></td>
                     </tr>
                     <tr>
                         <td>DefaultDraftsFolder</td>
-                        <td>[Gmail]\Drafts</td>
+                        <td>no</td>
+                        <td>\Drafts</td>
                         <td></td>
                     </tr>
                     <tr>
                         <td>DefaultInboxFolder</td>
+                        <td>no</td>
                         <td>INBOX</td>
                         <td></td>
                     </tr>
                     <tr>
                         <td>PostFetchMessageCount</td>
+                        <td>no</td>
                         <td>0</td>
                         <td>Number of Messages to post fetch</td>
                     </tr>
                     <tr>
                         <td>SessionDebug</td>
-                        <td>true</td>
+                        <td>no</td>
+                        <td>false</td>
                         <td>Log IMAP and SMTP dialog, Logger has to be in debug mode</td>
                     </tr>
                     <tr>
                         <td>SMTPServerAddress</td>
-                        <td>smtp.gmail.com</td>
+                        <td>yes</td>
+                        <td></td>
                         <td>The IP or domainname of the SMTP server</td>
                     </tr>
                     <tr>
                         <td>SMTPServerPort</td>
-                        <td>465</td>
+                        <td>yes</td>
+                        <td></td>
                         <td>The port of the SMTP server</td>
                     </tr>
                     <tr>
                         <td>SMTPAuth</td>
+                        <td>no</td>
                         <td>true</td>
                         <td>Use AUTH for SMTP</td>
                     </tr>
                     <tr>
                         <td>SMTPS</td>
-                        <td>true</td>
+                        <td>no</td>
+                        <td>false</td>
                         <td>Use SSL/TLS to connect to the SMTP server</td>
                     </tr>
                 </table>



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org