1 /*
2 * Copyright 2006 Simon Raess
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.beep4j.ext;
17
18 import net.sf.beep4j.Channel;
19 import net.sf.beep4j.ChannelHandler;
20 import net.sf.beep4j.CloseChannelRequest;
21 import net.sf.beep4j.Message;
22 import net.sf.beep4j.Reply;
23 import net.sf.beep4j.internal.util.Assert;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29 * Base implementation for ChannelHandler implementors. Implement the remaining
30 * unimplemented methods and override the methods you deem necessary.
31 *
32 * @author Simon Raess
33 */
34 public abstract class ChannelHandlerAdapter implements ChannelHandler {
35
36 private static final Logger LOG = LoggerFactory.getLogger(ChannelHandlerAdapter.class);
37
38 /**
39 * The associated Channel object.
40 */
41 private Channel channel;
42
43 /**
44 * Gets the channel object set by the {@link #channelOpened(Channel)} method.
45 *
46 * @return the Channel object
47 */
48 protected Channel getChannel() {
49 return channel;
50 }
51
52 /**
53 * This method ignores this event. If you want to react to it
54 * you must override this method.
55 *
56 * @param code the reply code
57 * @param message the diagnostic message
58 */
59 public void channelStartFailed(int code, String message) {
60 LOG.debug("starting channel failed: " + code + ",'" + message + "'");
61 }
62
63 /**
64 * Notifies this handler that the channel has been successfully opened.
65 * This method keeps a reference to the Channel object, which can be
66 * retrieved through the {@link #getChannel()} method.
67 *
68 * @param channel the Channel object
69 */
70 public void channelOpened(Channel channel) {
71 Assert.notNull("channel", channel);
72 LOG.debug("channel opened");
73 this.channel = channel;
74 }
75
76 /**
77 * Notifies this handler that a message has been received. If you expect
78 * messages to be received, implement this method. This default implementation
79 * throws an exception.
80 *
81 * @param message the received message
82 * @param reply the reply that can be used to send a reply
83 */
84 public void messageReceived(Message message, Reply reply) {
85 throw new UnsupportedOperationException();
86 }
87
88 /**
89 * Notifies this handler that the remote peer requested to close
90 * the channel associated with this handler. This method accepts
91 * the request. If you want to have the oppurtunity to cancel
92 * a close request, you must override this method.
93 *
94 * @param request the request to be declined or accepted
95 */
96 public void channelCloseRequested(CloseChannelRequest request) {
97 LOG.debug("close of channel requested: accepted");
98 request.accept();
99 }
100
101 /**
102 * Notifies this handler that the channel has been closed. This
103 * method sets the channel reference to null.
104 */
105 public void channelClosed() {
106 LOG.debug("channel closed");
107 this.channel = null;
108 }
109
110 }