Browse Source

Allow parser sub-classes to redefine the callback mananger

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

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

@@ -72,7 +72,7 @@ public abstract class BaseParser extends ThreadedParser {
72 72
     private URI proxy;
73 73
 
74 74
     /** The callback manager to use for this parser. */
75
-    private final CallbackManager callbackManager;
75
+    private CallbackManager callbackManager;
76 76
 
77 77
     /**
78 78
      * Creates a new base parser for the specified URI.
@@ -90,7 +90,7 @@ public abstract class BaseParser extends ThreadedParser {
90 90
             "CallToPrintStackTrace",
91 91
             "UseOfSystemOutOrSystemErr"
92 92
     })
93
-    private void handleCallbackError(final PublicationError e) {
93
+    protected void handleCallbackError(final PublicationError e) {
94 94
         if (Thread.holdsLock(errorHandlerLock)) {
95 95
             // ABORT ABORT ABORT - we're publishing an error on the same thread we just tried
96 96
             // to publish an error on. Something in the error reporting pipeline must be
@@ -175,6 +175,23 @@ public abstract class BaseParser extends ThreadedParser {
175 175
         return callbackManager;
176 176
     }
177 177
 
178
+    /**
179
+     * Change the {@link CallbackManager} in use by this parser.
180
+     *
181
+     * This will shutdown the old CallbackManager before setting the new one.
182
+     *
183
+     * Subscribers are not carried across between callback managers.
184
+     *
185
+     * @param manager new CallbackManager to use (ignored if null)
186
+     */
187
+    protected void setCallbackManager(final CallbackManager manager) {
188
+        if (manager == null) {
189
+            return;
190
+        }
191
+        callbackManager.shutdown();
192
+        callbackManager = manager;
193
+    }
194
+
178 195
     @Override
179 196
     public void joinChannel(final String channel) {
180 197
         joinChannels(new ChannelJoinRequest(channel));

+ 5
- 1
common/src/main/java/com/dmdirc/parser/common/CallbackManager.java View File

@@ -36,11 +36,15 @@ import net.engio.mbassy.bus.error.IPublicationErrorHandler;
36 36
 public class CallbackManager extends MBassador<ParserEvent> {
37 37
 
38 38
     public CallbackManager(final IPublicationErrorHandler errorHandler) {
39
-        super(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
39
+        this(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
40 40
                 .addFeature(Feature.AsynchronousHandlerInvocation.Default(1, 1))
41 41
                 .addFeature(Feature.AsynchronousMessageDispatch.Default()
42 42
                         .setNumberOfMessageDispatchers(1))
43 43
                 .addPublicationErrorHandler(errorHandler));
44 44
     }
45 45
 
46
+    protected CallbackManager(final BusConfiguration busConfiguration) {
47
+        super(busConfiguration);
48
+    }
49
+
46 50
 }

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

@@ -274,6 +274,7 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
274 274
      */
275 275
     public IRCParser(final MyInfo myDetails, final URI uri) {
276 276
         super(uri);
277
+        setCallbackManager(new IRCParserCallbackManager(this::handleCallbackError));
277 278
 
278 279
         // TODO: There should be a factory or builder for parsers that can construct the graph
279 280
         final ObjectGraph graph = ObjectGraph.create(new IRCParserModule(this, prefixModes,

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

@@ -0,0 +1,57 @@
1
+/*
2
+ * Copyright (c) 2006-2017 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
+
23
+package com.dmdirc.parser.irc;
24
+
25
+import com.dmdirc.parser.common.CallbackManager;
26
+import com.dmdirc.parser.events.ParserEvent;
27
+import net.engio.mbassy.bus.IMessagePublication;
28
+import net.engio.mbassy.bus.config.BusConfiguration;
29
+import net.engio.mbassy.bus.config.Feature;
30
+import net.engio.mbassy.bus.error.IPublicationErrorHandler;
31
+
32
+import java.util.concurrent.TimeUnit;
33
+
34
+/**
35
+ * Parser Callback Manager.
36
+ * Manages adding/removing/calling callbacks.
37
+ */
38
+public class IRCParserCallbackManager extends CallbackManager {
39
+    public IRCParserCallbackManager(final IPublicationErrorHandler errorHandler) {
40
+        super(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
41
+                .addFeature(Feature.AsynchronousHandlerInvocation.Default(1, 1))
42
+                .addFeature(Feature.AsynchronousMessageDispatch.Default()
43
+                        .setNumberOfMessageDispatchers(0))
44
+                .addPublicationErrorHandler(errorHandler));
45
+    }
46
+
47
+    @Override
48
+    public IMessagePublication publishAsync(final ParserEvent message) {
49
+        throw new UnsupportedOperationException("IRCParser does not support publishAsync");
50
+    }
51
+
52
+    @Override
53
+    public IMessagePublication publishAsync(final ParserEvent message, final long timeout, final TimeUnit unit) {
54
+        throw new UnsupportedOperationException("IRCParser does not support publishAsync");
55
+    }
56
+
57
+}

Loading…
Cancel
Save