Alon Bar-Lev has uploaded a new change for review. Change subject: notifier: use LocalConfig as configuration file ......................................................................
notifier: use LocalConfig as configuration file gives the advantage of defaults and conf.d structure. Change-Id: Iea3d8a9887d76920c51d26467c634912e64242dd Signed-off-by: Alon Bar-Lev <[email protected]> --- M .gitignore M Makefile M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java M backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java M backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java M backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java M backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java M backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/EngineMonitorServiceTest.java M backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java M backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/NotificationConfiguratorTest.java M packaging/bin/engine-notifier.sh R packaging/conf/notifier.conf.defaults M packaging/fedora/spec/ovirt-engine.spec.in 13 files changed, 101 insertions(+), 144 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/76/14376/1 diff --git a/.gitignore b/.gitignore index 154b1fb..e2293b7 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ ########################### packaging/bin/engine-prolog.sh packaging/conf/engine.conf.defaults +packaging/conf/notifier.conf.defaults packaging/etc/engine-config/log4j.xml packaging/etc/engine-manage-domains/log4j.xml packaging/etc/notifier/log4j.xml diff --git a/Makefile b/Makefile index ae0b36c..9710221 100644 --- a/Makefile +++ b/Makefile @@ -381,7 +381,8 @@ # Configuration files: install -m 644 packaging/etc/notifier/log4j.xml $(DESTDIR)$(PKG_SYSCONF_DIR)/notifier/log4j.xml - install -m 640 packaging/etc/notifier/notifier.conf $(DESTDIR)$(PKG_SYSCONF_DIR)/notifier/notifier.conf + install -d -m 755 $(DESTDIR)$(PKG_SYSCONF_DIR)/notifier/notifier.conf.d + install -m 640 packaging/conf/notifier.conf.defaults $(DESTDIR)$(DATA_DIR)/conf/notifier.conf.defaults # Main program: install -m 755 packaging/bin/engine-notifier.sh $(DESTDIR)$(DATA_DIR)/bin/engine-notifier.sh diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java index 89f4ad2..1c6f29b 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java @@ -212,6 +212,15 @@ } /** + * Get all properties. + * + * @return map of all properties. + */ + public Map<String, String> getProperties() { + return values; + } + + /** * Get the value of a property given its name. * * @param key the name of the property diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java index fa2c18b..516051d 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java @@ -43,16 +43,12 @@ long engineMonitorInterval; long notificationInterval; try { - String configurationFile = null; - if (args != null && args.length == 1) { - configurationFile = args[0]; - } - notificationConf = new NotificationConfigurator(configurationFile); + notificationConf = new NotificationConfigurator(); // This check will be not mandatory when SMS is implemented. String mailServer = notificationConf.getProperties().get(NotificationProperties.MAIL_SERVER); if ( mailServer == null || mailServer.isEmpty() ) { - throw new IllegalArgumentException("Check configuration file, " + configurationFile + " MAIL_SERVER is missing"); + throw new IllegalArgumentException("Check configuration file, MAIL_SERVER is missing"); } notificationService = new NotificationService(notificationConf); diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java index 00b893b..ae03c2a 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationConfigurator.java @@ -14,41 +14,11 @@ */ public class NotificationConfigurator { - private static final String DEFAULT_CONF_FILE_LOCATION = "/etc/engine/notifier/notifier.conf"; private static final Log log = LogFactory.getLog(NotificationConfigurator.class); private Map<String, String> prop = null; - /** - * Creates a {@code NotificationConfigurator} by evaluating properties values from a given file. - * @param confFile - * a path to the configuration file - * @throws NotificationServiceException - */ - public NotificationConfigurator(String confFile) throws NotificationServiceException { - setConfigurationFile(confFile); - } - - /** - * Set local configuration file for the notification service override the default configuration file - * @param localConfFile - * the path of the alternate configuration file - * @throws NotificationServiceException - */ - private void setConfigurationFile(String localConfFile) throws NotificationServiceException { - String confFileLocation; - if (StringUtils.isNotEmpty(localConfFile)) { - confFileLocation = localConfFile; - log.info("Starting event notification service with configuration file: " + confFileLocation); - } else { - confFileLocation = DEFAULT_CONF_FILE_LOCATION; - log.info("Starting event notification service with default configuration file: " + confFileLocation); - } - File file = new File(confFileLocation); - if (!file.canRead()) { - throw new NotificationServiceException("Configuration file does not exist or missing permissions: " - + file.getAbsoluteFile()); - } - prop = NotificationProperties.readPropertiesFile(confFileLocation); + public NotificationConfigurator() { + prop = NotificationProperties.getInstance().getProperties(); } /** diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java index 12255ae..3840265 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java @@ -11,11 +11,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ovirt.engine.core.notifier.NotificationServiceException; +import org.ovirt.engine.core.utils.LocalConfig; /** * Defines properties uses by the event notification service */ -public class NotificationProperties { +public class NotificationProperties extends LocalConfig { /** * Email parameters */ @@ -55,56 +56,36 @@ private static final Log log = LogFactory.getLog(NotificationProperties.class); - /** - * Reads a properties file into a Map of <String,String> key-value pairs - * @param propertiesFile - * the system dependent file name - * @return a map which holds the properties from file - * @throws NotificationServiceException - * exception for error reading the file - */ - public static Map<String, String> readPropertiesFile(String propertiesFile) throws NotificationServiceException { - FileInputStream inStream = null; - Properties properties = new Properties(); - try { - inStream = new FileInputStream(propertiesFile); - properties.load(inStream); - } catch (IOException e) { - throw new NotificationServiceException("Failed to read configuration file " + propertiesFile, e); - } finally { - if (inStream != null) { - try { - inStream.close(); - } catch (Exception e) { - throw new NotificationServiceException("Failed to close configuration file stream", e); - } - } - } - return fillPropertiesMap(properties); + // Default files for defaults and overridden values: + private static String DEFAULTS_PATH = "/usr/share/ovirt-engine/conf/notifier.conf.defaults"; + private static String VARS_PATH = "/etc/ovirt-engine/notifier/notifier.conf"; + + // This is a singleton and this is the instance: + private static final NotificationProperties instance = new NotificationProperties(); + + public static NotificationProperties getInstance() { + return instance; } - /** - * Populates properties collection, reporting on misconfigured properties. Is a duplicated property defined on - * configuration file, warning is filed and the later property will be used. - * @param properties - * properties which read from configuration file - * @return a collections holds unique representation of the configuration properties - */ - private static Map<String, String> fillPropertiesMap(Properties properties) { - Map<String, String> prop = new HashMap<String, String>(properties.size()); - Set<Entry<Object, Object>> entrySet = properties.entrySet(); - String key; - String value; - for (Entry<Object, Object> entry : entrySet) { - key = (String) entry.getKey(); - value = (String) entry.getValue(); - if (prop.containsKey(key)) { - log.error(String.format("Duplicate property [%s] is defined in configuration file. Using property with value [%s]", - key, - value)); - } - prop.put(key, value); - } - return prop; + public static void setDefaults(String defaultsPath, String varsPath) { + DEFAULTS_PATH = defaultsPath; + VARS_PATH = varsPath; } + + private NotificationProperties() { + // Locate the defaults file and add it to the list: + String defaultsPath = System.getenv("ENGINE_NOTIFIER_DEFAULTS"); + if (defaultsPath == null) { + defaultsPath = DEFAULTS_PATH; + } + + // Locate the overridden values file and add it to the list: + String varsPath = System.getenv("ENGINE_NOTIFIER_VARS"); + if (varsPath == null) { + varsPath = VARS_PATH; + } + + loadConfig(defaultsPath, varsPath); + } + } diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java index e5006a8..a3a60cd 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java @@ -46,11 +46,8 @@ // enable SSL if (Boolean.valueOf(aMailProps.get(NotificationProperties.MAIL_ENABLE_SSL))) { mailSessionProps.put("mail.transport.protocol", "smtps"); - String portString = aMailProps.get(NotificationProperties.MAIL_PORT_SSL); - if (StringUtils.isNotEmpty(portString)) { - mailSessionProps.put("mail.smtps.socketFactory.port", portString); - mailSessionProps.put("mail.smtp.port", portString); - } + mailSessionProps.put("mail.smtp.port", aMailProps.get(NotificationProperties.MAIL_PORT_SSL)); + mailSessionProps.put("mail.smtps.socketFactory.port", aMailProps.get(NotificationProperties.MAIL_PORT_SSL)); mailSessionProps.put("mail.smtps.auth", "true"); mailSessionProps.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); mailSessionProps.put("mail.smtps.socketFactory.fallback", false); @@ -58,10 +55,7 @@ this.isSSL = true; } else { mailSessionProps.put("mail.transport.protocol", "smtp"); - String portString = aMailProps.get(NotificationProperties.MAIL_PORT); - if (StringUtils.isNotEmpty(portString)) { - mailSessionProps.put("mail.smtp.port", portString); - } + mailSessionProps.put("mail.smtp.port", aMailProps.get(NotificationProperties.MAIL_PORT)); } String password = aMailProps.get(NotificationProperties.MAIL_PASSWORD); diff --git a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/EngineMonitorServiceTest.java b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/EngineMonitorServiceTest.java index 9058b62..2b73aef 100644 --- a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/EngineMonitorServiceTest.java +++ b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/EngineMonitorServiceTest.java @@ -2,8 +2,10 @@ import static org.junit.Assert.assertNotNull; +import java.io.File; import org.junit.Test; import org.ovirt.engine.core.notifier.utils.NotificationConfigurator; +import org.ovirt.engine.core.notifier.utils.NotificationProperties; /** * The test engage the engine monitor service, which sample the server status and report upon its status. @@ -13,8 +15,10 @@ public void testNotificationService() { EngineMonitorService engineMonitorService = null; try { - engineMonitorService = - new EngineMonitorService(new NotificationConfigurator("src/test/resources/conf/notifier.conf")); + File config = new File("src/test/resources/conf/notifier.conf"); + NotificationProperties.setDefaults(config.getAbsolutePath(), null); + + engineMonitorService = new EngineMonitorService(new NotificationConfigurator()); } catch (NotificationServiceException e) { e.printStackTrace(); } diff --git a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java index 838f766..c0bbe60 100644 --- a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java +++ b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/NotificationServiceTest.java @@ -2,8 +2,10 @@ import static org.junit.Assert.assertNotNull; +import java.io.File; import org.junit.Test; import org.ovirt.engine.core.notifier.utils.NotificationConfigurator; +import org.ovirt.engine.core.notifier.utils.NotificationProperties; /** * The tests runs a single loop @@ -32,8 +34,11 @@ public void testNotificationService() { NotificationService notificationService = null; try { + File config = new File("src/test/resources/conf/notifier.conf"); + NotificationProperties.setDefaults(config.getAbsolutePath(), null); + notificationService = - new NotificationService(new NotificationConfigurator("src/test/resources/conf/notifier.conf")); + new NotificationService(new NotificationConfigurator()); } catch (NotificationServiceException e) { e.printStackTrace(); } diff --git a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/NotificationConfiguratorTest.java b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/NotificationConfiguratorTest.java index 1737b17..5dd5d3c 100644 --- a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/NotificationConfiguratorTest.java +++ b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/NotificationConfiguratorTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.File; import org.junit.BeforeClass; import org.junit.Test; import org.ovirt.engine.core.notifier.NotificationServiceException; @@ -14,22 +15,17 @@ @BeforeClass static public void initConfigurator() { - try { - config = new NotificationConfigurator("src/test/resources/conf/error-notifier.conf"); - } catch (NotificationServiceException e) { - e.printStackTrace(); - } + File c = new File("src/test/resources/conf/notifier.conf"); + NotificationProperties.setDefaults(c.getAbsolutePath(), null); + + config = new NotificationConfigurator(); assertNotNull(config); } @Test public void testConfiguration() { NotificationConfigurator configurator = null; - try { - configurator = new NotificationConfigurator("src/test/resources/conf/notifier.conf"); - } catch (NotificationServiceException e) { - e.printStackTrace(); - } + configurator = new NotificationConfigurator(); assertNotNull(configurator); diff --git a/packaging/bin/engine-notifier.sh b/packaging/bin/engine-notifier.sh index 6cdc9ea..06b5bda 100755 --- a/packaging/bin/engine-notifier.sh +++ b/packaging/bin/engine-notifier.sh @@ -220,13 +220,13 @@ # not possible to debug the execution of the main method. # +NOTIFIER_VARS="${CONF_FILE}" "${JAVA_HOME}/bin/java" \ -Dlog4j.configuration="file:${ENGINE_ETC}/notifier/log4j.xml" \ -Djboss.modules.write-indexes=false \ -jar "${JBOSS_HOME}/jboss-modules.jar" \ -dependencies org.ovirt.engine.core.tools \ -class org.ovirt.engine.core.notifier.Notifier \ - "${CONF_FILE}" \ 2>/dev/null & echo $! >$NOTIFIER_PID diff --git a/packaging/etc/notifier/notifier.conf b/packaging/conf/notifier.conf.defaults similarity index 64% rename from packaging/etc/notifier/notifier.conf rename to packaging/conf/notifier.conf.defaults index 9fa3ea9..fee57bb 100644 --- a/packaging/etc/notifier/notifier.conf +++ b/packaging/conf/notifier.conf.defaults @@ -7,71 +7,71 @@ # Notification Service Configuration: # #-------------------------------------# # Interval (in seconds) between iterations of dispatching messages to subscribers. Default is 120 seconds. -#INTERVAL_IN_SECONDS=120 +INTERVAL_IN_SECONDS=120 # The SMTP mail server address. Required. MAIL_SERVER= -# The default port of non-secured SMTP server is 25, for secured (ssl enabled) 465. -#MAIL_PORT=25 -#MAIL_PORT_SSL=465 +# The SMTP port +MAIL_PORT=25 +MAIL_PORT_SSL=465 # Required if SSL enabled to authenticate the user. Used also to specify 'from' user address if mail server # supports, when MAIL_FROM is not set. Address is in RFC822 format -#MAIL_USER= +MAIL_USER= # Required to authenticate the user if mail server requires authentication or if SSL is enabled -#MAIL_PASSWORD= +MAIL_PASSWORD= -# Indicates whether SSL should be used to communicate with mail server. Default is false. -#MAIL_ENABLE_SSL=true +# Indicates whether SSL should be used to communicate with mail server. +MAIL_ENABLE_SSL=false -# If set to true, sends a message in HTML format. Default is false. -#HTML_MESSAGE_FORMAT=true +# If set to true, sends a message in HTML format. +HTML_MESSAGE_FORMAT=false # Specifies 'from' address on sent mail in RFC822 format, if supported by mail server. -#MAIL_FROM= +MAIL_FROM= # Specifies 'reply-to' address on sent mail in RFC822 format. -#MAIL_REPLY_TO= +MAIL_REPLY_TO= -# Amount of days to keep dispatched events on history table. If not set, events remain on history table. -#DAYS_TO_KEEP_HISTORY=30 +# Amount of days to keep dispatched events on history table. If 0, events remain on history table. +DAYS_TO_KEEP_HISTORY=0 # This parameter specifies how many days of old events are processed and sent # when the notifier starts. If set to 2, for example, the notifier will # process and send the events of the last two days, older events will just # be marked as processed and won't be sent. The default is 0, so no old # messages will be sent at all during startup. -#DAYS_TO_SEND_ON_STARTUP=0 +DAYS_TO_SEND_ON_STARTUP=0 #----------------------------------# # Engine Monitoring Configuration: # #----------------------------------# # Interval (in seconds) between engine server monitoring iterations. Interval is being measured from -# the time an iteration is completed. Default is 300 seconds. -#ENGINE_INTERVAL_IN_SECONDS=300 +# the time an iteration is completed. +ENGINE_INTERVAL_IN_SECONDS=300 -# Number of retries to monitor server status per iteration. Default is 3 retries. -#ENGINE_MONITOR_RETRIES=3 +# Number of retries to monitor server status per iteration. +ENGINE_MONITOR_RETRIES=3 -# Time (in seconds) to wait between retries. Default is 30 seconds. -#ENGINE_TIMEOUT_IN_SECONDS=30 +# Time (in seconds) to wait between retries. +ENGINE_TIMEOUT_IN_SECONDS=30 -# If running Jboss in secure mode, should be set to true. Default is false. -#IS_HTTPS_PROTOCOL=true +# If running Jboss in secure mode, should be set to true. +IS_HTTPS_PROTOCOL=false -# Specifies the protocol used by Jboss Configuration Connector when SSL is enabled. Default is 'TLS'. -#SSL_PROTOCOL=TLS +# Specifies the protocol used by Jboss Configuration Connector when SSL is enabled. +SSL_PROTOCOL=TLS -# If running Jboss in secure mode, and wishes to ignore SSL errors, should set to true. Default is false. -#SSL_IGNORE_CERTIFICATE_ERRORS=false +# If running Jboss in secure mode, and wishes to ignore SSL errors, should set to true. +SSL_IGNORE_CERTIFICATE_ERRORS=false # If running Jboss in secure mode, and wishes to ignore hostname verification, should set to true. Default is false. # If setting this property to 'true', SSL_IGNORE_CERTIFICATE_ERRORS considered to be set to 'true' as well: disabling host name # verification means that it will not be verified against the certification, therefore certification errors are ignored. -#SSL_IGNORE_HOST_VERIFICATION=false +SSL_IGNORE_HOST_VERIFICATION=false -# Specifies whether to repeat auditing of failure messages of non-responding engine server. Default is false (meaning -# repeated failure messages will NOT be sent to the subscribers) -#REPEAT_NON_RESPONSIVE_NOTIFICATION=false +# Specifies whether to repeat auditing of failure messages of non-responding engine server. false means +# repeated failure messages will NOT be sent to the subscribers. +REPEAT_NON_RESPONSIVE_NOTIFICATION=false diff --git a/packaging/fedora/spec/ovirt-engine.spec.in b/packaging/fedora/spec/ovirt-engine.spec.in index 1aaada7..ea5805e 100644 --- a/packaging/fedora/spec/ovirt-engine.spec.in +++ b/packaging/fedora/spec/ovirt-engine.spec.in @@ -705,7 +705,7 @@ # Configuration files for the notifier: %dir %{engine_etc}/notifier -%config(noreplace) %attr(-, %{engine_user}, %{engine_group}) %{engine_etc}/notifier/notifier.conf +%dir %{engine_etc}/notifier/notifier.conf.d %{engine_etc}/notifier/log4j.xml # Jar files: -- To view, visit http://gerrit.ovirt.org/14376 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iea3d8a9887d76920c51d26467c634912e64242dd Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alon Bar-Lev <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
