File Source: EventHandler.java
/*
P/P * Method: com.dmdirc.EventHandler__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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;
24
25 import com.dmdirc.logger.ErrorLevel;
26 import com.dmdirc.logger.Logger;
27 import com.dmdirc.parser.irc.IRCParser;
28 import com.dmdirc.parser.irc.callbacks.CallbackManager;
29 import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
30 import com.dmdirc.parser.irc.callbacks.interfaces.ICallbackInterface;
31
32 /**
33 * Abstracts some behaviour used by Event Handlers.
34 *
35 * @author chris
36 */
/*
P/P * Method: void com.dmdirc.EventHandler()
*/
37 public abstract class EventHandler implements ICallbackInterface {
38
39 /** The prefix indicating that the interface is a parser callback. */
40 private static final String CALLBACK_PREFIX = "com.dmdirc.parser.irc.callbacks.interfaces.I";
41
42 /**
43 * Registers all callbacks that this event handler implements with the
44 * owner's parser.
45 */
46 public void registerCallbacks() {
/*
P/P * Method: void registerCallbacks()
*
* Preconditions:
* this.owner != null
* (soft) this.owner.server != null
*
* Presumptions:
* arr$.length@50 <= 232-1
* arr$[i$]@50 != null
* init'ed(com.dmdirc.logger.ErrorLevel.FATAL)
* com.dmdirc.parser.irc.IRCParser:getCallbackManager(...)@47 != null
* getServer(...).parser@47 != null
* ...
*
* Test Vectors:
* java.lang.String:startsWith(...)@51: {0}, {1}
*/
47 final CallbackManager cbm = getServer().getParser().getCallbackManager();
48
49 try {
50 for (Class iface : this.getClass().getInterfaces()) {
51 if (iface.getName().startsWith(CALLBACK_PREFIX)) {
52 addCallback(cbm, "on"
53 + iface.getName().substring(CALLBACK_PREFIX.length()));
54 }
55 }
56 } catch (CallbackNotFoundException exception) {
57 Logger.appError(ErrorLevel.FATAL, "Unable to register callbacks",
58 exception);
59 }
60 }
61
62 /**
63 * Unregisters all callbacks that have been registered by this event handler.
64 */
65 public void unregisterCallbacks() {
/*
P/P * Method: void unregisterCallbacks()
*
* Preconditions:
* this.owner != null
* (soft) this.owner.server != null
*
* Presumptions:
* com.dmdirc.parser.irc.IRCParser:getCallbackManager(...)@67 != null
* getServer(...).parser@67 != null
*
* Test Vectors:
* getServer(...).parser@66: Addr_Set{null}, Inverse{null}
*/
66 if (getServer().getParser() != null) {
67 getServer().getParser().getCallbackManager().delAllCallback(this);
68 }
69 }
70
71 /**
72 * Adds a callback to this event handler.
73 *
74 * @param cbm The callback manager to use
75 * @param name The name of the callback to be added
76 * @throws com.dmdirc.parser.irc.callbacks.CallbackNotFoundException
77 * if the specified callback isn't found
78 */
79 protected abstract void addCallback(CallbackManager cbm, String name)
80 throws CallbackNotFoundException;
81
82 /**
83 * Retrieves the server belonging to this EventHandler's owner.
84 *
85 * @since 0.6.3m1
86 * @return This EventHandler's expected server
87 */
88 protected abstract Server getServer();
89
90 /**
91 * Checks that the specified parser is the same as the one the server is
92 * currently claiming to be using. If it isn't, we raise an exception to
93 * prevent further (erroneous) processing.
94 *
95 * @param parser The parser to check
96 */
97 protected void checkParser(final IRCParser parser) {
/*
P/P * Method: void checkParser(IRCParser)
*
* Preconditions:
* this.owner != null
* (soft) this.owner.server != null
*
* Presumptions:
* parser == getServer(...).parser@98
*/
98 if (parser != getServer().getParser()) {
99 parser.disconnect("Shouldn't be in use");
100 throw new IllegalArgumentException("Event called from a parser that's not in use."
101 + "\n\n " + getServer().getStatus().getTransitionHistory());
102 }
103 }
104
105 }
SofCheck Inspector Build Version : 2.17854
| EventHandler.java |
2009-Jun-25 01:54:24 |
| EventHandler.class |
2009-Sep-02 17:04:12 |