Author: [email protected]
Date: Wed Apr  4 11:40:41 2012
New Revision: 2179

Log:
[AMDATUAUTH-140] Moved config util to tools module

Added:
   trunk/amdatu-auth/tools/
   trunk/amdatu-auth/tools/config/
   trunk/amdatu-auth/tools/config/pom.xml
   trunk/amdatu-auth/tools/config/src/
   trunk/amdatu-auth/tools/config/src/main/
   trunk/amdatu-auth/tools/config/src/main/java/
   trunk/amdatu-auth/tools/config/src/main/java/org/
   trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/
   trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/
   trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/tools/
   trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/tools/config/
   
trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java
   trunk/amdatu-auth/tools/pom.xml
Modified:
   trunk/amdatu-auth/pom.xml
   trunk/amdatu-auth/useradmin-rest/pom.xml
   
trunk/amdatu-auth/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java

Modified: trunk/amdatu-auth/pom.xml
==============================================================================
--- trunk/amdatu-auth/pom.xml   (original)
+++ trunk/amdatu-auth/pom.xml   Wed Apr  4 11:40:41 2012
@@ -182,6 +182,13 @@
       </dependency>
       <dependency>
         <groupId>org.amdatu.auth</groupId>
+        <artifactId>org.amdatu.auth.tools.config</artifactId>
+        <version>${project.version}</version>
+        <scope>compile</scope>
+        <type>jar</type>
+      </dependency>
+      <dependency>
+        <groupId>org.amdatu.auth</groupId>
         <artifactId>org.amdatu.auth.oauth.example</artifactId>
         <version>${project.version}</version>
         <scope>runtime</scope>
@@ -288,6 +295,7 @@
     <module>test-integration</module>
     <module>test-performance</module>
     <module>tokenprovider</module>
+    <module>tools</module>
     <module>useradmin-gadget</module>
     <module>useradmin-rest</module>
   </modules>

Added: trunk/amdatu-auth/tools/config/pom.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-auth/tools/config/pom.xml      Wed Apr  4 11:40:41 2012
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright (c) 2010, 2011 The Amdatu Foundation
+
+  Licensed 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.verning permissions and limitations
+  under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.auth</groupId>
+    <artifactId>org.amdatu.auth.tools</artifactId>
+    <version>0.2.2-SNAPSHOT</version>
+  </parent>
+  <artifactId>org.amdatu.auth.tools.config</artifactId>
+  <packaging>jar</packaging>
+  <name>Amdatu Auth - Config Tools</name>
+  <description>Tools for configuration entries managed by OSGi Config 
Admin</description>
+</project>

Added: 
trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java
  Wed Apr  4 11:40:41 2012
