Browse Source

Only allow setCallbackManager to be called once.

pull/153/head
Shane Mc Cormack 7 years ago
parent
commit
448bd1e85a

+ 11
- 11
common/src/main/java/com/dmdirc/parser/common/BaseParser.java View File

@@ -81,8 +81,6 @@ public abstract class BaseParser extends ThreadedParser {
81 81
      */
82 82
     public BaseParser(final URI uri) {
83 83
         this.uri = uri;
84
-
85
-        callbackManager = new CallbackManager(this::handleCallbackError);
86 84
     }
87 85
 
88 86
     @SuppressWarnings({
@@ -101,7 +99,7 @@ public abstract class BaseParser extends ThreadedParser {
101 99
         }
102 100
 
103 101
         synchronized (errorHandlerLock) {
104
-            callbackManager.publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
102
+            getCallbackManager().publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
105 103
         }
106 104
     }
107 105
 
@@ -172,23 +170,25 @@ public abstract class BaseParser extends ThreadedParser {
172 170
 
173 171
     @Override
174 172
     public CallbackManager getCallbackManager() {
173
+        // If setCallbackManager hasn't been called, assume we want to use the default CallbackManager
174
+        if (callbackManager == null) {
175
+            setCallbackManager(new CallbackManager(this::handleCallbackError));
176
+        }
175 177
         return callbackManager;
176 178
     }
177 179
 
178 180
     /**
179
-     * Change the {@link CallbackManager} in use by this parser.
180
-     *
181
-     * This will shutdown the old CallbackManager before setting the new one.
181
+     * Set the {@link CallbackManager} used by this parser.
182
+     * This can only be called once
182 183
      *
183
-     * Subscribers are not carried across between callback managers.
184
-     *
185
-     * @param manager new CallbackManager to use (ignored if null)
184
+     * @param manager CallbackManager to use
186 185
      */
187 186
     protected void setCallbackManager(final CallbackManager manager) {
188 187
         if (manager == null) {
189
-            return;
188
+            throw new UnsupportedOperationException("setCallbackManager can not be called with a null parameter.");
189
+        } else if (callbackManager != null) {
190
+            throw new UnsupportedOperationException("setCallbackManager can only be called once.");
190 191
         }
191
-        callbackManager.shutdown();
192 192
         callbackManager = manager;
193 193
     }
194 194
 

+ 0
- 1
irc/src/main/java/com/dmdirc/parser/irc/IRCParserCallbackManager.java View File

@@ -58,5 +58,4 @@ public class IRCParserCallbackManager extends CallbackManager {
58 58
     public IMessagePublication publishAsync(final ParserEvent message, final long timeout, final TimeUnit unit) {
59 59
         throw new UnsupportedOperationException("IRCParser does not support publishAsync");
60 60
     }
61
-
62 61
 }

Loading…
Cancel
Save