Author: [email protected] Date: Fri Jun 22 12:43:28 2012 New Revision: 2500 Log: [AMDATUAUTH-168] Fixed possible NullPointer in log message, added copyright header, fixed possible concurrency issues, fixed handling of passed null config, changed log method to protected such that it can be overruled in subclasses
Added: branches/amdatu-auth-0.2.3/tools/config/src/test/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/tools/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/tools/config/ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/tools/config/ConfigToolsTest.java Modified: branches/amdatu-auth-0.2.3/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java branches/amdatu-auth-0.2.3/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java Modified: branches/amdatu-auth-0.2.3/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java ============================================================================== --- branches/amdatu-auth-0.2.3/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java (original) +++ branches/amdatu-auth-0.2.3/tools/config/src/main/java/org/amdatu/auth/tools/config/ConfigTools.java Fri Jun 22 12:43:28 2012 @@ -1,3 +1,18 @@ +/* + * 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. + */ package org.amdatu.auth.tools.config; import java.io.File; @@ -52,7 +67,7 @@ m_logService = logService; } - public void init(final ConfigProperty[] configProperties, final Dictionary<?, ?> config) { + public synchronized void init(final ConfigProperty[] configProperties, final Dictionary<?, ?> config) { m_config = config; for (ConfigProperty property : configProperties) { if (property.getType().isAssignableFrom(String.class)) { @@ -94,18 +109,23 @@ * @return The value of this property as type T */ @SuppressWarnings("unchecked") - public <T> T get(final String key, final Class<T> clazz) { + public synchronized <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."); + if (val != null) { + 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); + Object value = null; + if (m_config != null) { + 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); @@ -115,7 +135,10 @@ } private int getIntProperty(final String key, final int defaultValue) { - Object value = m_config.get(key); + Object value = null; + if (m_config != null) { + 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); @@ -133,7 +156,10 @@ } private long getLongProperty(final String key, final long defaultValue) { - Object value = m_config.get(key); + Object value = null; + if (m_config != null) { + 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); @@ -151,7 +177,10 @@ } private boolean getBooleanProperty(final String key, final boolean defaultValue) { - Object value = m_config.get(key); + Object value = null; + if (m_config != null) { + 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); @@ -161,7 +190,10 @@ } private File getFileProperty(final String key, final File defaultValue) { - Object value = m_config.get(key); + Object value = null; + if (m_config != null) { + 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); @@ -173,7 +205,7 @@ return file; } - private void log(final int level, final String msg) { + protected void log(final int level, final String msg) { if (m_logService != null) { m_logService.log(level, msg); } Added: branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/tools/config/ConfigToolsTest.java ============================================================================== --- (empty file) +++ branches/amdatu-auth-0.2.3/tools/config/src/test/java/org/amdatu/auth/test/unit/tools/config/ConfigToolsTest.java Fri Jun 22 12:43:28 2012 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2010-2012 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. + */ +package org.amdatu.auth.test.unit.tools.config; + +import org.amdatu.auth.tools.config.ConfigTools; +import org.amdatu.auth.tools.config.ConfigTools.ConfigProperty; + +import java.io.File; +import java.util.Dictionary; +import java.util.Hashtable; + +import org.junit.Assert; +import org.junit.Test; + +public class ConfigToolsTest { + // Keys + private static final String STRING_KEY = "stringKey"; + private static final String BOOLEAN_KEY = "booleanKey"; + private static final String INTEGER_KEY = "integerKey"; + private static final String LONG_KEY = "longKey"; + private static final String FILE_KEY = "fileKey"; + + // Defaults + private static final String DEF_STRING_VALUE = "default"; + private static final boolean DEF_BOOLEAN_VALUE = true; + private static final int DEF_INTEGER_VALUE = 37; + private static final long DEF_LONG_VALUE = 37; + private static final File DEF_FILE_VALUE = new File("target/37.37"); + + private static ConfigProperty[] CONFIG_PROPERTIES = + new ConfigProperty[] { + new ConfigProperty(STRING_KEY, String.class, DEF_STRING_VALUE), + new ConfigProperty(BOOLEAN_KEY, Boolean.class, DEF_BOOLEAN_VALUE), + new ConfigProperty(INTEGER_KEY, Integer.class, DEF_INTEGER_VALUE), + new ConfigProperty(LONG_KEY, Long.class, DEF_LONG_VALUE), + new ConfigProperty(FILE_KEY, File.class, DEF_FILE_VALUE)}; + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Test + public void runTest() { + ConfigTools tools = new ConfigTools(); + + // -1- Try with empty config, defaults should be returned + Dictionary config = new Hashtable(); + tools.init(CONFIG_PROPERTIES, config); + Assert.assertEquals(DEF_STRING_VALUE, tools.get(STRING_KEY, String.class)); + Assert.assertEquals(DEF_BOOLEAN_VALUE, tools.get(BOOLEAN_KEY, Boolean.class)); + Assert.assertEquals(DEF_INTEGER_VALUE, tools.get(INTEGER_KEY, Integer.class).intValue()); + Assert.assertEquals(DEF_LONG_VALUE, tools.get(LONG_KEY, Long.class).longValue()); + Assert.assertEquals(DEF_FILE_VALUE, tools.get(FILE_KEY, File.class)); + + // -2- Retrieve with wrong class, should return null + Assert.assertNull(tools.get(STRING_KEY, Double.class)); + Assert.assertNull(tools.get(BOOLEAN_KEY, Double.class)); + Assert.assertNull(tools.get(INTEGER_KEY, Double.class)); + Assert.assertNull(tools.get(LONG_KEY, Double.class)); + Assert.assertNull(tools.get(FILE_KEY, Double.class)); + + // -3- Re-initialize with null, should have the same effect as setting empty dictionary + tools.init(CONFIG_PROPERTIES, null); + Assert.assertEquals(DEF_STRING_VALUE, tools.get(STRING_KEY, String.class)); + Assert.assertEquals(DEF_BOOLEAN_VALUE, tools.get(BOOLEAN_KEY, Boolean.class)); + Assert.assertEquals(DEF_INTEGER_VALUE, tools.get(INTEGER_KEY, Integer.class).intValue()); + Assert.assertEquals(DEF_LONG_VALUE, tools.get(LONG_KEY, Long.class).longValue()); + Assert.assertEquals(DEF_FILE_VALUE, tools.get(FILE_KEY, File.class)); + + // -4- Re-initialize with explicit properties + config.put(STRING_KEY, ""); + config.put(BOOLEAN_KEY, false); + config.put(INTEGER_KEY, 0); + config.put(LONG_KEY, 0); + config.put(FILE_KEY, new File("target/yo")); + tools.init(CONFIG_PROPERTIES, config); + Assert.assertEquals("", tools.get(STRING_KEY, String.class)); + Assert.assertEquals(false, tools.get(BOOLEAN_KEY, Boolean.class)); + Assert.assertEquals(0, tools.get(INTEGER_KEY, Integer.class).intValue()); + Assert.assertEquals(0, tools.get(LONG_KEY, Long.class).longValue()); + Assert.assertEquals(new File("target/yo"), tools.get(FILE_KEY, File.class)); + } +} Modified: branches/amdatu-auth-0.2.3/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java ============================================================================== --- branches/amdatu-auth-0.2.3/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java (original) +++ branches/amdatu-auth-0.2.3/useradmin-rest/src/main/java/org/amdatu/auth/useradmin/rest/service/UsersResource.java Fri Jun 22 12:43:28 2012 @@ -84,7 +84,7 @@ // Service dependencies injected by the dependency manager private volatile Component m_component; - public void updated(final Dictionary properties) throws ConfigurationException { + public synchronized void updated(final Dictionary properties) throws ConfigurationException { if (properties == null) { // This is just initialization of the configuration m_properties = properties; @@ -97,7 +97,7 @@ } } - public void start() { + public synchronized void start() { m_configTools = new ConfigTools(getLogService()); m_configTools.init(CONFIG_PROPERTIES, m_properties); } _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