@@ -0,0 +1,185 @@
+package org.amdatu.auth.tools.config;
+
+import java.io.File;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import org.osgi.service.log.LogService;
+
+/**
+ * This class provides a handy utility method for handling configuration 
properties managed by OSGi
+ * config admin. It adds support for providing default values in case 
configuration properties are
+ * not available in OSGi config admin (useful for backwards compatibility) and 
provides type-safe
+ * casting the properties to required types.
+ * This utility class can be reused.
+ *
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class ConfigTools {
+    private LogService m_logService;
+    private Dictionary<?, ?> m_config = null;
+    private Dictionary<String, Object> m_values = new Hashtable<String, 
Object>();
+
+    public static class ConfigProperty {
+        private String m_name;
+        private Class<?> m_type;
+        private Object m_defaultValue;
+
+        public ConfigProperty(final String aName, final Class<?> aType, final 
Object aDefaultValue) {
+            m_name = aName;
+            m_type = aType;
+            m_defaultValue = aDefaultValue;
+        }
+
+        public String getName() {
+            return m_name;
+        }
+
+        public Class<?> getType() {
+            return m_type;
+        }
+
+        public Object getDefaultValue() {
+            return m_defaultValue;
+        }
+    }
+
+    public ConfigTools() {
+    }
+
+    public ConfigTools(final LogService logService) {
+        m_logService = logService;
+    }
+
+    public void init(final ConfigProperty[] configProperties, final 
Dictionary<?, ?> config) {
+        m_config = config;
+        for (ConfigProperty property : configProperties) {
+            if (property.getType().isAssignableFrom(String.class)) {
+                m_values.put(property.getName(), 
getStringProperty(property.getName(),
+                    (String) property.getDefaultValue()));
+            }
+            else if (property.getType().isAssignableFrom(Integer.class)) {
+                m_values.put(property.getName(), 
getIntProperty(property.getName(),
+                    (Integer) property.getDefaultValue()));
+            }
+            else if (property.getType().isAssignableFrom(Long.class)) {
+                m_values.put(property.getName(), 
getLongProperty(property.getName(),
+                    (Long) property.getDefaultValue()));
+            }
+            else if (property.getType().isAssignableFrom(Boolean.class)) {
+                m_values.put(property.getName(), 
getBooleanProperty(property.getName(),
+                    (Boolean) property.getDefaultValue()));
+            }
+            else if (property.getType().isAssignableFrom(File.class)) {
+                m_values.put(property.getName(), 
getFileProperty(property.getName(),
+                    (File) property.getDefaultValue()));
+            }
+        }
+
+        // Log the config properties
+        Enumeration<String> keys = m_values.keys();
+        log(LogService.LOG_DEBUG, "Starting UserAdmin REST service with 
configuration: ");
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            log(LogService.LOG_DEBUG, "  " + key + " = " + 
m_values.get(key).toString());
+        }
+    }
+
+    /**
+     * Returns a property from the configuration.
+     *
+     * @param key The key of the property to retrieve.
+     * @param class The type of the property value to retrieve
+     * @return The value of this property as type T
+     */
+    @SuppressWarnings("unchecked")
+    public <T> T get(final String key, final Class<T> clazz) {
+        Object val = m_values.get(key);
+        if (val != null && val.getClass().isAssignableFrom(clazz)) {
+            return (T) val;
+        }
+        log(LogService.LOG_ERROR, "Property '" + key + "' retrieved as '" + 
clazz.toString()
+            + "', but its value is of type '" + val.getClass() + "'. Returning 
null.");
+        return null;
+    }
+
+    private String getStringProperty(final String key, final String 
defaultValue) {
+        Object value = m_config.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(LogService.LOG_INFO, msg);
+            return defaultValue;
+        }
+        return value.toString();
+    }
+
+    private int getIntProperty(final String key, final int defaultValue) {
+        Object value = m_config.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(LogService.LOG_INFO, msg);
+            return defaultValue;
+        }
+        try {
+            return Integer.parseInt(value.toString().trim());
+        }
+        catch (NumberFormatException e) {
+            String msg = "Invalid value set for property '" + key + "' ('" + 
value.toString()
+                + "'), switching to default value '" + defaultValue + "'";
+            log(LogService.LOG_ERROR, msg);
+            return defaultValue;
+        }
+    }
+
+    private long getLongProperty(final String key, final long defaultValue) {
+        Object value = m_config.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(LogService.LOG_INFO, msg);
+            return defaultValue;
+        }
+        try {
+            return Long.parseLong(value.toString().trim());
+        }
+        catch (NumberFormatException e) {
+            String msg = "Invalid value set for property '" + key + "' ('" + 
value.toString()
+                + "'), switching to default value '" + defaultValue + "'";
+            log(LogService.LOG_ERROR, msg);
+            return defaultValue;
+        }
+    }
+
+    private boolean getBooleanProperty(final String key, final boolean 
defaultValue) {
+        Object value = m_config.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(LogService.LOG_INFO, msg);
+            return defaultValue;
+        }
+        return "true".equalsIgnoreCase(value.toString()) || 
"1".equals(value.toString());
+    }
+
+    private File getFileProperty(final String key, final File defaultValue) {
+        Object value = m_config.get(key);
+        if (value == null) {
+            String msg = "No value set for property '" + key + "', switching 
to default value '" + defaultValue + "'";
+            log(LogService.LOG_INFO, msg);
+            return defaultValue;
+        }
+
+        File file = new File(value.toString());
+        file.mkdirs();
+        return file;
+    }
+
+    private void log(int level, String msg) {
+               if (m_logService != null) {
+                       m_logService.log(level, msg);
+               } else {
+                       if (level >= LogService.LOG_WARNING) {
+                               System.err.println(msg);
+                       }
+               }
+       }
+}

