summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/main/java/org/apache/qpid/pool/Event.java
blob: 5996cbf89c9ccf4dcfe3e6350bb756ff4a5b6f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.pool;

import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoSession;

/**
 * An Event is a continuation, which is used to break a Mina filter chain and save the current point in the chain
 * for later processing. It is an abstract class, with different implementations for continuations of different kinds
 * of Mina events.
 *
 * <p/>These continuations are typically batched by {@link Job} for processing by a worker thread pool.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Process a continuation in the context of a Mina session.
 * </table>
 *
 * @todo Pull up _nextFilter and getNextFilter into Event, as all events use it. Inner classes need to be non-static
 *       to use instance variables in the parent. Consequently they need to be non-inner to be instantiable outside of
 *       the context of the outer Event class. The inner class construction used here is preventing common code re-use
 *       (though not by a huge amount), but makes for an inelegent way of handling inheritance and doesn't seem like
 *       a justifiable use of inner classes. Move the inner classes out into their own files.
 *
 * @todo Could make Event implement Runnable, FutureTask, or a custom Continuation interface, to clarify its status as
 *       a continuation. Job is also a continuation, as is the job completion handler. Or, as Event is totally abstract,
 *       it is really an interface, so could just drop it and use the continuation interface instead.
 */
public abstract class Event
{
    /**
     * Creates a continuation.
     */
    public Event()
    { }

    /**
     * Processes the continuation in the context of a Mina session.
     *
     * @param session The Mina session.
     */
    public abstract void process(IoSession session);

    /**
     * A continuation ({@link Event}) that takes a Mina messageReceived event, and passes it to a NextFilter.
     *
     * <p/><table id="crc"><caption>CRC Card</caption>
     * <tr><th> Responsibilities <th> Collaborations
     * <tr><td> Pass a Mina messageReceived event to a NextFilter. <td> {@link IoFilter.NextFilter}, {@link IoSession}
     * </table>
     */
    public static final class ReceivedEvent extends Event
    {
        private final Object _data;

        private final IoFilter.NextFilter _nextFilter;

        public ReceivedEvent(final IoFilter.NextFilter nextFilter, final Object data)
        {
            super();
            _nextFilter = nextFilter;
            _data = data;
        }

        public void process(IoSession session)
        {
            _nextFilter.messageReceived(session, _data);
        }

        public IoFilter.NextFilter getNextFilter()
        {
            return _nextFilter;
        }
    }

    /**
     * A continuation ({@link Event}) that takes a Mina filterWrite event, and passes it to a NextFilter.
     *
     * <p/><table id="crc"><caption>CRC Card</caption>
     * <tr><th> Responsibilities <th> Collaborations
     * <tr><td> Pass a Mina filterWrite event to a NextFilter.
     *     <td> {@link IoFilter.NextFilter}, {@link IoFilter.WriteRequest}, {@link IoSession}
     * </table>
     */
    public static final class WriteEvent extends Event
    {
        private final IoFilter.WriteRequest _data;
        private final IoFilter.NextFilter _nextFilter;

        public WriteEvent(final IoFilter.NextFilter nextFilter, final IoFilter.WriteRequest data)
        {
            super();
            _nextFilter = nextFilter;
            _data = data;
        }

        public void process(IoSession session)
        {
            _nextFilter.filterWrite(session, _data);
        }

        public IoFilter.NextFilter getNextFilter()
        {
            return _nextFilter;
        }
    }

    /**
     * A continuation ({@link Event}) that takes a Mina sessionClosed event, and passes it to a NextFilter.
     *
     * <p/><table id="crc"><caption>CRC Card</caption>
     * <tr><th> Responsibilities <th> Collaborations
     * <tr><td> Pass a Mina sessionClosed event to a NextFilter. <td> {@link IoFilter.NextFilter}, {@link IoSession}
     * </table>
     */
    public static final class CloseEvent extends Event
    {
        private final IoFilter.NextFilter _nextFilter;

        public CloseEvent(final IoFilter.NextFilter nextFilter)
        {
            super();
            _nextFilter = nextFilter;
        }

        public void process(IoSession session)
        {
            _nextFilter.sessionClosed(session);
        }

        public IoFilter.NextFilter getNextFilter()
        {
            return _nextFilter;
        }
    }
}