You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageSinkManager.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages.sink;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Date;
  27. import java.util.regex.Matcher;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import static com.dmdirc.util.LogUtils.USER_ERROR;
  31. /**
  32. * Manages message sinks and facilitates dispatching of messages to sinks.
  33. */
  34. @Deprecated
  35. public class MessageSinkManager {
  36. private static final Logger LOG = LoggerFactory.getLogger(MessageSinkManager.class);
  37. /** The configuration domain to use for looking up default sinks. */
  38. public static final String CONFIG_DOMAIN = "notifications";
  39. /** The default sink to use if none is specified or in case of error. */
  40. public static final String DEFAULT_SINK = "self";
  41. /** A list of known sinks. */
  42. private final Collection<MessageSink> sinks = new ArrayList<>();
  43. /**
  44. * Adds a new sink to the list of known sinks.
  45. *
  46. * @param sink The sink to be added
  47. */
  48. public void addSink(final MessageSink sink) {
  49. sinks.add(sink);
  50. }
  51. /**
  52. * Removes an existing sink from the list of known sinks.
  53. *
  54. * @param sink The sink to be removed
  55. */
  56. public void removeSink(final MessageSink sink) {
  57. sinks.remove(sink);
  58. }
  59. /**
  60. * Dispatches a message to the appropriate sink. This method will attempt to select an
  61. * appropriate target sink from the user's configuration.
  62. *
  63. * @param source The source of the message
  64. * @param date The date at which the message occurred
  65. * @param messageType The type (or 'format') of the message
  66. * @param args The message arguments
  67. */
  68. public void dispatchMessage(final WindowModel source, final Date date,
  69. final String messageType, final Object... args) {
  70. final String target;
  71. if (source.getConfigManager().hasOptionString(CONFIG_DOMAIN, messageType)) {
  72. target = source.getConfigManager().getOption(CONFIG_DOMAIN, messageType);
  73. } else {
  74. target = DEFAULT_SINK;
  75. }
  76. dispatchMessage(source, date, messageType, target, args);
  77. }
  78. /**
  79. * Dispatches a message to the appropriate sink.
  80. *
  81. * @param source The source of the message
  82. * @param date The date at which the message occurred
  83. * @param messageType The type (or 'format') of the message
  84. * @param targetSink The textual representation of the destination sink
  85. * @param args The message arguments
  86. */
  87. public void dispatchMessage(final WindowModel source, final Date date,
  88. final String messageType, final String targetSink, final Object... args) {
  89. for (MessageSink sink : sinks) {
  90. final Matcher matcher = sink.getPattern().matcher(targetSink);
  91. if (matcher.matches()) {
  92. final String[] matches = new String[matcher.groupCount()];
  93. for (int i = 0; i < matcher.groupCount(); i++) {
  94. matches[i] = matcher.group(i + 1);
  95. }
  96. sink.handleMessage(this, source, matches, date, messageType, args);
  97. return;
  98. }
  99. }
  100. // None of the sinks matched :(
  101. LOG.warn(USER_ERROR, "Invalid target message sink for type {}: {}", messageType, targetSink);
  102. if (!DEFAULT_SINK.equals(targetSink)) {
  103. dispatchMessage(source, date, messageType, DEFAULT_SINK, args);
  104. }
  105. }
  106. }