Added: trunk/amdatu-auth/tools/pom.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-auth/tools/pom.xml     Wed Apr  4 11:40:41 2012
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright (c) 2010, 2011 The Amdatu Foundation
+
+  Licensed 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.verning permissions and limitations
+  under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu.auth</groupId>
+    <artifactId>org.amdatu.auth</artifactId>
+    <version>0.2.2-SNAPSHOT</version>
+  </parent>
+  <artifactId>org.amdatu.auth.tools</artifactId>
+  <packaging>pom</packaging>
+  <name>Amdatu Aauth - Tools</name>
+  <description>This module holds several Auth related tools</description>
+
+  <modules>
+    <module>config</module>
+  </modules>
+</project>

Modified: trunk/amdatu-auth/useradmin-rest/pom.xml
==============================================================================
--- trunk/amdatu-auth/useradmin-rest/pom.xml    (original)
+++ trunk/amdatu-auth/useradmin-rest/pom.xml    Wed Apr  4 11:40:41 2012
@@ -34,6 +34,11 @@
       <type>bundle</type>
     </dependency>
     <dependency>
+      <groupId>org.amdatu.auth</groupId>
+      <artifactId>org.amdatu.auth.tools.config</artifactId>
+      <type>jar</type>
+    </dependency>
+    <dependency>
       <groupId>org.amdatu.core</groupId>
       <artifactId>org.amdatu.core.tenant</artifactId>
       <type>bundle</type>
@@ -95,7 +100,7 @@
             
<Bundle-Activator>org.amdatu.auth.useradmin.rest.osgi.Activator</Bundle-Activator>
             <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
             
<Private-Package>org.amdatu.auth.useradmin.rest.*,org.apache.wink.common.internal.uri</Private-Package>
-            
<Embed-Dependency>commons-lang3,json,org.amdatu.libraries.utilities;scope=compile</Embed-Dependency>
+            
<Embed-Dependency>commons-lang3,json,org.amdatu.libraries.utilities,org.amdatu.auth.tools.config;scope=compile</Embed-Dependency>
             <Export-Package>
               !*
             </Export-Package>

Modified: 
trunk/amdatu-auth/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java
==============================================================================
--- 
trunk/amdatu-auth/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java
    (original)
+++ 
trunk/amdatu-auth/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java
    Wed Apr  4 11:40:41 2012
@@ -15,10 +15,10 @@
  */
 package org.amdatu.auth.useradmin.rest.service;
 
+import org.amdatu.auth.tools.config.ConfigTools;
+import org.amdatu.auth.tools.config.ConfigTools.ConfigProperty;
 import org.amdatu.auth.useradmin.rest.bean.Label;
 import org.amdatu.auth.useradmin.rest.bean.PasswordPolicy;
-import org.amdatu.auth.useradmin.rest.util.ConfigurationUtil;
-import org.amdatu.auth.useradmin.rest.util.ConfigurationUtil.ConfigProperty;
 import org.amdatu.core.tenant.Tenant;
 
 import java.util.Dictionary;
@@ -75,7 +75,7 @@
     private final static String PASSWORD_CREDENTIAL_KEY = "password";
 
     private Dictionary m_properties;
-    private ConfigurationUtil m_configUtil;
+    private ConfigTools m_configTools;
 
     // Service dependencies injected by the dependency manager
     private volatile Component m_component;
