mooli tayer has uploaded a new change for review. Change subject: tools: cleanup notifier. ......................................................................
tools: cleanup notifier. Remove cumbersome NotificationMethod abstraction. Change-Id: I8b71c4e78bbdca3d02d2ac4ef419b9d3d7d58761 Signed-off-by: Mooli Tayer <mta...@redhat.com> --- A backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationMethodsMapper.java M backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java D backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/EventMethodFiller.java D backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactory.java D backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactoryEmailImpl.java D backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodMapBuilder.java R backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventMailSender.java M backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/mail/MailSenderTest.java 8 files changed, 50 insertions(+), 266 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/35/22135/1 diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationMethodsMapper.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationMethodsMapper.java new file mode 100644 index 0000000..927335a --- /dev/null +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationMethodsMapper.java @@ -0,0 +1,27 @@ +package org.ovirt.engine.core.notifier; + + +import org.ovirt.engine.core.common.EventNotificationMethods; +import org.ovirt.engine.core.notifier.utils.NotificationProperties; +import org.ovirt.engine.core.notifier.utils.sender.EventSender; +import org.ovirt.engine.core.notifier.utils.sender.mail.EventMailSender; + +import java.util.HashMap; +import java.util.Map; + +public class NotificationMethodsMapper { + + private Map<EventNotificationMethods,EventSender> eventSenders = new HashMap<>(); + + public NotificationMethodsMapper(NotificationProperties prop) { + eventSenders.put(EventNotificationMethods.EMAIL, new EventMailSender(prop)); + } + + public EventSender getEventSender(EventNotificationMethods method) { + return eventSenders.get(method); + } + + public EventSender getEventSender(int intValue) { + return eventSenders.get(EventNotificationMethods.forValue(intValue)); + } +} diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java index 9fd5782..326daa6 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java @@ -8,12 +8,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import javax.sql.DataSource; @@ -23,15 +18,12 @@ import org.ovirt.engine.core.common.EventNotificationMethods; import org.ovirt.engine.core.common.businessentities.DbUser; import org.ovirt.engine.core.common.businessentities.EventAuditLogSubscriber; -import org.ovirt.engine.core.common.businessentities.EventNotificationMethod; import org.ovirt.engine.core.common.businessentities.event_notification_hist; import org.ovirt.engine.core.compat.Guid; -import org.ovirt.engine.core.notifier.methods.EventMethodFiller; -import org.ovirt.engine.core.notifier.methods.NotificationMethodMapBuilder; -import org.ovirt.engine.core.notifier.methods.NotificationMethodMapBuilder.NotificationMethodFactoryMapper; import org.ovirt.engine.core.notifier.utils.NotificationProperties; import org.ovirt.engine.core.notifier.utils.sender.EventSender; import org.ovirt.engine.core.notifier.utils.sender.EventSenderResult; +import org.ovirt.engine.core.notifier.utils.sender.mail.EventMailSender; import org.ovirt.engine.core.notifier.utils.sender.mail.MessageHelper; import org.ovirt.engine.core.tools.common.db.StandaloneDataSource; import org.ovirt.engine.core.utils.db.DbUtils; @@ -45,13 +37,21 @@ private static final Logger log = Logger.getLogger(NotificationService.class); private DataSource ds; + private NotificationProperties prop = null; - private NotificationMethodFactoryMapper methodsMapper = null; + + private NotificationMethodsMapper methodsMapper; + private int daysToKeepHistory = 0; + private int daysToSendOnStartup = 0; + private EventSender failedQueriesEventSender; + private List<EventAuditLogSubscriber> failedQueriesEventSubscribers = Collections.emptyList(); + private int failedQueriesNotificationThreshold; + private int failedQueries = 0; public NotificationService(NotificationProperties prop) throws NotificationServiceException { @@ -81,9 +81,8 @@ if (failedQueriesNotificationThreshold == 0) { failedQueriesNotificationThreshold = 1; } - initMethodMapper(); - failedQueriesEventSender = - methodsMapper.getMethod(EventNotificationMethods.EMAIL); + methodsMapper = new NotificationMethodsMapper(prop); + failedQueriesEventSender = methodsMapper.getEventSender(EventNotificationMethods.EMAIL); } private int getNonNegativeIntegerProperty(final String name) throws NotificationServiceException { @@ -186,30 +185,6 @@ } } - private void initMethodMapper() throws NotificationServiceException { - EventMethodFiller methodFiller = new EventMethodFiller(); - Connection connection = null; - try { - connection = ds.getConnection(); - methodFiller.fillEventNotificationMethods(connection); - } catch (Exception e) { - throw new NotificationServiceException("Failed to initialize method mapper", e); - } - finally { - if (connection != null) { - try { - connection.close(); - } - catch (SQLException exception) { - log.error("Failed to release connection", exception); - } - } - } - - List<EventNotificationMethod> eventNotificationMethods = methodFiller.getEventNotificationMethods(); - methodsMapper = NotificationMethodMapBuilder.instance().createMethodsMapper(eventNotificationMethods, prop); - } - private void initConnectivity() throws NotificationServiceException { try { ds = new StandaloneDataSource(); @@ -259,8 +234,7 @@ for (EventAuditLogSubscriber eventSubscriber:eventSubscribers) { dbUser = getUserByUserId(eventSubscriber.getsubscriber_id()); if (dbUser != null) { - EventSender method = - methodsMapper.getMethod(EventNotificationMethods.forValue(eventSubscriber.getmethod_id())); + EventSender method = methodsMapper.getEventSender(eventSubscriber.getmethod_id()); EventSenderResult sendResult = null; try { sendResult = method.send(eventSubscriber, dbUser.getEmail()); @@ -343,6 +317,7 @@ eventHistory.setaudit_log_id(eals.getaudit_log_id()); eventHistory.setevent_name(eals.getevent_up_name()); eventHistory.setmethod_type(EventNotificationMethods.forValue(eals.getmethod_id()).name()); +// eventHistory.setmethod_type(EventNotificationMethods.forValue(eals.getmethod_id()).name()); eventHistory.setreason(reason); eventHistory.setsent_at(new Date()); eventHistory.setstatus(isNotified); diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/EventMethodFiller.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/EventMethodFiller.java deleted file mode 100644 index d280059..0000000 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/EventMethodFiller.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.ovirt.engine.core.notifier.methods; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; - -import org.ovirt.engine.core.common.EventNotificationMethods; -import org.ovirt.engine.core.common.businessentities.EventNotificationMethod; - -/** - * Populates a event notification method list by a database content<br> - * If no valid method type method is provided (meaning a method type which isn't defined in - * {@link EventNotificationMethods}),<br> - * a default value of {@code EventNotificationMethods.EMAIL} is set. - */ -public class EventMethodFiller { - - private List<EventNotificationMethod> methods = new ArrayList<EventNotificationMethod>(); - - /** - * Populates the list of event notification methods by its content in table <i>event_notification_methods</i> - * @param conn - * a connection to the database - * @throws SQLException - */ - public void fillEventNotificationMethods(Connection conn) throws SQLException { - Statement stmt = null; - ResultSet rs = null; - try { - stmt = conn.createStatement(); - rs = stmt.executeQuery("select * from event_notification_methods"); - EventNotificationMethod method; - while (rs.next()) { - method = new EventNotificationMethod(); - method.setmethod_id(rs.getInt("method_id")); - method.setmethod_type(getMethodTypeByName(rs.getString("method_type"))); - methods.add(method); - } - } finally { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - } - } - if (stmt != null) { - stmt.close(); - } - } - } - - private EventNotificationMethods getMethodTypeByName(String methodName) { - for (EventNotificationMethods value : EventNotificationMethods.values()) { - if (value.name().equalsIgnoreCase(methodName)) { - return value; - } - } - return EventNotificationMethods.EMAIL; - } - - /** - * A getter of the created methods list. Should be called and used after - * {@link #fillEventNotificationMethods(Connection)} was invoked - * @return list of configured notification methods - */ - public List<EventNotificationMethod> getEventNotificationMethods() { - return methods; - } -} diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactory.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactory.java deleted file mode 100644 index 88c8a50..0000000 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactory.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.ovirt.engine.core.notifier.methods; - -import org.ovirt.engine.core.notifier.utils.sender.EventSender; - -/** - * An interface of notification method factories, enforcing the created class to derive from {@link EventSender} - * @param <T> - * an implemented class of the {@link EventSender} - */ -public interface NotificationMethodFactory<T extends EventSender> { - /** - * Create an instance of the notification method implementation class - * @return an implemented class instance of the {@link EventSender} which will be used to notify a subscription - */ - public T createMethodClass(); -} diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactoryEmailImpl.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactoryEmailImpl.java deleted file mode 100644 index 9bec28d..0000000 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodFactoryEmailImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.ovirt.engine.core.notifier.methods; - -import org.ovirt.engine.core.notifier.utils.NotificationProperties; -import org.ovirt.engine.core.notifier.utils.sender.mail.EventSenderMailImpl; - -/** - * A factory of the email notification method class.<br> - * A single {@code EventSenderMailImpl} instance could serve multiple notification actions, therefore the factory will - * instantiate a single instance of it and will provide it to the dispatchers. - * @see NotificationMethodFactory - */ -public class NotificationMethodFactoryEmailImpl implements NotificationMethodFactory<EventSenderMailImpl> { - - private EventSenderMailImpl senderMailImpl = null; - - public NotificationMethodFactoryEmailImpl(NotificationProperties properties) { - senderMailImpl = new EventSenderMailImpl(properties); - } - - /** - * Returns a created email notification method - */ - @Override - public EventSenderMailImpl createMethodClass() { - return senderMailImpl; - } -} diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodMapBuilder.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodMapBuilder.java deleted file mode 100644 index ffd0a42..0000000 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/methods/NotificationMethodMapBuilder.java +++ /dev/null @@ -1,103 +0,0 @@ -package org.ovirt.engine.core.notifier.methods; - -import java.util.HashMap; -import java.util.List; - -import org.ovirt.engine.core.common.EventNotificationMethods; -import org.ovirt.engine.core.common.businessentities.EventNotificationMethod; -import org.ovirt.engine.core.notifier.utils.NotificationProperties; -import org.ovirt.engine.core.notifier.utils.sender.EventSender; - -/** - * The group of classes used to map a factory for each notification method.<br> - * Different notification methods might require different instantiation methods of the notification<br> - * method implementation class.</p> - * <b>Components description:</b><br> - * {@link EventSender} - interface of any notification method implementation<br> - * {@code EventSenderMailImpl} - implements email notification method<br> - * {@link NotificationMethodFactory} - interface of factories of notification method classes. Each factory will define the<br> - * the policy of creation notification classes (some factories might produce a single class for all notifications, others<br> - * might produce a single instance per notification)</br> - * {@link NotificationMethodFactoryEmailImpl} - factory for producing a single email notification method class<br> - * {@link NotificationMethodFactoryMapper} - stores the association between the notification method type to its factory<br> - * {@link NotificationMethodMapBuilder} - creates the map of notification method type to the factory. Could be recreated - * upon <br>modification of the notification method types.</p> <b>Adding new notification method</b><br> - * The following steps will describe how to add and register new notification method. Let's add support for SMS - * notification method:<br> - * <li>Add new notification method to {@link EventNotificationMethods}, e.g. {@code SMS(1)} - * <li>Add an entry representing the SMS notification method to database table <i>event_notification_methods<i> - * <li>Create {@code EventSenderSMSImpel} which implements the {@link EventSender}. The class will be responsible for - * dispatching a notification via SMS<br> <li>Create {@code NotificationMethodFactorySMS} which implements the - * {@link NotificationMethodFactory}. The class will be responsible for instantiating the SMS sender class. - * <li>Register the notification method and the factory in {@link NotificationMethodMapBuilder#createMethodsMapper(List)} - */ -public class NotificationMethodMapBuilder { - - private static NotificationMethodMapBuilder instance = null; - - static { - instance = new NotificationMethodMapBuilder(); - } - - private NotificationMethodMapBuilder() { - } - - /** - * a getter of single instance of the method builder - * @return a reference to the event notification map builder - */ - public static NotificationMethodMapBuilder instance() { - return instance; - } - - /** - * Maps pairs of notification method type to the factory which produces the class that handles the notification - * action.<br> - * Design meant to provide a varied instantiation of the method type implementation class.<br> - * @param notificationMethods - * supported list of notification methods - * @param properties - * configuration properties for the factories - * @return a map of notification map and its factory - */ - public NotificationMethodFactoryMapper createMethodsMapper(List<EventNotificationMethod> notificationMethods, - NotificationProperties properties) { - NotificationMethodFactoryMapper methodMapper = new NotificationMethodFactoryMapper(); - - for (EventNotificationMethod method : notificationMethods) { - if (EventNotificationMethods.EMAIL.equals(method.getmethod_type())) { - methodMapper.addMethodFactory(EventNotificationMethods.EMAIL, - new NotificationMethodFactoryEmailImpl(properties)); - } - } - return methodMapper; - } - - /** - * A map of the notification method type to its factory. Once a map was created it is non-modifiable.<br> - * The class designed to provide an implementation class instance by a given notification method type, <br> - * encapsulating the policy of the notification method implementation class instantiation.<br> - * Adding notification method types and factories could be done only using: - * {@link NotificationMethodMapBuilder.createMethodsMapper(List)} - */ - public class NotificationMethodFactoryMapper { - - private HashMap<EventNotificationMethods, NotificationMethodFactory<? extends EventSender>> methods = - new HashMap<EventNotificationMethods, NotificationMethodFactory<? extends EventSender>>(); - - private void addMethodFactory(EventNotificationMethods methodType, - NotificationMethodFactory<? extends EventSender> factory) { - methods.put(methodType, factory); - } - - /** - * Returns an instance of the notification method implementation class - * @param methodType - * a notification method type - * @return an instance of the notification method implementation class or null of no match found - */ - public EventSender getMethod(EventNotificationMethods methodType) { - return methods.get(methodType).createMethodClass(); - } - } -} diff --git a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventSenderMailImpl.java b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventMailSender.java similarity index 91% rename from backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventSenderMailImpl.java rename to backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventMailSender.java index d2d8839..9723593 100644 --- a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventSenderMailImpl.java +++ b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/EventMailSender.java @@ -14,7 +14,7 @@ import org.ovirt.engine.core.notifier.utils.sender.EventSenderResult; /** - * The class designed to send e-mails to subscriptions for events.<br> + * The class sends e-mails to event subscribers. * In order to define a proper mail client, the following properties should be provided: * <li><code>MAIL_SERVER</code> mail server name * <li><code>MAIL_PORT</code> mail server port</li><br> @@ -26,14 +26,14 @@ * <ul><li>"from" address should include a domain, same as <code>MAIL_USER</code> property * <li><code>MAIL_REPLY_TO</code> specifies "replyTo" address in outgoing message */ -public class EventSenderMailImpl implements EventSender { +public class eventMailSender implements EventSender { - private static final Logger log = Logger.getLogger(EventSenderMailImpl.class); + private static final Logger log = Logger.getLogger(eventMailSender.class); private JavaMailSender mailSender; private String hostName; private boolean isBodyHtml = false; - public EventSenderMailImpl(NotificationProperties mailProp) { + public eventMailSender(NotificationProperties mailProp) { mailSender = new JavaMailSender(mailProp); String isBodyHtmlStr = mailProp.getProperty(NotificationProperties.HTML_MESSAGE_FORMAT); if (StringUtils.isNotEmpty(isBodyHtmlStr)) { @@ -43,7 +43,7 @@ try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { - EventSenderMailImpl.log.error("Failed to resolve machine name, using localhost instead.", e); + eventMailSender.log.error("Failed to resolve machine name, using localhost instead.", e); hostName = "localhost"; } } diff --git a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/mail/MailSenderTest.java b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/mail/MailSenderTest.java index f934f69..19ac81a 100644 --- a/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/mail/MailSenderTest.java +++ b/backend/manager/tools/src/test/java/org/ovirt/engine/core/notifier/utils/mail/MailSenderTest.java @@ -11,7 +11,7 @@ import org.ovirt.engine.core.common.businessentities.EventAuditLogSubscriber; import org.ovirt.engine.core.notifier.utils.NotificationProperties; import org.ovirt.engine.core.notifier.utils.sender.EventSenderResult; -import org.ovirt.engine.core.notifier.utils.sender.mail.EventSenderMailImpl; +import org.ovirt.engine.core.notifier.utils.sender.mail.eventMailSender; /** * A tester of the {@link JavaMailSender}. Tests both secured and non-secured methods of mail sending as SMTP and SMTP @@ -49,7 +49,7 @@ "src/test/resources/conf/notifier-mail-test-plain.conf", "src/test/resources/conf/missing.conf" ); - EventSenderMailImpl mailSender = new EventSenderMailImpl(NotificationProperties.getInstance()); + eventMailSender mailSender = new eventMailSender(NotificationProperties.getInstance()); eventData.setmessage("a test message to be sent via non-secured mode"); EventSenderResult sentResult = null; try { @@ -86,7 +86,7 @@ "src/test/resources/conf/notifier-mail-test-secured.conf", "src/test/resources/conf/missing.conf" ); - EventSenderMailImpl mailSender = new EventSenderMailImpl(NotificationProperties.getInstance()); + eventMailSender mailSender = new eventMailSender(NotificationProperties.getInstance()); eventData.setmessage("a test message to be sent via secured mode"); mailSender.send(eventData, null); -- To view, visit http://gerrit.ovirt.org/22135 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8b71c4e78bbdca3d02d2ac4ef419b9d3d7d58761 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: mooli tayer <mta...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches