Event.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.pool;
022 
023 import org.apache.mina.common.IoFilter;
024 import org.apache.mina.common.IoSession;
025 
026 /**
027  * An Event is a continuation, which is used to break a Mina filter chain and save the current point in the chain
028  * for later processing. It is an abstract class, with different implementations for continuations of different kinds
029  * of Mina events.
030  *
031  <p/>These continuations are typically batched by {@link Job} for processing by a worker thread pool.
032  *
033  <p/><table id="crc"><caption>CRC Card</caption>
034  <tr><th> Responsibilities <th> Collaborations
035  <tr><td> Process a continuation in the context of a Mina session.
036  </table>
037  *
038  * @todo Pull up _nextFilter and getNextFilter into Event, as all events use it. Inner classes need to be non-static
039  *       to use instance variables in the parent. Consequently they need to be non-inner to be instantiable outside of
040  *       the context of the outer Event class. The inner class construction used here is preventing common code re-use
041  *       (though not by a huge amount), but makes for an inelegent way of handling inheritance and doesn't seem like
042  *       a justifiable use of inner classes. Move the inner classes out into their own files.
043  *
044  * @todo Could make Event implement Runnable, FutureTask, or a custom Continuation interface, to clarify its status as
045  *       a continuation. Job is also a continuation, as is the job completion handler. Or, as Event is totally abstract,
046  *       it is really an interface, so could just drop it and use the continuation interface instead.
047  */
048 public abstract class Event
049 {
050     /**
051      * Creates a continuation.
052      */
053     public Event()
054     { }
055 
056     /**
057      * Processes the continuation in the context of a Mina session.
058      *
059      @param session The Mina session.
060      */
061     public abstract void process(IoSession session);
062 
063     /**
064      * A continuation ({@link Event}) that takes a Mina messageReceived event, and passes it to a NextFilter.
065      *
066      <p/><table id="crc"><caption>CRC Card</caption>
067      <tr><th> Responsibilities <th> Collaborations
068      <tr><td> Pass a Mina messageReceived event to a NextFilter. <td> {@link IoFilter.NextFilter}{@link IoSession}
069      </table>
070      */
071     public static final class ReceivedEvent extends Event
072     {
073         private final Object _data;
074 
075         private final IoFilter.NextFilter _nextFilter;
076 
077         public ReceivedEvent(final IoFilter.NextFilter nextFilter, final Object data)
078         {
079             super();
080             _nextFilter = nextFilter;
081             _data = data;
082         }
083 
084         public void process(IoSession session)
085         {
086             _nextFilter.messageReceived(session, _data);
087         }
088 
089         public IoFilter.NextFilter getNextFilter()
090         {
091             return _nextFilter;
092         }
093     }
094 
095     /**
096      * A continuation ({@link Event}) that takes a Mina filterWrite event, and passes it to a NextFilter.
097      *
098      <p/><table id="crc"><caption>CRC Card</caption>
099      <tr><th> Responsibilities <th> Collaborations
100      <tr><td> Pass a Mina filterWrite event to a NextFilter.
101      *     <td> {@link IoFilter.NextFilter}{@link IoFilter.WriteRequest}{@link IoSession}
102      </table>
103      */
104     public static final class WriteEvent extends Event
105     {
106         private final IoFilter.WriteRequest _data;
107         private final IoFilter.NextFilter _nextFilter;
108 
109         public WriteEvent(final IoFilter.NextFilter nextFilter, final IoFilter.WriteRequest data)
110         {
111             super();
112             _nextFilter = nextFilter;
113             _data = data;
114         }
115 
116         public void process(IoSession session)
117         {
118             _nextFilter.filterWrite(session, _data);
119         }
120 
121         public IoFilter.NextFilter getNextFilter()
122         {
123             return _nextFilter;
124         }
125     }
126 
127     /**
128      * A continuation ({@link Event}) that takes a Mina sessionClosed event, and passes it to a NextFilter.
129      *
130      <p/><table id="crc"><caption>CRC Card</caption>
131      <tr><th> Responsibilities <th> Collaborations
132      <tr><td> Pass a Mina sessionClosed event to a NextFilter. <td> {@link IoFilter.NextFilter}{@link IoSession}
133      </table>
134      */
135     public static final class CloseEvent extends Event
136     {
137         private final IoFilter.NextFilter _nextFilter;
138 
139         public CloseEvent(final IoFilter.NextFilter nextFilter)
140         {
141             super();
142             _nextFilter = nextFilter;
143         }
144 
145         public void process(IoSession session)
146         {
147             _nextFilter.sessionClosed(session);
148         }
149 
150         public IoFilter.NextFilter getNextFilter()
151         {
152             return _nextFilter;
153         }
154     }
155 }