@@ -87,15 +87,15 @@
         }
         else {
             m_properties = properties;
-            if (m_configUtil != null) {
-                m_configUtil.init(CONFIG_PROPERTIES, properties);
+            if (m_configTools != null) {
+                m_configTools.init(CONFIG_PROPERTIES, properties);
             }
         }
     }
 
     public void start() {
-        m_configUtil = new ConfigurationUtil(getLogService());
-        m_configUtil.init(CONFIG_PROPERTIES, m_properties);
+        m_configTools = new ConfigTools(getLogService());
+        m_configTools.init(CONFIG_PROPERTIES, m_properties);
     }
 
     /**
@@ -130,27 +130,27 @@
         }
 
         PasswordPolicy policy = new PasswordPolicy();
-        policy.setMinimumdigits(m_configUtil.get(PASSWORD_MIN_DIGITS, 
Integer.class));
-        policy.setMinimumlength(m_configUtil.get(PASSWORD_MIN_LENGTH, 
Integer.class));
-        
policy.setMinimumspecialcharacters(m_configUtil.get(PASSWORD_MIN_SPEC_CHAR, 
Integer.class));
-        policy.setRequiremixedcase(m_configUtil.get(PASSWORD_REQ_MIXED_CASE, 
Boolean.class));
+        policy.setMinimumdigits(m_configTools.get(PASSWORD_MIN_DIGITS, 
Integer.class));
+        policy.setMinimumlength(m_configTools.get(PASSWORD_MIN_LENGTH, 
Integer.class));
+        
policy.setMinimumspecialcharacters(m_configTools.get(PASSWORD_MIN_SPEC_CHAR, 
Integer.class));
+        policy.setRequiremixedcase(m_configTools.get(PASSWORD_REQ_MIXED_CASE, 
Boolean.class));
 
         // Append en_US description
         String descr = "";
-        if (m_configUtil.get(PASSWORD_MIN_LENGTH, Integer.class) > 0) {
-            descr += "Minimum length: " + 
m_configUtil.get(PASSWORD_MIN_LENGTH, Integer.class) + ". ";
+        if (m_configTools.get(PASSWORD_MIN_LENGTH, Integer.class) > 0) {
+            descr += "Minimum length: " + 
m_configTools.get(PASSWORD_MIN_LENGTH, Integer.class) + ". ";
         }
-        if (m_configUtil.get(PASSWORD_REQ_MIXED_CASE, Boolean.class)) {
+        if (m_configTools.get(PASSWORD_REQ_MIXED_CASE, Boolean.class)) {
             descr +=
                 "Password must contain both lowercase and uppercase 
characters. ";
         }
-        if (m_configUtil.get(PASSWORD_MIN_DIGITS, Integer.class) > 0) {
-            descr += "Minimum amount of digits: " + 
m_configUtil.get(PASSWORD_MIN_DIGITS, Integer.class) + ". ";
+        if (m_configTools.get(PASSWORD_MIN_DIGITS, Integer.class) > 0) {
+            descr += "Minimum amount of digits: " + 
m_configTools.get(PASSWORD_MIN_DIGITS, Integer.class) + ". ";
         }
-        if (m_configUtil.get(PASSWORD_MIN_SPEC_CHAR, Integer.class) > 0) {
+        if (m_configTools.get(PASSWORD_MIN_SPEC_CHAR, Integer.class) > 0) {
             descr +=
                 "Minimum amount of non-alphanumeric characters: "
-                    + m_configUtil.get(PASSWORD_MIN_SPEC_CHAR, Integer.class) 
+ ". ";
+                    + m_configTools.get(PASSWORD_MIN_SPEC_CHAR, Integer.class) 
+ ". ";
         }
         Label label = new Label("en_US", descr);
         policy.addDescription(label);
@@ -347,20 +347,20 @@
 
     private boolean validatePasswordStrength(String password) {
         // Verify password length
-        int minLen = m_configUtil.get(PASSWORD_MIN_LENGTH, Integer.class);
+        int minLen = m_configTools.get(PASSWORD_MIN_LENGTH, Integer.class);
         if (password.length() < minLen) {
             return false;
         }
 
         // Verify mixed case
-        if (m_configUtil.get(PASSWORD_REQ_MIXED_CASE, Boolean.class)) {
+        if (m_configTools.get(PASSWORD_REQ_MIXED_CASE, Boolean.class)) {
             if (password.toLowerCase().equals(password) || 
password.toUpperCase().equals(password)) {
                 return false;
             }
         }
 
         // Verify minimum amount of digits
-        int minDigits = m_configUtil.get(PASSWORD_MIN_DIGITS, Integer.class);
+        int minDigits = m_configTools.get(PASSWORD_MIN_DIGITS, Integer.class);
         if (minDigits > 0) {
             int count = 0;
             for (int i = 0; i < password.length(); i++) {
@@ -374,7 +374,7 @@
         }
 
         // Verify minimum amount of special characters
-        int minSpecialCharacters = m_configUtil.get(PASSWORD_MIN_SPEC_CHAR, 
Integer.class);
+        int minSpecialCharacters = m_configTools.get(PASSWORD_MIN_SPEC_CHAR, 
Integer.class);
         if (minSpecialCharacters > 0) {
             int count = 0;
             for (int i = 0; i < password.length(); i++) {
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to